summaryrefslogtreecommitdiff
path: root/gui/form.inc
blob: 10d18450015e86a936645e84fb72a56fdf9e4e5e (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
576
{
    fpGUI  -  Free Pascal Graphical User Interface
    Copyright (C) 2000 - 2001 by
      Areca Systems GmbH / Sebastian Guenther
    Copyright (C) 2006 by Graeme Geldenhuys 
      member of the fpGUI development team.

    Form class declarations

    See the file COPYING.fpGUI, included in this distribution,
    for details about the copyright.

    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.

 **********************************************************************}


{%mainunit fpgui.pp}

{$IFDEF read_interface}

  { TCustomForm }

  TCustomForm = class(TBinWidget)
  private
    FFocusedWidget: TWidget;
    FMouseCaptureWidget: TWidget;
    FLastSetCursor: TGfxCursor;
    FWindowOptions: TGfxWindowOptions;
    FWnd: TFCustomWindow;
    FOnCreate: TNotifyEvent;
    FOnDestroy: TNotifyEvent;
    FOnActivate: TNotifyEvent;
    FOnDeactivate: TNotifyEvent;
    // Property access
    function    GetWnd: TFCustomWindow;
    procedure   SetFocusedWidget(AWidget: TWidget);
    procedure   SetMouseCaptureWidget(AWidget: TWidget);
    procedure   SetWindowOptions(const AValue: TGfxWindowOptions);
    // fpGFX event handling
    procedure   WndClose(Sender: TObject);
    procedure   WndFocusIn(Sender: TObject);
    procedure   WndFocusOut(Sender: TObject);
    procedure   WndHide(Sender: TObject);
    procedure   WndKeyPressed(Sender: TObject; AKey: Word; AShift: TShiftState);
    procedure   WndKeyReleased(Sender: TObject; AKey: Word; AShift: TShiftState);
    procedure   WndKeyChar(Sender: TObject; AKeyChar: Char);
    procedure   WndMouseEnter(Sender: TObject; AShift: TShiftState; const AMousePos: TPoint);
    procedure   WndMouseLeave(Sender: TObject);
    procedure   WndMouseMoved(Sender: TObject; AShift: TShiftState; const AMousePos: TPoint);
    procedure   WndMousePressed(Sender: TObject; AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
    procedure   WndMouseReleased(Sender: TObject; AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
    procedure   WndMouseWheel(Sender: TObject; AShift: TShiftState; AWheelDelta: Single; const AMousePos: TPoint);
    procedure   WndPaint(Sender: TObject; const ARect: TRect);
    procedure   WndMove(Sender: TObject);
    procedure   WndResize(Sender: TObject);
    procedure   WndShow(Sender: TObject);
  protected
    FBorderWidth: Integer;
    FIsActive, FResizedByUser, FPositionSpecified: Boolean;
    procedure   Loaded; override;
    procedure   Paint(Canvas: TFCanvas); override;
    procedure   Resized; override;
    function    WidgetCoords(AWidget: TWidget): TPoint;
    function    ProcessEvent(Event: TEventObj): Boolean; override;
    procedure   CalcSizes; override;
    procedure   EvTextChanged; override;
    procedure   CreateWnd;
    property    CanExpandWidth default True;
    property    CanExpandHeight default True;
    property    Cursor default crArrow;
    property    BorderWidth: Integer read FBorderWidth write FBorderWidth;
    property    OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
    property    OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
    property    OnActivate: TNotifyEvent read FOnActivate write FOnActivate;
    property    OnDeactivate: TNotifyEvent read FOnDeactivate write FOnDeactivate;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   Show; override;
    procedure   Close; virtual;
    procedure   SetPosition(APosition: TPoint);
    property    FocusedWidget: TWidget read FFocusedWidget write SetFocusedWidget;
    property    IsActive: Boolean read FIsActive;
    property    MouseCaptureWidget: TWidget read FMouseCaptureWidget write SetMouseCaptureWidget;
    property    WindowOptions: TGfxWindowOptions read FWindowOptions write SetWindowOptions;
    property    Wnd: TFCustomWindow read GetWnd;
  end;


  TForm = class(TCustomForm)
  published
    property Enabled;
    property BorderWidth;
    property WindowOptions;
    property Text;
    property OnCreate;
    property OnDestroy;
    property OnActivate;
    property OnDeactivate;
  end;

{$ENDIF read_interface}



{$IFDEF read_implementation}

// ===================================================================
//   TCustomForm
// ===================================================================

constructor TCustomForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FCanExpandWidth   := True;
  FCanExpandHeight  := True;
  FCursor           := crArrow;
  FWindowOptions    := [woWindow];

  if not Assigned(FStyle) then
    FStyle := gStyleManager.DefaultStyle;
end;


destructor TCustomForm.Destroy;
begin
  if Assigned(OnDestroy) then
    OnDestroy(Self);
  if Assigned(FWnd) then
    FWnd.Free;
  gApplication.Forms.Remove(Self);
  inherited Destroy;
end;


procedure TCustomForm.Show;
begin
  LAYOUTTRACE('TCustomForm.Show for %s:%s', [Name, ClassName]);

  if Assigned(Wnd) then ; // this makes sure that wnd is created

  FVisible := True;
  FWnd.Show;
end;


procedure TCustomForm.Close;
begin
  FVisible := False;
  FWnd.Free;
  FWnd := nil;
end;


procedure TCustomForm.SetPosition(APosition: TPoint);
begin
  if Assigned(FWnd) then
    Wnd.SetPosition(APosition)
  else
  begin
    FOrigin := APosition;
    FPositionSpecified := True;
  end;
end;


function TCustomForm.WidgetCoords(AWidget: TWidget): TPoint;
begin
  Result := Point(0, 0);
  while AWidget <> Self do
  begin
    Result := Result + AWidget.Parent.ClientToWidget(AWidget.Origin);
    AWidget := AWidget.Parent;
  end;
end;


procedure TCustomForm.Loaded;
begin
  inherited Loaded;
  if Assigned(OnCreate) then
    OnCreate(Self);
end;


procedure TCustomForm.Paint(Canvas: TFCanvas);
begin
  Style.DrawWindowBackground(Canvas, Rect(0, 0, Width, Height));
end;


procedure TCustomForm.CalcSizes;
begin
  if Assigned(Child) then
  begin
    FMinSize := Child.MinSize + 2 * BorderWidth;
    FDefSize := Child.DefSize + 2 * BorderWidth;
    FMaxSize.cx := Min(InfiniteSize, Child.MaxSize.cx + 2 * BorderWidth);
    FMaxSize.cy := Min(InfiniteSize, Child.MaxSize.cy + 2 * BorderWidth);
  end;
end;


function TCustomForm.ProcessEvent(Event: TEventObj): Boolean;
begin
  if Event is TDestroyEventObj then
    FWnd := nil;

  Result := inherited ProcessEvent(Event);
end;


procedure TCustomForm.EvTextChanged;
begin
  if Assigned(FWnd) then
    Wnd.Title := Text;
end;


procedure TCustomForm.CreateWnd;
var
  ParentWnd: TFCustomWindow;
begin
  if Parent is TCustomForm then
    ParentWnd := TCustomForm(Parent).Wnd
  else
    ParentWnd := nil;

  FWnd :=  TFWindow.Create(ParentWnd, [woWindow]);
//  FWnd := Application.DefaultScreen.CreateWindow(ParentWnd, [woWindow]);
  if FPositionSpecified then
    Wnd.SetPosition(Origin);

  Wnd.OnClose           := @WndClose;
  Wnd.OnFocusIn         := @WndFocusIn;
  Wnd.OnFocusOut        := @WndFocusOut;
  Wnd.OnHide            := @WndHide;
  Wnd.OnKeyPressed      := @WndKeyPressed;
  Wnd.OnKeyReleased     := @WndKeyReleased;
  Wnd.OnKeyChar         := @WndKeyChar;
  Wnd.OnMouseEnter      := @WndMouseEnter;
  Wnd.OnMouseLeave      := @WndMouseLeave;
  Wnd.OnMouseMove       := @WndMouseMoved;
  Wnd.OnMousePressed    := @WndMousePressed;
  Wnd.OnMouseReleased   := @WndMouseReleased;
  Wnd.OnMouseWheel      := @WndMouseWheel;
  Wnd.OnPaint           := @WndPaint;
  Wnd.OnMove            := @WndMove;
  Wnd.OnResize          := @WndResize;
  Wnd.OnShow            := @WndShow;

  if Length(Text) = 0 then
    Wnd.Title := gApplication.Title
  else
    Wnd.Title := Text;
end;


{
procedure TCustomForm.ApplyNewLayout;
var
  OrigW, OrigH: Integer;
begin
  Wnd.SetMinMaxClientSize(MinW, MinH, MaxW, MaxH);

  OrigW := ClientRect.Right;
  OrigH := ClientRect.Bottom;

  if (ClientRect.Right < MinW) or (ClientRect.Bottom < MinW) or
    (ClientRect.Right > MaxW) or (ClientRect.Bottom > MaxH) then
  begin
    if ClientRect.Right < MinW then
      FClientRect.Right := MinW;
    if ClientRect.Bottom < MinH then
      FClientRect.Bottom := MinH;
    if ClientRect.Right > MaxW then
      FClientRect.Right := MaxW;
    if ClientRect.Bottom > MaxH then
      FClientRect.Bottom := MaxH;
//    Wnd.SetClientSize(ClientRect.Right, ClientRect.Bottom);
  end;

  if not FResizedByUser then
  begin
    FClientRect.Right := DefW;
    FClientRect.Bottom := DefH;
  end;

  if (ClientRect.Right <> OrigW) or (ClientRect.Bottom <> OrigH) then
  begin
    LAYOUTTRACE('TCustomForm.EvRecalcLayout for %s:%s: Setting size to %dx%d',
      [Name, ClassName, ClientRect.Right, ClientRect.Bottom]);
    Wnd.SetClientSize(ClientRect.Right, ClientRect.Bottom);
  end;
end;}


procedure TCustomForm.Resized;
begin
  ClientRect.Right := Wnd.ClientWidth;
  ClientRect.Bottom := Wnd.ClientHeight;
  if Assigned(Child) then
    Child.SetBounds(Point(BorderWidth, BorderWidth),
      TSize(ClientRect.BottomRight) - 2 * BorderWidth);
end;


procedure TCustomForm.SetFocusedWidget(AWidget: TWidget);
begin
  if AWidget <> FocusedWidget then
  begin
    if Assigned(FocusedWidget) then
    begin
      Exclude(FFocusedWidget.WidgetState, wsHasFocus);
      FocusedWidget.EvFocusChanged;
    end;
    FFocusedWidget := AWidget;
    if Assigned(FocusedWidget) then
    begin
      Include(FFocusedWidget.WidgetState, wsHasFocus);
      FocusedWidget.EvFocusChanged;
    end;
  end;
end;

function TCustomForm.GetWnd: TFCustomWindow;
begin
  if not Assigned(FWnd) then
  begin
    CreateWnd;
    // !!!: Doesn't handle a set initial size yet
    SendEvent(TCalcSizesEventObj.Create(Self));
    Wnd.SetMinMaxClientSize(MinSize, MaxSize);
    Wnd.SetClientSize(DefSize);
  end;
  Result := FWnd;
end;


procedure TCustomForm.SetMouseCaptureWidget(AWidget: TWidget);
begin
  if AWidget <> FMouseCaptureWidget then
  begin
    FMouseCaptureWidget := AWidget;
    if Assigned(AWidget) then
      Wnd.CaptureMouse
    else
      Wnd.ReleaseMouse;
  end;
end;

procedure TCustomForm.SetWindowOptions(const AValue: TGfxWindowOptions);
begin
  if FWindowOptions=AValue then exit;
  FWindowOptions:=AValue;
  if Assigned(FWnd) then Wnd.WindowOptions := AValue;
end;


// GfxWindow message handlers

procedure TCustomForm.WndClose(Sender: TObject);
begin
  SendEvent(TDestroyEventObj.Create(Self));
  FMouseCaptureWidget := nil;
end;

procedure TCustomForm.WndFocusIn(Sender: TObject);
begin
  FIsActive := True;
  if Assigned(FocusedWidget) then
    FocusedWidget.EvFocusChanged;
  if Assigned(OnActivate) then
    OnActivate(Self);
end;

procedure TCustomForm.WndFocusOut(Sender: TObject);
begin
  FIsActive := False;
  if Assigned(FocusedWidget) then
    FocusedWidget.EvFocusChanged;
  if Assigned(OnDeactivate) then
    OnDeactivate(Self);
end;

procedure TCustomForm.WndHide(Sender: TObject);
begin
  LAYOUTTRACE('TCustomForm.WndHide for %s:%s', [Name, ClassName]);
  if wsIsVisible in WidgetState then
  begin
    Exclude(WidgetState, wsIsVisible);
    SendEvent(TVisibilityChangeEventObj.Create(Self));
    Update;
  end;
end;

procedure TCustomForm.WndKeyPressed(Sender: TObject;
  AKey: Word; AShift: TShiftState);
begin
  if Assigned(FocusedWidget) then
    FocusedWidget.EvKeyPressed(AKey, AShift)
  else
    EvKeyPressed(AKey, AShift);
end;

procedure TCustomForm.WndKeyReleased(Sender: TObject;
  AKey: Word; AShift: TShiftState);
begin
  if Assigned(FocusedWidget) then
    FocusedWidget.EvKeyReleased(AKey, AShift)
  else
    EvKeyReleased(AKey, AShift);
end;

procedure TCustomForm.WndKeyChar(Sender: TObject; AKeyChar: Char);
begin
  if Assigned(FocusedWidget) then
    FocusedWidget.EvKeyChar(AKeyChar)
  else
    EvKeyChar(AKeyChar);
end;

procedure TCustomForm.WndMouseEnter(Sender: TObject;
  AShift: TShiftState; const AMousePos: TPoint);
begin
  if wsEnabled in WidgetState then
    DoMouseEnter(AShift, AMousePos);
end;


procedure TCustomForm.WndMouseLeave(Sender: TObject);
begin
  if wsEnabled in WidgetState then
    SendEvent(TMouseLeaveEventObj.Create(Self));
end;


procedure TCustomForm.WndMouseMoved(Sender: TObject;
  AShift: TShiftState; const AMousePos: TPoint);
{var
  dx, dy: Integer;
  IsInside: Boolean;
begin
  if Assigned(MouseCaptureWidget) then
  begin
    WidgetCoords(MouseCaptureWidget, dx, dy);

    // Emulate MouseEnter/MouseLeave events
    IsInside := (x >= dx) and (y >= dy) and
      (x < dx + MouseCaptureWidget.Width) and (y < dy + MouseCaptureWidget.Height);
    if IsInside and not (wsMouseInside in MouseCaptureWidget.WidgetState) then
      MouseCaptureWidget.EvMouseEnter(Shift, x - dy, y - dy)
    else if (not IsInside) and (wsMouseInside in MouseCaptureWidget.WidgetState) then
      MouseCaptureWidget.EvMouseLeave;

    MouseCaptureWidget.SendEvent(
     TMouseMovedEventObj.Create(Self, Shift, x - dx, y - dy));
  end else}

  procedure SendMouseEvents(Widget: TWidget; APos: TPoint);
  var
    LeaveCheckEvent: TMouseLeaveCheckEventObj;
  begin
    LeaveCheckEvent := TMouseLeaveCheckEventObj.Create(Self, AShift, APos);
    LeaveCheckEvent.AddRef;
    Widget.SendEvent(LeaveCheckEvent);
    Widget.SendEvent(TMouseMoveEventObj.Create(Self, AShift, APos));
    if (LeaveCheckEvent.NewCursor <> crDefault) and
      (LeaveCheckEvent.NewCursor <> Wnd.Cursor) then
      Wnd.Cursor := LeaveCheckEvent.NewCursor;
    LeaveCheckEvent.Free;
  end;

begin
  if wsEnabled in WidgetState then
  begin
    if Assigned(MouseCaptureWidget) then
    begin
      SendMouseEvents(MouseCaptureWidget,
        AMousePos - WidgetCoords(MouseCaptureWidget));
      if not Assigned(MouseCaptureWidget) then
	      SendMouseEvents(Self, AMousePos);
    end else
      SendMouseEvents(Self, AMousePos);
  end;  { if }
end;


procedure TCustomForm.WndMousePressed(Sender: TObject; AButton: TMouseButton;
  AShift: TShiftState; const AMousePos: TPoint);
begin
  if wsEnabled in WidgetState then
  begin
    if Assigned(MouseCaptureWidget) then
    begin
      MouseCaptureWidget.SendEvent(
          TMousePressedEventObj.Create(Self, AButton, AShift,
	        AMousePos - WidgetCoords(MouseCaptureWidget)));
      if not Assigned(MouseCaptureWidget) then
	      SendEvent(TMouseMoveEventObj.Create(Self, AShift, AMousePos));
    end
    else
      SendEvent(TMousePressedEventObj.Create(Self, AButton, AShift, AMousePos));
  end;  { if }
end;


procedure TCustomForm.WndMouseReleased(Sender: TObject; AButton: TMouseButton;
  AShift: TShiftState; const AMousePos: TPoint);
begin
  if wsEnabled in WidgetState then
    if Assigned(MouseCaptureWidget) then
    begin
      MouseCaptureWidget.SendEvent(
        TMouseReleasedEventObj.Create(Self, AButton, AShift,
	  AMousePos - WidgetCoords(MouseCaptureWidget)));
      if not Assigned(MouseCaptureWidget) then
	SendEvent(TMouseMoveEventObj.Create(Self, AShift, AMousePos));
    end else
      SendEvent(TMouseReleasedEventObj.Create(Self,
        AButton, AShift, AMousePos));
end;


procedure TCustomForm.WndMouseWheel(Sender: TObject; AShift: TShiftState;
  AWheelDelta: Single; const AMousePos: TPoint);
begin
  if wsEnabled in WidgetState then
    SendEvent(TMouseWheelEventObj.Create(Self, AShift, AWheelDelta, AMousePos));
end;


procedure TCustomForm.WndPaint(Sender: TObject; const ARect: TRect);
begin
  LAYOUTTRACE('TCustomForm.WndPaint for %s:%s (%d/%d-%d/%d)',
    [Name, ClassName, ARect.Left, ARect.Top, ARect.Right, ARect.Bottom]);
  if wsIsVisible in WidgetState then
    SendEvent(TPaintEventObj.Create(Self, TFCanvas(Wnd.Canvas)));
end;


procedure TCustomForm.WndMove(Sender: TObject);
begin
  FOrigin := Point(Wnd.Left, Wnd.Top);
end;


procedure TCustomForm.WndResize(Sender: TObject);
begin
  LAYOUTTRACE('TCustomForm.WndResize for %s:%s: New size is %dx%d. Visible? %d',
    [Name, ClassName, Wnd.Width, Wnd.Height, Ord(wsIsVisible in WidgetState)]);
  if Visible or (wsIsVisible in WidgetState) then
  begin
    FResizedByUser := (Wnd.Width <> DefSize.cx) or (Wnd.Height <> DefSize.cy);
    SetBounds(Origin, gfxBase.Size(Wnd.Width, Wnd.Height));
  end;
end;


procedure TCustomForm.WndShow(Sender: TObject);
begin
  LAYOUTTRACE('TCustomForm.WndShow for %s:%s', [Name, ClassName]);
  if not (wsIsVisible in WidgetState) then
  begin
    Include(WidgetState, wsIsVisible);
    SendEvent(TVisibilityChangeEventObj.Create(Self));
  end;
end;


{$ENDIF read_implementation}