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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
{
fpGUI - Free Pascal GUI Library
Copyright (C) 2006 - 2008 See the file AUTHORS.txt, included in this
distribution, for details of the copyright.
See the file COPYING.modifiedLGPL, included in this distribution,
for details about redistributing fpGUI.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Description:
Defines a Form control. Also known as a Window which holds other
controls.
}
unit gui_form;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
gfxbase,
gfx_widget;
type
TWindowPosition = (wpUser, wpAuto, wpScreenCenter);
TfpgForm = class(TfpgWidget)
private
FFullScreen: boolean;
FOnActivate: TNotifyEvent;
FOnClose: TNotifyEvent;
FOnCreate: TNotifyEvent;
FOnDeactivate: TNotifyEvent;
FOnDestroy: TNotifyEvent;
FOnHide: TNotifyEvent;
FOnShow: TNotifyEvent;
protected
FModalResult: integer;
FParentForm: TfpgForm;
FWindowPosition: TWindowPosition;
FWindowTitle: string;
FSizeable: boolean;
procedure AdjustWindowStyle; override;
procedure SetWindowParameters; override;
procedure SetWindowTitle(const ATitle: string); override;
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 HandlePaint; override;
procedure HandleClose; virtual;
procedure HandleHide; override;
procedure HandleShow; override;
procedure HandleMove(x, y: TfpgCoord); override;
procedure HandleResize(awidth, aheight: TfpgCoord); override;
procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure DoOnClose; virtual;
public
constructor Create(AOwner: TComponent); override;
procedure AfterCreate; virtual;
procedure Show;
procedure Hide;
function ShowModal: integer;
procedure Close;
property Sizeable: boolean read FSizeable write FSizeable;
property ModalResult: integer read FModalResult write FModalResult;
property FullScreen: boolean read FFullScreen write FFullScreen default False;
published
{$Note Refactor this to a TfpgCustomForm and only surface it here }
property BackgroundColor;
property TextColor;
property WindowPosition: TWindowPosition read FWindowPosition write FWindowPosition default wpAuto;
property WindowTitle: string read FWindowTitle write SetWindowTitle;
property OnActivate: TNotifyEvent read FOnActivate write FOnActivate;
property OnClose: TNotifyEvent read FOnClose write FOnClose;
property OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
property OnDeactivate: TNotifyEvent read FOnDeactivate write FOnDeactivate;
property OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
property OnHide: TNotifyEvent read FOnHide write FOnHide;
property OnPaint;
property OnShow: TNotifyEvent read FOnShow write FOnShow;
end;
function WidgetParentForm(wg: TfpgWidget): TfpgForm;
implementation
uses
fpgfx,
gfx_popupwindow,
gui_menu;
type
// to access protected methods
TfpgMenuBarFriend = class(TfpgMenuBar)
end;
function WidgetParentForm(wg: TfpgWidget): TfpgForm;
var
w: TfpgWidget;
begin
w := wg;
while w <> nil do
begin
if w is TfpgForm then
begin
Result := TfpgForm(w);
Exit; //==>
end;
w := w.Parent;
end;
Result := nil;
end;
{ TfpgForm }
procedure TfpgForm.SetWindowTitle(const ATitle: string);
begin
FWindowTitle := ATitle;
inherited SetWindowTitle(ATitle);
end;
procedure TfpgForm.MsgActivate(var msg: TfpgMessageRec);
begin
if (fpgApplication.TopModalForm = nil) or (fpgApplication.TopModalForm = self) then
begin
FocusRootWidget := self;
if FFormDesigner <> nil then
begin
FFormDesigner.Dispatch(msg);
Exit;
end;
if ActiveWidget = nil then
ActiveWidget := FindFocusWidget(nil, fsdFirst)
else
ActiveWidget.SetFocus;
end;
if Assigned(FOnActivate) then
FOnActivate(self);
end;
procedure TfpgForm.MsgDeActivate(var msg: TfpgMessageRec);
begin
ClosePopups;
if ActiveWidget <> nil then
ActiveWidget.KillFocus;
if Assigned(FOnDeactivate) then
FOnDeactivate(self);
end;
procedure TfpgForm.HandlePaint;
begin
Canvas.BeginDraw;
inherited;
Canvas.Clear(FBackgroundColor);
Canvas.EndDraw(0, 0, FWidth, FHeight);
end;
procedure TfpgForm.AdjustWindowStyle;
begin
if fpgApplication.MainForm = nil then
fpgApplication.MainForm := self;
if FWindowPosition = wpAuto then
Include(FWindowAttributes, waAutoPos)
else
Exclude(FWindowAttributes, waAutoPos);
if FWindowPosition = wpScreenCenter then
Include(FWindowAttributes, waScreenCenterPos)
else
Exclude(FWindowAttributes, waScreenCenterPos);
if FSizeable then
Include(FWindowAttributes, waSizeable)
else
Exclude(FWindowAttributes, waSizeable);
if FFullScreen then
Include(FWindowAttributes, waFullScreen)
else
Exclude(FWindowAttributes, waFullScreen);
end;
procedure TfpgForm.SetWindowParameters;
begin
inherited;
DoSetWindowTitle(FWindowTitle);
end;
constructor TfpgForm.Create(AOwner: TComponent);
begin
inherited;
FWindowPosition := wpAuto;
FWindowTitle := '';
FSizeable := True;
FParentForm := nil;
FBackgroundColor := clWindowBackground;
FTextColor := clText1;
FMinWidth := 32;
FMinHeight := 32;
FModalResult := 0;
FFullScreen := False;
AfterCreate;
end;
procedure TfpgForm.AfterCreate;
begin
// for the user
end;
procedure TfpgForm.Show;
begin
FVisible := True;
HandleShow;
end;
function TfpgForm.ShowModal: integer;
begin
FWindowType := wtModalForm;
fpgApplication.PushModalForm(self);
ModalResult := 0;
Show;
// processing messages until this form ends.
// delivering the remaining messages
fpgApplication.ProcessMessages;
repeat
fpgWaitWindowMessage;
until (ModalResult <> 0) or (not Visible);
fpgApplication.PopModalForm;
Result := ModalResult;
end;
procedure TfpgForm.MsgClose(var msg: TfpgMessageRec);
begin
HandleClose;
end;
procedure TfpgForm.HandleClose;
begin
Close;
end;
procedure TfpgForm.HandleHide;
begin
if Assigned(FOnHide) then
FOnHide(self);
inherited HandleHide;
end;
procedure TfpgForm.HandleShow;
begin
inherited HandleShow;
if Assigned(FOnShow) then
FOnShow(self);
end;
procedure TfpgForm.HandleMove(x, y: TfpgCoord);
begin
ClosePopups;
inherited HandleMove(x, y);
end;
procedure TfpgForm.HandleResize(awidth, aheight: TfpgCoord);
begin
ClosePopups;
inherited HandleResize(awidth, aheight);
end;
procedure TfpgForm.HandleKeyPress(var keycode: word;
var shiftstate: TShiftState; var consumed: boolean);
var
i: integer;
wg: TfpgWidget;
begin
// writeln(Classname, '.Keypress');
// find the TfpgMenuBar
if not consumed then
begin
for i := 0 to ComponentCount-1 do
begin
wg := TfpgWidget(Components[i]);
if (wg <> nil) and (wg <> self) and (wg is TfpgMenuBar) then
begin
TfpgMenuBarFriend(wg).HandleKeyPress(keycode, shiftstate, consumed);
Break; //==>
end;
end;
end; { if }
inherited HandleKeyPress(keycode, shiftstate, consumed);
end;
procedure TfpgForm.AfterConstruction;
begin
inherited AfterConstruction;
if Assigned(FOnCreate) then
FOnCreate(self);
end;
procedure TfpgForm.BeforeDestruction;
begin
inherited BeforeDestruction;
if Assigned(FOnDestroy) then
FOnDestroy(self);
end;
procedure TfpgForm.DoOnClose;
begin
if Assigned(FOnClose) then
OnClose(self);
end;
procedure TfpgForm.Hide;
begin
if (fpgApplication.TopModalForm = self) then
fpgApplication.PopModalForm;
HandleHide;
if ModalResult = 0 then
ModalResult := -1;
end;
procedure TfpgForm.Close;
begin
DoOnClose;
Hide;
fpgApplication.RemoveComponent(self);
if fpgApplication.MainForm = self then
fpgApplication.Terminated := True;
end;
end.
|