summaryrefslogtreecommitdiff
path: root/examples/gui/command_interface
diff options
context:
space:
mode:
Diffstat (limited to 'examples/gui/command_interface')
-rw-r--r--examples/gui/command_interface/commands.pas90
-rw-r--r--examples/gui/command_interface/extrafpc.cfg5
-rw-r--r--examples/gui/command_interface/frm_main.pas124
-rw-r--r--examples/gui/command_interface/test.lpi61
-rw-r--r--examples/gui/command_interface/test.lpr32
5 files changed, 312 insertions, 0 deletions
diff --git a/examples/gui/command_interface/commands.pas b/examples/gui/command_interface/commands.pas
new file mode 100644
index 00000000..2824a04d
--- /dev/null
+++ b/examples/gui/command_interface/commands.pas
@@ -0,0 +1,90 @@
+{
+ Here we define some commands that can be reused throughout a application.
+ Command actions are kept separate from the UI code (Forms).
+}
+unit commands;
+
+{$mode objfpc}{$H+}
+{$INTERFACES CORBA}
+
+interface
+
+uses
+ gfx_command_intf,
+ gui_memo;
+
+type
+ // non reference counted interface
+ TNullInterfacedObject = class(TObject)
+ protected
+ function QueryInterface(const IID: TGUID; out Obj): longint; stdcall;
+ function _AddRef: longint; stdcall;
+ function _Release: longint; stdcall;
+ end;
+
+
+ TAddCommand = class(TInterfacedObject, ICommand)
+ private
+ FMemo: TfpgMemo;
+ public
+ constructor Create(AMemo: TfpgMemo); reintroduce;
+ procedure Execute;
+ end;
+
+
+ TExitCommand = class(TInterfacedObject, ICommand)
+ public
+ procedure Execute;
+ end;
+
+
+implementation
+
+uses
+ fpgfx, SysUtils;
+
+{ TNullInterfacedObject }
+
+function TNullInterfacedObject.QueryInterface(const IID: TGUID; out Obj): longint; stdcall;
+begin
+ if GetInterface(IID, Obj) then
+ Result := 0
+ else
+ result := integer(e_nointerface);
+end;
+
+function TNullInterfacedObject._AddRef: longint; stdcall;
+begin
+ Result := -1;
+end;
+
+function TNullInterfacedObject._Release: longint; stdcall;
+begin
+ Result := -1;
+end;
+
+{ TAddCommand }
+
+constructor TAddCommand.Create(AMemo: TfpgMemo);
+begin
+ inherited Create;
+ FMemo := AMemo;
+end;
+
+procedure TAddCommand.Execute;
+begin
+ Writeln('>> TAddComand.Execute');
+ FMemo.Lines.Add('Hello ' + IntToStr(Random(500)));
+ FMemo.Invalidate;
+end;
+
+{ TExitCommand }
+
+procedure TExitCommand.Execute;
+begin
+ Writeln('>> TExitComand.Execute');
+ fpgApplication.Terminated := True;
+end;
+
+end.
+
diff --git a/examples/gui/command_interface/extrafpc.cfg b/examples/gui/command_interface/extrafpc.cfg
new file mode 100644
index 00000000..073dc4b6
--- /dev/null
+++ b/examples/gui/command_interface/extrafpc.cfg
@@ -0,0 +1,5 @@
+-FUunits
+-Fu../../../lib
+-Xs
+-XX
+-CX
diff --git a/examples/gui/command_interface/frm_main.pas b/examples/gui/command_interface/frm_main.pas
new file mode 100644
index 00000000..28bb18cf
--- /dev/null
+++ b/examples/gui/command_interface/frm_main.pas
@@ -0,0 +1,124 @@
+{
+ This demonstrates the usage of ICommand and ICommandHolder. They work
+ similar to Delphi's TAction classes
+}
+unit frm_main;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+ SysUtils, Classes, gfxbase, fpgfx, gui_edit,
+ gfx_widget, gui_form, gui_label, gui_button,
+ gui_listbox, gui_memo, gui_combobox, gui_grid,
+ gui_dialogs, gui_checkbox, gui_tree, gui_trackbar,
+ gui_progressbar, gui_radiobutton, gui_tab, gui_menu,
+ gui_bevel;
+
+type
+
+ TMainForm = class(TfpgForm)
+ private
+ procedure CommandHandler(Sender: TObject);
+ public
+ {@VFD_HEAD_BEGIN: MainForm}
+ btnAdd: TfpgButton;
+ memName1: TfpgMemo;
+ btnQuit: TfpgButton;
+ MainMenu: TfpgMenuBar;
+ mnuFile: TfpgPopupMenu;
+ {@VFD_HEAD_END: MainForm}
+ procedure AfterCreate; override;
+ end;
+
+{@VFD_NEWFORM_DECL}
+
+implementation
+
+uses
+ gfx_command_intf,
+ commands;
+
+{@VFD_NEWFORM_IMPL}
+
+{ A single event handler that handles all Command based events. }
+procedure TMainForm.CommandHandler(Sender: TObject);
+var
+ cmd: ICommand;
+ holder: ICommandHolder;
+begin
+ if Supports(Sender, ICommandHolder, holder) then
+ begin
+ cmd := holder.GetCommand;
+ cmd.Execute;
+ end;
+end;
+
+procedure TMainForm.AfterCreate;
+begin
+ {@VFD_BODY_BEGIN: MainForm}
+ Name := 'MainForm';
+ SetPosition(293, 236, 284, 254);
+ WindowTitle := 'Command Interface Test';
+ WindowPosition := wpScreenCenter;
+
+ btnAdd := TfpgButton.Create(self);
+ with btnAdd do
+ begin
+ Name := 'btnAdd';
+ SetPosition(204, 36, 75, 24);
+ Text := 'Add';
+ FontDesc := '#Label1';
+ ImageName := '';
+ OnClick := @CommandHandler;
+ end;
+
+ memName1 := TfpgMemo.Create(self);
+ with memName1 do
+ begin
+ Name := 'memName1';
+ SetPosition(8, 36, 188, 208);
+ FontDesc := '#Edit1';
+ end;
+
+ btnQuit := TfpgButton.Create(self);
+ with btnQuit do
+ begin
+ Name := 'btnQuit';
+ SetPosition(204, 220, 75, 24);
+ Text := 'Quit';
+ FontDesc := '#Label1';
+ ImageName := '';
+ OnClick := @CommandHandler;
+ end;
+
+ MainMenu := TfpgMenuBar.Create(self);
+ with MainMenu do
+ begin
+ Name := 'MainMenu';
+ SetPosition(0, 0, 284, 24);
+ Anchors := [anLeft,anRight,anTop];
+ end;
+
+ mnuFile := TfpgPopupMenu.Create(self);
+ with mnuFile do
+ begin
+ Name := 'mnuFile';
+ SetPosition(44, 72, 120, 20);
+ AddMenuItem('Quit', '', @CommandHandler);
+ end;
+
+ {@VFD_BODY_END: MainForm}
+
+ MainMenu.AddMenuItem('File', nil).SubMenu := mnuFile;
+
+ // instantiate the Command classes
+ btnAdd.SetCommand(TAddCommand.Create(memName1));
+ btnQuit.SetCommand(TExitCommand.Create);
+ // The menu item File|Quit shares the command of btnQuit
+ mnuFile.MenuItemByName('Quit').SetCommand(btnQuit.GetCommand);
+end;
+
+
+end.
diff --git a/examples/gui/command_interface/test.lpi b/examples/gui/command_interface/test.lpi
new file mode 100644
index 00000000..64acd093
--- /dev/null
+++ b/examples/gui/command_interface/test.lpi
@@ -0,0 +1,61 @@
+<?xml version="1.0"?>
+<CONFIG>
+ <ProjectOptions>
+ <PathDelim Value="/"/>
+ <Version Value="6"/>
+ <General>
+ <Flags>
+ <SaveOnlyProjectUnits Value="True"/>
+ </Flags>
+ <SessionStorage Value="InProjectDir"/>
+ <MainUnit Value="0"/>
+ <IconPath Value="./"/>
+ <TargetFileExt Value=""/>
+ </General>
+ <VersionInfo>
+ <ProjectVersion Value=""/>
+ </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/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
+ </local>
+ </RunParams>
+ <RequiredPackages Count="1">
+ <Item1>
+ <PackageName Value="fpgui_package"/>
+ </Item1>
+ </RequiredPackages>
+ <Units Count="3">
+ <Unit0>
+ <Filename Value="test.lpr"/>
+ <IsPartOfProject Value="True"/>
+ <UnitName Value="test"/>
+ </Unit0>
+ <Unit1>
+ <Filename Value="frm_main.pas"/>
+ <IsPartOfProject Value="True"/>
+ <UnitName Value="frm_main"/>
+ </Unit1>
+ <Unit2>
+ <Filename Value="commands.pas"/>
+ <IsPartOfProject Value="True"/>
+ <UnitName Value="commands"/>
+ </Unit2>
+ </Units>
+ </ProjectOptions>
+ <CompilerOptions>
+ <Version Value="5"/>
+ <CodeGeneration>
+ <Generate Value="Faster"/>
+ </CodeGeneration>
+ <Other>
+ <CompilerPath Value="$(CompPath)"/>
+ </Other>
+ </CompilerOptions>
+</CONFIG>
diff --git a/examples/gui/command_interface/test.lpr b/examples/gui/command_interface/test.lpr
new file mode 100644
index 00000000..663e2e2f
--- /dev/null
+++ b/examples/gui/command_interface/test.lpr
@@ -0,0 +1,32 @@
+program test;
+
+{$mode objfpc}{$H+}
+
+uses
+ {$IFDEF UNIX}{$IFDEF UseCThreads}
+ cthreads,
+ {$ENDIF}{$ENDIF}
+ Classes, fpgfx, gui_form, frm_main, commands, fpgui_package;
+
+
+procedure MainProc;
+var
+ frm: TMainForm;
+begin
+ fpgApplication.Initialize;
+ Randomize;
+ frm := TMainForm.Create(nil);
+ try
+ frm.Show;
+ fpgApplication.Run;
+ finally
+ frm.Free;
+ end;
+end;
+
+begin
+ MainProc;
+end.
+
+
+