2009-10-06 19:25:18 +00:00
|
|
|
unit mainform;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
2014-05-08 12:12:06 +00:00
|
|
|
StdCtrls, Menus, ExtCtrls, ComCtrls, ActnList, Spin, ColorBox,graphutil,
|
2013-12-28 15:29:22 +00:00
|
|
|
fpspreadsheetgrid, fpspreadsheet, fpsallformats;
|
2009-10-06 19:25:18 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
TForm1 = class(TForm)
|
2014-04-19 19:29:13 +00:00
|
|
|
AcOpen: TAction;
|
|
|
|
AcSaveAs: TAction;
|
|
|
|
AcQuit: TAction;
|
2014-05-07 22:44:00 +00:00
|
|
|
AcEdit: TAction;
|
|
|
|
AcLeftAlign: TAction;
|
|
|
|
AcHorCenterAlign: TAction;
|
|
|
|
AcRightAlign: TAction;
|
|
|
|
AcHorDefaultAlign: TAction;
|
2014-05-08 08:08:37 +00:00
|
|
|
AcFontBold: TAction;
|
|
|
|
AcFontItalic: TAction;
|
|
|
|
AcFontStrikeout: TAction;
|
|
|
|
AcFontUnderline: TAction;
|
2014-05-08 12:12:06 +00:00
|
|
|
AcFont: TAction;
|
2014-04-19 19:29:13 +00:00
|
|
|
ActionList1: TActionList;
|
2014-05-03 21:27:31 +00:00
|
|
|
CbShowHeaders: TCheckBox;
|
|
|
|
CbShowGridLines: TCheckBox;
|
2014-05-07 22:44:00 +00:00
|
|
|
FontComboBox: TComboBox;
|
2014-05-04 18:07:54 +00:00
|
|
|
EdFrozenRows: TSpinEdit;
|
2014-05-08 12:12:06 +00:00
|
|
|
FontDialog1: TFontDialog;
|
2014-05-07 22:44:00 +00:00
|
|
|
FontSizeComboBox: TComboBox;
|
2014-04-19 19:29:13 +00:00
|
|
|
ImageList1: TImageList;
|
2014-05-04 18:07:54 +00:00
|
|
|
Label1: TLabel;
|
|
|
|
Label2: TLabel;
|
2013-12-28 15:29:22 +00:00
|
|
|
MainMenu1: TMainMenu;
|
2014-04-19 19:29:13 +00:00
|
|
|
MenuItem1: TMenuItem;
|
2014-05-07 22:44:00 +00:00
|
|
|
MenuItem2: TMenuItem;
|
|
|
|
MenuItem3: TMenuItem;
|
|
|
|
MenuItem4: TMenuItem;
|
|
|
|
MenuItem5: TMenuItem;
|
2014-05-08 12:12:06 +00:00
|
|
|
MenuItem6: TMenuItem;
|
2014-05-07 22:44:00 +00:00
|
|
|
MnuHorDefault: TMenuItem;
|
|
|
|
MnuHorAlignment: TMenuItem;
|
|
|
|
mnuFormat: TMenuItem;
|
|
|
|
mnuEdit: TMenuItem;
|
2013-12-28 15:29:22 +00:00
|
|
|
mnuFile: TMenuItem;
|
|
|
|
mnuOpen: TMenuItem;
|
|
|
|
mnuQuit: TMenuItem;
|
|
|
|
mnuSaveAs: TMenuItem;
|
|
|
|
OpenDialog1: TOpenDialog;
|
2014-04-19 19:29:13 +00:00
|
|
|
PageControl1: TPageControl;
|
|
|
|
Panel1: TPanel;
|
2013-12-28 15:29:22 +00:00
|
|
|
SaveDialog1: TSaveDialog;
|
2014-05-04 18:07:54 +00:00
|
|
|
EdFrozenCols: TSpinEdit;
|
2009-10-06 19:25:18 +00:00
|
|
|
sWorksheetGrid1: TsWorksheetGrid;
|
2014-04-19 19:29:13 +00:00
|
|
|
TabSheet1: TTabSheet;
|
|
|
|
ToolBar1: TToolBar;
|
2014-05-07 22:44:00 +00:00
|
|
|
FormatToolBar: TToolBar;
|
2014-04-19 19:29:13 +00:00
|
|
|
ToolButton1: TToolButton;
|
2014-05-07 22:44:00 +00:00
|
|
|
ToolButton10: TToolButton;
|
2014-05-08 08:08:37 +00:00
|
|
|
ToolButton11: TToolButton;
|
2014-05-07 22:44:00 +00:00
|
|
|
ToolButton12: TToolButton;
|
|
|
|
ToolButton13: TToolButton;
|
2014-05-08 08:08:37 +00:00
|
|
|
ToolButton14: TToolButton;
|
2014-04-19 19:29:13 +00:00
|
|
|
ToolButton2: TToolButton;
|
|
|
|
ToolButton3: TToolButton;
|
2014-05-07 22:44:00 +00:00
|
|
|
ToolButton4: TToolButton;
|
2014-04-19 19:29:13 +00:00
|
|
|
ToolButton5: TToolButton;
|
2014-05-07 22:44:00 +00:00
|
|
|
ToolButton6: TToolButton;
|
2014-05-08 08:08:37 +00:00
|
|
|
ToolButton7: TToolButton;
|
|
|
|
ToolButton8: TToolButton;
|
|
|
|
ToolButton9: TToolButton;
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure AcEditExecute(Sender: TObject);
|
2014-05-08 12:12:06 +00:00
|
|
|
procedure AcFontExecute(Sender: TObject);
|
2014-05-08 08:08:37 +00:00
|
|
|
procedure AcFontStyleExecute(Sender: TObject);
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure AcHorAlignmentExecute(Sender: TObject);
|
2014-05-03 21:27:31 +00:00
|
|
|
procedure CbShowHeadersClick(Sender: TObject);
|
|
|
|
procedure CbShowGridLinesClick(Sender: TObject);
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure acOpenExecute(Sender: TObject);
|
|
|
|
procedure acQuitExecute(Sender: TObject);
|
|
|
|
procedure acSaveAsExecute(Sender: TObject);
|
2014-05-04 18:07:54 +00:00
|
|
|
procedure EdFrozenColsChange(Sender: TObject);
|
|
|
|
procedure EdFrozenRowsChange(Sender: TObject);
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure FontComboBoxSelect(Sender: TObject);
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure FormActivate(Sender: TObject);
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure FormCreate(Sender: TObject);
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure PageControl1Change(Sender: TObject);
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure sWorksheetGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
|
|
|
|
var CanSelect: Boolean);
|
2009-10-06 19:25:18 +00:00
|
|
|
private
|
|
|
|
{ private declarations }
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure LoadFile(const AFileName: String);
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure UpdateHorAlignment(AValue: TsHorAlignment);
|
|
|
|
procedure UpdateFont(AFont: TsFont);
|
2009-10-06 19:25:18 +00:00
|
|
|
public
|
|
|
|
{ public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form1: TForm1;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
uses
|
2014-05-07 22:44:00 +00:00
|
|
|
fpcanvas, Grids;
|
|
|
|
|
|
|
|
const
|
|
|
|
HORALIGN_TAG = 100;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
|
2009-10-06 19:25:18 +00:00
|
|
|
{ TForm1 }
|
|
|
|
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure TForm1.AcEditExecute(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if AcEdit.Checked then
|
|
|
|
sWorksheetGrid1.Options := sWorksheetGrid1.Options + [goEditing]
|
|
|
|
else
|
|
|
|
sWorksheetGrid1.Options := sWorksheetGrid1.Options - [goEditing];
|
|
|
|
end;
|
|
|
|
|
2014-05-08 12:12:06 +00:00
|
|
|
{ Changes the font of the selected cell by calling a standard font dialog.
|
|
|
|
Note that the worksheet's and grid's fonts are implemented differently.
|
|
|
|
In particular, the worksheet's font color is an index into the workbook's
|
|
|
|
palette while the grid's font color is an rgb value. }
|
|
|
|
procedure TForm1.AcFontExecute(Sender: TObject);
|
|
|
|
var
|
|
|
|
r,c: Cardinal;
|
|
|
|
f: Integer;
|
|
|
|
style: TsFontStyles;
|
|
|
|
lFont: TsFont;
|
|
|
|
begin
|
|
|
|
with sWorksheetGrid1 do begin
|
|
|
|
if Worksheet <> nil then begin
|
|
|
|
c := GetWorksheetCol(Col);
|
|
|
|
r := GetWorksheetRow(Row);
|
|
|
|
f := Worksheet.GetCell(r, c)^.FontIndex;
|
|
|
|
Convert_sFont_to_Font(Workbook.GetFont(f), FontDialog1.Font);
|
|
|
|
if FontDialog1.Execute then begin
|
|
|
|
lFont := TsFont.Create;
|
|
|
|
try
|
|
|
|
Convert_Font_to_sFont(FontDialog1.Font, lFont);
|
|
|
|
WorkSheet.WriteFont(r, c, lFont.FontName, lFont.Size, lFont.Style, lFont.Color);
|
|
|
|
finally
|
|
|
|
lFont.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-08 08:08:37 +00:00
|
|
|
procedure TForm1.AcFontStyleExecute(Sender: TObject);
|
|
|
|
var
|
|
|
|
style: TsFontstyles;
|
|
|
|
f: Integer;
|
|
|
|
r,c: Cardinal;
|
|
|
|
lFont: TsFont;
|
|
|
|
begin
|
|
|
|
with sWorksheetGrid1 do begin
|
|
|
|
c := GetWorksheetCol(Col);
|
|
|
|
r := GetWorksheetRow(Row);
|
|
|
|
if Worksheet <> nil then begin
|
|
|
|
f := Worksheet.GetCell(r, c)^.FontIndex;
|
|
|
|
lFont := Workbook.GetFont(f);
|
|
|
|
style := lFont.Style;
|
|
|
|
if TAction(Sender).Checked then
|
|
|
|
Include(style, TsFontStyle(TAction(Sender).Tag))
|
|
|
|
else
|
|
|
|
Exclude(style, TsFontStyle(TAction(Sender).Tag));
|
|
|
|
Worksheet.WriteFontStyle(r, c, style);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure TForm1.AcHorAlignmentExecute(Sender: TObject);
|
|
|
|
var
|
|
|
|
horalign: TsHorAlignment;
|
|
|
|
c, r: Cardinal;
|
|
|
|
begin
|
|
|
|
horalign := TsHorAlignment(TAction(Sender).Tag - HORALIGN_TAG);
|
|
|
|
if TAction(Sender).Checked then
|
|
|
|
horalign := haDefault;
|
|
|
|
UpdateHorAlignment(horalign);
|
|
|
|
with sWorksheetGrid1 do begin
|
|
|
|
c := GetWorksheetCol(Col);
|
|
|
|
r := GetWorksheetRow(Row);
|
|
|
|
if Worksheet <> nil then
|
|
|
|
Worksheet.WriteHorAlignment(r, c, horalign);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-05-03 21:27:31 +00:00
|
|
|
procedure TForm1.CbShowHeadersClick(Sender: TObject);
|
2014-04-19 19:29:13 +00:00
|
|
|
begin
|
2014-05-03 21:27:31 +00:00
|
|
|
sWorksheetGrid1.ShowHeaders := CbShowHeaders.Checked;
|
2014-04-19 19:29:13 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-03 21:27:31 +00:00
|
|
|
procedure TForm1.CbShowGridLinesClick(Sender: TObject);
|
2014-04-19 19:29:13 +00:00
|
|
|
begin
|
2014-05-03 21:27:31 +00:00
|
|
|
sWorksheetGrid1.ShowGridLines := CbShowGridLines.Checked;
|
2009-10-06 19:25:18 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.acOpenExecute(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
begin
|
|
|
|
if OpenDialog1.Execute then
|
2014-04-29 21:58:48 +00:00
|
|
|
LoadFile(OpenDialog1.FileName);
|
2013-12-28 15:29:22 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.acQuitExecute(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
begin
|
|
|
|
Close;
|
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.acSaveAsExecute(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
// Saves sheet in grid to file, overwriting existing file
|
|
|
|
begin
|
2014-05-07 22:44:00 +00:00
|
|
|
if sWorksheetGrid1.Workbook = nil then
|
|
|
|
exit;
|
2014-04-19 19:29:13 +00:00
|
|
|
|
2013-12-28 15:29:22 +00:00
|
|
|
if SaveDialog1.Execute then
|
2014-05-07 22:44:00 +00:00
|
|
|
sWorksheetGrid1.SaveToSpreadsheetFile(SaveDialog1.FileName);
|
2013-12-28 15:29:22 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-04 18:07:54 +00:00
|
|
|
procedure TForm1.EdFrozenColsChange(Sender: TObject);
|
|
|
|
begin
|
|
|
|
sWorksheetGrid1.FrozenCols := EdFrozenCols.Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.EdFrozenRowsChange(Sender: TObject);
|
|
|
|
begin
|
|
|
|
sWorksheetGrid1.FrozenRows := EdFrozenRows.Value;
|
|
|
|
end;
|
|
|
|
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure TForm1.FontComboBoxSelect(Sender: TObject);
|
|
|
|
var
|
|
|
|
c, r: Cardinal;
|
|
|
|
f: Integer;
|
|
|
|
lFont: TsFont;
|
|
|
|
h: Integer;
|
|
|
|
s: String;
|
|
|
|
begin
|
|
|
|
if sWorksheetGrid1.Workbook = nil then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
with sWorksheetGrid1 do begin
|
|
|
|
c := GetWorksheetCol(Col);
|
|
|
|
r := GetWorksheetRow(Row);
|
|
|
|
f := Worksheet.GetCell(r, c)^.FontIndex;
|
|
|
|
lFont := Workbook.GetFont(f);
|
|
|
|
|
|
|
|
if FontCombobox.ItemIndex = -1 then
|
|
|
|
s := lFont.FontName
|
|
|
|
else
|
|
|
|
s := FontCombobox.Items[FontCombobox.ItemIndex];
|
|
|
|
|
|
|
|
if FontSizeCombobox.ItemIndex = -1 then
|
|
|
|
h := round(lFont.Size)
|
|
|
|
else
|
|
|
|
h := StrToInt(FontSizeCombobox.Items[FontSizeCombobox.ItemIndex]);
|
|
|
|
|
|
|
|
Worksheet.WriteFont(r, c, s, h, lFont.Style, lFont.Color);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure TForm1.FormActivate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if ParamCount > 0 then
|
|
|
|
LoadFile(ParamStr(1));
|
|
|
|
end;
|
|
|
|
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure TForm1.FormCreate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
// Populate font combobox
|
|
|
|
FontCombobox.Items.Assign(Screen.Fonts);
|
|
|
|
end;
|
|
|
|
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure TForm1.LoadFile(const AFileName: String);
|
|
|
|
// Loads first worksheet from file into grid
|
|
|
|
var
|
|
|
|
pages: TStrings;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
2014-05-04 18:07:54 +00:00
|
|
|
// Load file
|
2014-04-29 21:58:48 +00:00
|
|
|
sWorksheetGrid1.LoadFromSpreadsheetFile(AFileName);
|
2014-05-04 18:07:54 +00:00
|
|
|
|
|
|
|
// Update user interface
|
2014-04-29 21:58:48 +00:00
|
|
|
Caption := Format('fpsGrid - %s (%s)', [
|
|
|
|
AFilename,
|
|
|
|
GetFileFormatName(sWorksheetGrid1.Workbook.FileFormat)
|
|
|
|
]);
|
2014-05-04 18:07:54 +00:00
|
|
|
CbShowGridLines.Checked := (soShowGridLines in sWorksheetGrid1.Worksheet.Options);
|
|
|
|
CbShowHeaders.Checked := (soShowHeaders in sWorksheetGrid1.Worksheet.Options);
|
|
|
|
EdFrozenCols.Value := sWorksheetGrid1.FrozenCols;
|
|
|
|
EdFrozenRows.Value := sWorksheetGrid1.FrozenRows;
|
2014-04-29 21:58:48 +00:00
|
|
|
|
|
|
|
// Create a tab in the pagecontrol for each worksheet contained in the workbook
|
|
|
|
// This would be easer with a TTabControl. This has display issues, though.
|
|
|
|
pages := TStringList.Create;
|
|
|
|
try
|
|
|
|
sWorksheetGrid1.GetSheets(pages);
|
|
|
|
sWorksheetGrid1.Parent := PageControl1.Pages[0];
|
|
|
|
while PageControl1.PageCount > pages.Count do PageControl1.Pages[1].Free;
|
|
|
|
while PageControl1.PageCount < pages.Count do PageControl1.AddTabSheet;
|
|
|
|
for i:=0 to PageControl1.PageCount-1 do
|
|
|
|
PageControl1.Pages[i].Caption := pages[i];
|
|
|
|
finally
|
|
|
|
pages.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.PageControl1Change(Sender: TObject);
|
|
|
|
begin
|
|
|
|
sWorksheetGrid1.Parent := PageControl1.Pages[PageControl1.ActivePageIndex];
|
|
|
|
sWorksheetGrid1.SelectSheetByIndex(PageControl1.ActivePageIndex);
|
|
|
|
end;
|
|
|
|
|
2014-05-07 22:44:00 +00:00
|
|
|
procedure TForm1.sWorksheetGrid1SelectCell(Sender: TObject;
|
|
|
|
aCol, aRow: Integer; var CanSelect: Boolean);
|
|
|
|
var
|
|
|
|
cell: PCell;
|
|
|
|
c, r: Cardinal;
|
|
|
|
lFont: TsFont;
|
|
|
|
begin
|
|
|
|
with sWorksheetGrid1 do begin
|
|
|
|
if Worksheet = nil then exit;
|
|
|
|
c := GetWorksheetCol(ACol);
|
|
|
|
r := GetWorksheetRow(ARow);
|
|
|
|
cell := Worksheet.FindCell(r, c);
|
|
|
|
end;
|
|
|
|
if cell = nil then
|
|
|
|
exit;
|
|
|
|
UpdateHorAlignment(cell^.HorAlignment);
|
|
|
|
lFont := sWorksheetGrid1.Workbook.GetFont(cell^.FontIndex);
|
|
|
|
UpdateFont(lFont);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.UpdateHorAlignment(AValue: TsHorAlignment);
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
ac: TAction;
|
|
|
|
begin
|
|
|
|
for i:=0 to ActionList1.ActionCount-1 do begin
|
|
|
|
ac := TAction(ActionList1.Actions[i]);
|
|
|
|
if (ac.Tag >= HORALIGN_TAG) and (ac.Tag < HORALIGN_TAG+10) then
|
|
|
|
ac.Checked := ((ac.Tag - HORALIGN_TAG) = ord(AValue));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.UpdateFont(AFont: TsFont);
|
|
|
|
begin
|
|
|
|
FontCombobox.ItemIndex := FontCombobox.Items.IndexOf(AFont.FontName);
|
|
|
|
FontsizeCombobox.ItemIndex := FontSizeCombobox.Items.IndexOf(IntToStr(Round(AFont.Size)));
|
2014-05-08 08:08:37 +00:00
|
|
|
AcFontBold.Checked := fssBold in AFont.Style;
|
|
|
|
AcFontItalic.Checked := fssItalic in AFont.Style;
|
|
|
|
AcFontUnderline.Checked := fssUnderline in AFont.Style;
|
|
|
|
AcFontStrikeout.Checked := fssStrikeOut in AFont.Style;
|
2014-05-07 22:44:00 +00:00
|
|
|
end;
|
|
|
|
|
2009-10-06 19:25:18 +00:00
|
|
|
initialization
|
|
|
|
{$I mainform.lrs}
|
|
|
|
|
|
|
|
end.
|
|
|
|
|