summaryrefslogtreecommitdiff
path: root/examples/gui/gridediting
diff options
context:
space:
mode:
authorGraeme Geldenhuys <graeme@mastermaths.co.za>2010-09-23 14:32:12 +0200
committerGraeme Geldenhuys <graeme@mastermaths.co.za>2010-09-23 14:32:12 +0200
commitba3777d4c35ecdf196e0b9add276ec51f611689e (patch)
tree9c102df96a0e288989f3a41d5830569ba478fcfe /examples/gui/gridediting
parent083274be42b205136eb267daeae556bb1be49f96 (diff)
downloadfpGUI-ba3777d4c35ecdf196e0b9add276ec51f611689e.tar.xz
Editable Grid Demo: a very quick example how inline grid editing.
Diffstat (limited to 'examples/gui/gridediting')
-rw-r--r--examples/gui/gridediting/extrafpc.cfg5
-rw-r--r--examples/gui/gridediting/frm_main.pas172
-rw-r--r--examples/gui/gridediting/gridediting.lpi71
-rw-r--r--examples/gui/gridediting/gridediting.lpr29
-rw-r--r--examples/gui/gridediting/units/placeholder.txt0
5 files changed, 277 insertions, 0 deletions
diff --git a/examples/gui/gridediting/extrafpc.cfg b/examples/gui/gridediting/extrafpc.cfg
new file mode 100644
index 00000000..775d592f
--- /dev/null
+++ b/examples/gui/gridediting/extrafpc.cfg
@@ -0,0 +1,5 @@
+-FUunits
+-Fu../../../lib/$fpctarget
+-Xs
+-XX
+-CX
diff --git a/examples/gui/gridediting/frm_main.pas b/examples/gui/gridediting/frm_main.pas
new file mode 100644
index 00000000..51480595
--- /dev/null
+++ b/examples/gui/gridediting/frm_main.pas
@@ -0,0 +1,172 @@
+unit frm_main;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+ SysUtils, Classes, fpg_base, fpg_main, fpg_form, fpg_basegrid,
+ fpg_grid, fpg_button, fpg_edit, fpg_label;
+
+type
+
+ TMainForm = class(TfpgForm)
+ private
+ {@VFD_HEAD_BEGIN: MainForm}
+ Grid1: TfpgStringGrid;
+ btnQuit: TfpgButton;
+ Label1: TfpgLabel;
+ {@VFD_HEAD_END: MainForm}
+ FCellEdit: TfpgEdit;
+ FFocusRect: TfpgRect;
+ FLastGrid: TfpgStringGrid; // reference only
+ procedure SetupCellEdit(AGrid: TfpgStringGrid);
+ procedure CellEditExit(Sender: TObject);
+ procedure CellEditKeypressed(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
+ procedure GridKeyPressed(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
+ procedure GridDrawCell(Sender: TObject; const ARow, ACol: Integer; const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
+ procedure ButtonQuitClicked(Sender: TObject);
+ public
+ procedure AfterCreate; override;
+ end;
+
+{@VFD_NEWFORM_DECL}
+
+implementation
+
+{@VFD_NEWFORM_IMPL}
+
+procedure TMainForm.GridKeyPressed(Sender: TObject; var KeyCode: word;
+ var ShiftState: TShiftState; var Consumed: boolean);
+begin
+ if (KeyCode = keyInsert) and (ssCtrl in ShiftState) then
+ begin
+ TfpgStringGrid(Sender).RowCount := TfpgStringGrid(Sender).RowCount + 1;
+ Consumed := True;
+ Exit;
+ end;
+
+ if (KeyCode = keyF2) or (KeyCode = keyReturn) then
+ begin
+ // we need to edit the cell contents
+ SetupCellEdit(TfpgStringGrid(Sender));
+ Consumed := True;
+ end;
+end;
+
+procedure TMainForm.GridDrawCell(Sender: TObject; const ARow, ACol: Integer;
+ const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
+begin
+ if (gdSelected in AFlags) then
+ begin
+ FFocusRect := ARect;
+ end;
+end;
+
+procedure TMainForm.ButtonQuitClicked(Sender: TObject);
+begin
+ Close;
+end;
+
+procedure TMainForm.SetupCellEdit(AGrid: TfpgStringGrid);
+var
+ pt: TPoint;
+begin
+ if Assigned(FCellEdit) then
+ FCellEdit.Free;
+
+ FLastGrid := AGrid;
+ FCellEdit := TfpgEdit.Create(FLastGrid.Parent);
+ pt.X := FLastGrid.Left + FFocusRect.Left;
+ pt.Y := FLastGrid.Top + FFocusRect.Top;
+ with FCellEdit do
+ begin
+ Name := 'FCellEdit';
+ SetPosition(pt.X, pt.Y, FFocusRect.Width, FFocusRect.Height);
+ BorderStyle := ebsSingle;
+ FontDesc := '#Grid';
+ Text := AGrid.Cells[AGrid.FocusCol, AGrid.FocusRow];
+ OnKeyPress := @CellEditKeypressed;
+ OnExit := @CellEditExit;
+ SetFocus;
+ end;
+end;
+
+procedure TMainForm.CellEditExit(Sender: TObject);
+begin
+ FCellEdit.Visible := False;
+end;
+
+procedure TMainForm.CellEditKeypressed(Sender: TObject; var KeyCode: word;
+ var ShiftState: TShiftState; var Consumed: boolean);
+begin
+ if KeyCode = keyReturn then
+ begin
+ FLastGrid.Cells[FLastGrid.FocusCol, FLastGrid.FocusRow] := FCellEdit.Text;
+ FCellEdit.Visible := False;
+ FLastGrid.SetFocus;
+ end;
+end;
+
+procedure TMainForm.AfterCreate;
+begin
+ {%region 'Auto-generated GUI code' -fold}
+ {@VFD_BODY_BEGIN: MainForm}
+ Name := 'MainForm';
+ SetPosition(356, 172, 431, 361);
+ WindowTitle := 'Editable StringGrid Demo';
+ Hint := '';
+
+ Grid1 := TfpgStringGrid.Create(self);
+ with Grid1 do
+ begin
+ Name := 'Grid1';
+ SetPosition(16, 76, 400, 239);
+ Anchors := [anLeft,anRight,anTop,anBottom];
+ AddColumn('#1', 100, taLeftJustify);
+ AddColumn('#2', 100, taLeftJustify);
+ AddColumn('#3', 100, taLeftJustify);
+ AddColumn('#4', 75, taLeftJustify);
+ FontDesc := '#Grid';
+ HeaderFontDesc := '#GridHeader';
+ Hint := '';
+ RowCount := 5;
+ RowSelect := False;
+ TabOrder := 1;
+ OnKeyPress := @GridKeyPressed;
+ OnDrawCell := @GridDrawCell;
+ end;
+
+ btnQuit := TfpgButton.Create(self);
+ with btnQuit do
+ begin
+ Name := 'btnQuit';
+ SetPosition(336, 329, 80, 24);
+ Anchors := [anRight,anBottom];
+ Text := 'Quit';
+ Down := False;
+ FontDesc := '#Label1';
+ Hint := '';
+ ImageName := '';
+ TabOrder := 2;
+ OnClick := @ButtonQuitClicked;
+ end;
+
+ Label1 := TfpgLabel.Create(self);
+ with Label1 do
+ begin
+ Name := 'Label1';
+ SetPosition(16, 8, 400, 56);
+ Anchors := [anLeft,anRight,anTop];
+ FontDesc := '#Label1';
+ Hint := '';
+ Text := 'You can use Ctrl+Ins to add new grid lines. F2 or Enter will put you in edit mode. While in edit mode, changing focus or pressing Enter will take you back to non-edit mode.';
+ WrapText := True;
+ end;
+
+ {@VFD_BODY_END: MainForm}
+ {%endregion}
+end;
+
+
+end.
diff --git a/examples/gui/gridediting/gridediting.lpi b/examples/gui/gridediting/gridediting.lpi
new file mode 100644
index 00000000..13caff41
--- /dev/null
+++ b/examples/gui/gridediting/gridediting.lpi
@@ -0,0 +1,71 @@
+<?xml version="1.0"?>
+<CONFIG>
+ <ProjectOptions>
+ <Version Value="9"/>
+ <General>
+ <Flags>
+ <SaveOnlyProjectUnits Value="True"/>
+ </Flags>
+ <SessionStorage Value="None"/>
+ <MainUnit Value="0"/>
+ <UseAppBundle Value="False"/>
+ </General>
+ <i18n>
+ <EnableI18N LFM="False"/>
+ </i18n>
+ <VersionInfo>
+ <StringTable ProductVersion=""/>
+ </VersionInfo>
+ <PublishOptions>
+ <Version Value="2"/>
+ <IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
+ <ExcludeFileFilter Value="*.(bak|ppu|ppw|o|so);*~;backup"/>
+ </PublishOptions>
+ <RunParams>
+ <local>
+ <FormatVersion Value="1"/>
+ <LaunchingApplication PathPlusParams="/usr/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
+ </local>
+ </RunParams>
+ <RequiredPackages Count="1">
+ <Item1>
+ <PackageName Value="fpgui_toolkit"/>
+ </Item1>
+ </RequiredPackages>
+ <Units Count="2">
+ <Unit0>
+ <Filename Value="gridediting.lpr"/>
+ <IsPartOfProject Value="True"/>
+ <UnitName Value="gridediting"/>
+ </Unit0>
+ <Unit1>
+ <Filename Value="frm_main.pas"/>
+ <IsPartOfProject Value="True"/>
+ <UnitName Value="frm_main"/>
+ </Unit1>
+ </Units>
+ </ProjectOptions>
+ <CompilerOptions>
+ <Version Value="9"/>
+ <Other>
+ <CompilerMessages>
+ <UseMsgFile Value="True"/>
+ </CompilerMessages>
+ <CustomOptions Value="-FUunits"/>
+ <CompilerPath Value="$(CompPath)"/>
+ </Other>
+ </CompilerOptions>
+ <Debugging>
+ <Exceptions Count="3">
+ <Item1>
+ <Name Value="EAbort"/>
+ </Item1>
+ <Item2>
+ <Name Value="ECodetoolError"/>
+ </Item2>
+ <Item3>
+ <Name Value="EFOpenError"/>
+ </Item3>
+ </Exceptions>
+ </Debugging>
+</CONFIG>
diff --git a/examples/gui/gridediting/gridediting.lpr b/examples/gui/gridediting/gridediting.lpr
new file mode 100644
index 00000000..fc21969d
--- /dev/null
+++ b/examples/gui/gridediting/gridediting.lpr
@@ -0,0 +1,29 @@
+program gridediting;
+
+{$mode objfpc}{$H+}
+
+uses
+ {$IFDEF UNIX}{$IFDEF UseCThreads}
+ cthreads,
+ {$ENDIF}{$ENDIF}
+ Classes, fpg_main, fpg_form, frm_main;
+
+
+procedure MainProc;
+var
+ frm: TMainForm;
+begin
+ fpgApplication.Initialize;
+ frm := TMainForm.Create(nil);
+ try
+ frm.Show;
+ fpgApplication.Run;
+ finally
+ frm.Free;
+ end;
+end;
+
+begin
+ MainProc;
+end.
+
diff --git a/examples/gui/gridediting/units/placeholder.txt b/examples/gui/gridediting/units/placeholder.txt
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/examples/gui/gridediting/units/placeholder.txt