summaryrefslogtreecommitdiff
path: root/examples/corelib/eventtest/eventtest.lpr
blob: baac5d80ed0ac7ff6e8c75efe3dd3414307ad644 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
program eventtest;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils,
  fpg_base,
  fpg_main,
  fpg_widget;
  

const
  ButtonNames: array[TMouseButton] of PChar =
    ('Left', 'Right', 'Middle');

type

  { TMainForm }

  TMainForm = class(TfpgWindow)
  private
    FMoveEventCount: integer;
    function    ShiftStateToStr(Shift: TShiftState): string;
    function    MouseState(AShift: TShiftState; const AMousePos: TPoint): string;
    procedure   MsgActivate(var msg: TfpgMessageRec); message FPGM_ACTIVATE;
    procedure   MsgDeActivate(var msg: TfpgMessageRec); message FPGM_DEACTIVATE;
    procedure   MsgClose(var msg: TfpgMessageRec); message FPGM_CLOSE;
    procedure   MsgPaint(var msg: TfpgMessageRec); message FPGM_PAINT;
    procedure   MsgResize(var msg: TfpgMessageRec); message FPGM_RESIZE;
    procedure   MsgMove(var msg: TfpgMessageRec); message FPGM_MOVE;
    procedure   MsgKeyChar(var msg: TfpgMessageRec); message FPGM_KEYCHAR;
    procedure   MsgKeyPress(var msg: TfpgMessageRec); message FPGM_KEYPRESS;
    procedure   MsgKeyRelease(var msg: TfpgMessageRec); message FPGM_KEYRELEASE;
    procedure   MsgMouseDown(var msg: TfpgMessageRec); message FPGM_MOUSEDOWN;
    procedure   MsgMouseUp(var msg: TfpgMessageRec); message FPGM_MOUSEUP;
    procedure   MsgMouseMove(var msg: TfpgMessageRec); message FPGM_MOUSEMOVE;
    procedure   MsgDoubleClick(var msg: TfpgMessageRec); message FPGM_DOUBLECLICK;
    procedure   MsgMouseEnter(var msg: TfpgMessageRec); message FPGM_MOUSEENTER;
    procedure   MsgMouseExit(var msg: TfpgMessageRec); message FPGM_MOUSEEXIT;
    procedure   MsgScroll(var msg: TfpgMessageRec); message FPGM_SCROLL;
  protected
  public
    constructor Create(AOwner: TComponent); override;
    procedure   Show;
  end;

{ TMainForm }

function TMainForm.ShiftStateToStr(Shift: TShiftState): string;
begin
  SetLength(Result, 0);
  if ssShift in Shift then
    Result := 'Shift ';
  if ssAlt in Shift then
    Result := Result + 'Alt ';
  if ssCtrl in Shift then
    Result := Result + 'Ctrl ';
  if ssMeta in Shift then
    Result := Result + 'Meta ';
  if ssSuper in Shift then
    Result := Result + 'Super ';
  if ssHyper in Shift then
    Result := Result + 'Hyper ';
  if ssAltGr in Shift then
    Result := Result + 'AltGr ';
  if ssCaps in Shift then
    Result := Result + 'Caps ';
  if ssNum in Shift then
    Result := Result + 'Num ';
  if ssScroll in Shift then
    Result := Result + 'Scroll ';
  if ssLeft in Shift then
    Result := Result + 'Left ';
  if ssRight in Shift then
    Result := Result + 'Right ';
  if ssMiddle in Shift then
    Result := Result + 'Middle ';
  if ssDouble in Shift then
    Result := Result + 'Double ';
  if Length(Result) > 0 then
    SetLength(Result, Length(Result) - 1);

end;

function TMainForm.MouseState(AShift: TShiftState; const AMousePos: TPoint): string;
var
  ShiftStateStr: String;
begin
  ShiftStateStr := ShiftStateToStr(AShift);
  Result := '[X=' + IntToStr(AMousePos.x) + ' Y=' + IntToStr(AMousePos.y);
  if Length(ShiftStateStr) > 0 then
    Result := Result + ' ' + ShiftStateStr;
  Result := Result + '] ';
end;

procedure TMainForm.MsgActivate(var msg: TfpgMessageRec);
begin
  Writeln('Window Activate message');
end;

procedure TMainForm.MsgDeActivate(var msg: TfpgMessageRec);
begin
  Writeln('Window Deactivate message');
end;

procedure TMainForm.MsgClose(var msg: TfpgMessageRec);
begin
  Writeln('Window Close message');
  Halt(0);
end;

procedure TMainForm.MsgPaint(var msg: TfpgMessageRec);
var
  h: integer;
begin
  Writeln('Paint message');
  Canvas.BeginDraw;
  h := Canvas.Font.Height;
  Canvas.SetColor(clWhite);
  Canvas.FillRectangle(0, 0, Width, Height);
  Canvas.SetTextColor(clBlack);
  Canvas.DrawString(0, 0, 'Event test');
  Canvas.DrawString(0, h, 'Do something interactive (move mouse, press keys...)');
  Canvas.DrawString(0, h*2, 'and watch the output on the console.');
  Canvas.EndDraw;
end;

procedure TMainForm.MsgResize(var msg: TfpgMessageRec);
begin
  Writeln('Resize message');
  FWidth  := msg.Params.rect.Width;
  FHeight := msg.Params.rect.Height;
  WriteLn('  Window has been resized. New size: ', Width, ' x ', Height);
end;

procedure TMainForm.MsgMove(var msg: TfpgMessageRec);
begin
  Writeln('Move message');
  WriteLn('  Window has been moved to (', msg.Params.rect.Left, ',', msg.Params.rect.Top, ')');
end;

procedure TMainForm.MsgKeyChar(var msg: TfpgMessageRec);
var
  AKeyChar: TfpgChar;
begin
  Write('Character generated: ');
  AKeyChar := msg.Params.keyboard.keychar;
  if AKeyChar >= ' ' then
    WriteLn('''', AKeyChar, '''')
  else
    WriteLn('#', Ord(AKeyChar[1]));
end;

procedure TMainForm.MsgKeyPress(var msg: TfpgMessageRec);
begin
  WriteLn('[', ShiftStateToStr(msg.Params.keyboard.shiftstate), '] Key pressed: ',
    KeycodeToText(msg.Params.keyboard.keycode, []));
end;

procedure TMainForm.MsgKeyRelease(var msg: TfpgMessageRec);
begin
  WriteLn('[', ShiftStateToStr(msg.Params.keyboard.shiftstate), '] Key released: ',
    KeycodeToText(msg.Params.keyboard.keycode, []));
end;

procedure TMainForm.MsgMouseDown(var msg: TfpgMessageRec);
begin
  WriteLn(MouseState(msg.Params.mouse.shiftstate, Point(msg.Params.mouse.x, msg.Params.mouse.y)),
    'Mouse button pressed: ', ' button=' + IntToStr(msg.Params.mouse.Buttons));
//    ButtonNames[msg.Params.mouse.Buttons]);
end;

procedure TMainForm.MsgMouseUp(var msg: TfpgMessageRec);
begin
  WriteLn(MouseState(msg.Params.mouse.shiftstate, Point(msg.Params.mouse.x, msg.Params.mouse.y)),
    'Mouse button released: ', ' button=' + IntToStr(msg.Params.mouse.Buttons));
//    ButtonNames[msg.Params.mouse.Buttons]);
end;

procedure TMainForm.MsgMouseMove(var msg: TfpgMessageRec);
begin
  inc(FMoveEventCount);
  // only report mouse moves every 10 messages - just to limit the output a bit
  if (FMoveEventCount mod 10) = 0 then
  begin
    WriteLn(MouseState(msg.Params.mouse.shiftstate, Point(msg.Params.mouse.x, msg.Params.mouse.y)), 'Mouse moved');
  end;
end;

procedure TMainForm.MsgDoubleClick(var msg: TfpgMessageRec);
begin
  Writeln('Mouse doubleclick message');
end;

procedure TMainForm.MsgMouseEnter(var msg: TfpgMessageRec);
begin
  WriteLn(MouseState(msg.Params.mouse.shiftstate, Point(msg.Params.mouse.x, msg.Params.mouse.y)), 'Mouse entered window');
end;

procedure TMainForm.MsgMouseExit(var msg: TfpgMessageRec);
begin
  WriteLn('Mouse left window');
end;

procedure TMainForm.MsgScroll(var msg: TfpgMessageRec);
var
  delta: Integer;
begin
  delta := msg.Params.mouse.delta;
  Writeln('Mouse scroll delta=' + IntToStr(delta) + ' button=' + IntToStr(msg.Params.mouse.Buttons));
end;

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited Create(aowner);
  FMoveEventCount := 0;
  FWidth    := 400;
  FHeight   := 100;
  WindowAttributes := [waSizeable, waScreenCenterPos];
end;

procedure TMainForm.Show;
begin
  AllocateWindowHandle;
  DoSetWindowVisible(True);
  // We can't set a title if we don't have a window handle. So we do that here
  // and not in the constructor.
  SetWindowTitle('fpGFX event test');
end;
  
  
procedure MainProc;
var
  frm: TMainForm;
begin
  fpgApplication.Initialize;
  frm := TMainForm.Create(nil);
  frm.Show;
  fpgApplication.Run;
  frm.Free;
end;

begin
  MainProc;
end.