summaryrefslogtreecommitdiff
path: root/examples/gui/command_interface/commands.pas
blob: aa9482202120d3f48b41757eb67c8a342fabf97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{
  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+}

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.