forked from synopse/mORMot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmormot.ui.core.pas
822 lines (720 loc) · 22.2 KB
/
mormot.ui.core.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
/// Framework Core Visual Shared Types and Functions
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.ui.core;
{
*****************************************************************************
Basic types and reusable functions for VCL/LCL User Interface support
- Some LCL/VCL cross-compatibility definitions
- High-Level UI Wrapper Functions
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
uses
{$ifdef OSWINDOWS}
Windows,
{$endif OSWINDOWS}
{$ifdef FPC}
LCLType,
LCLProc,
LCLIntf,
LMessages,
{$else}
Types,
Messages,
{$endif FPC}
{$ifdef NEEDVCLPREFIX}
vcl.Graphics,
vcl.Controls,
vcl.Themes,
{$else}
Graphics,
Controls,
Themes,
{$endif NEEDVCLPREFIX}
sysutils,
classes,
variants,
mormot.core.base,
mormot.core.os,
mormot.core.unicode;
{************ Some LCL/VCL cross-compatibility definitions }
{$ifdef FPC}
type
TWMTimer = TLMTimer;
const
WM_TIMER = LM_TIMER;
{$ifdef OSWINDOWS}
type
TMetaFile = class;
/// FPC LCL is missing Windows MetaFile support: minimal Canvas wrapper
// - just encapsulate the WinAPI - so is not cross platform yet
TMetaFileCanvas = class(TCanvas)
private
fMetafile: TMetaFile;
public
/// setup a TCanvas to write into a TMetaFile
constructor Create(AMetafile: TMetaFile; ReferenceDevice: HDC);
/// setup a TCanvas to write into a TMetaFile
constructor CreateWithComment(AMetafile: TMetaFile; ReferenceDevice: HDC;
const CreatedBy, Description: string);
/// will close the Canvas HDC and assign it to the associated TMetaFile
destructor Destroy; override;
end;
/// FPC LCL is missing Windows MetaFile support: minimal MetaFile wrapper
// - just encapsulate the WinAPI - so is not cross platform yet
TMetaFile = class(TGraphic)
protected
fImageHandle: HENHMETAFILE;
fImageMMWidth: integer; // in 0.01 mm logical pixels
fImageMMHeight: integer; // in 0.01 mm logical pixels
fImagePxWidth: integer; // in device pixels
fImagePxHeight: integer; // in device pixels
fHeader: TEnhMetaHeader;
function GetAuthor: string;
function GetDescription: string;
procedure SetHandle(Value: HENHMETAFILE);
procedure SetMMHeight(Value: integer);
procedure SetMMWidth(Value: integer);
procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
function GetEmpty: boolean; override;
function GetHeight: integer; override;
function GetWidth: integer; override;
procedure SetHeight(Value: integer); override;
procedure SetWidth(Value: integer); override;
function GetTransparent: boolean; override;
procedure SetTransparent(Value: boolean); override;
public
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Clear; override;
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToStream(Stream: TStream); override;
function ReleaseHandle: HENHMETAFILE;
property Handle: HENHMETAFILE
read fImageHandle write SetHandle;
property Empty: boolean
read GetEmpty;
property CreatedBy: string
read GetAuthor;
property Description: string
read GetDescription;
property MMWidth: integer
read fImageMMWidth write SetMMWidth;
property MMHeight: integer
read fImageMMHeight write SetMMHeight;
end;
const
EMR_HEADER = 1;
EMR_POLYBEZIER = 2;
EMR_POLYGON = 3;
EMR_POLYLINE = 4;
EMR_POLYBEZIERTO = 5;
EMR_POLYLINETO = 6;
EMR_POLYPOLYLINE = 7;
EMR_POLYPOLYGON = 8;
EMR_SETWINDOWEXTEX = 9;
EMR_SETWINDOWORGEX = 10;
EMR_SETVIEWPORTEXTEX = 11;
EMR_SETVIEWPORTORGEX = 12;
EMR_SETBRUSHORGEX = 13;
EMR_EOF = 14;
EMR_SETPIXELV = 15;
EMR_SETMAPMODE = 17;
EMR_SETBKMODE = 18;
EMR_SETPOLYFILLMODE = 19;
EMR_SETROP2 = 20;
EMR_SETSTRETCHBLTMODE = 21;
EMR_SETTEXTALIGN = 22;
EMR_SETTEXTCOLOR = 24;
EMR_SETBKCOLOR = 25;
EMR_OFFSETCLIPRGN = 26;
EMR_MOVETOEX = 27;
EMR_SETMETARGN = 28;
EMR_EXCLUDECLIPRECT = 29;
EMR_INTERSECTCLIPRECT = 30;
EMR_SAVEDC = 33;
EMR_RESTOREDC = 34;
EMR_SETWORLDTRANSFORM = 35;
EMR_MODIFYWORLDTRANSFORM = 36;
EMR_SELECTOBJECT = 37;
EMR_CREATEPEN = 38;
EMR_CREATEBRUSHINDIRECT = 39;
EMR_DELETEOBJECT = 40;
EMR_ELLIPSE = 42;
EMR_RECTANGLE = 43;
EMR_ROUNDRECT = 44;
EMR_ARC = 45;
EMR_CHORD = 46;
EMR_PIE = 47;
EMR_SELECTPALETTE = 48;
EMR_CREATEPALETTE = 49;
EMR_SETPALETTEENTRIES = 50;
EMR_RESIZEPALETTE = 51;
EMR_REALIZEPALETTE = 52;
EMR_EXTFLOODFILL = 53;
EMR_LINETO = 54;
EMR_ARCTO = 55;
EMR_POLYDRAW = 56;
EMR_SETARCDIRECTION = 57;
EMR_SETMITERLIMIT = 58;
EMR_BEGINPATH = 59;
EMR_ENDPATH = 60;
EMR_CLOSEFIGURE = 61;
EMR_FILLPATH = 62;
EMR_STROKEANDFILLPATH = 63;
EMR_STROKEPATH = 64;
EMR_FLATTENPATH = 65;
EMR_WIDENPATH = 66;
EMR_SELECTCLIPPATH = 67;
EMR_ABORTPATH = 68;
EMR_GDICOMMENT = 70;
EMR_FILLRGN = 71;
EMR_EXTSELECTCLIPRGN = 75;
EMR_BITBLT = 76;
EMR_STRETCHBLT = 77;
EMR_STRETCHDIBITS = 81;
EMR_EXTCREATEFONTINDIRECTW = 82;
EMR_EXTTEXTOUTA = 83;
EMR_EXTTEXTOUTW = 84;
EMR_POLYBEZIER16 = 85;
EMR_POLYGON16 = 86;
EMR_POLYLINE16 = 87;
EMR_POLYBEZIERTO16 = 88;
EMR_POLYLINETO16 = 89;
EMR_POLYPOLYLINE16 = 90;
EMR_POLYPOLYGON16 = 91;
EMR_POLYDRAW16 = 92;
EMR_EXTCREATEPEN = 95;
EMR_SETICMMODE = 98;
EMR_SMALLTEXTOUT = 108;
EMR_ALPHADIBBLEND = 115;
EMR_TRANSPARENTBLT = 116;
EMR_GRADIENTFILL = 118;
type
PEMRTransparentBLT = ^TEMRTransparentBLT;
TEMRTransparentBLT = packed record
emr: TEMR;
rclBounds: TRect;
xDest: integer;
yDest: integer;
cxDest: integer;
cyDest: integer;
dwRop: DWORD;
xSrc: integer;
ySrc: integer;
xformSrc: XFORM; { Source DC transform}
crBkColorSrc: COLORREF; { Source DC BkColor in RGB}
iUsageSrc: DWORD; { Source bitmap info color table usage}
offBmiSrc: DWORD; { Offset to the source TBitmapInfo structure}
cbBmiSrc: DWORD; { Size of the source TBitmapInfo structure}
offBitsSrc: DWORD; { Offset to the source bitmap bits}
cbBitsSrc: DWORD; { Size of the source bitmap bits}
cxSrc: integer;
cySrc: integer;
end;
PEMGradientFill = ^TEMGradientFill;
TEMGradientFill = record
emr: TEMR;
rclBounds: TRect; { Inclusive-inclusive bounds in device units}
nVer: DWORD;
nTri: DWORD;
ulMode: ULONG;
Ver: array[0..0] of TTriVertex;
end;
{$endif OSWINDOWS}
{$endif FPC}
{************ High-Level UI Wrapper Functions }
const
CheckBoxWidth = 13;
/// draw a CheckBox in the Canvas Handle of the Wwindow hWnd, in the middle
// of the Rect coordinates
// - use theming under XP, Vista and Seven
procedure DrawCheckBox(
hWnd: THandle; Handle: HDC; const Rect: TRect; Checked: boolean);
/// draw a small vertical Arrow, e.g. to display grid sorting order
procedure DrawSortArrow(Canvas: TCanvas; X, Y: integer; Asc: boolean);
/// wrapper around DrawText() with proper BiDir and Unicode support
procedure DrawTextUtf8(Control: TCustomControl; Canvas: TCanvas;
const Text: RawUtf8; var Rect: TRect; CalcOnly: boolean = false);
/// same as TControl.DrawTextBiDiModeFlagsReadingOnly but also for LCL
function DrawTextBiDiModeFlags(Control: TControl): integer;
/// compute the DrawText() flags for a given Control, with Bidir and word brakes
function DrawTextFlags(Control: TControl; CalcOnly: boolean): integer;
/// wrapper around ExtTextOutW() or TextRect() with alignment and Unicode support
procedure TextRectUtf8(const Rect: TRect; Canvas: TCanvas; X, Y: integer;
Text: RawUtf8; Align: TAlignment = taLeftJustify; NoControlChar: boolean = false);
/// wrapper around ExtTextOutW() or TextRect() with alignment and Unicode support
// - X,Y are relative to the Rect.Left/Top corner
procedure TextRectString(const Rect: TRect; Canvas: TCanvas; X, Y: integer;
const Text: string; Align: TAlignment = taLeftJustify; NoControlChar: boolean = false);
/// alternative to the VCL Canvas.TextFlas property for text output
function TextFlags(Canvas: TCanvas): integer;
{$ifdef OSWINDOWS}
/// test if the ClearType is enabled for font display
// - ClearType is a software technology that improves the readability of text
// on liquid crystal display (LCD) monitors
function IsClearTypeEnabled: boolean;
/// enable the ClearType font display
// - under Windows 2000, standard font smoothing is forced, since Clear Type
// was introduced with XP
procedure ClearTypeEnable;
{$else}
/// some LCL compatibility function
function ExtTextOutW(DC: HDC; X, Y, Options: integer; Rect: PRect;
Str: PWideChar; Count: integer; Dx: ObjPas.PInteger): boolean;
{$endif OSWINDOWS}
implementation
{************ Some LCL/VCL cross-compatibility definitions }
{$ifdef FPC}
{$ifdef OSWINDOWS}
// those types are not defined on LCL :( -> simple WinAPI encapsulation
{ TMetaFileCanvas }
constructor TMetaFileCanvas.Create(AMetafile: TMetaFile; ReferenceDevice: HDC);
begin
CreateWithComment(AMetafile, ReferenceDevice, AMetafile.CreatedBy,
AMetafile.Description);
end;
constructor TMetaFileCanvas.CreateWithComment(AMetafile: TMetaFile;
ReferenceDevice: HDC; const CreatedBy, Description: string);
var
R: TRect;
ref, tmp: HDC;
desc: SynUnicode;
begin
inherited Create;
fMetafile := AMetafile;
ref := ReferenceDevice;
if ref = 0 then
ref := GetDC(0);
try
if fMetafile.MMWidth = 0 then
if fMetafile.Width = 0 then
fMetafile.MMWidth := GetDeviceCaps(ref, HORZSIZE) * 100
else
fMetafile.MMWidth := MulDiv(fMetafile.Width,
GetDeviceCaps(ref, HORZSIZE) * 100, GetDeviceCaps(ref, HORZRES));
if fMetafile.MMHeight = 0 then
if fMetafile.Height = 0 then
fMetafile.MMHeight := GetDeviceCaps(ref, VERTSIZE) * 100
else
fMetafile.MMHeight := MulDiv(fMetafile.Height,
GetDeviceCaps(ref, VERTSIZE) * 100, GetDeviceCaps(ref, VERTRES));
R := Rect(0, 0, fMetafile.MMWidth, fMetafile.MMHeight);
if (CreatedBy <> '') or
(Description <> '') then
StringToSynUnicode(CreatedBy + #0 + Description + #0#0, desc);
tmp := CreateEnhMetafileW(ref, nil, @R, pointer(desc));
if tmp = 0 then
raise EOutOfResources.Create('TMetaFileCanvas: Out of Resources');
Handle := tmp;
finally
if ReferenceDevice = 0 then
ReleaseDC(0, ref);
end;
end;
destructor TMetaFileCanvas.Destroy;
begin
fMetafile.SetHandle(CloseEnhMetafile(Handle));
inherited Destroy;
end;
{ TMetaFile }
destructor TMetaFile.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TMetaFile.Assign(Source: TPersistent);
begin
if (Source = nil) or
(Source is TMetaFile) then
begin
if fImageHandle <> 0 then
Clear;
if Assigned(Source) then
begin
fImageHandle := TMetaFile(Source).Handle;
fImageMMWidth := TMetaFile(Source).MMWidth;
fImageMMHeight := TMetaFile(Source).MMHeight;
fImagePxWidth := TMetaFile(Source).Width;
fImagePxHeight := TMetaFile(Source).Height;
fHeader.nSize := 0;
end
end
else
inherited Assign(Source);
end;
procedure TMetaFile.Clear;
begin
if fImageHandle <> 0 then
DeleteEnhMetafile(fImageHandle);
fImageHandle := 0;
fHeader.nSize := 0;
end;
function GetRawDescription(H: HENHMETAFILE; var tmp: TSynTempBuffer): boolean;
var
n: integer;
begin
result := false;
if H = 0 then
exit;
n := GetEnhMetafileDescriptionW(H, 0, nil);
if n <= 0 then
exit;
tmp.Init(n * 2);
GetEnhMetafileDescriptionW(H, n, tmp.buf);
result := true;
// tmp.buf contains UTF-16 encoded Author + #0 + Description + #0#0
end;
function TMetaFile.GetAuthor: string;
var
tmp: TSynTempBuffer;
begin
result := '';
if not GetRawDescription(fImageHandle, tmp) then
exit;
RawUnicodeToString(tmp.buf, StrLenW(tmp.buf), result);
tmp.Done;
end;
function TMetaFile.GetDescription: string;
var
tmp: TSynTempBuffer;
P: PWideChar;
begin
result := '';
if not GetRawDescription(fImageHandle, tmp) then
exit;
P := tmp.buf;
inc(P, StrLenW(tmp.buf) + 1);
RawUnicodeToString(P, StrLenW(P), result);
tmp.Done;
end;
function TMetaFile.GetEmpty: boolean;
begin
result := (self = nil) or
(fImageHandle = 0);
end;
procedure TMetaFile.SetHandle(Value: HENHMETAFILE);
var
hdr: TEnhMetaHeader;
begin
if (Value <> 0) and
(GetEnhMetafileHeader(Value, SizeOf(hdr), @hdr) = 0) then
raise EInvalidImage.Create('Invalid Metafile');
if fImageHandle <> 0 then
Clear;
fImageHandle := Value;
fImagePxWidth := 0;
fImagePxHeight := 0;
if Value = 0 then
exit;
fHeader := hdr;
fImageMMWidth := hdr.rclFrame.Right - hdr.rclFrame.Left;
fImageMMHeight := hdr.rclFrame.Bottom - hdr.rclFrame.Top;
end;
procedure TMetaFile.SetMMHeight(Value: integer);
begin
fImagePxHeight := 0;
if fImageMMHeight <> Value then
fImageMMHeight := Value;
end;
procedure TMetaFile.SetMMWidth(Value: integer);
begin
fImagePxWidth := 0;
if fImageMMWidth <> Value then
fImageMMWidth := Value;
end;
procedure TMetaFile.Draw(ACanvas: TCanvas; const Rect: TRect);
var
R: TRect;
begin
if fImageHandle = 0 then
exit;
R := Rect;
PlayEnhMetaFile(ACanvas.Handle, fImageHandle, R);
end;
function TMetaFile.GetHeight: integer;
begin
if fImageHandle = 0 then
result := fImagePxHeight
else
with fHeader do
result := MulDiv(fImageMMHeight, szlDevice.cy, szlMillimeters.cy * 100);
end;
function TMetaFile.GetWidth: integer;
begin
if fImageHandle = 0 then
result := fImagePxWidth
else
with fHeader do
result := MulDiv(fImageMMWidth, szlDevice.cx, szlMillimeters.cx * 100);
end;
procedure TMetaFile.SetHeight(Value: integer);
begin
if fImageHandle = 0 then
fImagePxHeight := Value
else
with fHeader do
MMHeight := MulDiv(Value, szlMillimeters.cy * 100, szlDevice.cy);
end;
procedure TMetaFile.SetWidth(Value: integer);
begin
if fImageHandle = 0 then
fImagePxWidth := Value
else
with fHeader do
MMWidth := MulDiv(Value, szlMillimeters.cx * 100, szlDevice.cx);
end;
procedure TMetaFile.LoadFromStream(Stream: TStream);
var
tmp: TSynTempBuffer;
p: PENHMETAHEADER;
begin
Clear;
Stream.ReadBuffer(fHeader, SizeOf(fHeader));
if fHeader.dSignature <> ENHMETA_SIGNATURE then
raise EInvalidImage.Create('Invalid Metafile Stream');
p := tmp.Init(fHeader.nBytes);
try
p^ := fHeader; // we already read EMF header bytes from Stream
inc(p);
Stream.ReadBuffer(p^, fHeader.nBytes - SizeOf(p^));
SetHandle(SetEnhMetaFileBits(fHeader.nBytes, tmp.Buf));
finally
tmp.Done;
end;
end;
procedure TMetaFile.SaveToStream(Stream: TStream);
var
tmp: TSynTempBuffer;
begin
if fImageHandle = 0 then
exit;
tmp.Init(GetEnhMetaFileBits(fImageHandle, 0, nil));
if tmp.len <> 0 then
try
GetEnhMetaFileBits(fImageHandle, tmp.len, tmp.buf);
Stream.WriteBuffer(tmp.buf^, tmp.len);
finally
tmp.Done;
end;
end;
function TMetaFile.ReleaseHandle: HENHMETAFILE;
begin
Clear;
result := 0;
end;
function TMetaFile.GetTransparent: boolean;
begin
// not implemented
result := false;
end;
procedure TMetaFile.SetTransparent(Value: boolean);
begin
// not implemented
end;
{$endif OSWINDOWS}
{$endif FPC}
{************ High-Level UI Wrapper Functions }
{$WARN SYMBOL_DEPRECATED OFF} // for ThemeServices
const
XPState: array[boolean] of TThemedButton = (
tbCheckBoxUncheckedNormal,
tbCheckBoxCheckedNormal);
Win32State: array[boolean] of cardinal = (
DFCS_BUTTONCHECK,
DFCS_BUTTONCHECK or DFCS_CHECKED);
procedure DrawCheckBox(hWnd: THandle; Handle: HDC; const Rect: TRect; Checked: boolean);
var
DrawRect: TRect;
begin
DrawRect.Left := Rect.Left + (Rect.Right - Rect.Left - CheckBoxWidth) shr 1;
DrawRect.Top := Rect.Top + 2;
DrawRect.Right := DrawRect.Left + CheckBoxWidth;
DrawRect.Bottom := DrawRect.Top + CheckBoxWidth;
if ThemeServices.ThemesEnabled then
// Windows XP and later: use theming
ThemeServices.DrawElement(Handle,
ThemeServices.GetElementDetails(XPState[Checked]), DrawRect)
else
DrawFrameControl(Handle, DrawRect, DFC_BUTTON, Win32State[Checked]);
end;
procedure DrawSortArrow(Canvas: TCanvas; X, Y: integer; Asc: boolean);
var
Points: array[0..2] of TPoint;
begin
if Canvas = nil then
exit;
if Asc then
begin
Points[0].X := X + 5; // ascending order arrow
Points[1].X := X;
Points[2].X := X - 5;
Points[0].Y := Y - 5;
Points[1].Y := Y + 5;
Points[2].Y := Y - 5
end
else
begin
Points[0].X := X - 5; // descending order arrow
Points[1].X := X + 5;
Points[2].X := X;
Points[0].Y := Y + 5;
Points[1].Y := Y + 5;
Points[2].Y := Y - 5
end;
Canvas.Brush.Color := clWhite; // fill the arrow content
Canvas.Polygon(Points);
Canvas.Pen.Color := clLtGray; // draw the arrow border
Canvas.MoveTo(Points[0].X, Points[0].Y);
Canvas.LineTo(Points[1].X, Points[1].Y);
Canvas.Pen.Color := clGray;
Canvas.LineTo(Points[2].X, Points[2].Y);
Canvas.LineTo(Points[0].X, Points[0].Y);
end;
{$ifdef OSWINDOWS} // ClearType is a really Windows specific feature
const // for Delphi 6 compilation
SPI_GETFONTSMOOTHINGTYPE = $200A;
SPI_SETFONTSMOOTHINGTYPE = $200B;
FE_FONTSMOOTHINGSTANDARD = $0001;
FE_FONTSMOOTHINGCLEARTYPE = $0002;
function IsClearTypeEnabled: boolean;
// see http://blogs.msdn.com/michkap/archive/2008/03/01/7971061.aspx
var
MType, SmoothFonts: DWORD;
begin
SmoothFonts := 0;
SystemParametersInfo(SPI_GETFONTSMOOTHING, 1, @SmoothFonts, 0);
SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, @MType, 0);
result := boolean(SmoothFonts) and
(MType = FE_FONTSMOOTHINGCLEARTYPE);
end;
procedure ClearTypeEnable;
var
MType: PtrUInt;
SmoothFonts: DWORD;
begin
if (Win32MajorVersion < 5) or
IsClearTypeEnabled then
exit; // no font smoothing before Win2K
SystemParametersInfo(SPI_GETFONTSMOOTHING, 1, @SmoothFonts, 0);
if not boolean(SmoothFonts) then
SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, nil,
SPIF_UPDATEINIFILE or SPIF_SENDCHANGE);
if (Win32MajorVersion = 5) and
(Win32MinorVersion = 0) then
// no Clear Type on Win2K
MType := FE_FONTSMOOTHINGSTANDARD
else
MType := FE_FONTSMOOTHINGCLEARTYPE;
SystemParametersInfo(SPI_SETFONTSMOOTHINGTYPE, 0, pointer(MType),
SPIF_UPDATEINIFILE or SPIF_SENDCHANGE);
end;
const
TEXT_ALIGN: array[TAlignment] of integer =
(TA_LEFT, // taLeftJustify
TA_RIGHT, // taRightJustify
TA_CENTER); // taCenter
{$else}
function ExtTextOutW(DC: HDC; X, Y, Options: integer; Rect: PRect;
Str: PWideChar; Count: integer; Dx: ObjPas.PInteger): boolean;
var
temp: Utf8String;
L: integer; // no need to resize temp buffer, just get size
begin
temp := RawUnicodeToUtf8(Str, Count, L);
result := ExtTextOut(DC, X, Y, Options, Rect, pointer(temp), L, Dx);
end;
{$endif OSWINDOWS}
function DrawTextBiDiModeFlags(Control: TControl): integer;
begin
if Assigned(Control) and
Control.UseRightToLeftReading then
result := DT_RTLREADING
else
result := 0;
end;
function DrawTextFlags(Control: TControl; CalcOnly: boolean): integer;
begin
result := DT_LEFT or DT_WORDBREAK or DT_NOPREFIX;
if CalcOnly then
result := result or DT_CALCRECT;
if Assigned(Control) and
Control.UseRightToLeftReading then
result := result or DT_RTLREADING
end;
{$ifdef OSWINDOWS}
procedure DrawTextUtf8(Control: TCustomControl; Canvas: TCanvas;
const Text: RawUtf8; var Rect: TRect; CalcOnly: boolean);
var
tmp: TSynTempBuffer;
n: integer;
begin
n := Utf8DecodeToUnicode(Text, tmp);
DrawText(Canvas.Handle, tmp.buf, n, Rect, DrawTextFlags(Control, CalcOnly));
tmp.Done;
end;
{$else}
procedure DrawTextUtf8(Control: TCustomControl; Canvas: TCanvas;
const Text: RawUtf8; var Rect: TRect; CalcOnly: boolean);
begin
DrawText(Canvas.Handle, pointer(Text), length(Text), Rect, // LCL is UTF-8
DrawTextFlags(Control, CalcOnly));
end;
{$endif OSWINDOWS}
function TextFlags(Canvas: TCanvas): integer;
begin
{$ifdef FPC}
result := ETO_CLIPPED;
if Canvas.Brush.Style <> bsClear then
result := result or ETO_OPAQUE;
{$else}
result := Canvas.TextFlags;
{$endif FPC}
end;
procedure TextRectUtf8(const Rect: TRect; Canvas: TCanvas; X, Y: integer;
Text: RawUtf8; Align: TAlignment; NoControlChar: boolean);
var
i: PtrInt;
{$ifdef OSWINDOWS}
options, n: integer;
tmp: TSynTempBuffer;
{$else}
style: TTextStyle;
{$endif OSWINDOWS}
begin
if (Text = '') or
not Assigned(Canvas) then
exit;
if NoControlChar then
for i := 1 to length(Text) do
if Text[i] < ' ' then
Text[i] := ' ';
inc(X, Rect.Left);
inc(Y, Rect.Top);
{$ifdef OSWINDOWS}
// direct Win32 API call
options := ETO_CLIPPED {$ifdef ISDELPHI} or Canvas.TextFlags{$endif};
if Canvas.Brush.Style <> bsClear then
options := options or ETO_OPAQUE;
if Align <> taLeftJustify then
SetTextAlign(Canvas.Handle, TEXT_ALIGN[Align]);
n := Utf8DecodeToUnicode(Text, tmp);
ExtTextOutW(Canvas.Handle, X, Y, options, @Rect, tmp.buf, n, nil);
tmp.Done;
if Align <> taLeftJustify then
SetTextAlign(Canvas.Handle, TA_LEFT);
{$else}
// LCL cross-platform display, expecting Text = string = Utf8String
style := Canvas.TextStyle;
style.Alignment := Align;
Canvas.TextRect(Rect, X, Y, Text, style);
{$endif OSWINDOWS}
end;
procedure TextRectString(const Rect: TRect; Canvas: TCanvas; X, Y: integer;
const Text: string; Align: TAlignment; NoControlChar: boolean);
begin
TextRectUtf8(Rect, Canvas, X, Y, StringToUtf8(Text), Align, NoControlChar);
end;
end.