summaryrefslogtreecommitdiff
path: root/src/gui/fpg_form.pas
blob: 7d5fe042a59a0e304daef6e93c9d5f4bffa8c3b9 (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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2011 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 fpg_form;

{$mode objfpc}{$H+}

{.$Define CStackDebug}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_widget;

type
  TWindowPosition = (wpUser, wpAuto, wpScreenCenter, wpOneThirdDown);
  TCloseAction = (caNone, caHide, caFree{, caMinimize});
  
  TFormCloseEvent = procedure(Sender: TObject; var CloseAction: TCloseAction) of object;
  TFormCloseQueryEvent = procedure(Sender: TObject; var CanClose: boolean) of object;
  TfpgHelpEvent = function(AHelpType: THelpType; AHelpContext: THelpContext;
       const AHelpKeyword: String; const AHelpFile: String;
       var AHandled: Boolean): Boolean of object;


  TfpgBaseForm = class(TfpgWidget)
  private
    FFullScreen: boolean;
    FOnActivate: TNotifyEvent;
    FOnClose: TFormCloseEvent;
    FOnCloseQuery: TFormCloseQueryEvent;
    FOnCreate: TNotifyEvent;
    FOnDeactivate: TNotifyEvent;
    FOnDestroy: TNotifyEvent;
    FOnHide: TNotifyEvent;
    FOnShow: TNotifyEvent;
    FOnHelp: TfpgHelpEvent;
    FDNDEnabled: boolean;
    procedure   SetDNDEnabled(const AValue: boolean);
  protected
    FModalResult: TfpgModalResult;
    FParentForm: TfpgBaseForm;
    FWindowPosition: TWindowPosition;
    FWindowTitle: string;
    FSizeable: boolean;
    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   DoOnClose(var CloseAction: TCloseAction); virtual;
    function    DoOnHelp(AHelpType: THelpType; AHelpContext: THelpContext; const AHelpKeyword: String; const AHelpFile: String; var AHandled: Boolean): Boolean; virtual;
    procedure   DoKeyShortcut(const AOrigin: TfpgWidget; const keycode: word; const shiftstate: TShiftState; var consumed: boolean; const IsChildOfOrigin: boolean = False); override;
    { -- properties -- }
    property    DNDEnabled: boolean read FDNDEnabled write SetDNDEnabled default False;
    property    Sizeable: boolean read FSizeable write FSizeable;
    property    ModalResult: TfpgModalResult read FModalResult write FModalResult;
    property    FullScreen: boolean read FFullScreen write FFullScreen default False;
    property    WindowPosition: TWindowPosition read FWindowPosition write FWindowPosition default wpAuto;
    property    WindowTitle: string read FWindowTitle write SetWindowTitle;
    { -- events -- }
    property    OnActivate: TNotifyEvent read FOnActivate write FOnActivate;
    property    OnClose: TFormCloseEvent read FOnClose write FOnClose;
    property    OnCloseQuery: TFormCloseQueryEvent read FOnCloseQuery write FOnCloseQuery;
    property    OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
    property    OnDeactivate: TNotifyEvent read FOnDeactivate write FOnDeactivate;
    property    OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
    property    OnHelp: TfpgHelpEvent read FOnHelp write FOnHelp;
    property    OnHide: TNotifyEvent read FOnHide write FOnHide;
    property    OnShow: TNotifyEvent read FOnShow write FOnShow;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   AfterConstruction; override;
    procedure   BeforeDestruction; override;
    procedure   AfterCreate; virtual;
    procedure   AdjustWindowStyle; override;
    procedure   SetWindowParameters; override;
    procedure   InvokeHelp; override;
    procedure   Show;
    procedure   Hide;
    function    ShowModal: TfpgModalResult;
    procedure   Close;
    function    CloseQuery: boolean; virtual;
  end;
  
  
  TfpgForm = class(TfpgBaseForm)
  published
    property    BackgroundColor;
    property    DNDEnabled;
    property    FullScreen;
    property    Height;
    property    Hint;
    property    Left;
    property    MaxHeight;
    property    MaxWidth;
    property    MinHeight;
    property    MinWidth;
    property    ModalResult;
    property    ShowHint;
    property    Sizeable;
    property    TextColor;
    property    Top;
    property    Width;
    property    WindowPosition;
    property    WindowState;
    property    WindowTitle;
    property    OnActivate;
    property    OnClick;
    property    OnClose;
    property    OnCloseQuery;
    property    OnCreate;
    property    OnDeactivate;
    property    OnDestroy;
    property    OnDoubleClick;
    property    OnEnter;
    property    OnExit;
    property    OnHide;
    property    OnKeyPress;
    property    OnMouseDown;
    property    OnMouseEnter;
    property    OnMouseExit;
    property    OnMouseMove;
    property    OnMouseScroll;
    property    OnMouseUp;
    property    OnPaint;
    property    OnResize;
    property    OnShow;
    property    OnShowHint;
  end;


function WidgetParentForm(wg: TfpgWidget): TfpgForm;


implementation

uses
  fpg_main,
  fpg_popupwindow,
  fpg_menu;
  
type
  // to access protected methods
  TfpgMenuBarFriend = class(TfpgMenuBar)
  end;

  TfpgWidgetFriend = class(TfpgWidget)
  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;

{ TfpgBaseForm }

procedure TfpgBaseForm.SetDNDEnabled(const AValue: boolean);
begin
  if FDNDEnabled = AValue then exit;
  FDNDEnabled := AValue;
  DoDNDEnabled(AValue);
end;

procedure TfpgBaseForm.SetWindowTitle(const ATitle: string);
begin
  FWindowTitle := ATitle;
  inherited SetWindowTitle(ATitle);
end;

procedure TfpgBaseForm.MsgActivate(var msg: TfpgMessageRec);
begin
  {$IFDEF DEBUG}
  DebugLn(Classname + ' ' + Name + '.BaseForm - MsgActivate');
  {$ENDIF}
  if (fpgApplication.TopModalForm = nil) or (fpgApplication.TopModalForm = self) then
  begin
    {$IFDEF DEBUG}
    DebugLn('Inside if block');
    {$ENDIF}
    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 TfpgBaseForm.MsgDeActivate(var msg: TfpgMessageRec);
begin
  ClosePopups;
  if ActiveWidget <> nil then
    ActiveWidget.KillFocus;
  if Assigned(FOnDeactivate) then
    FOnDeactivate(self);
end;

procedure TfpgBaseForm.HandlePaint;
begin
  inherited HandlePaint;
  Canvas.Clear(FBackgroundColor);
end;

procedure TfpgBaseForm.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 FWindowPosition = wpOneThirdDown then
    Include(FWindowAttributes, waOneThirdDownPos)
  else
    Exclude(FWindowAttributes, waOneThirdDownPos);

  if FSizeable then
    Include(FWindowAttributes, waSizeable)
  else
    Exclude(FWindowAttributes, waSizeable);
    
  if FFullScreen then
    Include(FWindowAttributes, waFullScreen)
  else
    Exclude(FWindowAttributes, waFullScreen);
end;

procedure TfpgBaseForm.SetWindowParameters;
begin
  inherited;
  DoSetWindowTitle(FWindowTitle);
end;

constructor TfpgBaseForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWindowPosition  := wpAuto;
  FWindowTitle     := '';
  FSizeable        := True;
  FParentForm      := nil;
  FBackgroundColor := clWindowBackground;
  FTextColor       := clText1;
  FMinWidth        := 32;
  FMinHeight       := 32;
  FModalResult     := mrNone;
  FFullScreen      := False;
  FIsContainer     := True;
  FDNDEnabled      := False;
end;

destructor TfpgBaseForm.Destroy;
begin
  fpgApplication.RemoveWindowFromModalStack(Self);
  inherited Destroy;
end;

procedure TfpgBaseForm.AfterCreate;
begin
  // for the user
end;

procedure TfpgBaseForm.InvokeHelp;
var
  lEventHandled: Boolean;
  lSucceeded: Boolean;
begin
  lEventHandled := False;
  lSucceeded := False;
  lSucceeded := DoOnHelp(HelpType, HelpContext, HelpKeyword, fpgApplication.HelpFile, lEventHandled);
  if (not lSucceeded) or (not lEventHandled) then
    inherited InvokeHelp;
end;

procedure TfpgBaseForm.Show;
{$IFDEF CStackDebug}
var
  itf: IInterface;
{$ENDIF}
begin
  {$IFDEF CStackDebug}
  itf := DebugMethodEnter('TfpgBaseForm.Show - ' + ClassName + ' ('+Name+')');
  {$ENDIF}
  FVisible := True;
  HandleShow;
end;

function TfpgBaseForm.ShowModal: TfpgModalResult;
var
  lCloseAction: TCloseAction;
begin
  FWindowType := wtModalForm;
  fpgApplication.PushModalForm(self);
  ModalResult := mrNone;

  try
    Show;
    // processing messages until this form ends.
    // delivering the remaining messages
    fpgApplication.ProcessMessages;
    try
      repeat
        fpgWaitWindowMessage;
      until (ModalResult <> mrNone) or (not Visible);
    except
      on E: Exception do
      begin
        Visible := False;
        fpgApplication.HandleException(self);
      end;
    end;  { try..except }
  finally
    { we have to pop the form even in an error occurs }
    fpgApplication.PopModalForm;
    Result := ModalResult;
  end;  { try..finally }
  
  if ModalResult <> mrNone then
  begin
    lCloseAction := caHide; // Dummy variable - we do nothing with it
    DoOnClose(lCloseAction); // Simply so the OnClose event fires.
  end;
end;

procedure TfpgBaseForm.MsgClose(var msg: TfpgMessageRec);
begin
  HandleClose;
end;

procedure TfpgBaseForm.HandleClose;
begin
  Close;
end;

procedure TfpgBaseForm.HandleHide;
begin
  if Assigned(FOnHide) then
    FOnHide(self);
  inherited HandleHide;
end;

procedure TfpgBaseForm.HandleShow;
begin
  inherited HandleShow;
  HandleAlignments(0, 0);
  if Assigned(FOnShow) then
    FOnShow(self);
end;

procedure TfpgBaseForm.HandleMove(x, y: TfpgCoord);
{$IFDEF CStackDebug}
var
  itf: IInterface;
{$ENDIF}
begin
  {$IFDEF CStackDebug}
  itf := DebugMethodEnter('TfpgBaseForm.HandleMove - ' + ClassName + ' ('+Name+')');
  DebugLn(Format('x:%d  y:%d', [x, y]));
  {$ENDIF}
  ClosePopups;
  inherited HandleMove(x, y);
end;

procedure TfpgBaseForm.HandleResize(awidth, aheight: TfpgCoord);
{$IFDEF CStackDebug}
var
  i: iinterface;
{$ENDIF}
begin
  {$IFDEF CStackDebug}
  DebugMethodEnter('TfpgBaseForm.HandleResize - ' + ClassName + ' ('+Name+')');
  DebugLn(Format('w:%d  h:%d', [awidth, aheight]));
  {$ENDIF}
  ClosePopups;
  inherited HandleResize(awidth, aheight);
end;

procedure TfpgBaseForm.AfterConstruction;
begin
  AfterCreate;
  inherited AfterConstruction;
  if Assigned(FOnCreate) then
    FOnCreate(self);
end;

procedure TfpgBaseForm.BeforeDestruction;
begin
  inherited BeforeDestruction;
  if Assigned(FOnDestroy) then
    FOnDestroy(self);
end;

procedure TfpgBaseForm.DoOnClose(var CloseAction: TCloseAction);
begin
  if Assigned(FOnClose) then
    OnClose(self, CloseAction);
end;

function TfpgBaseForm.DoOnHelp(AHelpType: THelpType; AHelpContext: THelpContext;
  const AHelpKeyword: String; const AHelpFile: String; var AHandled: Boolean): Boolean;
begin
  if Assigned(FOnHelp) then
    Result := FOnHelp(AHelpType, AHelpContext, AHelpKeyword, AHelpFile, AHandled);
end;

procedure TfpgBaseForm.DoKeyShortcut(const AOrigin: TfpgWidget;
  const keycode: word; const shiftstate: TShiftState; var consumed: boolean; const IsChildOfOrigin: boolean = False);
var
  wg: TfpgWidget;
  menu: TfpgMenuBar;
  i: integer;
  ss: TShiftState;
  key: word;
  c: TfpgComponent;

  function FindMenuBar(AWidget: TfpgWidget): TfpgWidget;
  var
    n: integer;
    w: TfpgWidget;
  begin
    Result := nil;
    for n := 0 to AWidget.ComponentCount-1 do
    begin
      w := TfpgWidget(AWidget.Components[n]);
      if (w <> nil) and (w <> self) and (w <> AOrigin) and (w is TfpgMenuBar) then
      begin
        Result := w;
        exit;
      end;
      if w.ComponentCount > 0 then
        Result := FindMenuBar(w);
      if Result <> nil then
        exit;
    end;
  end;

begin
  // find the first TfpgMenuBar - if it exits
  wg := FindMenuBar(self);
  if (wg <> nil) then
  begin
    menu := wg as TfpgMenuBar;
    key := keycode;
    ss := shiftstate;
    TfpgMenuBarFriend(wg).HandleKeyPress(key, ss, consumed);
  end;

  if consumed then
    Exit;
  // now send to each widget on the form - excluding AOrigin and MenuBar widgets
  for i := 0 to ComponentCount-1 do
  begin
    c := TfpgComponent(Components[i]);
    if c is TfpgWidget then
      wg := TfpgWidget(c)
    else
      wg := nil;
    if (wg <> nil) and (wg <> self) and (wg <> AOrigin) and (wg <> menu) and (not (wg is TfpgPopupMenu)) then
    begin
      if (not wg.Visible) or (not wg.Enabled) then
        continue
      else
      begin
        TfpgWidgetFriend(wg).DoKeyShortcut(AOrigin, keycode, shiftstate, consumed);
        if consumed then
          Exit;
      end;
    end;
  end;
end;

procedure TfpgBaseForm.Hide;
begin
  Visible := False;
end;

procedure TfpgBaseForm.Close;
var
  CloseAction: TCloseAction;
  IsMainForm: Boolean;
begin
  if CloseQuery then  // May we close the form? User could override decision
  begin
    IsMainForm := fpgApplication.MainForm = self;
    if IsMainForm then
      CloseAction := caFree
    else
      CloseAction := caHide;

    // execute event handler - maybe user wants to modify it.
    DoOnClose(CloseAction);
    // execute action according to close action
    case CloseAction of
      caHide:
        begin
          Hide;
        end;
        // fpGUI Forms don't have a WindowState property yet!
//      caMinimize: WindowState := wsMinimized;
      caFree:
        begin
          HandleHide;
          if IsMainForm then
            fpgApplication.Terminate
          else
            // We can't free ourselves, somebody else needs to do it
            fpgPostMessage(Self, fpgApplication, FPGM_FREEME);
        end;
    end;  { case CloseAction }
  end;  { if CloseQuery }
end;

function TfpgBaseForm.CloseQuery: boolean;
begin
  Result := True;
  if Assigned(FOnCloseQuery) then
    FOnCloseQuery(self, Result);
end;


end.