-
Notifications
You must be signed in to change notification settings - Fork 4
/
fpg_base.pas
1547 lines (1334 loc) · 47.7 KB
/
fpg_base.pas
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
fpGUI - Free Pascal GUI Toolkit
Copyright (C) 2006 - 2013 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:
The Big Bang starts here! The starting unit for fpGUI.
}
unit fpg_base;
{$mode objfpc}{$H+}
// To enable the AggPas powered Canvas
{.$define AGGCanvas}
// For debug use only
{.$define GDEBUG}
interface
uses
Classes,
SysUtils,
syncobjs, // TCriticalSection usage
variants, contnrs;
type
TfpgCoord = integer; // we might use floating point coordinates in the future...
TfpgColor = type longword; // Always in AARRGGBB (Alpha, Red, Green, Blue) format!!
TfpgString = type AnsiString;
TfpgChar = type String[4];
PPoint = ^TPoint;
TRGBTriple = packed record
Red: byte;
Green: byte;
Blue: byte;
Alpha: byte;
end;
// Same declaration as in FPImage unit, but we don't use FPImage yet, so declare it here
TFPColor = record
Red: byte;
Green: byte;
Blue: byte;
Alpha: byte;
end deprecated;
TWindowType = (wtChild, wtWindow, wtModalForm, wtPopup);
TWindowAttribute = (waSizeable, waAutoPos, waScreenCenterPos, waStayOnTop,
waFullScreen, waBorderless, waUnblockableMessages, waX11SkipWMHints,
waOneThirdDownPos);
TWindowAttributes = set of TWindowAttribute;
TfpgWindowState = (wsNormal, wsMinimized, wsMaximized);
TMouseCursor = (mcDefault, mcArrow, mcCross, mcIBeam, mcSizeEW, mcSizeNS,
mcSizeNWSE, mcSizeNESW, mcSizeSWNE, mcSizeSENW, mcMove, mcHourGlass,
mcHand, mcDrag, mcNoDrop);
TGradientDirection = (gdVertical, // Fill vertical
gdHorizontal); // Fill Horizontal
TClipboardKeyType = (ckNone, ckCopy, ckPaste, ckCut);
// If you have to convert this to an Integer, mrNone = 0 etc.
TfpgModalResult = (mrNone, mrOK, mrCancel, mrYes, mrNo, mrAbort, mrRetry,
mrIgnore, mrAll, mrNoToAll, mrYesToAll, mrHelp);
TfpgDropAction = (daIgnore, daCopy, daMove, daLink, daAsk);
TfpgDropActions = set of TfpgDropAction;
TfpgEditBorderStyle = (ebsNone, ebsDefault, ebsSingle);
// in case we wanted to trap any fpGUI specific exceptions
EfpGUIException = class(Exception);
// For providing user feedback. No need to display backtrace information
EfpGUIUserFeedbackException = class(EfpGUIException);
TfpgTextEncoding = (encUTF8, encCP437, encCP850, encCP866, encCP1250, encIBMGraph);
const
MOUSE_LEFT = 1;
MOUSE_RIGHT = 3;
MOUSE_MIDDLE = 2;
// Platform independent messages used by fpGUI (TfpgWidget)
FPGM_PAINT = 1;
FPGM_ACTIVATE = 2;
FPGM_DEACTIVATE = 3;
FPGM_KEYPRESS = 4;
FPGM_KEYRELEASE = 5;
FPGM_KEYCHAR = 6;
FPGM_MOUSEDOWN = 7;
FPGM_MOUSEUP = 8;
FPGM_MOUSEMOVE = 9;
FPGM_DOUBLECLICK = 10;
FPGM_MOUSEENTER = 11;
FPGM_MOUSEEXIT = 12;
FPGM_CLOSE = 13;
FPGM_SCROLL = 14;
FPGM_RESIZE = 15;
FPGM_MOVE = 16;
FPGM_POPUPCLOSE = 17;
FPGM_HINTTIMER = 18;
FPGM_FREEME = 19;
FPGM_DROPENTER = 20;
FPGM_DROPEXIT = 21;
FPGM_HSCROLL = 22;
FPGM_USER = 50000;
FPGM_KILLME = MaxInt;
var
{$IFDEF MSWINDOWS}
FPG_DEFAULT_FONT_DESC: string = 'Arial-8:antialias=true';
FPG_DEFAULT_SANS: string = 'Arial';
{$ENDIF}
{$IFDEF UNIX}
FPG_DEFAULT_FONT_DESC: string = 'Liberation Sans-10:antialias=true';
FPG_DEFAULT_SANS: string = 'Liberation Sans';
{$ENDIF}
const
UserNamedColorStart = 128;
type
TfpgRect = object // not class for static allocations
Top: TfpgCoord;
Left: TfpgCoord;
Width: TfpgCoord;
Height: TfpgCoord;
procedure SetRect(aleft, atop, awidth, aheight: TfpgCoord);
function Bottom: TfpgCoord;
function Right: TfpgCoord;
procedure SetBottom(Value: TfpgCoord);
procedure SetRight(Value: TfpgCoord);
end;
TfpgPoint = object // not class for static allocations
X: integer;
Y: integer;
procedure SetPoint(AX, AY: integer);
function ManhattanLength: integer; { See URL for explanation http://en.wikipedia.org/wiki/Taxicab_geometry }
function ManhattanLength(const PointB: TfpgPoint): integer;
end;
TfpgSize = object // not class for static allocations
W: integer;
H: integer;
procedure SetSize(AWidth, AHeight: integer);
end;
TfpgMsgParmMouse = record
x: TfpgCoord;
y: TfpgCoord;
Buttons: word;
shiftstate: TShiftState;
delta: Integer;
timestamp: TDateTime; // for future use
end;
TfpgMsgParmKeyboard = record
keycode: word;
keychar: TfpgChar;
shiftstate: TShiftState;
end;
TfpgMsgParmUser = record
Param1: Integer;
Param2: Integer;
Param3: Integer;
end;
TfpgMessageParams = record
case integer of
0: (mouse: TfpgMsgParmMouse);
1: (keyboard: TfpgMsgParmKeyboard);
2: (rect: TfpgRect);
3: (user: TfpgMsgParmUser);
end;
TfpgMessageRec = record
MsgCode: integer;
Sender: TObject;
Dest: TObject;
Params: TfpgMessageParams;
Stop: Boolean;
end;
PfpgMessageRec = ^TfpgMessageRec;
TfpgMoveEventRec = record
Sender: TObject;
x: TfpgCoord;
y: TfpgCoord;
end;
TfpgLineStyle = (lsSolid, lsDash, lsDot, lsDashDot, lsDashDotDot);
// forward declaration
TfpgWindowBase = class;
TfpgCanvasBase = class;
TfpgImageBase = class(TObject)
private
function GetColor(x, y: TfpgCoord): TfpgColor;
procedure SetColor(x, y: TfpgCoord; const AValue: TfpgColor);
protected
FWidth: integer;
FHeight: integer;
FColorDepth: integer;
FMasked: boolean;
FImageData: pointer;
FImageDataSize: integer;
FMaskData: pointer;
FMaskDataSize: integer;
FMaskPoint: TPoint;
procedure DoFreeImage; virtual; abstract;
procedure DoInitImage(acolordepth, awidth, aheight: integer; aimgdata: Pointer); virtual; abstract;
procedure DoInitImageMask(awidth, aheight: integer; aimgdata: Pointer); virtual; abstract;
public
constructor Create;
destructor Destroy; override;
procedure Invert(IncludeMask: Boolean = False);
procedure FreeImage;
procedure AllocateImage(acolordepth, awidth, aheight: integer);
procedure AllocateMask;
procedure CreateMaskFromSample(x, y: TfpgCoord);
{ Must always be called AFTER you populated the ImageData array. Then only does it allocate OS resources. }
procedure UpdateImage;
{ Internal representation of color data is always ARGB }
property ImageData: pointer read FImageData;
property ImageDataSize: integer read FImageDataSize;
property MaskData: pointer read FMaskData;
property MaskDataSize: integer read FMaskDataSize;
property Width: integer read FWidth;
property Height: integer read FHeight;
property ColorDepth: integer read FColorDepth;
property Masked: boolean read FMasked;
property MaskPoint: TPoint read FMaskPoint;
property Colors[x, y: TfpgCoord]: TfpgColor read GetColor write SetColor;
end;
TfpgFontResourceBase = class(TObject)
public
function GetAscent: integer; virtual; abstract;
function GetDescent: integer; virtual; abstract;
function GetHeight: integer; virtual; abstract;
function GetTextWidth(const txt: string): integer; virtual; abstract;
end;
TfpgFontBase = class(TObject)
protected
FFontDesc: string;
FFontRes: TfpgFontResourceBase;
function GetIsFixedWidth: boolean; virtual;
public
function TextWidth(const txt: TfpgString): integer;
function Ascent: integer;
function Descent: integer;
function Height: integer;
property FontDesc: string read FFontDesc;
property FontRes: TfpgFontResourceBase read FFontRes;
property Handle: TfpgFontResourceBase read FFontRes;
property IsFixedWidth: boolean read GetIsFixedWidth;
end;
TfpgCustomInterpolation = class(TObject)
private
FCanvas: TfpgCanvasBase;
FImage: TfpgImageBase;
protected
procedure Initialize(AImage: TfpgImageBase; ACanvas: TfpgCanvasBase); virtual;
procedure Execute(x, y, w, h: integer); virtual; abstract;
public
property Canvas: TfpgCanvasBase read FCanvas;
property Image: TfpgImageBase read FImage;
end;
TfpgBaseInterpolation = class(TfpgCustomInterpolation)
private
xfactor: double;
yfactor: double;
xsupport: double;
ysupport: double;
tempimage: TfpgImageBase;
procedure Horizontal(width: integer);
procedure Vertical(dx, dy, width, height: integer);
protected
procedure Execute(x, y, w, h: integer); override;
function Filter(x : double): double; virtual; abstract;
function MaxSupport: double; virtual; abstract;
public
destructor Destroy; override;
end;
TfpgMitchelInterpolation = class(TfpgBaseInterpolation)
protected
function Filter(x: double): double; override;
function MaxSupport: double; override;
end;
TfpgCanvasBase = class(TObject)
private
FFastDoubleBuffer: Boolean;
FInterpolation: TfpgCustomInterpolation;
procedure SetInterpolation(const AValue: TfpgCustomInterpolation);
protected
FBufferedDraw: boolean;
FBeginDrawCount: integer;
FWindow: TfpgWindowBase;
FColor: TfpgColor;
FTextColor: TfpgColor;
FLineWidth: integer;
FLineStyle: TfpgLineStyle;
FFont: TfpgFontBase;
FPersistentResources: boolean;
procedure DoSetFontRes(fntres: TfpgFontResourceBase); virtual; abstract;
procedure DoSetTextColor(cl: TfpgColor); virtual; abstract;
procedure DoSetColor(cl: TfpgColor); virtual; abstract;
procedure DoSetLineStyle(awidth: integer; astyle: TfpgLineStyle); virtual; abstract;
procedure DoGetWinRect(out r: TfpgRect); virtual; abstract;
procedure DoFillRectangle(x, y, w, h: TfpgCoord); virtual; abstract;
procedure DoXORFillRectangle(col: TfpgColor; x, y, w, h: TfpgCoord); virtual; abstract;
procedure DoFillTriangle(x1, y1, x2, y2, x3, y3: TfpgCoord); virtual; abstract;
procedure DoDrawRectangle(x, y, w, h: TfpgCoord); virtual; abstract;
procedure DoDrawLine(x1, y1, x2, y2: TfpgCoord); virtual; abstract;
procedure DoDrawImagePart(x, y: TfpgCoord; img: TfpgImageBase; xi, yi, w, h: integer); virtual; abstract;
procedure DoDrawString(x, y: TfpgCoord; const txt: string); virtual; abstract;
procedure DoSetClipRect(const ARect: TfpgRect); virtual; abstract;
function DoGetClipRect: TfpgRect; virtual; abstract;
procedure DoAddClipRect(const ARect: TfpgRect); virtual; abstract;
procedure DoClearClipRect; virtual; abstract;
procedure DoBeginDraw(awin: TfpgWindowBase; buffered: boolean); virtual; abstract;
procedure DoPutBufferToScreen(x, y, w, h: TfpgCoord); virtual; abstract;
procedure DoEndDraw; virtual; abstract;
function GetPixel(X, Y: integer): TfpgColor; virtual; abstract;
procedure SetPixel(X, Y: integer; const AValue: TfpgColor); virtual; abstract;
procedure DoDrawArc(x, y, w, h: TfpgCoord; a1, a2: Extended); virtual; abstract;
procedure DoFillArc(x, y, w, h: TfpgCoord; a1, a2: Extended); virtual; abstract;
procedure DoDrawPolygon(Points: PPoint; NumPts: Integer; Winding: boolean = False); virtual; abstract;
public
constructor Create(awin: TfpgWindowBase); virtual;
destructor Destroy; override;
procedure DrawRectangle(x, y, w, h: TfpgCoord); overload;
procedure DrawRectangle(r: TfpgRect); overload;
procedure DrawLine(x1, y1, x2, y2: TfpgCoord);
procedure DrawLineClipped(var x1, y1, x2, y2: TfpgCoord; const AClipRect: TfpgRect);
procedure ClipLine(var x1, y1, x2, y2: TfpgCoord; const AClipRect: TfpgRect; out FallsOutsideRegion: Boolean);
procedure DrawImage(x, y: TfpgCoord; img: TfpgImageBase);
procedure DrawImagePart(x, y: TfpgCoord; img: TfpgImageBase; xi, yi, w, h: integer);
procedure DrawArc(x, y, w, h: TfpgCoord; a1, a2: double);
procedure DrawPolygon(const Points: array of TPoint; Winding: Boolean; StartIndex: Integer = 0; NumPts: Integer = -1);
procedure DrawPolygon(Points: PPoint; NumPts: Integer; Winding: boolean = False); virtual;
procedure DrawPolygon(const Points: array of TPoint);
procedure StretchDraw (x, y, w, h: TfpgCoord; ASource: TfpgImageBase);
procedure CopyRect(ADest_x, ADest_y: TfpgCoord; ASrcCanvas: TfpgCanvasBase; var ASrcRect: TfpgRect); virtual;
// x,y is the top/left corner of where the text output will start.
procedure DrawString(x, y: TfpgCoord; const txt: string);
procedure FillRectangle(x, y, w, h: TfpgCoord); overload;
procedure FillRectangle(r: TfpgRect); overload;
procedure FillTriangle(x1, y1, x2, y2, x3, y3: TfpgCoord);
procedure FillArc(x, y, w, h: TfpgCoord; a1, a2: double);
procedure GradientFill(ARect: TfpgRect; AStart, AStop: TfpgColor; ADirection: TGradientDirection);
procedure XORFillRectangle(col: TfpgColor; x, y, w, h: TfpgCoord); overload;
procedure XORFillRectangle(col: TfpgColor; r: TfpgRect); overload;
procedure SetClipRect(const ARect: TfpgRect);
function GetClipRect: TfpgRect;
function GetLineWidth: integer;
procedure AddClipRect(const ARect: TfpgRect);
procedure ClearClipRect;
procedure Clear(AColor: TfpgColor);
procedure GetWinRect(out r: TfpgRect);
procedure SetColor(AColor: TfpgColor);
procedure SetTextColor(AColor: TfpgColor);
procedure SetLineStyle(AWidth: integer; AStyle: TfpgLineStyle);
procedure SetFont(AFont: TfpgFontBase);
procedure BeginDraw; overload;
procedure BeginDraw(ABuffered: boolean); overload;
procedure EndDraw(x, y, w, h: TfpgCoord); overload;
procedure EndDraw(ARect: TfpgRect); overload;
procedure EndDraw; overload;
procedure FreeResources;
property Color: TfpgColor read FColor write SetColor;
property TextColor: TfpgColor read FTextColor write SetTextColor;
property Font: TfpgFontBase read FFont write SetFont;
property Pixels[X, Y: integer]: TfpgColor read GetPixel write SetPixel;
property InterpolationFilter: TfpgCustomInterpolation read FInterpolation write SetInterpolation;
property FastDoubleBuffer: Boolean read FFastDoubleBuffer write FFastDoubleBuffer;
property LineStyle: TfpgLineStyle read FLineStyle;
end;
TfpgCanvasBaseClass = class of TfpgCanvasBase;
TfpgComponent = class(TComponent)
private
FTagPointer: Pointer;
FHelpContext: THelpContext;
FHelpKeyword: TfpgString;
FHelpType: THelpType;
protected
procedure SetHelpContext(const AValue: THelpContext); virtual;
procedure SetHelpKeyword(const AValue: TfpgString); virtual;
public
constructor Create(AOwner: TComponent); override;
property TagPointer: Pointer read FTagPointer write FTagPointer;
published
property HelpContext: THelpContext read FHelpContext write SetHelpContext default 0;
property HelpKeyword: TfpgString read FHelpKeyword write SetHelpKeyword;
property HelpType: THelpType read FHelpType write FHelpType default htKeyword;
end;
TfpgWindowBase = class(TfpgComponent)
private
FParent: TfpgWindowBase;
procedure SetMouseCursor(const AValue: TMouseCursor);
function ConstraintWidth(NewWidth: TfpgCoord): TfpgCoord;
function ConstraintHeight(NewHeight: TfpgCoord): TfpgCoord;
protected
FMouseCursor: TMouseCursor;
FWindowType: TWindowType;
FWindowAttributes: TWindowAttributes;
FTop: TfpgCoord;
FLeft: TfpgCoord;
FWidth: TfpgCoord;
FHeight: TfpgCoord;
FPrevTop: TfpgCoord;
FPrevLeft: TfpgCoord;
FPrevWidth: TfpgCoord;
FPrevHeight: TfpgCoord;
FMinWidth: TfpgCoord;
FMinHeight: TfpgCoord;
FMaxHeight: TfpgCoord;
FMaxWidth: TfpgCoord;
FCanvas: TfpgCanvasBase;
FSizeIsDirty: Boolean;
FPosIsDirty: Boolean;
FMouseCursorIsDirty: Boolean;
FOnDragStartDetected: TNotifyEvent;
FDragActive: boolean;
FWindowState: TfpgWindowState;
function HandleIsValid: boolean; virtual; abstract;
procedure DoUpdateWindowPosition; virtual; abstract;
procedure DoAllocateWindowHandle(AParent: TfpgWindowBase); virtual; abstract;
procedure DoReleaseWindowHandle; virtual; abstract;
procedure DoRemoveWindowLookup; virtual; abstract;
procedure DoSetWindowVisible(const AValue: Boolean); virtual; abstract;
procedure DoMoveWindow(const x: TfpgCoord; const y: TfpgCoord); virtual; abstract;
function DoWindowToScreen(ASource: TfpgWindowBase; const AScreenPos: TPoint): TPoint; virtual; abstract;
procedure DoSetWindowTitle(const ATitle: string); virtual; abstract;
procedure DoSetMouseCursor; virtual; abstract;
procedure DoDNDEnabled(const AValue: boolean); virtual; abstract;
procedure DoAcceptDrops(const AValue: boolean); virtual; abstract;
function GetWindowState: TfpgWindowState; virtual;
procedure SetWindowState(const AValue: TfpgWindowState); virtual;
procedure DoDragStartDetected; virtual;
procedure SetParent(const AValue: TfpgWindowBase); virtual;
function GetParent: TfpgWindowBase; virtual;
function GetCanvas: TfpgCanvasBase; virtual;
procedure AllocateWindowHandle;
procedure ReleaseWindowHandle;
procedure SetWindowTitle(const ATitle: string); virtual;
procedure SetTop(const AValue: TfpgCoord);
procedure SetLeft(const AValue: TfpgCoord);
procedure SetHeight(const AValue: TfpgCoord);
procedure SetWidth(const AValue: TfpgCoord);
procedure HandleMove(x, y: TfpgCoord); virtual;
procedure HandleResize(AWidth, AHeight: TfpgCoord); virtual;
property OnDragStartDetected: TNotifyEvent read FOnDragStartDetected write FOnDragStartDetected;
property WindowState: TfpgWindowState read GetWindowState {write SetWindowState} default wsNormal;
public
// The standard constructor.
constructor Create(AOwner: TComponent); override;
procedure AfterConstruction; override;
// Make some setup before the window shows. Forms modify the window creation parameters.
procedure AdjustWindowStyle; virtual;
// Make some setup before the window shows. Invoked after the window is created.
procedure SetWindowParameters; virtual;
// general properties and functions
function Right: TfpgCoord;
function Bottom: TfpgCoord;
procedure UpdateWindowPosition;
procedure MoveWindow(const x: TfpgCoord; const y: TfpgCoord);
function WindowToScreen(ASource: TfpgWindowBase; const AScreenPos: TPoint): TPoint;
function HasParent: Boolean; override;
function GetClientRect: TfpgRect; virtual;
function GetBoundsRect: TfpgRect; virtual;
procedure ActivateWindow; virtual; abstract;
procedure CaptureMouse; virtual; abstract;
procedure ReleaseMouse; virtual; abstract;
procedure BringToFront; virtual; abstract;
procedure SetFullscreen(AValue: Boolean); virtual;
property HasHandle: boolean read HandleIsValid;
property WindowType: TWindowType read FWindowType write FWindowType;
property WindowAttributes: TWindowAttributes read FWindowAttributes write FWindowAttributes;
property Left: TfpgCoord read FLeft write SetLeft;
property Top: TfpgCoord read FTop write SetTop;
property Width: TfpgCoord read FWidth write SetWidth;
property Height: TfpgCoord read FHeight write SetHeight;
property MinWidth: TfpgCoord read FMinWidth write FMinWidth;
property MinHeight: TfpgCoord read FMinHeight write FMinHeight;
property MaxWidth: TfpgCoord read FMaxWidth write FMaxWidth default 0;
property MaxHeight: TfpgCoord read FMaxHeight write FMaxHeight default 0;
property Canvas: TfpgCanvasBase read GetCanvas;
property Parent: TfpgWindowBase read GetParent write SetParent;
property MouseCursor: TMouseCursor read FMouseCursor write SetMouseCursor;
end;
TfpgApplicationBase = class(TfpgComponent)
private
FMainForm: TfpgWindowBase;
FTerminated: boolean;
FCritSect: TCriticalSection;
FHelpKey: word;
FHelpFile: TfpgString;
function GetForm(Index: Integer): TfpgWindowBase;
function GetFormCount: integer;
function GetTopModalForm: TfpgWindowBase;
function GetHelpFile: TfpgString;
protected
FOnIdle: TNotifyEvent;
FIsInitialized: Boolean;
FModalFormStack: TList;
function DoGetFontFaceList: TStringList; virtual; abstract;
procedure DoWaitWindowMessage(atimeoutms: integer); virtual; abstract;
function MessagesPending: boolean; virtual; abstract;
function GetHelpViewer: TfpgString; virtual;
public
constructor Create(const AParams: string); virtual; reintroduce;
destructor Destroy; override;
function GetFontFaceList: TStringList;
procedure PushModalForm(AForm: TfpgWindowBase);
procedure PopModalForm;
function PrevModalForm: TfpgWindowBase;
function RemoveWindowFromModalStack(AForm: TfpgWindowBase): Integer;
procedure CreateForm(InstanceClass: TComponentClass; out Reference);
function GetScreenWidth: TfpgCoord; virtual; abstract;
function GetScreenHeight: TfpgCoord; virtual; abstract;
function Screen_dpi_x: integer; virtual; abstract;
function Screen_dpi_y: integer; virtual; abstract;
function Screen_dpi: integer; virtual; abstract;
procedure Terminate;
procedure Lock;
procedure Unlock;
procedure InvokeHelp;
function ContextHelp(const AHelpContext: THelpContext): Boolean;
function KeywordHelp(const AHelpKeyword: string): Boolean;
property FormCount: integer read GetFormCount;
property Forms[Index: Integer]: TfpgWindowBase read GetForm;
property HelpContext;
property HelpFile: TfpgString read GetHelpFile write FHelpFile;
property IsInitialized: boolean read FIsInitialized;
property TopModalForm: TfpgWindowBase read GetTopModalForm;
property MainForm: TfpgWindowBase read FMainForm write FMainForm;
property Terminated: boolean read FTerminated write FTerminated;
property OnIdle: TNotifyEvent read FOnIdle write FOnIdle;
end;
TfpgClipboardBase = class(TObject)
protected
function DoGetText: TfpgString; virtual; abstract;
procedure DoSetText(const AValue: TfpgString); virtual; abstract;
procedure InitClipboard; virtual; abstract;
public
constructor Create; virtual;
property Text: TfpgString read DoGetText write DoSetText;
end;
TFileEntryType = (etFile, etDir);
TFileListSortOrder = (soNone, soFileName, soCSFileName, soFileExt, soSize, soTime);
TFileModeString = string[9];
TfpgSearchMode = (smAny, smFiles, smDirs);
// A simple data object
TFileEntry = class(TObject)
private
FEntryType: TFileEntryType;
FExtension: string;
FName: string;
FModTime: TDateTime;
FSize: int64;
FIsLink: boolean;
FLinkTarget: string;
FIsExecutable: boolean;
FModeString: TFileModeString;
FOwner: TfpgString;
FGroup: TfpgString;
FAttrString: TFileModeString;
public
constructor Create;
property Name: string read FName write FName;
property Extension: string read FExtension write FExtension;
property Size: int64 read FSize write FSize;
property EntryType: TFileEntryType read FEntryType write FEntryType;
property IsLink: boolean read FIsLink write FIsLink;
property LinkTarget: string read FLinkTarget write FLinkTarget;
property IsExecutable: boolean read FIsExecutable write FIsExecutable;
property ModTime: TDateTime read FModTime write FModTime;
property Mode: TFileModeString read FModeString write FModeString;
property Owner: TfpgString read FOwner write FOwner;
property Group: TfpgString read FGroup write FGroup;
property Attributes: TFileModeString read FAttrString write FAttrString;
end;
TfpgFileListBase = class(TObject)
private
FEntries: TList;
FDirectoryName: TfpgString;
FFileMask: TfpgString;
FSearchMode: TfpgSearchMode;
FShowHidden: boolean;
FCurrentSpecialDir: integer;
procedure AddEntry(sr: TSearchRec);
function GetEntry(i: integer): TFileEntry;
function HasAttrib(fileAttrib, testAttrib: Integer): Boolean;
protected
FSpecialDirs: TStringList;
FHasFileMode: boolean;
function InitializeEntry(sr: TSearchRec): TFileEntry; virtual;
procedure PopulateSpecialDirs(const aDirectory: TfpgString); virtual;
public
constructor Create; virtual;
destructor Destroy; override;
function Count: integer;
function CurrentSpecialDir: integer;
function ReadDirectory(const aDirectory: TfpgString = ''): boolean;
procedure Clear;
procedure Sort(AOrder: TFileListSortOrder);
property DirectoryName: TfpgString read FDirectoryName;
property Entry[i: integer]: TFileEntry read GetEntry;
property FileMask: TfpgString read FFileMask write FFileMask;
property HasFileMode: boolean read FHasFileMode;
property SearchMode: TfpgSearchMode read FSearchMode write FSearchMode;
property ShowHidden: boolean read FShowHidden write FShowHidden;
property SpecialDirs: TStringList read FSpecialDirs;
end;
TfpgMimeDataItem = class(TObject)
public
format: TfpgString; { mime string type }
data: Variant;
constructor Create(const AFormat: TfpgString; const AData: variant); reintroduce;
end;
TfpgMimeDataBase = class(TObject)
private
{ TODO: This is wrong, we must have one Data Storage object }
FDataList: TObjectList;
FUrlList: TList;
function GetItem(AIndex: Integer): TfpgMimeDataItem;
function Geturls: TList;
procedure Seturls(const AValue: TList);
function GetText: TfpgString;
procedure SetText(const AValue: TfpgString);
function GetHTML: TfpgString;
procedure SetHTML(const AValue: TfpgString);
function GetCount: integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function HasFormat(const AMimeType: TfpgString): boolean;
function Formats: TStrings;
function GetData(const AMimeType: TfpgString): Variant;
procedure SetData(const AMimeType: TfpgString; const AData: Variant);
property Items[AIndex: Integer]: TfpgMimeDataItem read GetItem; default;
property urls: TList read Geturls write Seturls;
property Text: TfpgString read GetText write SetText;
property HTML: TfpgString read GetHTML write SetHTML;
property Count: integer read GetCount;
end;
TfpgDragBase = class(TObject)
protected
FDragging: Boolean;
FMimeData: TfpgMimeDataBase;
public
constructor Create;
destructor Destroy; override;
function Execute(const ADropActions: TfpgDropActions; const ADefaultAction: TfpgDropAction = daCopy): TfpgDropAction; virtual; abstract;
end;
{ TfpgBaseTimer }
TfpgBaseTimer = class(TObject)
private
FNextAlarm: TDateTime;
FInterval: integer;
FOnTimer: TNotifyEvent;
procedure SetInterval(const AValue: integer);
protected
FEnabled: boolean;
procedure SetEnabled(const AValue: boolean); virtual;
public
constructor Create(AInterval: integer); virtual;
destructor Destroy; override;
procedure CheckAlarm(ACurrentTime: TDateTime);
procedure Reset;
procedure Pause(ASeconds: integer);
property Enabled: boolean read FEnabled write SetEnabled;
property NextAlarm: TDateTime read FNextAlarm;
{ Interval is in milliseconds. }
property Interval: integer read FInterval write SetInterval;
property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
end;
{ ******** Helper functions ******** }
{ Keyboard }
function KeycodeToText(AKey: Word; AShiftState: TShiftState): string;
function CheckClipboardKey(AKey: Word; AShiftstate: TShiftState): TClipboardKeyType;
{ Color }
function fpgColorToRGBTriple(const AColor: TfpgColor): TRGBTriple;
function fpgColorToFPColor(const AColor: TfpgColor): TFPColor; deprecated;
function RGBTripleTofpgColor(const AColor: TRGBTriple): TfpgColor;
function FPColorTofpgColor(const AColor: TFPColor): TfpgColor; deprecated;
function fpgGetRed(const AColor: TfpgColor): byte;
function fpgGetGreen(const AColor: TfpgColor): byte;
function fpgGetBlue(const AColor: TfpgColor): byte;
function fpgGetAlpha(const AColor: TfpgColor): byte;
function fpgGetAvgColor(const AColor1, AColor2: TfpgColor): TfpgColor;
function fpgColor(const ARed, AGreen, ABlue: byte): TfpgColor;
function fpgColor(const ARed, AGreen, ABlue, AAlpha: byte): TfpgColor;
function fpgDarker(const AColor: TfpgColor; APercent: Byte = 50): TfpgColor;
function fpgLighter(const AColor: TfpgColor; APercent: Byte = 50): TfpgColor;
{ Points }
procedure SortRect(var ARect: TRect);
procedure SortRect(var ARect: TfpgRect);
procedure SortRect(var left, top, right, bottom: integer);
implementation
uses
typinfo,
process,
{$IFDEF GDEBUG}
fpg_dbugintf,
{$ENDIF}
dateutils;
const
NoDefault = $80000000;
tkPropsWithDefault = [tkInteger, tkChar, tkSet, tkEnumeration];
procedure SortRect(var ARect: TRect);
begin
with ARect do
SortRect(left, top, right, bottom);
end;
procedure SortRect(var ARect: TfpgRect);
var
r: TfpgCoord;
b: TfpgCoord;
begin
r := ARect.Right;
b := ARect.Bottom;
SortRect(ARect.Left, ARect.Top, r, b);
ARect.SetRight(r);
ARect.SetBottom(b);
end;
procedure SortRect(var left, top, right, bottom: integer);
var
r: integer;
begin
if left > right then
begin
r := left;
left := right;
right := r;
end;
if top > bottom then
begin
r := top;
top := bottom;
bottom := r;
end;
end;
// This function uses RTTI to automatically set the default values of properties.
// That means we don't have to do it in the constructor anymore! :-)
procedure SetDefaults(Obj: TObject);
var
PropInfos: PPropList;
Count, Loop: Integer;
begin
PropInfos := nil;
{ Find out how many properties we'll be considering }
Count := GetPropList(Obj.ClassInfo, tkPropsWithDefault, nil);
{ Allocate memory to hold their RTTI data }
GetMem(PropInfos, Count * SizeOf(PPropInfo));
try
{ Get hold of the property list in our new buffer }
GetPropList(Obj.ClassInfo, tkPropsWithDefault, PropInfos);
{ Loop through all the selected properties }
for Loop := 0 to Count - 1 do
begin
with PropInfos^[Loop]^ do
begin
{ If there is supposed to be a default value... }
if Default <> NoDefault then
{ ...then jolly well set it }
SetOrdProp(Obj, PropInfos^[Loop], Default)
end;
end;
finally
FreeMem(PropInfos, Count * SizeOf(PPropInfo));
end;
end;
{ TfpgRect }
procedure TfpgRect.SetRect(aleft, atop, awidth, aheight: TfpgCoord);
begin
Left := aleft;
Top := atop;
Width := awidth;
Height := aheight;
end;
function TfpgRect.Bottom: TfpgCoord;
begin
Result := Top + Height - 1;
end;
function TfpgRect.Right: TfpgCoord;
begin
Result := Left + Width - 1;
end;
procedure TfpgRect.SetBottom(Value: TfpgCoord);
begin
Height := Value - Top + 1;
end;
procedure TfpgRect.SetRight(Value: TfpgCoord);
begin
Width := Value - Left + 1;
end;
{ TfpgPoint }
procedure TfpgPoint.SetPoint(AX, AY: integer);
begin
X := AX;
Y := AY;
end;
function TfpgPoint.ManhattanLength: integer;
begin
Result := Abs(X) + Abs(Y);
end;
function TfpgPoint.ManhattanLength(const PointB: TfpgPoint): integer;
begin
Result := Abs(PointB.X-X) + Abs(PointB.Y-Y);
end;
{ TfpgSize }
procedure TfpgSize.SetSize(AWidth, AHeight: integer);
begin
W := AWidth;
H := AHeight;
end;
{ TfpgWindowBase }
procedure TfpgWindowBase.SetMouseCursor(const AValue: TMouseCursor);
begin
if FMouseCursor = AValue then
Exit; //==>
FMouseCursor := AValue;
DoSetMouseCursor;
end;
function TfpgWindowBase.ConstraintWidth(NewWidth: TfpgCoord): TfpgCoord;
begin
Result := NewWidth;
if (MaxWidth >= MinWidth) and (Result > MaxWidth) and (MaxWidth > 0) then
Result := MaxWidth;
if Result < MinWidth then
Result := MinWidth;
end;
function TfpgWindowBase.ConstraintHeight(NewHeight: TfpgCoord): TfpgCoord;
begin
Result := NewHeight;
if (MaxHeight >= MinHeight) and (Result > MaxHeight) and (MaxHeight > 0) then
Result := MaxHeight;
if Result < MinHeight then
Result := MinHeight;
end;
function TfpgWindowBase.GetWindowState: TfpgWindowState;
begin
Result := FWindowState;
end;
procedure TfpgWindowBase.SetWindowState(const AValue: TfpgWindowState);
begin
// do nothing
end;
procedure TfpgWindowBase.DoDragStartDetected;
begin
if Assigned(FOnDragStartDetected) then
FOnDragStartDetected(self);
end;
procedure TfpgWindowBase.SetParent(const AValue: TfpgWindowBase);
begin
FParent := AValue;
end;
function TfpgWindowBase.GetParent: TfpgWindowBase;
begin
result := FParent;
end;
function TfpgWindowBase.GetCanvas: TfpgCanvasBase;
begin
Result := FCanvas;
end;
procedure TfpgWindowBase.AllocateWindowHandle;
begin
DoAllocateWindowHandle(FParent);
if FMouseCursorIsDirty then
DoSetMouseCursor;
end;
procedure TfpgWindowBase.ReleaseWindowHandle;
begin
if HasHandle then
begin
Canvas.FreeResources;
DoReleaseWindowHandle;
end;
DoRemoveWindowLookup;
end;
procedure TfpgWindowBase.SetWindowTitle(const ATitle: string);