-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathVirtualTrees.pas
2013 lines (1707 loc) · 79.1 KB
/
VirtualTrees.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
unit VirtualTrees;
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
// Alternatively, you may redistribute this library, use and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
// You may obtain a copy of the LGPL at http://www.gnu.org/copyleft/.
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
// specific language governing rights and limitations under the License.
//
// The original code is VirtualTrees.pas, released September 30, 2000.
//
// The initial developer of the original code is digital publishing AG (Munich, Germany, www.digitalpublishing.de),
// most code was written by Mike Lischke 2000-2009 (public@soft-gems.net, www.soft-gems.net)
//
// Portions created by digital publishing AG are Copyright
// (C) 1999-2001 digital publishing AG. All Rights Reserved.
//----------------------------------------------------------------------------------------------------------------------
//
// For a list of recent changes please see file CHANGES.TXT
//
// Credits for their valuable assistance and code donations go to:
// Freddy Ertl, Marian Aldenhoevel, Thomas Bogenrieder, Jim Kuenemann, Werner Lehmann, Jens Treichler,
// Paul Gallagher (IBO tree), Ondrej Kelle, Ronaldo Melo Ferraz, Heri Bender, Roland Beduerftig (BCB)
// Anthony Mills, Alexander Egorushkin (BCB), Mathias Torell (BCB), Frank van den Bergh, Vadim Sedulin, Peter Evans,
// Milan Vandrovec (BCB), Steve Moss, Joe White, David Clark, Anders Thomsen, Igor Afanasyev, Eugene Programmer,
// Corbin Dunn, Richard Pringle, Uli Gerhardt, Azza, Igor Savkic, Daniel Bauten, Timo Tegtmeier, Dmitry Zegebart,
// Andreas Hausladen, Joachim Marder, Roman Kassebaum, Vincent Parrett, Dietmar Roesler, Sanjay Kanade,
// and everyone that sent pull requests: https://github.com/Virtual-TreeView/Virtual-TreeView/pulls?q=
// Beta testers:
// Freddy Ertl, Hans-Juergen Schnorrenberg, Werner Lehmann, Jim Kueneman, Vadim Sedulin, Moritz Franckenstein,
// Wim van der Vegt, Franc v/d Westelaken
// Indirect contribution (via publicly accessible work of those persons):
// Alex Denissov, Hiroyuki Hori (MMXAsm expert)
// Documentation:
// Markus Spoettl and toolsfactory GbR (http://www.doc-o-matic.com/, sponsoring Virtual TreeView development
// with a free copy of the Doc-O-Matic help authoring system), Sven H. (Step by step tutorial)
// Source repository:
// https://github.com/Virtual-TreeView/Virtual-TreeView
// Accessability implementation:
// Marco Zehe (with help from Sebastian Modersohn)
// Port to Firemonkey:
// Karol Bieniaszewski (github user livius2)
//----------------------------------------------------------------------------------------------------------------------
interface
{$if CompilerVersion < 24}{$MESSAGE FATAL 'This version supports only RAD Studio XE3 and higher. Please use V5 from http://www.jam-software.com/virtual-treeview/VirtualTreeViewV5.5.3.zip or https://github.com/Virtual-TreeView/Virtual-TreeView/archive/V5_stable.zip'}{$ifend}
{$booleval off} // Use fastest possible boolean evaluation
// For some things to work we need code, which is classified as being unsafe for .NET.
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$WARN UNSAFE_CODE OFF}
{$LEGACYIFEND ON}
{$WARN UNSUPPORTED_CONSTRUCT OFF}
{$HPPEMIT '#include <objidl.h>'}
{$HPPEMIT '#include <oleidl.h>'}
{$HPPEMIT '#include <oleacc.h>'}
{$ifdef BCB}
{$HPPEMIT '#pragma comment(lib, "VirtualTreesCR")'}
{$else}
{$HPPEMIT '#pragma comment(lib, "VirtualTreesR")'}
{$endif}
{$HPPEMIT '#pragma comment(lib, "Shell32")'}
{$HPPEMIT '#pragma comment(lib, "uxtheme")'}
{$HPPEMIT '#pragma link "VirtualTrees.Accessibility"'}
uses
Winapi.Windows, Winapi.Messages, Winapi.ActiveX,
System.Classes, System.SysUtils,
Vcl.Graphics, Vcl.Controls, Vcl.ImgList, Vcl.Menus, Vcl.Themes,
VirtualTrees.Types,
VirtualTrees.Header,
VirtualTrees.BaseTree,
{$IFDEF VT_FMX}
VirtualTrees.AncestorFMX,
{$ELSE}
VirtualTrees.AncestorVCL
{$ENDIF}
;
{$MinEnumSize 1, make enumerations as small as possible}
type
// Some aliases for backward compatiblity
PVirtualNode = VirtualTrees.Types.PVirtualNode;
TVirtualNode = VirtualTrees.Types.TVirtualNode;
TVTHeaderColumnLayout = VirtualTrees.Types.TVTHeaderColumnLayout;
TSmartAutoFitType = VirtualTrees.Types.TSmartAutoFitType;
TVirtualTreeStates = VirtualTrees.Types.TVirtualTreeStates;
TCheckState = VirtualTrees.Types.TCheckState;
TCheckType = VirtualTrees.Types.TCheckType;
TSortDirection = VirtualTrees.Types.TSortDirection;
TColumnIndex = VirtualTrees.Types.TColumnIndex;
TVTColumnOption = VirtualTrees.Types.TVTColumnOption;
TVTHeaderHitInfo = VirtualTrees.Types.TVTHeaderHitInfo;
TVTHeaderHitPosition = VirtualTrees.Types.TVTHeaderHitPosition;
TVTHeaderHitPositions = VirtualTrees.Types.TVTHeaderHitPositions;
THeaderState = VirtualTrees.Types.THeaderState;
THeaderStates = VirtualTrees.Types.THeaderStates;
TDropMode = VirtualTrees.Types.TDropMode;
TFormatArray = VirtualTrees.Types.TFormatArray;
TVTHeaderOption = VirtualTrees.Types.TVTHeaderOption;
TVTHeaderOptions = VirtualTrees.Types.TVTHeaderOptions;
TVTHeaderStyle = VirtualTrees.Types.TVTHeaderStyle;
TVTExportType = VirtualTrees.Types.TVTExportType;
TVTImageKind = VirtualTrees.Types.TVTImageKind;
TVTExportMode = VirtualTrees.Types.TVTExportMode;
TVTOperationKind = VirtualTrees.Types.TVTOperationKind;
TVTUpdateState = VirtualTrees.Types.TVTUpdateState;
TVTCellPaintMode = VirtualTrees.Types.TVTCellPaintMode;
TVirtualNodeState = VirtualTrees.Types.TVirtualNodeState;
TVirtualNodeInitState = VirtualTrees.Types.TVirtualNodeInitState;
TVirtualNodeInitStates = VirtualTrees.Types.TVirtualNodeInitStates;
TVTTooltipLineBreakStyle = VirtualTrees.Types.TVTTooltipLineBreakStyle;
TVTNodeAttachMode = VirtualTrees.Types.TVTNodeAttachMode;
TNodeArray = VirtualTrees.Types.TNodeArray;
THitInfo = VirtualTrees.Types.THitInfo;
THitPosition = VirtualTrees.Types.THitPosition;
TVTPaintOption = VirtualTrees.Types.TVTPaintOption;
TVTAutoOption = VirtualTrees.Types.TVTAutoOption;
TVTAutoOptions = VirtualTrees.Types.TVTAutoOptions;
TVTSelectionOption = VirtualTrees.Types.TVTSelectionOption;
TVSTTextType = VirtualTrees.Types.TVSTTextType;
TVTHintMode = VirtualTrees.Types.TVTHintMode;
TBaseVirtualTree = VirtualTrees.BaseTree.TBaseVirtualTree;
IVTEditLink = VirtualTrees.BaseTree.IVTEditLink;
TVTHeaderNotifyEvent = VirtualTrees.BaseTree.TVTHeaderNotifyEvent;
TVTCompareEvent = VirtualTrees.BaseTree.TVTCompareEvent;
TVirtualTreeColumn = VirtualTrees.Header.TVirtualTreeColumn;
TVirtualTreeColumns = VirtualTrees.Header.TVirtualTreeColumns;
TVTHeader = VirtualTrees.Header.TVTHeader;
TVTHeaderClass = VirtualTrees.Header.TVTHeaderClass;
THeaderPaintInfo = VirtualTrees.Header.THeaderPaintInfo;
TVTConstraintPercent = VirtualTrees.Header.TVTConstraintPercent;
TVTFixedAreaConstraints = VirtualTrees.Header.TVTFixedAreaConstraints;
TColumnsArray = VirtualTrees.Header.TColumnsArray;
TCanvas = Vcl.Graphics.TCanvas;
const
// Aliases for increased compatibility with V7, feel free to extend by pull requests
NoColumn = VirtualTrees.Types.NoColumn;
InvalidColumn = VirtualTrees.Types.InvalidColumn;
sdAscending = VirtualTrees.Types.TSortDirection.sdAscending;
sdDescending = VirtualTrees.Types.TSortDirection.sdDescending;
toAutoSort = VirtualTrees.Types.TVTAutoOption.toAutoSort;
toCheckSupport = VirtualTrees.Types.TVTMiscOption.toCheckSupport;
toEditable = VirtualTrees.Types.TVTMiscOption.toEditable;
toShowRoot = VirtualTrees.Types.TVTPaintOption.toShowRoot;
ctNone = VirtualTrees.Types.TCheckType.ctNone;
ctTriStateCheckBox = VirtualTrees.Types.TCheckType.ctTriStateCheckBox;
ctCheckBox = VirtualTrees.Types.TCheckType.ctCheckBox;
ctRadioButton = VirtualTrees.Types.TCheckType.ctRadioButton;
ctButton = VirtualTrees.Types.TCheckType.ctButton;
csUncheckedNormal = VirtualTrees.Types.TCheckState.csUncheckedNormal;
csUncheckedPressed = VirtualTrees.Types.TCheckState.csUncheckedPressed;
csCheckedNormal = VirtualTrees.Types.TCheckState.csCheckedNormal;
csCheckedPressed = VirtualTrees.Types.TCheckState.csCheckedPressed;
csMixedNormal = VirtualTrees.Types.TCheckState.csMixedNormal;
csMixedPressed = VirtualTrees.Types.TCheckState.csMixedPressed;
csUncheckedDisabled = VirtualTrees.Types.TCheckState.csUncheckedDisabled;
csCheckedDisabled = VirtualTrees.Types.TCheckState.csCheckedDisabled;
csMixedDisable = VirtualTrees.Types.TCheckState.csMixedDisabled;
coVisible = VirtualTrees.Types.TVTColumnOption.coVisible;
vsDisabled = VirtualTrees.Types.TVirtualNodeState.vsDisabled;
etHTML = VirtualTrees.Types.TVTExportType.etHTML;
hiOnItemButton = VirtualTrees.Types.THitPosition.hiOnItemButton;
dmOnNode = VirtualTrees.Types.TDropMode.dmOnNode;
hlbForceMultiLine = VirtualTrees.Types.TVTTooltipLineBreakStyle.hlbForceMultiLine;
hmHintAndDefault = VirtualTrees.Types.TVTHintMode.hmHintAndDefault;
hmTooltip = VirtualTrees.Types.TVTHintMode.hmTooltip;
type
TCustomVirtualStringTree = class;
{$IFDEF VT_FMX}
TVTAncestor = TVTAncestorFMX;
{$ELSE}
TVTAncestor = TVTAncestorVcl;
{$ENDIF}
// Describes the source to use when converting a string tree into a string for clipboard etc.
TVSTTextSourceType = (
tstAll, // All nodes are rendered. Initialization is done on the fly.
tstInitialized, // Only initialized nodes are rendered.
tstSelected, // Only selected nodes are rendered.
tstCutCopySet, // Only nodes currently marked as being in the cut/copy clipboard set are rendered.
tstVisible, // Only visible nodes are rendered.
tstChecked // Only checked nodes are rendered
);
TVSTGetTextEvent = procedure(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType; var CellText: string) of object;
TVSTGetHintEvent = procedure(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
var LineBreakStyle: TVTTooltipLineBreakStyle; var HintText: string) of object;
// New text can only be set for variable caption.
TVSTNewTextEvent = procedure(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
NewText: string) of object;
/// <summary>String tree event for custom handling of string abbreviations.</summary>
/// <param name="Sender">The instance that fired the event.</param>
/// <param name="TargetCanvas">Teh canvas on that the sending control will paint.</param>
/// <param name="Node">The Node that is going to be painted.</param>
/// <param name="Column">The column index that is going to be painted.</param>
/// <param name="Result">Var parameter that contains the caption or string that should be used.</param>
/// <param name="Done">Boolean var paramter: Assign True if a string is passed in the Result parameter. Leave the default value False if no shorting is need or the control shuld do it. </param>
/// <remarks>
/// If the text of a node does not fit into its cell (in grid mode) or is too wide for the width of the tree view it is being abbreviated with an ellipsis (...). By default the ellipsis is added to the end of the node text.
/// Occasionally you may want to shorten the node text at a different position, for example if the node text is a path string and not the last folder or filename should be cut off but rather some mid level folders if possible.
/// </remarks>
TVSTShortenStringEvent = procedure(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
Column: TColumnIndex; const S: string; TextSpace: TDimension; var Result: string;
var Done: Boolean) of object;
TVTMeasureTextEvent = procedure(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
Column: TColumnIndex; const Text: string; var Extent: TDimension) of object;
TVTDrawTextEvent = procedure(Sender: TBaseVirtualTree; TargetCanvas: TCanvas; Node: PVirtualNode;
Column: TColumnIndex; const Text: string; const CellRect: TRect; var DefaultDraw: Boolean) of object;
/// Event arguments of the OnGetCellText event
TVSTGetCellTextEventArgs = record
Node: PVirtualNode;
Column: TColumnIndex;
CellText: string;
StaticText: string;
StaticTextAlignment: TAlignment;
ExportType: TVTExportType;
constructor Create(pNode: PVirtualNode; pColumn: TColumnIndex; pExportType: TVTExportType = TVTExportType.etNone);
end;
/// Event signature which is called when text is painted on the canvas or needed for the export.
TVSTGetCellTextEvent = procedure(Sender: TCustomVirtualStringTree; var E: TVSTGetCellTextEventArgs) of object;
TCustomVirtualStringTree = class(TVTAncestor)
private
FInternalDataOffset: Cardinal; // offset to the internal data of the string tree
FDefaultText: string; // text to show if there's no OnGetText event handler (e.g. at design time)
FTextHeight: Integer; // true size of the font
FEllipsisWidth: Integer; // width of '...' for the current font
FOnGetText: TVSTGetTextEvent; // used to retrieve the string to be displayed for a specific node
fOnGetCellText: TVSTGetCellTextEvent; // used to retrieve the normal and static text of a tree node
FOnGetHint: TVSTGetHintEvent; // used to retrieve the hint to be displayed for a specific node
FOnNewText: TVSTNewTextEvent; // used to notify the application about an edited node caption
FOnShortenString: TVSTShortenStringEvent; // used to allow the application a customized string shortage
FOnMeasureTextWidth: TVTMeasureTextEvent; // used to adjust the width of the cells
FOnMeasureTextHeight: TVTMeasureTextEvent;
FOnDrawText: TVTDrawTextEvent; // used to custom draw the node text
/// Returns True if the property DefaultText has a value that differs from the default value, False otherwise.
function IsDefaultTextStored(): Boolean;
function GetImageText(Node: PVirtualNode; Kind: TVTImageKind;
Column: TColumnIndex): string;
function GetOptions: TCustomStringTreeOptions;
function GetStaticText(Node: PVirtualNode; Column: TColumnIndex): string;
function GetText(Node: PVirtualNode; Column: TColumnIndex): string;
procedure ReadText(Reader: TReader);
procedure WriteText(Writer: TWriter);
procedure ResetInternalData(Node: PVirtualNode; Recursive: Boolean);
procedure SetDefaultText(const Value: string);
procedure SetOptions(const Value: TCustomStringTreeOptions);
procedure SetText(Node: PVirtualNode; Column: TColumnIndex; const Value: string);
procedure WMSetFont(var Msg: TWMSetFont); message WM_SETFONT;
procedure GetDataFromGrid(const AStrings : TStringList; const IncludeHeading : Boolean = True);
protected
/// <summary>Contains the name of the string that should be restored as selection</summary>
/// <seealso cref="TVTSelectionOption.toRestoreSelection">
FPreviouslySelected: TStringList;
procedure InitializeTextProperties(var PaintInfo: TVTPaintInfo);
procedure PaintNormalText(var PaintInfo: TVTPaintInfo; TextOutFlags: Integer; Text: string); virtual;
procedure PaintStaticText(const PaintInfo: TVTPaintInfo; pStaticTextAlignment: TAlignment; const Text: string); virtual; // [IPK] - private to protected
procedure AdjustPaintCellRect(var PaintInfo: TVTPaintInfo; var NextNonEmpty: TColumnIndex); override;
function CanExportNode(Node: PVirtualNode): Boolean;
function CalculateStaticTextWidth(Canvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; const Text: string): TDimension; virtual;
function CalculateTextWidth(Canvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; const Text: string): TDimension; virtual;
function ColumnIsEmpty(Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure DefineProperties(Filer: TFiler); override;
function DoCreateEditor(Node: PVirtualNode; Column: TColumnIndex): IVTEditLink; override;
procedure DoAddToSelection(Node: PVirtualNode); override;
function DoGetNodeHint(Node: PVirtualNode; Column: TColumnIndex; var LineBreakStyle: TVTTooltipLineBreakStyle): string; override;
function DoGetNodeTooltip(Node: PVirtualNode; Column: TColumnIndex; var LineBreakStyle: TVTTooltipLineBreakStyle): string; override;
function DoGetNodeExtraWidth(Node: PVirtualNode; Column: TColumnIndex; Canvas: TCanvas = nil): TDimension; override;
function DoGetNodeWidth(Node: PVirtualNode; Column: TColumnIndex; Canvas: TCanvas = nil): TDimension; override;
procedure DoGetText(var pEventArgs: TVSTGetCellTextEventArgs); virtual;
function DoIncrementalSearch(Node: PVirtualNode; const Text: string): Integer; override;
procedure DoNewText(Node: PVirtualNode; Column: TColumnIndex; const Text: string); virtual;
procedure DoPaintNode(var PaintInfo: TVTPaintInfo); override;
function DoShortenString(Canvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; const S: string; Width: TDimension;
EllipsisWidth: TDimension = 0): string; virtual;
procedure DoTextDrawing(var PaintInfo: TVTPaintInfo; const Text: string; CellRect: TRect; DrawFormat: Cardinal); virtual;
function DoTextMeasuring(Canvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; const Text: string): TSize; virtual;
function GetOptionsClass: TTreeOptionsClass; override;
procedure GetRenderStartValues(Source: TVSTTextSourceType; var Node: PVirtualNode;
var NextNodeProc: TGetNextNodeProc);
function InternalData(Node: PVirtualNode): Pointer;
procedure MainColumnChanged; override;
function ReadChunk(Stream: TStream; Version: Integer; Node: PVirtualNode; ChunkType,
ChunkSize: Integer): Boolean; override;
procedure ReadOldStringOptions(Reader: TReader);
function RenderOLEData(const FormatEtcIn: TFormatEtc; out Medium: TStgMedium; ForClipboard: Boolean): HResult; override;
procedure SetChildCount(Node: PVirtualNode; NewChildCount: Cardinal); override;
procedure WriteChunks(Stream: TStream; Node: PVirtualNode); override;
property DefaultText: string read FDefaultText write SetDefaultText stored False;// Stored via own writer
property EllipsisWidth: Integer read FEllipsisWidth;
property TreeOptions: TCustomStringTreeOptions read GetOptions write SetOptions;
property OnGetHint: TVSTGetHintEvent read FOnGetHint write FOnGetHint;
property OnGetText: TVSTGetTextEvent read FOnGetText write FOnGetText;
property OnGetCellText: TVSTGetCellTextEvent read fOnGetCellText write fOnGetCellText;
property OnNewText: TVSTNewTextEvent read FOnNewText write FOnNewText;
property OnShortenString: TVSTShortenStringEvent read FOnShortenString write FOnShortenString;
property OnMeasureTextWidth: TVTMeasureTextEvent read FOnMeasureTextWidth write FOnMeasureTextWidth;
property OnMeasureTextHeight: TVTMeasureTextEvent read FOnMeasureTextHeight write FOnMeasureTextHeight;
property OnDrawText: TVTDrawTextEvent read FOnDrawText write FOnDrawText;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
function AddChild(Parent: PVirtualNode; UserData: Pointer = nil): PVirtualNode; override;
function ComputeNodeHeight(Canvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; S: string = ''): TDimension; virtual;
function ContentToClipboard(Format: Word; Source: TVSTTextSourceType): HGLOBAL;
procedure ContentToCustom(Source: TVSTTextSourceType);
function ContentToHTML(Source: TVSTTextSourceType; const Caption: string = ''): String;
function ContentToRTF(Source: TVSTTextSourceType): RawByteString;
function ContentToText(Source: TVSTTextSourceType; Separator: Char): String; overload;
function ContentToUnicode(Source: TVSTTextSourceType; Separator: WideChar): string; overload; deprecated 'Use ContentToText instead';
function ContentToText(Source: TVSTTextSourceType; const Separator: string): string; overload;
procedure GetTextInfo(Node: PVirtualNode; Column: TColumnIndex; const AFont: TFont; var R: TRect;
var Text: string); override;
function InvalidateNode(Node: PVirtualNode): TRect; override;
function Path(Node: PVirtualNode; Column: TColumnIndex; Delimiter: Char): string;
procedure ReinitNode(Node: PVirtualNode; Recursive: Boolean; ForceReinit:
Boolean = False); override;
procedure RemoveFromSelection(Node: PVirtualNode); override;
function SaveToCSVFile(const FileNameWithPath : TFileName; const IncludeHeading : Boolean) : Boolean;
/// Alternate text for images used in Accessibility.
property ImageText[Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex]: string read GetImageText;
property StaticText[Node: PVirtualNode; Column: TColumnIndex]: string read GetStaticText;
property Text[Node: PVirtualNode; Column: TColumnIndex]: string read GetText write SetText;
end;
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
TVirtualStringTree = class(TCustomVirtualStringTree)
private
function GetOptions: TStringTreeOptions;
procedure SetOptions(const Value: TStringTreeOptions);
protected
function GetOptionsClass: TTreeOptionsClass; override;
public
property Canvas;
property RangeX;
property LastDragEffect;
property CheckImageKind; // should no more be published to make #622 fix working
published
property AccessibleName;
property Action;
property Align;
property Alignment;
property Anchors;
property AnimationDuration;
property AutoExpandDelay;
property AutoScrollDelay;
property AutoScrollInterval;
property Background;
property BackGroundImageTransparent;
property BackgroundOffsetX;
property BackgroundOffsetY;
property BiDiMode;
property BevelEdges;
property BevelInner;
property BevelOuter;
property BevelKind;
property BevelWidth;
property BorderStyle;
property BottomSpace;
property ButtonFillMode;
property ButtonStyle;
property BorderWidth;
property ChangeDelay;
property ClipboardFormats;
property Color;
property Colors;
property Constraints;
property Ctl3D;
property CustomCheckImages;
property DefaultNodeHeight;
property DefaultPasteMode;
property DefaultText;
property DragCursor;
property DragHeight;
property DragKind;
property DragImageKind;
property DragMode;
property DragOperations;
property DragType;
property DragWidth;
property DrawSelectionMode;
property EditDelay;
property EmptyListMessage;
property Enabled;
property Font;
property Header;
property HintMode;
property HotCursor;
property Images;
property IncrementalSearch;
property IncrementalSearchDirection;
property IncrementalSearchStart;
property IncrementalSearchTimeout;
property Indent;
property LineMode;
property LineStyle;
property Margin;
property NodeAlignment;
property NodeDataSize;
property OperationCanceled;
property ParentBiDiMode;
property ParentColor default False;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property RootNodeCount;
property ScrollBarOptions;
property SelectionBlendFactor;
property SelectionCurveRadius;
property ShowHint;
property StateImages;
property StyleElements;
{$if CompilerVersion >= 34}property StyleName;{$ifend}
property TabOrder;
property TabStop default True;
property TextMargin;
property TreeOptions: TStringTreeOptions read GetOptions write SetOptions;
property Visible;
property WantTabs;
property OnAddToSelection;
property OnAdvancedHeaderDraw;
property OnAfterAutoFitColumn;
property OnAfterAutoFitColumns;
property OnAfterCellPaint;
property OnAfterColumnExport;
property OnAfterColumnWidthTracking;
property OnAfterGetMaxColumnWidth;
property OnAfterHeaderExport;
property OnAfterHeaderHeightTracking;
property OnAfterItemErase;
property OnAfterItemPaint;
property OnAfterNodeExport;
property OnAfterPaint;
property OnAfterTreeExport;
property OnBeforeAutoFitColumn;
property OnBeforeAutoFitColumns;
property OnBeforeCellPaint;
property OnBeforeColumnExport;
property OnBeforeColumnWidthTracking;
property OnBeforeDrawTreeLine;
property OnBeforeGetMaxColumnWidth;
property OnBeforeHeaderExport;
property OnBeforeHeaderHeightTracking;
property OnBeforeItemErase;
property OnBeforeItemPaint;
property OnBeforeNodeExport;
property OnBeforePaint;
property OnBeforeTreeExport;
property OnCanSplitterResizeColumn;
property OnCanSplitterResizeHeader;
property OnCanSplitterResizeNode;
property OnChange;
property OnChecked;
property OnChecking;
property OnClick;
property OnCollapsed;
property OnCollapsing;
property OnColumnChecked;
property OnColumnChecking;
property OnColumnClick;
property OnColumnDblClick;
property OnColumnExport;
property OnColumnResize;
property OnColumnVisibilityChanged;
property OnColumnWidthDblClickResize;
property OnColumnWidthTracking;
property OnCompareNodes;
property OnContextPopup;
property OnCreateDataObject;
property OnCreateDragManager;
property OnCreateEditor;
property OnDblClick;
property OnDragAllowed;
property OnDragOver;
property OnDragDrop;
property OnDrawHint;
property OnDrawText;
property OnEditCancelled;
property OnEdited;
property OnEditing;
property OnEndDock;
property OnEndDrag;
property OnEndOperation;
property OnEnter;
property OnExit;
property OnExpanded;
property OnExpanding;
property OnFocusChanged;
property OnFocusChanging;
property OnFreeNode;
property OnGetCellText;
property OnGetCellIsEmpty;
property OnGetCursor;
property OnGetHeaderCursor;
property OnGetText;
property OnPaintText;
property OnGetHelpContext;
property OnGetHintKind;
property OnGetHintSize;
property OnGetImageIndex;
property OnGetImageIndexEx;
property OnGetImageText;
property OnGetHint;
property OnGetLineStyle;
property OnGetNodeDataSize;
property OnGetPopupMenu;
property OnGetUserClipboardFormats;
property OnHeaderAddPopupItem;
property OnHeaderClick;
property OnHeaderDblClick;
property OnHeaderDragged;
property OnHeaderDraggedOut;
property OnHeaderDragging;
property OnHeaderDraw;
property OnHeaderDrawQueryElements;
property OnHeaderHeightDblClickResize;
property OnHeaderHeightTracking;
property OnHeaderMouseDown;
property OnHeaderMouseMove;
property OnHeaderMouseUp;
property OnHotChange;
property OnIncrementalSearch;
property OnInitChildren;
property OnInitNode;
property OnKeyAction;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnLoadNode;
property OnLoadTree;
property OnMeasureItem;
property OnMeasureTextWidth;
property OnMeasureTextHeight;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseEnter;
property OnMouseLeave;
property OnNewText;
property OnNodeClick;
property OnNodeCopied;
property OnNodeCopying;
property OnNodeDblClick;
property OnNodeExport;
property OnNodeHeightDblClickResize;
property OnNodeHeightTracking;
property OnNodeMoved;
property OnNodeMoving;
property OnPaintBackground;
property OnPrepareButtonBitmaps;
property OnRemoveFromSelection;
property OnRenderOLEData;
property OnResetNode;
property OnResize;
property OnSaveNode;
property OnSaveTree;
property OnScroll;
property OnShortenString;
property OnShowScrollBar;
property OnBeforeGetCheckState;
property OnStartDock;
property OnStartDrag;
property OnStartOperation;
property OnStateChange;
property OnStructureChange;
property OnUpdating;
property OnCanResize;
property OnGesture;
property Touch;
property OnColumnHeaderSpanning;
end;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
System.TypInfo, // for migration stuff
System.StrUtils,
System.Types, // prevent inline compiler warning
System.UITypes, // prevent inline compiler warning
VirtualTrees.StyleHooks,
VirtualTrees.ClipBoard,
VirtualTrees.Utils,
VirtualTrees.Export,
VirtualTrees.EditLink,
VirtualTrees.BaseAncestorVcl{to eliminate H2443 about inline expanding}
;
const
cDefaultText = 'Node';
RTLFlag: array[Boolean] of Integer = (0, ETO_RTLREADING);
AlignmentToDrawFlag: array[TAlignment] of Cardinal = (DT_LEFT, DT_RIGHT, DT_CENTER);
gInitialized: Integer = 0; // >0 if global structures have been initialized; otherwise 0
//// initialization of stuff global to the unit
procedure InitializeGlobalStructures();
begin
if (gInitialized > 0) or (AtomicIncrement(gInitialized) <> 1) then // Ensure threadsafe that this code is executed only once
exit;
// Clipboard format registration.
// Specialized string tree formats.
CF_HTML := RegisterVTClipboardFormat(CFSTR_HTML, TCustomVirtualStringTree, 80);
CF_VRTFNOOBJS := RegisterVTClipboardFormat(CFSTR_RTFNOOBJS, TCustomVirtualStringTree, 84);
CF_VRTF := RegisterVTClipboardFormat(CFSTR_RTF, TCustomVirtualStringTree, 85);
CF_CSV := RegisterVTClipboardFormat(CFSTR_CSV, TCustomVirtualStringTree, 90);
// Predefined clipboard formats. Just add them to the internal list.
RegisterVTClipboardFormat(CF_TEXT, TCustomVirtualStringTree, 100);
RegisterVTClipboardFormat(CF_UNICODETEXT, TCustomVirtualStringTree, 95);
end;
//----------------- TCustomVirtualString -------------------------------------------------------------------------------
constructor TCustomVirtualStringTree.Create(AOwner: TComponent);
begin
InitializeGlobalStructures();
inherited;
FPreviouslySelected := nil;
FDefaultText := cDefaultText;
FInternalDataOffset := AllocateInternalDataArea(SizeOf(Cardinal));
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCustomVirtualStringTree.GetRenderStartValues(Source: TVSTTextSourceType; var Node: PVirtualNode;
var NextNodeProc: TGetNextNodeProc);
begin
case Source of
tstInitialized:
begin
Node := GetFirstInitialized;
NextNodeProc := GetNextInitialized;
end;
tstSelected:
begin
Node := GetFirstSelected;
NextNodeProc := GetNextSelected;
end;
tstCutCopySet:
begin
Node := GetFirstCutCopy;
NextNodeProc := GetNextCutCopy;
end;
tstVisible:
begin
Node := GetFirstVisible(nil, True);
NextNodeProc := GetNextVisible;
end;
tstChecked:
begin
Node := GetFirstChecked;
NextNodeProc := GetNextChecked;
end;
else // tstAll
Node := GetFirst;
NextNodeProc := GetNext;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCustomVirtualStringTree.GetDataFromGrid(const AStrings: TStringList;
const IncludeHeading: Boolean);
var
LColIndex : Integer;
LStartIndex : Integer;
LAddString : string;
LCellText : string;
LChildNode : PVirtualNode;
begin
{ Start from the First column. }
LStartIndex := 0;
{ Do it for Header first }
if IncludeHeading then
begin
LAddString := EmptyStr;
for LColIndex := LStartIndex to Pred(Header.Columns.Count) do
begin
if (LColIndex > LStartIndex) then
LAddString := LAddString + ',';
LAddString := LAddString + AnsiQuotedStr(Header.Columns.Items[LColIndex].Text, '"');
end;//for
AStrings.Add(LAddString);
end;//if
{ Loop thru the virtual tree for Data }
LChildNode := GetFirst;
while Assigned(LChildNode) do
begin
LAddString := EmptyStr;
{ Read for each column and then populate the text }
for LColIndex := LStartIndex to Pred(Header.Columns.Count) do
begin
LCellText := Text[LChildNode, LColIndex];
if (LCellText = EmptyStr) then
LCellText := ' ';
if (LColIndex > LStartIndex) then
LAddString := LAddString + ',';
LAddString := LAddString + AnsiQuotedStr(LCellText, '"');
end;//for - Header.Columns.Count
AStrings.Add(LAddString);
LChildNode := LChildNode.NextSibling;
end;//while Assigned(LChildNode);
end;
function TCustomVirtualStringTree.GetImageText(Node: PVirtualNode;
Kind: TVTImageKind; Column: TColumnIndex): string;
begin
Assert(Assigned(Node), 'Node must not be nil.');
if not (vsInitialized in Node.States) then
InitNode(Node);
Result := '';
DoGetImageText(Node, Kind, Column, Result);
end;
//----------------------------------------------------------------------------------------------------------------------
function TCustomVirtualStringTree.GetOptions: TCustomStringTreeOptions;
begin
Result := inherited TreeOptions as TCustomStringTreeOptions;
end;
//----------------------------------------------------------------------------------------------------------------------
function TCustomVirtualStringTree.GetStaticText(Node: PVirtualNode; Column: TColumnIndex): string;
var
lEventArgs: TVSTGetCellTextEventArgs;
begin
Assert(Assigned(Node), 'Node must not be nil.');
lEventArgs := TVSTGetCellTextEventArgs.Create(Node, Column);
DoGetText(lEventArgs);
Exit(lEventArgs.StaticText);
end;
//----------------------------------------------------------------------------------------------------------------------
function TCustomVirtualStringTree.GetText(Node: PVirtualNode; Column: TColumnIndex): string;
var
lEventArgs: TVSTGetCellTextEventArgs;
begin
Assert(Assigned(Node), 'Node must not be nil.');
lEventArgs := TVSTGetCellTextEventArgs.Create(Node, Column);
lEventArgs.CellText := FDefaultText;
DoGetText(lEventArgs);
Exit(lEventArgs.CellText)
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCustomVirtualStringTree.InitializeTextProperties(var PaintInfo: TVTPaintInfo);
// Initializes default values for customization in PaintNormalText.
begin
with PaintInfo do
begin
// Set default font values first.
Canvas.Font.Assign(Font);
if Enabled then // Otherwise only those colors are used, which are passed from Font to Canvas.Font.
Canvas.Font.Color := Colors.NodeFontColor
else
Canvas.Font.Color := Colors.DisabledColor;
if (toHotTrack in TreeOptions.PaintOptions) and (Node = HotNode) then
begin
if not (tsUseExplorerTheme in TreeStates) then
begin
Canvas.Font.Style := Canvas.Font.Style + [TFontStyle.fsUnderline];
Canvas.Font.Color := Colors.HotColor;
end;
end;
// Change the font color only if the node also is drawn in selected style.
if poDrawSelection in PaintOptions then
begin
if (Column = FocusedColumn) or (toFullRowSelect in TreeOptions.SelectionOptions) then
begin
if Node = DropTargetNode then
begin
if ((LastDropMode = dmOnNode) or (vsSelected in Node.States)) then
Canvas.Font.Color := Colors.GetSelectedNodeFontColor(True); // See #1083, since drop highlight color is chosen independent of the focus state, we need to choose Font color also independent of it.
end
else
if vsSelected in Node.States then
begin
Canvas.Font.Color := Colors.GetSelectedNodeFontColor(Focused or (toPopupMode in TreeOptions.PaintOptions));
end;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCustomVirtualStringTree.PaintNormalText(var PaintInfo: TVTPaintInfo; TextOutFlags: Integer;
Text: string);
// This method is responsible for painting the given text to target canvas (under consideration of the given rectangles).
// The text drawn here is considered as the normal text in a node.
// Note: NodeWidth is the actual width of the text to be drawn. This does not necessarily correspond to the width of
// the node rectangle. The clipping rectangle comprises the entire node (including tree lines, buttons etc.).
var
TripleWidth: TDimension;
R: TRect;
DrawFormat: Cardinal;
Height: TDimension;
lNewNodeWidth: TDimension;
begin
InitializeTextProperties(PaintInfo);
with PaintInfo do
begin
R := ContentRect;
Canvas.TextFlags := 0;
InflateRect(R, -TextMargin, 0);
if (vsDisabled in Node.States) or not Enabled then
Canvas.Font.Color := Colors.DisabledColor;
// Multiline nodes don't need special font handling or text manipulation.
// Note: multiline support requires the Unicode version of DrawText, which is able to do word breaking.
// The emulation in this unit does not support this so we have to use the OS version. However
// DrawTextW is only available on NT/2000/XP and up. Hence there is only partial multiline support
// for 9x/Me.
if vsMultiline in Node.States then
begin
DoPaintText(Node, Canvas, Column, ttNormal);
Height := ComputeNodeHeight(Canvas, Node, Column);
// The edit control flag will ensure that no partial line is displayed, that is, only lines
// which are (vertically) fully visible are drawn.
DrawFormat := DT_NOPREFIX or DT_WORDBREAK or DT_END_ELLIPSIS or DT_EDITCONTROL or AlignmentToDrawFlag[Alignment];
if BidiMode <> bdLeftToRight then
DrawFormat := DrawFormat or DT_RTLREADING;
// Center the text vertically if it fits entirely into the content rect.
if R.Bottom - R.Top > Height then
InflateRect(R, 0, Divide(Height - R.Bottom - R.Top, 2));
end
else
begin
FFontChanged := False;
TripleWidth := FEllipsisWidth;
DoPaintText(Node, Canvas, Column, ttNormal);
if FFontChanged then
begin
// If the font has been changed then the ellipsis width must be recalculated.
TripleWidth := 0;
// Recalculate also the width of the normal text.
lNewNodeWidth := DoTextMeasuring(Canvas, Node, Column, Text).cx + 2 * TextMargin;
if lNewNodeWidth <> NodeWidth then
begin
NodeWidth := lNewNodeWidth;
InvalidateNode(Node); // repaint node and selection as the font chnaged, see #1084
end;//if
end;// if FFontChanged
DrawFormat := DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE;
if BidiMode <> bdLeftToRight then
DrawFormat := DrawFormat or DT_RTLREADING;
// Check if the text must be shortend.
if (Column > NoColumn) and ((NodeWidth - 2 * TextMargin) > R.Width) then
begin
Text := DoShortenString(Canvas, Node, Column, Text, R.Right - R.Left, TripleWidth);
if Alignment = taRightJustify then
DrawFormat := DrawFormat or DT_RIGHT
else
DrawFormat := DrawFormat or DT_LEFT;
end
else
DrawFormat := DrawFormat or AlignmentToDrawFlag[Alignment];
end;
if Canvas.TextFlags and ETO_OPAQUE = 0 then
SetBkMode(Canvas.Handle, TRANSPARENT)
else
SetBkMode(Canvas.Handle, OPAQUE);
DoTextDrawing(PaintInfo, Text, R, DrawFormat);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCustomVirtualStringTree.PaintStaticText(const PaintInfo: TVTPaintInfo; pStaticTextAlignment: TAlignment; const Text: string);
// This method retrives and draws the static text bound to a particular node.
var
R: TRect;
DrawFormat: Cardinal;
begin
with PaintInfo do
begin
Canvas.Font.Assign(Font);
if toFullRowSelect in TreeOptions.SelectionOptions then
begin
if Node = DropTargetNode then
begin
if (LastDropMode = dmOnNode) or (vsSelected in Node.States) then
Canvas.Font.Color := Colors.GetSelectedNodeFontColor(Focused or (toPopupMode in TreeOptions.PaintOptions))
else
Canvas.Font.Color := Colors.NodeFontColor;
end
else
if vsSelected in Node.States then
begin
if Focused or (toPopupMode in TreeOptions.PaintOptions) then
Canvas.Font.Color := Colors.GetSelectedNodeFontColor(Focused or (toPopupMode in TreeOptions.PaintOptions))
else
Canvas.Font.Color := Colors.NodeFontColor;
end;
end;
DrawFormat := DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE;
Canvas.TextFlags := 0;
DoPaintText(Node, Canvas, Column, ttStatic);
// Disabled node color overrides all other variants.
if (vsDisabled in Node.States) or not Enabled then
Canvas.Font.Color := Colors.DisabledColor;
R := ContentRect;
if pStaticTextAlignment = taRightJustify then begin
DrawFormat := DrawFormat or DT_RIGHT;
Dec(R.Right, TextMargin);
if PaintInfo.Alignment = taRightJustify then
Dec(R.Right, NodeWidth); // room for node text
end
else begin
Inc(R.Left, TextMargin);
if PaintInfo.Alignment = taLeftJustify then
Inc(R.Left, NodeWidth); // room for node text
end;
if Canvas.TextFlags and ETO_OPAQUE = 0 then
SetBkMode(Canvas.Handle, TRANSPARENT)
else
SetBkMode(Canvas.Handle, OPAQUE);
Winapi.Windows.DrawTextW(Canvas.Handle, PWideChar(Text), Length(Text), R, DrawFormat);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCustomVirtualStringTree.ReadText(Reader: TReader);
begin
SetDefaultText(Reader.ReadString);
end;
//----------------------------------------------------------------------------------------------------------------------
function TCustomVirtualStringTree.SaveToCSVFile(
const FileNameWithPath: TFileName; const IncludeHeading: Boolean): Boolean;
var
LResultList : TStringList;
begin
Result := False;