forked from digao-dalpiaz/DzHTMLText
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDzHTMLText2.pas
More file actions
3149 lines (2674 loc) · 83.9 KB
/
DzHTMLText2.pas
File metadata and controls
3149 lines (2674 loc) · 83.9 KB
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
unit DzHTMLText2;
{$I dz2.inc}
{$IFDEF FPC}{$mode delphi}{$ENDIF}
{------------------------------------------------------------------------------
TDzHTMLText component
Developed by Rodrigo Depiné Dalpiaz (digăo dalpiaz)
Label with formatting tags support
https://github.com/digao-dalpiaz/DzHTMLText
Please, read the documentation at GitHub link.
Supported Tags:
<A[:abc]></A> - Link
<B></B> - Bold
<I></I> - Italic
<U></U> - Underline
<S></S> - Strike out
<FN:abc></FN> - Font Name
<FS:123></FS> - Font Size
<FC:clcolor|$999999></FC> - Font Color
<BC:clcolor|$999999></BC> - Background Color
<BR> - Line Break
<L></L> - Align Left
<C></C> - Align Center
<R></R> - Aligh Right
<T:123> - Tab
<TF:123> - Tab with aligned break
--------------------------
Jacek Pazera
Last mod: 2019.05.23
Lazarus support
=== New tags ===
<HR> - Horizontal line drawn in the color selected for the text with <fc> tag.
<H1> - Header 1
<H2> - Header 2
<H3> - Header 3
<LI> - List item - 1st level
<LI2> - List item - 2nd level
<LC> - Background line color drawn from the current position to the end of the current line
<BBC> - Body background color drawn from the beginning of the current line to the end of the document
<IMG> - Image
<SUB> - Subscript
<SUP> - Superscript
=== Additional changes ===
Support for HTML colors:
#FFAA88
#ABC - short notation for #AABBCC
RGB colors:
rgb(50,100,150)
rgb(50) - short notation for rgb(50,50,50)
ReplaceForcedChars function renamed to ReplaceHtmlEntities
Additional HTML entities: € .. ∘
ExtraLineSpacing
ExtraWordSpacing
Internal margins: left and right
Border - Warnig! Flickering when border width > 1. Place DzHTMLText on TPanel with DoubleBuffered set to True.
Vertical alignment - Used when AutoHeight = False
SaveToFile
LoadFromFile
SaveToBitmap
SaveToBitmapFile
AddPng
AddPngFromFile
GetPngImage
GetPngImageFromFile
PngCollection
TODO:
Text -> Lines
BeginUpdate, EndUpdate
TCustomDzHTMLText
------------------------------------------------------------------------------}
interface
uses
Controls, Classes, Messages, Generics.Collections, Graphics, {$IFDEF FPC}LCLType,{$ENDIF}
Types,
//{$IFDEF FPC}LazarusPackageIntf,{$ENDIF}
DzPngCollection,
{$IFDEF DCC}{$IFDEF HAS_UNIT_SCOPE}Vcl.Imaging.pngimage,{$ELSE}pngimage,{$ENDIF}{$ENDIF}
{$IFDEF DELPHIXE_OR_BELOW}DzHTMLText2_Helpers,{$ENDIF}
StrUtils
;
const
TAG_ID_HR = 10; // Horizontal line
TAG_ID_LC = 11; // Line color
TAG_ID_LI = 12; // List item
TAG_ID_LI2 = 13; // List item (2nd level)
TAG_ID_IMG = 14; // Image
TAG_ID_BBC = 15; // Body background color
//TAG_ID_SUB = 16; // Subscript
TAG_ID_LN = 20; // Line
type
TDHLSSmallInt = -5..10; // jp: for ExtraLineSpacing
TDHWSSmallInt = 0..5; // jp: for ExtraWordSpacing
TDHVerticalAlignment = (vaTop, vaCenter, vaBottom);
TDHBulletType = (btLongDash, btDash, btBullet, btCircle, btCustomString);
{$region ' --- TDHWord --- '}
{DHWord is an object to each word. Will be used to paint event.
The words are separated by space/tag/line break.}
TDHWord = class
private
Rect: TRect;
Text: String;
Group: Integer; //group number
{The group is isolated at each line or tabulation to delimit text align area}
Align: TAlignment;
Font: TFont;
BColor: TColor; //background color
Link: Boolean; //is a link
LinkID: Integer; //link number
{The link number is created sequencially, when reading text links
and works to know the link target, stored on a TStringList, because if
the link was saved here at a work, it will be repeat if has multiple words
per link, spending a lot of unnecessary memory.}
Space: Boolean; //is an space
Hover: Boolean; //the mouse is over the link
//HorizontalLine: Boolean;
ExtraColor: TColor;
TagID: integer;
public
constructor Create;
destructor Destroy; override;
end;
{$endregion TDHWord}
TDHWordList = class(TObjectList<TDHWord>)
private
procedure Add(Rect: TRect; Text: String; Group: Integer; Align: TAlignment;
Font: TFont; BColor: TColor; Link: Boolean; LinkID: Integer; Space: Boolean; ExtraColor: TColor = clNone; TagID: integer = -1);
end;
TDzHTMLText2 = class;
TDHKindStyleLinkProp = (tslpNormal, tslpHover); //kind of link style
{$region ' --- TDHStyleLinkProp --- '}
{DHStyleLinkProp is a subproperty used at Object Inspector that contains
link formatting when selected and not selected}
TDHStyleLinkProp = class(TPersistent)
private
Lb: TDzHTMLText2; //owner
Kind: TDHKindStyleLinkProp;
FFontColor: TColor;
FBackColor: TColor;
FUnderline: Boolean;
procedure SetFontColor(const Value: TColor);
procedure SetBackColor(const Value: TColor);
procedure SetUnderline(const Value: Boolean);
function GetDefaultFontColor: TColor;
function GetStoredFontColor: Boolean;
procedure SetPropsToCanvas(C: TCanvas); //method to use at paint event
function GetStored: Boolean; //getstored general to use at owner
protected
function GetOwner: TPersistent; override;
public
constructor Create(xLb: TDzHTMLText2; xKind: TDHKindStyleLinkProp);
procedure Assign(Source: TPersistent); override;
published
property FontColor: TColor read FFontColor write SetFontColor stored GetStoredFontColor;
property BackColor: TColor read FBackColor write SetBackColor default clNone;
property Underline: Boolean read FUnderline write SetUnderline default False;
end;
{$endregion TDHStyleLinkProp}
{$region ' --- TDHLinkData --- '}
TDHLinkData = class
private
FTarget: String;
FText: String;
public
property Target: String read FTarget;
property Text: String read FText;
end;
{$endregion TDHLinkData}
TDHLinkDataList = class(TObjectList<TDHLinkData>);
TDHEvLink = procedure(Sender: TObject; LinkID: Integer; LinkData: TDHLinkData) of object;
TDHEvLinkClick = procedure(Sender: TObject; LinkID: Integer; LinkData: TDHLinkData; var Handled: Boolean) of object;
{$region ' --- TDHTagHRParams --- '}
TDHTagHRParams = class(TPersistent)
private
FStyle: TPenStyle;
FLineHeight: integer;
FOnChange: TNotifyEvent;
procedure SetStyle(const Value: TPenStyle);
procedure SetLineHeight(const Value: integer);
procedure SetOnChange(const Value: TNotifyEvent);
public
constructor Create;
published
property Style: TPenStyle read FStyle write SetStyle default psSolid;
property LineHeight: integer read FLineHeight write SetLineHeight default 1;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHTagHRParams}
{$region ' --- TDHTagHeaderParams --- '}
TDHTagHeaderParams = class(TPersistent)
private
FFont: TFont;
FOnChange: TNotifyEvent;
FBackgroundColor: TColor;
FAlignment: TAlignment;
FTransparent: Boolean;
procedure SetFont(const Value: TFont);
procedure SetOnChange(const Value: TNotifyEvent);
procedure SetBackgroundColor(const Value: TColor);
procedure SetAlignment(const Value: TAlignment);
procedure SetTransparent(const Value: Boolean);
public
constructor Create;
destructor Destroy; override;
published
property Font: TFont read FFont write SetFont;
property BackgroundColor: TColor read FBackgroundColor write SetBackgroundColor default clBtnFace;
property Transparent: Boolean read FTransparent write SetTransparent default True;
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHTagHeaderParams}
{$region ' --- TDHTagH1Params --- '}
TDHTagH1Params = class(TDHTagHeaderParams)
private
public
constructor Create;
published
property Font;
property BackgroundColor;
property Transparent;
property Alignment;
property OnChange;
end;
{$endregion TDHTagH1Params}
{$region ' --- TDHTagH2Params --- '}
TDHTagH2Params = class(TDHTagHeaderParams)
private
public
constructor Create;
published
property Font;
property BackgroundColor;
property Transparent;
property Alignment;
property OnChange;
end;
{$endregion TDHTagH2Params}
{$region ' --- TDHTagH3Params --- '}
TDHTagH3Params = class(TDHTagHeaderParams)
private
public
constructor Create;
published
property Font;
property BackgroundColor;
property Transparent;
property Alignment;
property OnChange;
end;
{$endregion TDHTagH3Params}
{$region ' --- TDHInternalMargins --- '}
TDHInternalMargins = class(TPersistent)
private
FLeft: integer;
FRight: integer;
FEnabled: Boolean;
FOnChange: TNotifyEvent;
procedure SetLeft(const Value: integer);
procedure SetRight(const Value: integer);
procedure SetEnabled(const Value: Boolean);
procedure SetOnChange(const Value: TNotifyEvent);
public
constructor Create;
published
property Left: integer read FLeft write SetLeft default 8;
property Right: integer read FRight write SetRight default 8;
property Enabled: Boolean read FEnabled write SetEnabled default False;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHInternalMargins}
{$region ' --- TDHTagLIParams --- '}
TDHTagLIParams = class(TPersistent)
private
FMargin: integer;
FOnChange: TNotifyEvent;
FBulletType: TDHBulletType;
FCustomString: string;
FSpacing: Byte;
procedure SetMargin(const Value: integer);
procedure SetOnChange(const Value: TNotifyEvent);
procedure SetBulletType(const Value: TDHBulletType);
procedure SetCustomString(const Value: string);
procedure SetSpacing(const Value: Byte); public
constructor Create;
published
property Margin: integer read FMargin write SetMargin;
property BulletType: TDHBulletType read FBulletType write SetBulletType default btBullet;
property CustomString: string read FCustomString write SetCustomString;
property Spacing: Byte read FSpacing write SetSpacing default 5;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHTagLIParams}
{$region ' --- TDHTagLI1Params --- '}
TDHTagLI1Params = class(TDHTagLIParams)
public
constructor Create;
published
property Margin default 20;
property BulletType default btBullet;
property CustomString;
property Spacing default 5;
property OnChange;
end;
{$endregion TDHTagLI1Params}
{$region ' --- TDHTagLI2Params --- '}
TDHTagLI2Params = class(TDHTagLIParams)
public
constructor Create;
published
property Margin default 35;
property BulletType default btDash;
property CustomString;
property Spacing default 5;
property OnChange;
end;
{$endregion TDHTagLI2Params}
{$region ' --- TDHBorder --- '}
TDHBorder = class(TPersistent)
private
FPen: TPen;
FOnChange: TNotifyEvent;
FVisible: Boolean;
procedure SetPen(const Value: TPen);
procedure SetOnChange(const Value: TNotifyEvent);
procedure SetVisible(const Value: Boolean);
public
constructor Create;
destructor Destroy; override;
published
property Pen: TPen read FPen write SetPen;
property Visible: Boolean read FVisible write SetVisible default False;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHBorder}
{$region ' --- TDHTagSubParams --- '}
TDHTagSubParams = class(TPersistent)
private
FOnChange: TNotifyEvent;
FPosYDelta: ShortInt;
FFontSizeDelta: ShortInt;
procedure SetOnChange(const Value: TNotifyEvent);
procedure SetPosYDelta(const Value: ShortInt);
procedure SetFontSizeDelta(const Value: ShortInt); public
constructor Create;
published
property PosYDelta: ShortInt read FPosYDelta write SetPosYDelta default 0;
property FontSizeDelta: ShortInt read FFontSizeDelta write SetFontSizeDelta default 0;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHTagSubParams}
{$region ' --- TDHTagSupParams --- '}
TDHTagSupParams = class(TPersistent)
private
FOnChange: TNotifyEvent;
FPosYDelta: ShortInt;
FFontSizeDelta: ShortInt;
procedure SetOnChange(const Value: TNotifyEvent);
procedure SetPosYDelta(const Value: ShortInt);
procedure SetFontSizeDelta(const Value: ShortInt); public
constructor Create;
published
property PosYDelta: ShortInt read FPosYDelta write SetPosYDelta default 0;
property FontSizeDelta: ShortInt read FFontSizeDelta write SetFontSizeDelta default 0;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
{$endregion TDHTagSupParams}
{$region ' --- TDzHTMLText2 --- '}
TDzHTMLText2 = class(TGraphicControl)
private
FAbout: String;
LWords: TDHWordList; //word list to paint event
LLinkData: TDHLinkDataList; //list of links info
FBitmapTop: integer;
FBitmapWidth: integer;
FText: String;
FAutoWidth: Boolean;
FAutoHeight: Boolean;
FMaxWidth: Integer; //max width when using AutoWidth
//FTransparent: Boolean; //not used becaus of flickering
FAutoOpenLink: Boolean; //link auto-open with ShellExecute
FLines: Integer; //read-only
FTextWidth: Integer; //read-only
FTextHeight: Integer; //read-only
FStyleLinkNormal, FStyleLinkHover: TDHStyleLinkProp;
FOnLinkEnter, FOnLinkLeave: TDHEvLink;
FOnLinkClick, FOnLinkRightClick: TDHEvLinkClick;
FIsLinkHover: Boolean; //if has a selected link
FSelectedLinkID: Integer; //selected link ID
NoCursorChange: Boolean; //lock CursorChange event
DefaultCursor: TCursor; //default cursor when not over a link
FTagHRParams: TDHTagHRParams;
FTagH1Params: TDHTagH1Params;
FTagH2Params: TDHTagH2Params;
FTagH3Params: TDHTagH3Params;
FVerticalAlignment: TDHVerticalAlignment;
FInternalMargins: TDHInternalMargins;
FExtraLineSpacing: TDHLSSmallInt;
FExtraWordSpacing: TDHWSSmallInt;
FTagLIParams: TDHTagLI1Params;
FTagLI2Params: TDHTagLI2Params;
FBorder: TDHBorder;
FTagSUBParams: TDHTagSubParams;
FTagSUPParams: TDHTagSupParams;
FPngCollection: TDzPngCollection;
FTagStr: string;
procedure SetText(const Value: String);
procedure SetAutoHeight(const Value: Boolean);
procedure SetAutoWidth(const Value: Boolean);
procedure SetMaxWidth(const Value: Integer);
function GetStoredStyleLink(const Index: Integer): Boolean;
procedure SetStyleLink(const Index: Integer; const Value: TDHStyleLinkProp);
procedure DoPaint;
procedure Rebuild; //rebuild words
procedure BuildAndPaint; //rebuild and repaint
procedure CheckMouse(X, Y: Integer);
procedure SetCursorWithoutChange(C: TCursor); //check links by mouse position
procedure SetTagHRParams(const Value: TDHTagHRParams);
procedure SetTagH1Params(const Value: TDHTagH1Params);
procedure SetTagH2Params(const Value: TDHTagH2Params);
procedure SetTagH3Params(const Value: TDHTagH3Params);
procedure SetVerticalAlignment(const Value: TDHVerticalAlignment);
procedure SetInternalMargins(const Value: TDHInternalMargins);
procedure SetExtraLineSpacing(const Value: TDHLSSmallInt);
procedure SetExtraWordSpacing(const Value: TDHWSSmallInt);
procedure SetTagLIParams(const Value: TDHTagLI1Params);
function GetBulletStr(const LIParams: TDHTagLIParams): string;
procedure SetTagLI2Params(const Value: TDHTagLI2Params);
procedure SetBorder(const Value: TDHBorder);
procedure SetTagSUBParams(const Value: TDHTagSubParams);
procedure SetTagSUPParams(const Value: TDHTagSupParams);
procedure SetPngCollection(const Value: TDzPngCollection);
procedure SetTagStr(const Value: string); //procedure SetTransparent(const Value: Boolean);
protected
procedure Loaded; override;
procedure Paint; override;
procedure Click; override;
procedure Resize; override;
procedure CMColorchanged(var Message: TMessage); message CM_COLORCHANGED;
procedure CMFontchanged(var Message: TMessage); message CM_FONTCHANGED;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
procedure CMCursorchanged(var Message: TMessage); message CM_CURSORCHANGED;
procedure PropsChanged(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property IsLinkHover: Boolean read FIsLinkHover;
property SelectedLinkID: Integer read FSelectedLinkID;
function GetLinkData(LinkID: Integer): TDHLinkData; //get data by link id
function GetSelectedLinkData: TDHLinkData; //get data of selected link
//jp
procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);
procedure SaveToBitmap(const Bmp: {$IFDEF HAS_UNIT_SCOPE}Vcl.{$ENDIF}Graphics.TBitmap);
procedure SaveToBitmapFile(const BmpFile: string);
function AddPngImage(const Image: TPngImage): integer; // Returns the index of the added image
function AddPngImageFromFile(const PngFileName: string): integer; // Returns the index of the added image
function GetPngImageFromFile(const PngFileName: string): TPngImage;
function GetPngImage(const Index: integer): TPngImage;
published
property Align;
property Anchors;
property Color;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
{$IFDEF FPC}property BorderSpacing;{$ENDIF}
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
{$IFDEF DELPHI2010_OR_ABOVE}property OnGesture;{$ENDIF}
{$IFDEF DCC}property OnMouseActivate;{$ENDIF}
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property Text: String read FText write SetText;
//property Transparent: Boolean read FTransparent write SetTransparent default False;
property AutoWidth: Boolean read FAutoWidth write SetAutoWidth default False;
property AutoHeight: Boolean read FAutoHeight write SetAutoHeight default False;
property MaxWidth: Integer read FMaxWidth write SetMaxWidth default 0;
property StyleLinkNormal: TDHStyleLinkProp index 1 read FStyleLinkNormal write SetStyleLink stored GetStoredStyleLink;
property StyleLinkHover: TDHStyleLinkProp index 2 read FStyleLinkHover write SetStyleLink stored GetStoredStyleLink;
property Lines: Integer read FLines;
property TextWidth: Integer read FTextWidth;
property TextHeight: Integer read FTextHeight;
property OnLinkEnter: TDHEvLink read FOnLinkEnter write FOnLinkEnter;
property OnLinkLeave: TDHEvLink read FOnLinkLeave write FOnLinkLeave;
property OnLinkClick: TDHEvLinkClick read FOnLinkClick write FOnLinkClick;
property OnLinkRightClick: TDHEvLinkClick read FOnLinkRightClick write FOnLinkRightClick;
property AutoOpenLink: Boolean read FAutoOpenLink write FAutoOpenLink default True;
property About: String read FAbout;
// jp
property TagHRParams: TDHTagHRParams read FTagHRParams write SetTagHRParams;
property TagH1Params: TDHTagH1Params read FTagH1Params write SetTagH1Params;
property TagH2Params: TDHTagH2Params read FTagH2Params write SetTagH2Params;
property TagH3Params: TDHTagH3Params read FTagH3Params write SetTagH3Params;
// Vertical alignment of the internal bitmap in DzHTMLText control. Used only when AutoHeight = False
property VerticalAlignment: TDHVerticalAlignment read FVerticalAlignment write SetVerticalAlignment default vaTop;
property InternalMargins: TDHInternalMargins read FInternalMargins write SetInternalMargins;
property ExtraLineSpacing: TDHLSSmallInt read FExtraLineSpacing write SetExtraLineSpacing default 0;
property ExtraWordSpacing: TDHWSSmallInt read FExtraWordSpacing write SetExtraWordSpacing default 0;
property TagLIParams: TDHTagLI1Params read FTagLIParams write SetTagLIParams;
property TagLI2Params: TDHTagLI2Params read FTagLI2Params write SetTagLI2Params;
property Border: TDHBorder read FBorder write SetBorder;
property TagSUBParams: TDHTagSubParams read FTagSUBParams write SetTagSUBParams;
property TagSUPParams: TDHTagSupParams read FTagSUPParams write SetTagSUPParams;
property PngCollection: TDzPngCollection read FPngCollection write SetPngCollection;
property TagStr: string read FTagStr write SetTagStr;
end;
{$endregion TDzHTMLText2}
//procedure Register;
implementation
uses
{$IFDEF MSWINDOWS}Windows, {$ENDIF} {$IFDEF DCC}ShellAPI,{$ENDIF}
SysUtils
{$IFDEF FPC}, LCLIntf{$ENDIF}
{$IFDEF DCC}{$IFDEF HAS_SYSTEM_UITYPES}, System.UITypes{$ENDIF}{$ENDIF}
;
//procedure Register;
//begin
// RegisterComponents('Digao', [TDzHTMLText2]);
//end;
function IndexOfAny(const s: string; Chars: array of Char): integer; // 0-based!
var
i, x: integer;
begin
Result := -1;
for i := 1 to Length(s) do
for x := 0 to High(Chars) do
if s[i] = Chars[x] then Exit(i - 1);
end;
{$region ' ---------------------- TDHWord ------------------- '}
constructor TDHWord.Create;
begin
inherited;
Font := TFont.Create;
end;
destructor TDHWord.Destroy;
begin
Font.Free;
inherited;
end;
{$endregion TDHWord}
procedure TDHWordList.Add(Rect: TRect; Text: String; Group: Integer; Align: TAlignment;
Font: TFont; BColor: TColor; Link: Boolean; LinkID: Integer; Space: Boolean; ExtraColor: TColor; TagID: integer);
var W: TDHWord;
begin
W := TDHWord.Create;
inherited Add(W);
W.Rect := Rect;
W.Text := Text;
W.Group := Group;
W.Align := Align;
W.Font.Assign(Font);
W.BColor := BColor;
W.Link := Link;
W.LinkID := LinkID;
W.Space := Space;
W.ExtraColor := ExtraColor;
W.TagID := TagID;
end;
{$region ' -------------------------------------------- TDzHTMLText / TBuilder (part 1) ------------------------------------------------ '}
constructor TDzHTMLText2.Create(AOwner: TComponent);
begin
inherited;
ControlStyle := ControlStyle + [csOpaque];
//Warning! The use of transparency in the component causes flickering
FAbout := 'Digăo Dalpiaz / Version 1.0';
FStyleLinkNormal := TDHStyleLinkProp.Create(Self, tslpNormal);
FStyleLinkHover := TDHStyleLinkProp.Create(Self, tslpHover);
LWords := TDHWordList.Create;
LLinkData := TDHLinkDataList.Create;
FAutoOpenLink := True;
FSelectedLinkID := -1;
DefaultCursor := Cursor;
// jp
{$IFDEF FPC}
// default values: Black, Width 5, Height 5
// So we have to set some better values:
Color := clBtnFace;
Width := 200;
Height := 200;
{$ENDIF}
FTagHRParams := TDHTagHRParams.Create;
FTagHRParams.OnChange := PropsChanged;
FTagH1Params := TDHTagH1Params.Create;
FTagH1Params.OnChange := PropsChanged;
FTagH2Params := TDHTagH2Params.Create;
FTagH2Params.OnChange := PropsChanged;
FTagH3Params := TDHTagH3Params.Create;
FTagH3Params.OnChange := PropsChanged;
FVerticalAlignment := vaTop;
FInternalMargins := TDHInternalMargins.Create;
FInternalMargins.OnChange := PropsChanged;
FExtraLineSpacing := 0;
FTagLIParams := TDHTagLI1Params.Create;
FTagLIParams.OnChange := PropsChanged;
FTagLI2Params := TDHTagLI2Params.Create;
FTagLI2Params.OnChange := PropsChanged;
FBorder := TDHBorder.Create;
FBorder.OnChange := PropsChanged;
FBorder.Visible := False;
FTagSUBParams := TDHTagSubParams.Create;
FTagSUBParams.OnChange := PropsChanged;
FTagSUPParams := TDHTagSupParams.Create;
FTagSUPParams.OnChange := PropsChanged;
FPngCollection := nil;
end;
destructor TDzHTMLText2.Destroy;
begin
FStyleLinkNormal.Free;
FStyleLinkHover.Free;
LWords.Free;
LLinkData.Free;
FTagHRParams.Free;
FTagH1Params.Free;
FTagH2Params.Free;
FTagH3Params.Free;
FInternalMargins.Free;
FTagLIParams.Free;
FTagLI2Params.Free;
FBorder.Free;
FTagSUBParams.Free;
FTagSUPParams.Free;
inherited;
end;
procedure TDzHTMLText2.Loaded;
begin
{Warning! When a component is inserted at design-time, the Loaded
is not fired, because there is nothing to load. The Loaded is only fired
when loading component that already has saved properties on DFM file.}
inherited;
Rebuild;
end;
function TDzHTMLText2.AddPngImage(const Image: TPngImage): integer;
var
Item: TDzPngCollectionItem;
begin
Result := -1;
if not Assigned(FPngCollection) then Exit;
Item := FPngCollection.Items.Add;
Item.PngImage.Assign(Image);
Result := Item.Index;
end;
function TDzHTMLText2.AddPngImageFromFile(const PngFileName: string): integer;
var
Png: TPngImage;
begin
Result := -1;
if not FileExists(PngFileName) then Exit;
Png := TPngImage.Create;
try
Png.LoadFromFile(PngFileName);
Result := AddPngImage(Png);
except
// Eating an exception!
end;
end;
procedure TDzHTMLText2.BuildAndPaint;
begin
//Rebuild words and repaint
Rebuild;
Invalidate;
end;
procedure TDzHTMLText2.SaveToBitmap(const Bmp: {$IFDEF HAS_UNIT_SCOPE}Vcl.{$ENDIF}Graphics.TBitmap);
begin
Bmp.SetSize(Canvas.ClipRect.Right, Canvas.ClipRect.Bottom);
BitBlt(Bmp.Canvas.Handle, 0, 0, Width, Height, Canvas.Handle, 0, 0, SRCCOPY);
end;
procedure TDzHTMLText2.SaveToBitmapFile(const BmpFile: string);
var
Bmp: {$IFDEF HAS_UNIT_SCOPE}Vcl.{$ENDIF}Graphics.TBitmap;
begin
Bmp := {$IFDEF HAS_UNIT_SCOPE}Vcl.{$ENDIF}Graphics.TBitmap.Create;
try
SaveToBitmap(Bmp);
Bmp.SaveToFile(BmpFile);
finally
Bmp.Free;
end;
end;
procedure TDzHTMLText2.SaveToFile(const FileName: string);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Text := FText;
{$IFDEF FPC}
{$IFDEF HAS_SAVE_WITH_ENCODING}sl.SaveToFile(FileName, TEncoding.UTF8);{$ELSE}sl.SaveToFile(FileName);{$ENDIF}
{$ELSE}
sl.SaveToFile(FileName, TEncoding.UTF8);
{$ENDIF}
finally
sl.Free;
end;
end;
procedure TDzHTMLText2.LoadFromFile(const FileName: string);
var
sl: TStringList;
begin
FText := '';
sl := TStringList.Create;
try
sl.LoadFromFile(FileName);
FText := sl.Text;
BuildAndPaint;
finally
sl.Free;
end;
end;
procedure TDzHTMLText2.SetAutoHeight(const Value: Boolean);
begin
if Value<>FAutoHeight then
begin
FAutoHeight := Value;
if Value then Rebuild;
end;
end;
procedure TDzHTMLText2.SetAutoWidth(const Value: Boolean);
begin
if Value<>FAutoWidth then
begin
FAutoWidth := Value;
if Value then Rebuild;
end;
end;
procedure TDzHTMLText2.SetBorder(const Value: TDHBorder);
begin
FBorder := Value;
PropsChanged(Self);
end;
procedure TDzHTMLText2.SetMaxWidth(const Value: Integer);
begin
if Value<>FMaxWidth then
begin
FMaxWidth := Value;
Rebuild;
end;
end;
procedure TDzHTMLText2.SetPngCollection(const Value: TDzPngCollection);
begin
FPngCollection := Value;
end;
procedure TDzHTMLText2.SetText(const Value: String);
begin
if Value<>FText then
begin
FText := Value;
BuildAndPaint;
end;
end;
procedure TDzHTMLText2.SetVerticalAlignment(const Value: TDHVerticalAlignment);
begin
if FVerticalAlignment = Value then Exit;
FVerticalAlignment := Value;
BuildAndPaint;
end;
{procedure TDzHTMLText.SetTransparent(const Value: Boolean);
begin
if Value<>FTransparent then
begin
FTransparent := Value;
Invalidate;
end;
end;}
procedure TDzHTMLText2.CMColorchanged(var Message: TMessage);
begin
Invalidate;
end;
procedure TDzHTMLText2.CMFontchanged(var Message: TMessage);
begin
BuildAndPaint;
end;
procedure TDzHTMLText2.Resize;
begin
//on component creating, there is no parent and the resize is fired,
//so, the canvas is not present at this moment.
if HasParent then
Rebuild;
inherited;
end;
procedure TDzHTMLText2.PropsChanged(Sender: TObject);
begin
//Invalidate;
BuildAndPaint;
end;
// jp
procedure DrawLineEx(Canvas: TCanvas; Rect: TRect; LineHeight: integer; bVerticalCenter: Boolean = True);
var
OldWidth, LHeight, xhs: integer;
begin
OldWidth := Canvas.Pen.Width;
Canvas.Pen.Width := 1;
LHeight := LineHeight;
if bVerticalCenter then xhs := (Rect.Height div 2) - (LineHeight div 2)
else xhs := 0;
while LHeight > 0 do
begin
Dec(LHeight);
Canvas.MoveTo(Rect.Left, Rect.Top + xhs);
Canvas.LineTo(Rect.Right, Rect.Top + xhs);
Inc(Rect.Top);
end;
Canvas.Pen.Width := OldWidth;
end;
procedure JppFrame3D(Canvas: TCanvas; Rect: TRect; LeftColor, RightColor, TopColor, BottomColor: TColor; Width: Integer); overload;
procedure DoRect;
begin
with Canvas, Rect do
begin
Pen.Color := TopColor;
MoveTo(Left, Top);
LineTo(Right, Top);
Pen.Color := RightColor;
LineTo(Right, Bottom);
Pen.Color := BottomColor;
LineTo(Left, Bottom);
Pen.Color := LeftColor;
LineTo(Left, Top);
end;
end;
begin
Canvas.Pen.Width := 1;
Dec(Rect.Bottom);
Dec(Rect.Right);
while Width > 0 do
begin
Dec(Width);
DoRect;
InflateRect(Rect, -1, -1);