File: fpguiobjects.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (857 lines) | stat: -rw-r--r-- 21,558 bytes parent folder | download | duplicates (4)
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
{
 *****************************************************************************
 *                             FpGUIObjects.pas                              *
 *                              --------------                               *
 *      Place for wrapper classes which aren't widgets                       *
 *                                                                           *
 *****************************************************************************

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit fpguiobjects;

{$mode objfpc}{$H+}

{.$DEFINE VERBOSEDEBUG_FPGUI_REGION}

interface

uses
  // RTL, FCL, LCL
  Classes, SysUtils,
  Graphics, Menus, LCLType,
  // Widgetset
  fpguiwsprivate,
  //Others
  fpguiproc,
  // interface
  fpg_main, fpg_base, fpg_menu;

type

  TFPGUIRegionType=(eRegionNULL,eRegionSimple,eRegionComplex,eRegionNotCombinableOrError);
  TFPGUIRegionCombine=(eRegionCombineAnd,eRegionCombineCopy, eRegionCombineDiff, eRegionCombineOr, eRegionCombineXor);

  TFPGUIWinAPIElement = class(TObject);

  TFPGUIWinAPIObject = class(TFPGUIWinAPIElement);


  type TFPGUIWinAPIBrushStyle = (
    bsSolid,
    bsClear
  );

  type TFPGUIWinAPIPenStyle = (
    psSolid,
    psClear
  );

  type tagTFPGUIBrush= record
    Color: TfpgColor;
    Style: TFPGUIWinAPIBrushStyle;
  end;

  type tagTFPGUIPen= record
    Color: TfpgColor;
    Width: Integer;
    Style: TFPGUIWinAPIPenStyle;
  end;

  { TFPGUIWinAPIBrush }

  TFPGUIWinAPIBrush = class (TFPGUIWinAPIObject)
  private
    FBrush: tagTFPGUIBrush;
    function GetColor: TfpgColor;
    function GetStyle: TFPGUIWinAPIBrushStyle;
    procedure SetColor(const AValue: TfpgColor);
    procedure SetStyle(AValue: TFPGUIWinAPIBrushStyle);
  public
    property Color: TfpgColor read GetColor Write SetColor;
    property Style: TFPGUIWinAPIBrushStyle read GetStyle write SetStyle;
    Constructor Create;
    Constructor Create(const ABrushData: TLogBrush);
    Destructor Destroy; override;
  end;

  { TFPGUIWinAPIPen }

  TFPGUIWinAPIPen = class (TFPGUIWinAPIObject)
  private
    FPen: tagTFPGUIPen;
    function GetColor: TfpgColor;
    function GetLineWidthX: integer;
    function GetStyle: TFPGUIWinAPIPenStyle;
    procedure SetColor(const AValue: TfpgColor);
    procedure SetLineWidthX(AValue: integer);
    procedure SetStyle(AValue: TFPGUIWinAPIPenStyle);
  public
    property Color: TfpgColor read GetColor Write SetColor;
    property LineWidthX: integer read GetLineWidthX write SetLineWidthX;
    property Style: TFPGUIWinAPIPenStyle read GetStyle write SetStyle;
    Constructor Create;
    Constructor Create(const APenData: TLogPen);
    Destructor Destroy; override;
  end;

  { TFPGUIWinAPIFont }

  TFPGUIWinAPIFont = class (TFPGUIWinAPIObject)
  private
    fpgFont: TfpgFontBase;
    FFontHeight: integer;
    FFontFace: String;
    function GetFontHeight: integer;
    function GetFontSize: integer;
    procedure SetFontHeight(const AValue: integer);
    procedure SetFontSize(const AValue: integer);
  public
    Constructor Create;
    Constructor Create(const AFontData: TFontData);
    Constructor Create(const AfpgCanvas: TfpgCanvas);
    Constructor Create(const AFontData: TLogFont);
    Constructor Create(const AFontData: TLogFont; const ALongFontName: string);
    Destructor Destroy; override;
    property fpguiFont: TfpgFontBase read fpgFont write fpgFont;
    property Size: integer read GetFontSize write SetFontSize;
    property Height: integer read GetFontHeight write SetFontHeight;
  end;

  { TFPGUIWinAPIBitmap }

  TFPGUIWinAPIBitmap = class(TFPGUIWinAPIObject)
  private
    fpgImage: TfpgImage;
  protected
    SelectedInDC: HDC;
  public
    Constructor Create(const ABitsPerPixel,Width,Height: integer);
    Destructor Destroy; override;
    property Image: TfpgImage read fpgImage;
  end;

  TFPGUIBasicRegion = class;

  { TFpGuiDeviceContext }

  TFPGUIDeviceContext = class(TFPGUIWinAPIElement)
  private
    FDCStack: array of TFPGUIDeviceContext;
    procedure CopyDCToInstance(const ATarget: TFPGUIDeviceContext);
    procedure SetupFont;
    procedure SetupBrush;
    procedure SetupPen;
    procedure SetupBitmap;
    procedure SetupClipping;
  public
    fpgCanvas: TfpgCanvas;
    FPrivateWidget: TFPGUIPrivateWidget;
    FOrg: TPoint;
    FDrawXY: TPoint;
    FBrush: TFPGUIWinAPIBrush;
    FPen: TFPGUIWinAPIPen;
    FFont: TFPGUIWinAPIFont;
    FTextColor: TfpgColor;
    FBitmap: TFPGUIWinAPIBitmap;
    FClipping: TFPGUIBasicRegion;
  public
    constructor Create(AFPGUIPrivate: TFPGUIPrivateWidget);
    destructor Destroy; override;
    procedure SetOrigin(const AX,AY: integer);
    function SaveDC: integer;
    function RestoreDC(const Index: SizeInt): Boolean;
    function SelectObject(const AGDIOBJ: HGDIOBJ): HGDIOBJ;
    function SetTextColor(const AColor: TColorRef): TColorRef;
    function GetTextColor: TColorRef;
    function PrepareRectOffsets(const ARect: TRect): TfpgRect;
    function PreparePointOffsets(const APoint: TPoint): TPoint;
    function UnPrepareRectOffsets(const ARect: TRect): TfpgRect;
    function UnPreparePointOffsets(const APoint: TPoint): TPoint;
    procedure ClearRectangle(const AfpgRect: TfpgRect);
    procedure ClearDC;
    function UseBrush: Boolean;
    function UsePen: Boolean;
  end;

  { TFPGUIPrivateMenuItem }

  TFPGUIPrivateMenuItem = class(TObject)
  private
  protected
  public
    MenuItem: TfpgMenuItem;
    LCLMenuItem: TMenuItem;
    procedure HandleOnClick(ASender: TObject);
  end;

  { TFPGUIBasicRegion }

  TFPGUIBasicRegion=class(TFPGUIWinAPIObject)
  private
    FRegionType: TFPGUIRegionType;
    function GetDebugString: string;
    function GetfpgRectRegion: TfpgRect;
    function GetRegionType: TFPGUIRegionType;
  protected
    FRectRegion: TRect;
  public
    constructor Create; overload;
    constructor Create(const ARect: TRect); overload;
    destructor Destroy; override;
    procedure CreateRectRegion(const ARect: TRect);
    function CombineWithRegion(const ARegion: TFPGUIBasicRegion; const ACombineMode: TFPGUIRegionCombine): TFPGUIBasicRegion;
    property RegionType: TFPGUIRegionType read GetRegionType;
    property fpgRectRegion: TfpgRect read GetfpgRectRegion;
    property DebugString: string read GetDebugString;
  end;

  function FPGUIGetDesktopDC(): TFPGUIDeviceContext;

implementation

var
  FPGUIDesktopDC: TFPGUIDeviceContext=nil;

function FPGUIGetDesktopDC(): TFPGUIDeviceContext;
var
  a: TCreateParams;
begin
  if not Assigned(FPGUIDesktopDC) then begin
    FPGUIDesktopDC:=TFPGUIDeviceContext.Create(nil);
    //FPGUIDesktopDC:=TFPGUIDeviceContext.Create(TFPGUIPrivateCustomPanel.Create(nil,a));
  end;
  Result:=FPGUIDesktopDC;
end;

{ TFPGUIWinAPIBitmap }

constructor TFPGUIWinAPIBitmap.Create(const ABitsPerPixel, Width,
  Height: integer);
begin
  fpgImage:=TfpgImage.Create;
  fpgImage.AllocateImage(ABitsPerPixel,Width,Height);
  fpgImage.UpdateImage;
end;

destructor TFPGUIWinAPIBitmap.Destroy;
var
  Context: TFPGUIDeviceContext;
begin
  Context:=TFPGUIDeviceContext(SelectedInDC);
  if Assigned(Context) then begin
    Context.FBitmap:=nil;
  end;
  fpgImage.Free;
  inherited Destroy;
end;

{ TFpGuiDeviceContext }

procedure TFPGUIDeviceContext.CopyDCToInstance(
  const ATarget: TFPGUIDeviceContext);
begin
  ATarget.fpgCanvas:=fpgCanvas;
  ATarget.FPrivateWidget:=FPrivateWidget;
  ATarget.FBrush:=FBrush;
  ATarget.FPen:=FPen;
  ATarget.FFont:=FFont;
  ATarget.FOrg:=FOrg;
  ATarget.FDrawXY:=FDrawXY;
  ATarget.FTextColor:=FTextColor;
  ATarget.FClipping:=FClipping;
end;

procedure TFPGUIDeviceContext.SetupFont;
begin
  if Assigned(fpgCanvas) then
   if Assigned(FFont) then
    fpgCanvas.Font:=FFont.fpguiFont;
end;

procedure TFPGUIDeviceContext.SetupBrush;
begin
  if Assigned(fpgCanvas) then
   if Assigned(FBrush) then
    fpgCanvas.Color:=FBrush.Color;
end;

procedure TFPGUIDeviceContext.SetupPen;
begin
  if Assigned(fpgCanvas) then begin
    if Assigned(FPen) then begin
     fpgCanvas.Color:=FPen.Color;
     fpgCanvas.SetLineStyle(FPen.LineWidthX,TfpgLineStyle.lsSolid);
    end;
  end;
end;

procedure TFPGUIDeviceContext.SetupBitmap;
begin
  if Assigned(fpgCanvas) then
    fpgCanvas.DrawImage(0,0,FBitmap.fpgImage);
end;

procedure TFPGUIDeviceContext.SetupClipping;
var
  r: TfpgRect;
  RECT: TRECT;
begin
  if Assigned(fpgCanvas) then
    if Assigned(FClipping) then begin
      RECT:=fpgRectToRect(FClipping.fpgRectRegion);
      r:=PrepareRectOffsets(RECT);
      // Note: The fpGUI clipping is one pixel smaller than WinAPI, or it looks like...
      r.Width:=r.Width+1;
      r.Height:=r.Height+1;
      fpgCanvas.SetClipRect(r);
    end else begin
      fpgCanvas.ClearClipRect;
    end;
end;

constructor TFPGUIDeviceContext.Create(AFPGUIPrivate: TFPGUIPrivateWidget);
begin
  // The Widget.Visible check is just for extra precaution.
  if Assigned(AFPGUIPrivate) and AFPGUIPrivate.Widget.Visible then
  begin
    fpgCanvas := AFPGUIPrivate.Widget.Canvas;
    fpgCanvas.BeginDraw;
    FPrivateWidget:=AFPGUIPrivate;
    FPrivateWidget.DC:=HDC(Self);
  end
  else
  begin
    fpgCanvas := nil;
    FPrivateWidget := nil;
  end;

  with FOrg do begin
    X:=0;
    Y:=0;
  end;
  with FDrawXY do begin
    X:=0;
    Y:=0;
  end;
  FBrush:=nil;
  FPen:=nil;
  FFont:=nil;
end;

destructor TFPGUIDeviceContext.Destroy;
var
  j: integer;
begin
  if Assigned(fpgCanvas) then fpgCanvas.EndDraw;
  for j := 0 to High(FDCStack) do begin
    FDCStack[j].Free;
    FDCStack[j]:=nil;
  end;
  if Assigned(FPrivateWidget) then
    FPrivateWidget.DC:=0;
end;

procedure TFPGUIDeviceContext.SetOrigin(const AX, AY: integer);
begin
  With FOrg do begin
    X:=AX;
    Y:=AY;
  end;
end;

function TFPGUIDeviceContext.SaveDC: integer;
var
  Tmp: TFPGUIDeviceContext;
begin
  SetLength(FDCStack,Length(FDCStack)+1);
  Tmp:=TFPGUIDeviceContext.Create(FPrivateWidget);
  FDCStack[High(FDCStack)]:=Tmp;
  Self.CopyDCToInstance(Tmp);
  Result:=High(FDCStack);
end;

function TFPGUIDeviceContext.RestoreDC(const Index: SizeInt): Boolean;
var
  Tmp: TFPGUIDeviceContext;
  TargetIndex: SizeInt;
  j: SizeInt;
begin
  Result:=false;
  if Index>=0 then begin
    TargetIndex:=Index;
    if TargetIndex>High(FDCStack) then Exit;
  end else begin
    TargetIndex:=High(FDCStack)-Index+1;
    If TargetIndex<0 then Exit;
  end;
  Tmp:=FDCStack[TargetIndex];
  Tmp.CopyDCToInstance(Self);
  if Assigned(FPrivateWidget) then begin
    FPrivateWidget.DC:=HDC(Self);
  end;
  SetupFont;
  SetupBrush;
  SetupPen;
  SetupClipping;
  for j := TargetIndex to High(FDCStack) do begin
    FDCStack[j].Free;
    FDCStack[j]:=nil;
  end;
  SetLength(FDCStack,TargetIndex);
  Result:=true;
end;

function TFPGUIDeviceContext.SelectObject(const AGDIOBJ: HGDIOBJ): HGDIOBJ;
var
  gObject: TObject;
begin
  Result:=0;
  gObject:=TObject(AGDIOBJ);
  if AGDIOBJ<5 then begin
    case AGDIOBJ of
      1:  begin
            Result:=HGDIOBJ(FFont);
            FFont:=nil;
          end;
      2:  begin
            Result:=HGDIOBJ(FBrush);
            FBrush:=nil;
          end;
      3:  begin
            Result:=HGDIOBJ(FPen);
            FPen:=nil;
          end;
      4:  begin
            Result:=HGDIOBJ(FBitmap);
            FBitmap:=nil;
          end;
      5:  begin
            Result:=HGDIOBJ(FClipping);
            FClipping:=nil;
          end;
    end;
    Exit;
  end;
  if gObject is TFPGUIWinAPIFont then begin
    Result:=HGDIOBJ(FFont);
    FFont:=TFPGUIWinAPIFont(gObject);
    SetupFont;
    if Result=0 then Result:=1;
  end else if gObject is TFPGUIWinAPIBrush then begin
    Result:=HGDIOBJ(FBrush);
    FBrush:=TFPGUIWinAPIBrush(gObject);
    SetupBrush;
    if Result=0 then Result:=2;
  end else if gObject is TFPGUIWinAPIPen then begin
    Result:=HGDIOBJ(FPen);
    FPen:=TFPGUIWinAPIPen(gObject);
    SetupPen;
    if Result=0 then Result:=3;
  end else if gObject is TFPGUIWinAPIBitmap then begin
    Result:=HGDIOBJ(FBitmap);
    FBitmap:=TFPGUIWinAPIBitmap(gObject);
    FBitmap.SelectedInDC:=HDC(Self);
    SetupBitmap;
    if Result=0 then Result:=4;
  end else if gObject is TFPGUIBasicRegion then begin
    Result:=HGDIOBJ(FClipping);
    FClipping:=TFPGUIBasicRegion(gObject);
    SetupClipping;
    if Result=0 then Result:=5;
  end;
end;

function TFPGUIDeviceContext.SetTextColor(const AColor: TColorRef): TColorRef;
begin
  Result:=TTfpgColorToTColor(FTextColor);
  FTextColor:=TColorToTfpgColor(AColor);
end;

function TFPGUIDeviceContext.GetTextColor: TColorRef;
begin
  Result:=TTfpgColorToTColor(FTextColor);
end;

function TFPGUIDeviceContext.PrepareRectOffsets(const ARect: TRect): TfpgRect;
begin
  TRectTofpgRect(ARect,Result);
  AdjustRectToOrg(Result,FOrg);
  if Assigned(FPrivateWidget) then
    FPrivateWidget.AdjustRectXY(Result);
end;

function TFPGUIDeviceContext.PreparePointOffsets(const APoint: TPoint): TPoint;
begin
  Result:=APoint;
  AdjustPointToOrg(Result,FOrg);
  if Assigned(FPrivateWidget) then
    FPrivateWidget.AdjustPointXY(Result);
end;

function TFPGUIDeviceContext.UnPrepareRectOffsets(const ARect: TRect): TfpgRect;
var
  r: TRect;
begin
  r:=ARect;
  if Assigned(FPrivateWidget) then
    FPrivateWidget.AdjustRectXYToInterface(r);
  TRectTofpgRect(r,Result)
end;

function TFPGUIDeviceContext.UnPreparePointOffsets(const APoint: TPoint
  ): TPoint;
begin
  Result:=APoint;
  if Assigned(FPrivateWidget) then
    FPrivateWidget.AdjustPointXYToInterface(Result);
end;

procedure TFPGUIDeviceContext.ClearRectangle(const AfpgRect: TfpgRect);
var
  OldColor: TfpgColor;
begin
  OldColor:=fpgCanvas.Color;
  fpgCanvas.Color:=FPrivateWidget.Widget.BackgroundColor;
  fpgCanvas.FillRectangle(AfpgRect);
  fpgCanvas.Color:=OldColor;
end;

procedure TFPGUIDeviceContext.ClearDC;
begin
  ClearRectangle(fpgCanvas.GetClipRect);
end;

function TFPGUIDeviceContext.UseBrush: Boolean;
begin
  Result:=true;
  fpgCanvas.Color:=FBrush.Color;
  if FBrush.Style=TFPGUIWinAPIBrushStyle.bsClear then begin
    Result:=false;
  end;
end;

function TFPGUIDeviceContext.UsePen: Boolean;
begin
  fpgCanvas.Color:=FPen.Color;
  case FPen.Style of
    psSolid: begin
      Result:=true;
    end;
    psClear: begin
      Result:=false;
    end;
  end;
end;

{ TFPGUIPrivateMenuItem }

procedure TFPGUIPrivateMenuItem.HandleOnClick(ASender: TObject);
begin
  if Assigned(LCLMenuItem) and Assigned(LCLMenuItem.OnClick) then
   LCLMenuItem.OnClick(LCLMenuItem);
end;

{ TFPGUIWinAPIFont }

function TFPGUIWinAPIFont.GetFontHeight: integer;
begin
  Result:=FFontHeight;
end;

function TFPGUIWinAPIFont.GetFontSize: integer;
begin
  Result:=(-FFontHeight * 72) div 96;
end;

procedure TFPGUIWinAPIFont.SetFontHeight(const AValue: integer);
begin
  FFontHeight:=AValue;
end;

procedure TFPGUIWinAPIFont.SetFontSize(const AValue: integer);
begin
  FFontHeight:=(-96 * AValue) div 72;
end;

constructor TFPGUIWinAPIFont.Create;
begin
  FFontFace:='';
  Size:=8;
  fpgFont:=fpgGetFont('');
end;

constructor TFPGUIWinAPIFont.Create(const AFontData: TFontData);
var
  lFontDesc: string;
begin
  lFontDesc:=TFontToTfpgFontDesc(AFontData);
  FFontHeight:=AFontData.Height*-1;
  FFontFace:=AFontData.Name;
  fpgFont:=fpgGetFont(lFontDesc);
end;

constructor TFPGUIWinAPIFont.Create(const AfpgCanvas: TfpgCanvas);
begin
  fpgFont:=AfpgCanvas.Font;
end;

constructor TFPGUIWinAPIFont.Create(const AFontData: TLogFont);
var
  lFontDesc: string;
begin
  lFontDesc:=AFontData.lfFaceName;
  if AFontData.lfHeight<0 then begin
    lFontDesc:=lFontDesc+'-'+inttostr(AFontData.lfHeight*-1);
  end;
  if AFontData.lfWeight>400 then lfontdesc:=lFontDesc+':bold';
  if AFontData.lfItalic>0 then lFontDesc:=lFontDesc+':italic';
  if AFontData.lfUnderline>0 then lFontDesc:=lFontDesc+':underline';
  if AFontData.lfStrikeOut>0 then lfontdesc:=lfontdesc+':strikeout';

  FFontHeight:=AFontData.lfHeight;
  FFontFace:=AFontData.lfFaceName;
  fpgFont:=fpgGetFont(lFontDesc);

end;

constructor TFPGUIWinAPIFont.Create(const AFontData: TLogFont;
  const ALongFontName: string);
var
  lFontDesc: string;
begin
  lFontDesc:=ALongFontName;
  if AFontData.lfHeight<0 then begin
    lFontDesc:=lFontDesc+'-'+inttostr(round((AFontData.lfHeight*-1)*72/96));
  end;
  if AFontData.lfWeight>400 then lfontdesc:=lFontDesc+':bold';
  if AFontData.lfItalic>0 then lFontDesc:=lFontDesc+':italic';
  if AFontData.lfUnderline>0 then lFontDesc:=lFontDesc+':underline';
  if AFontData.lfStrikeOut>0 then lfontdesc:=lfontdesc+':strikeout';

  FFontFace:=ALongFontName;
  FFontHeight:=AFontData.lfHeight;
  fpgFont:=fpgGetFont(lFontDesc);
end;

destructor TFPGUIWinAPIFont.Destroy;
begin
  FreeAndNIL(fpgFont);
  inherited Destroy;
end;

{ TFPGUIWinAPIPen }

function TFPGUIWinAPIPen.GetColor: TfpgColor;
begin
  Result:=FPen.Color;
end;

function TFPGUIWinAPIPen.GetLineWidthX: integer;
begin
  Result:=FPen.Width;
end;

function TFPGUIWinAPIPen.GetStyle: TFPGUIWinAPIPenStyle;
begin
  Result:=FPen.Style;
end;

procedure TFPGUIWinAPIPen.SetColor(const AValue: TfpgColor);
begin
  FPen.Color:=AValue;
end;

procedure TFPGUIWinAPIPen.SetLineWidthX(AValue: integer);
begin
  FPen.Width:=AValue;
end;

procedure TFPGUIWinAPIPen.SetStyle(AValue: TFPGUIWinAPIPenStyle);
begin
  FPen.Style:=AValue;
end;

constructor TFPGUIWinAPIPen.Create;
begin
  FPen.Color:=0;
  FPen.Width:=1;
  FPen.Style:=psSolid;
end;

constructor TFPGUIWinAPIPen.Create(const APenData: TLogPen);
begin
  Create;
  FPen.Color:=TColorToTfpgColor(APenData.lopnColor);
  FPen.Width:=APenData.lopnWidth.x;
  case (APenData.lopnStyle and PS_STYLE_MASK) of
    0: FPen.Style:=psSolid;
    5: FPen.Style:=psClear;
    otherwise
      FPen.Style:=psSolid;
  end;
end;

destructor TFPGUIWinAPIPen.Destroy;
begin
  inherited Destroy;
end;

{ TFPGUIWinAPIBrush }

function TFPGUIWinAPIBrush.GetColor: TfpgColor;
begin
  Result:=FBrush.Color
end;

function TFPGUIWinAPIBrush.GetStyle: TFPGUIWinAPIBrushStyle;
begin
  Result:=FBrush.Style;
end;

procedure TFPGUIWinAPIBrush.SetColor(const AValue: TfpgColor);
begin
  FBrush.Color:=AValue;
end;

procedure TFPGUIWinAPIBrush.SetStyle(AValue: TFPGUIWinAPIBrushStyle);
begin
  FBrush.Style:=AValue;
end;

constructor TFPGUIWinAPIBrush.Create;
begin
  FBrush.Color:=TColorToTfpgColor(clBtnFace);
  FBrush.Style:=bsSolid;
end;

constructor TFPGUIWinAPIBrush.Create(const ABrushData: TLogBrush);
begin
  Create;
  FBrush.Color:=TColorToTfpgColor(ABrushData.lbColor);
  case ABrushData.lbStyle of
    0: FBrush.Style:=bsSolid;
    1: FBrush.Style:=bsClear;
    otherwise
      FBrush.Style:=bsSolid;
  end;
end;

destructor TFPGUIWinAPIBrush.Destroy;
begin
  inherited Destroy;
end;

{ TFPGUIBasicRegion }

function TFPGUIBasicRegion.GetRegionType: TFPGUIRegionType;
begin
  Result:=FRegionType;
end;

function TFPGUIBasicRegion.GetfpgRectRegion: TfpgRect;
begin
  TRectTofpgRect(FRectRegion,Result);
end;

function TFPGUIBasicRegion.GetDebugString: string;
begin
  Result:='';
  Str(FRegionType,Result);
  Result:=Result+': '+Format('%d,%d-%d,%d',[FRectRegion.Left,FRectRegion.Top,FRectRegion.Right,FRectRegion.Bottom]);
end;

constructor TFPGUIBasicRegion.Create;
var
  ARect: TRect;
begin
  FillByte(ARect,sizeof(ARect),0);
  CreateRectRegion(ARect);
end;

constructor TFPGUIBasicRegion.Create(const ARect: TRect);
begin
  CreateRectRegion(ARect);
end;

destructor TFPGUIBasicRegion.Destroy;
begin
  inherited Destroy;
end;

procedure TFPGUIBasicRegion.CreateRectRegion(const ARect: TRect);
begin
  FRectRegion:=ARect;
  if (FRectRegion.Left=FRectRegion.Top) and (FRectRegion.Right=FRectRegion.Bottom) and
    (FRectRegion.Top=FRectRegion.Bottom) then begin
    FRegionType:=eRegionNULL;
  end else begin
    FRegionType:=eRegionSimple;
  end;
end;

function TFPGUIBasicRegion.CombineWithRegion(const ARegion: TFPGUIBasicRegion;
  const ACombineMode: TFPGUIRegionCombine): TFPGUIBasicRegion;
  function Min(const V1,V2: SizeInt): SizeInt;
  begin
    if V1<V2 then Result:=V1 else Result:=V2;
  end;
  function Max(const V1,V2: SizeInt): SizeInt;
  begin
    if V1>V2 then Result:=V1 else Result:=V2;
  end;
  procedure CombineAnd(const TargetRegion: TFPGUIBasicRegion; const r1,r2: TRect);
  var
    Intersect: Boolean;
  begin
    if (r2.Left>r1.Right) or
       (r2.Right<r1.Left) or
       (r2.Top>r1.Bottom) or
       (r2.Bottom<r1.Top) then begin
      Intersect:=false;
    end else begin
      Intersect:=true;
    end;
  	if Intersect then begin
      TargetRegion.CreateRectRegion(
        classes.Rect(
          Max(r1.Left,r2.Left),
          Max(r1.Top,r2.Top),
          Min(r1.Right,r2.Right),
          Min(r1.Bottom,r2.Bottom)
        )
      );
   	end else begin
      TargetRegion.CreateRectRegion(classes.Rect(0,0,0,0));
    end;
  end;
begin
  Result:=TFPGUIBasicRegion.Create;
  Case ACombineMode of
    eRegionCombineAnd:  CombineAnd(Result,ARegion.FRectRegion,Self.FRectRegion);
    eRegionCombineCopy,
    eRegionCombineDiff:
      begin
        Result.CreateRectRegion(rect(0,0,0,0));
      end;
    eRegionCombineOr,
    eRegionCombineXor:
      begin
        Raise Exception.CreateFmt('Region mode %d not supported',[integer(ACombineMode)]);
      end;
  end;
{$IFDEF VERBOSEDEBUG_FPGUI_REGION}
    writeln('fpGUI Region: ',ARegion.DebugString,' ',ACombineMode,' ',Self.DebugString,' = ',Result.DebugString);
{$ENDIF}
end;

finalization
  FreeAndNil(FPGUIDesktopDC);

end.