-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathVirtualTrees.Export.pas
1143 lines (1003 loc) · 37.2 KB
/
VirtualTrees.Export.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.Export;
{$WARN UNSAFE_CODE OFF}
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
interface
uses Winapi.Windows,
VirtualTrees,
VirtualTrees.Classes;
function ContentToHTML(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType; const Caption: string = ''): String;
function ContentToRTF(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType): RawByteString;
function ContentToUnicodeString(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType; const Separator: string): string;
function ContentToClipboard(Tree: TCustomVirtualStringTree; Format: Word; Source: TVSTTextSourceType): HGLOBAL;
procedure ContentToCustom(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType);
implementation
uses
System.Classes,
System.SysUtils,
System.StrUtils,
System.Generics.Collections,
System.UITypes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
VirtualTrees.Types,
VirtualTrees.ClipBoard,
VirtualTrees.Header,
VirtualTrees.BaseTree;
type
TCustomVirtualStringTreeCracker = class(TCustomVirtualStringTree)
end;
const
WideCR = Char(#13);
WideLF = Char(#10);
function ContentToHTML(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType; const Caption: string): String;
// Renders the current tree content (depending on Source) as HTML text encoded in UTF-8.
// If Caption is not empty then it is used to create and fill the header for the table built here.
// Based on ideas and code from Frank van den Bergh and Andreas H�rstemeier.
var
Buffer: TBufferedString;
//--------------- local functions -------------------------------------------
procedure WriteColorAsHex(Color: TColor);
var
WinColor: COLORREF;
I: Integer;
Component,
Value: Byte;
begin
Buffer.Add('#');
WinColor := ColorToRGB(Color);
I := 1;
while I <= 6 do
begin
Component := WinColor and $FF;
Value := 48 + (Component shr 4);
if Value > $39 then
System.Inc(Value, 7);
Buffer.Add(AnsiChar(Value));
System.Inc(I);
Value := 48 + (Component and $F);
if Value > $39 then
System.Inc(Value, 7);
Buffer.Add(AnsiChar(Value));
System.Inc(I);
WinColor := WinColor shr 8;
end;
end;
//---------------------------------------------------------------------------
procedure WriteStyle(const Name: String; Font: TFont);
// Creates a CSS style entry with the given name for the given font.
// If Name is empty then the entry is created as inline style.
begin
if Length(Name) = 0 then
Buffer.Add(' style="')
else
begin
Buffer.Add('.');
Buffer.Add(Name);
Buffer.Add('{');
end;
Buffer.Add(Format('font-family: ''%s''; ', [Font.Name]));
if Font.Size < 0 then
Buffer.Add(Format('font-size: %dpx; ', [Font.Height]))
else
Buffer.Add(Format('font-size: %dpt; ', [Font.Size]));
Buffer.Add(Format('font-style: %s; ', [IfThen(TFontStyle.fsItalic in Font.Style, 'italic', 'normal')]));
Buffer.Add(Format('font-weight: %s; ', [IfThen(TFontStyle.fsBold in Font.Style, 'bold', 'normal')]));
Buffer.Add(Format('text-decoration: %s; ', [IfThen(TFontStyle.fsUnderline in Font.Style, 'underline', 'none')]));
Buffer.Add('color: ');
WriteColorAsHex(Font.Color);
Buffer.Add(';');
if Length(Name) = 0 then
Buffer.Add('"')
else
Buffer.Add('}');
end;
//--------------- end local functions ---------------------------------------
var
I, J : Integer;
Level, MaxLevel: Cardinal;
AddHeader: String;
Save, Run: PVirtualNode;
GetNextNode: TGetNextNodeProc;
RenderColumns: Boolean;
Columns: TColumnsArray;
ColumnColors: array of String;
Index: Integer;
IndentWidth,
LineStyleText: String;
Alignment: TAlignment;
BidiMode: TBidiMode;
CellPadding: String;
CrackTree: TCustomVirtualStringTreeCracker;
lGetCellTextEventArgs: TVSTGetCellTextEventArgs;
begin
CrackTree := TCustomVirtualStringTreeCracker(Tree);
CrackTree.StartOperation(TVTOperationKind.okExport);
Buffer := TBufferedString.Create;
lGetCellTextEventArgs.ExportType := TVTExportType.etHtml;
try
// For customization by the application or descendants we use again the redirected font change event.
CrackTree.RedirectFontChangeEvent(CrackTree.Canvas);
CellPadding := Format('padding-left: %dpx; padding-right: %0:dpx;', [CrackTree.Margin]);
IndentWidth := IntToStr(CrackTree.Indent);
AddHeader := ' ';
// Add title if adviced so by giving a caption.
if Length(Caption) > 0 then
AddHeader := AddHeader + 'caption="' + Caption + '"';
if CrackTree.Borderstyle <> TFormBorderStyle.bsNone then
AddHeader := AddHeader + Format(' border="%d" frame=box', [CrackTree.BorderWidth + 1]);
Buffer.Add('<META http-equiv="Content-Type" content="text/html; charset=utf-8">');
// Create HTML table based on the CrackTree structure. To simplify formatting we use styles defined in a small CSS area.
Buffer.Add('<style type="text/css">');
Buffer.AddnewLine;
WriteStyle('default', CrackTree.Font);
Buffer.AddNewLine;
WriteStyle('header', CrackTree.Header.Font);
Buffer.AddNewLine;
// Determine grid/table lines and create CSS for it.
// Vertical and/or horizontal border to show.
if CrackTree.LineStyle = lsSolid then
LineStyleText := 'solid;'
else
LineStyleText := 'dotted;';
if toShowHorzGridLines in CrackTree.TreeOptions.PaintOptions then
begin
Buffer.Add('.noborder{');
Buffer.Add(' border-bottom:1px; border-left: 0px; border-right: 0px; border-top: 1px;');
Buffer.Add('border-style:');
Buffer.Add(LineStyleText);
Buffer.Add(CellPadding);
Buffer.Add('}');
end
else
begin
Buffer.Add('.noborder{border-style: none;');
Buffer.Add(CellPadding);
Buffer.Add('}');
end;
Buffer.AddNewLine;
Buffer.Add('.normalborder {vertical-align: top; ');
if toShowVertGridLines in CrackTree.TreeOptions.PaintOptions then
Buffer.Add('border-right: 1px; border-left: 1px; ')
else
Buffer.Add('border-right: none; border-left:none; ');
if toShowHorzGridLines in CrackTree.TreeOptions.PaintOptions then
Buffer.Add('border-top: 1px; border-bottom: 1px; ')
else
Buffer.Add('border-top:none; border-bottom: none;');
Buffer.Add('border-width: thin; border-style: ');
Buffer.Add(LineStyleText);
Buffer.Add(CellPadding);
Buffer.Add('}');
Buffer.Add('</style>');
Buffer.AddNewLine;
// General table properties.
Buffer.Add('<table class="default" style="border-collapse: collapse;" bgcolor=');
WriteColorAsHex(CrackTree.Color);
Buffer.Add(AddHeader);
Buffer.Add(' cellspacing="0">');
Buffer.AddNewLine;
Columns := nil;
ColumnColors := nil;
RenderColumns := CrackTree.Header.UseColumns;
if RenderColumns then
begin
Columns := CrackTree.Header.Columns.GetVisibleColumns;
SetLength(ColumnColors, Length(Columns));
end;
CrackTree.GetRenderStartValues(Source, Run, GetNextNode);
Save := Run;
MaxLevel := 0;
// The table consists of visible columns and rows as used in the CrackTree, but the main CrackTree column is splitted
// into several HTML columns to accomodate the indentation.
while Assigned(Run) and not CrackTree.OperationCanceled do
begin
if (CrackTree.CanExportNode(Run)) then
begin
Level := CrackTree.GetNodeLevel(Run);
if Level > MaxLevel then
MaxLevel := Level;
end;
Run := GetNextNode(Run);
end;
if RenderColumns then
begin
if Assigned(CrackTree.OnBeforeHeaderExport) then
CrackTree.OnBeforeHeaderExport(CrackTree, etHTML);
Buffer.Add('<tr class="header" style="');
Buffer.Add(CellPadding);
Buffer.Add('">');
Buffer.AddNewLine;
// Make the first row in the HTML table an image of the CrackTree header.
for I := 0 to High(Columns) do
begin
if Assigned(CrackTree.OnBeforeColumnExport) then
CrackTree.OnBeforeColumnExport(CrackTree, etHTML, Columns[I]);
Buffer.Add('<th height="');
Buffer.Add(IntToStr(CrackTree.Header.Height));
Buffer.Add('px"');
Alignment := Columns[I].CaptionAlignment;
// Consider directionality.
if Columns[I].BiDiMode <> bdLeftToRight then
begin
ChangeBidiModeAlignment(Alignment);
Buffer.Add(' dir="rtl"');
end;
// Consider aligment.
case Alignment of
taRightJustify:
Buffer.Add(' align=right');
taCenter:
Buffer.Add(' align=center');
else
Buffer.Add(' align=left');
end;
Index := Columns[I].Index;
// Merge cells of the header emulation in the main column.
if (MaxLevel > 0) and (Index = CrackTree.Header.MainColumn) then
begin
Buffer.Add(' colspan="');
Buffer.Add(IntToStr(MaxLevel + 1));
Buffer.Add('"');
end;
// The color of the header is usually clBtnFace.
Buffer.Add(' bgcolor=');
WriteColorAsHex(clBtnFace);
// Set column width in pixels.
Buffer.Add(' width="');
Buffer.Add(IntToStr(Columns[I].Width));
Buffer.Add('px">');
if Length(Columns[I].Text) > 0 then
Buffer.Add(Columns[I].Text);
Buffer.Add('</th>');
if Assigned(CrackTree.OnAfterColumnExport) then
CrackTree.OnAfterColumnExport(CrackTree, etHTML, Columns[I]);
end;
Buffer.Add('</tr>');
Buffer.AddNewLine;
if Assigned(CrackTree.OnAfterHeaderExport) then
CrackTree.OnAfterHeaderExport(CrackTree, etHTML);
end;
// Now go through the CrackTree.
Run := Save;
while Assigned(Run) and not CrackTree.OperationCanceled do
begin
if (not CrackTree.CanExportNode(Run)) then
begin
Run := GetNextNode(Run);
Continue;
end;
if Assigned(CrackTree.OnBeforeNodeExport) then
CrackTree.OnBeforeNodeExport(CrackTree, etHTML, Run);
Level := CrackTree.GetNodeLevel(Run);
Buffer.Add(' <tr class="default">');
Buffer.AddNewLine;
I := 0;
while (I < Length(Columns)) or not RenderColumns do
begin
if RenderColumns then
Index := Columns[I].Index
else
Index := NoColumn;
if not RenderColumns or (coVisible in Columns[I].Options) then
begin
// Call back the application to know about font customization.
CrackTree.Canvas.Font := CrackTree.Font;
CrackTree.FFontChanged := False;
CrackTree.DoPaintText(Run, CrackTree.Canvas, Index, ttNormal);
if Index = CrackTree.Header.MainColumn then
begin
// Create a cell for each indentation level.
if RenderColumns and not (coParentColor in Columns[I].Options) then
begin
for J := 1 to Level do
begin
Buffer.Add('<td class="noborder" width="');
Buffer.Add(IndentWidth);
Buffer.Add('" height="');
Buffer.Add(IntToStr(CrackTree.NodeHeight[Run]));
Buffer.Add('px"');
if not (coParentColor in Columns[I].Options) then
begin
Buffer.Add(' bgcolor=');
WriteColorAsHex(Columns[I].Color);
end;
Buffer.Add('> </td>');
end;
end
else
begin
for J := 1 to Level do
if J = 1 then
begin
Buffer.Add(' <td height="');
Buffer.Add(IntToStr(CrackTree.NodeHeight[Run]));
Buffer.Add('px" class="normalborder"> </td>');
end
else
Buffer.Add(' <td> </td>');
end;
end;
if CrackTree.FFontChanged then
begin
Buffer.Add(' <td class="normalborder" ');
WriteStyle('', CrackTree.Canvas.Font);
Buffer.Add(' height="');
Buffer.Add(IntToStr(CrackTree.NodeHeight[Run]));
Buffer.Add('px"');
end
else
begin
Buffer.Add(' <td class="normalborder" height="');
Buffer.Add(IntToStr(CrackTree.NodeHeight[Run]));
Buffer.Add('px"');
end;
if RenderColumns then
begin
Alignment := Columns[I].Alignment;
BidiMode := Columns[I].BidiMode;
end
else
begin
Alignment := CrackTree.Alignment;
BidiMode := CrackTree.BidiMode;
end;
// Consider directionality.
if BiDiMode <> bdLeftToRight then
begin
ChangeBidiModeAlignment(Alignment);
Buffer.Add(' dir="rtl"');
end;
// Consider aligment.
case Alignment of
taRightJustify:
Buffer.Add(' align=right');
taCenter:
Buffer.Add(' align=center');
else
Buffer.Add(' align=left');
end;
// Merge cells in the main column.
if (MaxLevel > 0) and (Index = CrackTree.Header.MainColumn) and (Level < MaxLevel) then
begin
Buffer.Add(' colspan="');
Buffer.Add(IntToStr(MaxLevel - Level + 1));
Buffer.Add('"');
end;
if RenderColumns and not (coParentColor in Columns[I].Options) then
begin
Buffer.Add(' bgcolor=');
WriteColorAsHex(Columns[I].Color);
end;
Buffer.Add('>');
// Get the text
lGetCellTextEventArgs.Node := Run;
lGetCellTextEventArgs.Column := Index;
CrackTree.DoGetText(lGetCellTextEventArgs);
Buffer.Add(lGetCellTextEventArgs.CellText);
if not lGetCellTextEventArgs.StaticText.IsEmpty and (toShowStaticText in TStringTreeOptions(CrackTree.TreeOptions).StringOptions) then
Buffer.Add(' ' + lGetCellTextEventArgs.StaticText);
Buffer.Add('</td>');
end;
if not RenderColumns then
Break;
System.Inc(I);
end;
if Assigned(CrackTree.OnAfterNodeExport) then
CrackTree.OnAfterNodeExport(CrackTree, etHTML, Run);
Run := GetNextNode(Run);
Buffer.Add(' </tr>');
Buffer.AddNewLine;
end;
Buffer.Add('</table>');
CrackTree.RestoreFontChangeEvent(CrackTree.Canvas);
Result := Buffer.AsString;
finally
CrackTree.EndOperation(TVTOperationKind.okExport);
Buffer.Free;
end;
end;
function ContentToRTF(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType): RawByteString;
// Renders the current tree content (depending on Source) as RTF (rich text).
// Based on ideas and code from Frank van den Bergh and Andreas Hoerstemeier.
var
Fonts: TStringList;
Colors: TList<TColor>;
CurrentFontIndex,
CurrentFontColor,
CurrentFontSize: Integer;
Buffer: TBufferedRawByteString;
//--------------- local functions -------------------------------------------
procedure SelectFont(const Font: string);
var
I: Integer;
begin
I := Fonts.IndexOf(Font);
if I > -1 then
begin
// Font has already been used
if I <> CurrentFontIndex then
begin
Buffer.Add('\f');
Buffer.Add(IntToStr(I));
CurrentFontIndex := I;
end;
end
else
begin
I := Fonts.Add(Font);
Buffer.Add('\f');
Buffer.Add(IntToStr(I));
CurrentFontIndex := I;
end;
end;
//---------------------------------------------------------------------------
procedure SelectColor(Color: TColor);
var
I: Integer;
begin
I := Colors.IndexOf(Color);
if I > -1 then
begin
// Color has already been used
if I <> CurrentFontColor then
begin
Buffer.Add('\cf');
Buffer.Add(IntToStr(I + 1));
CurrentFontColor := I;
end;
end
else
begin
I := Colors.Add(Color);
Buffer.Add('\cf');
Buffer.Add(IntToStr(I + 1));
CurrentFontColor := I;
end;
end;
//---------------------------------------------------------------------------
procedure TextPlusFont(const Text: string; Font: TFont);
var
UseUnderline,
UseItalic,
UseBold: Boolean;
I: Integer;
begin
if Length(Text) > 0 then
begin
UseUnderline := TFontStyle.fsUnderline in Font.Style;
if UseUnderline then
Buffer.Add('\ul');
UseItalic := TFontStyle.fsItalic in Font.Style;
if UseItalic then
Buffer.Add('\i');
UseBold := TFontStyle.fsBold in Font.Style;
if UseBold then
Buffer.Add('\b');
SelectFont(Font.Name);
SelectColor(Font.Color);
if Font.Size <> CurrentFontSize then
begin
// Font size must be given in half points.
Buffer.Add('\fs');
Buffer.Add(IntToStr(2 * Font.Size));
CurrentFontSize := Font.Size;
end;
// Use escape sequences to note Unicode text.
Buffer.Add(' ');
// Note: Unicode values > 32767 must be expressed as negative numbers. This is implicitly done
// by interpreting the wide chars (word values) as small integers.
for I := 1 to Length(Text) do
begin
if (Text[I] = WideLF) then
Buffer.Add( '{\par}' )
else
if (Text[I] <> WideCR) then
begin
Buffer.Add(Format('\u%d\''3f', [SmallInt(Text[I])]));
Continue;
end;
end;
if UseUnderline then
Buffer.Add('\ul0');
if UseItalic then
Buffer.Add('\i0');
if UseBold then
Buffer.Add('\b0');
end;
end;
//--------------- end local functions ---------------------------------------
var
Level, LastLevel: Integer;
I, J: Integer;
Save, Run: PVirtualNode;
GetNextNode: TGetNextNodeProc;
S, Tabs : RawByteString;
Twips: Integer;
RenderColumns: Boolean;
Columns: TColumnsArray;
Index: Integer;
Alignment: TAlignment;
BidiMode: TBidiMode;
LocaleBuffer: array [0..1] of Char;
CrackTree: TCustomVirtualStringTreeCracker;
lGetCellTextEventArgs: TVSTGetCellTextEventArgs;
begin
CrackTree := TCustomVirtualStringTreeCracker(Tree);
Buffer := TBufferedRawByteString.Create;
lGetCellTextEventArgs.ExportType := TVTExportType.etRtf;
CrackTree.StartOperation(TVTOperationKind.okExport);
try
// For customization by the application or descendants we use again the redirected font change event.
CrackTree.RedirectFontChangeEvent(CrackTree.Canvas);
Fonts := TStringList.Create;
Colors := TList<TColor>.Create;
CurrentFontIndex := -1;
CurrentFontColor := -1;
CurrentFontSize := -1;
Columns := nil;
Tabs := '';
LastLevel := 0;
RenderColumns := CrackTree.Header.UseColumns;
if RenderColumns then
Columns := CrackTree.Header.Columns.GetVisibleColumns;
CrackTree.GetRenderStartValues(Source, Run, GetNextNode);
Save := Run;
// First make a table structure. The \rtf and other header stuff is included
// when the font and color tables are created.
Buffer.Add('\uc1\trowd\trgaph70');
J := 0;
if RenderColumns then
begin
for I := 0 to High(Columns) do
begin
System.Inc(J, Columns[I].Width);
// This value must be expressed in twips (1 inch = 1440 twips).
Twips := Round(1440 * J / Screen.PixelsPerInch);
Buffer.Add('\cellx');
Buffer.Add(IntToStr(Twips));
end;
end
else
begin
Twips := Round(1440 * CrackTree.ClientWidth / Screen.PixelsPerInch);
Buffer.Add('\cellx');
Buffer.Add(IntToStr(Twips));
end;
// Fill table header.
if RenderColumns then
begin
if Assigned(CrackTree.OnBeforeHeaderExport) then
CrackTree.OnBeforeHeaderExport(CrackTree, etRTF);
Buffer.Add('\pard\intbl');
for I := 0 to High(Columns) do
begin
if Assigned(CrackTree.OnBeforeColumnExport) then
CrackTree.OnBeforeColumnExport(CrackTree, etRTF, Columns[I]);
Alignment := Columns[I].CaptionAlignment;
BidiMode := Columns[I].BidiMode;
// Alignment is not supported with older RTF formats, however it will be ignored.
if BidiMode <> bdLeftToRight then
ChangeBidiModeAlignment(Alignment);
case Alignment of
taLeftJustify:
Buffer.Add('\ql');
taRightJustify:
Buffer.Add('\qr');
taCenter:
Buffer.Add('\qc');
end;
TextPlusFont(Columns[I].Text, CrackTree.Header.Font);
Buffer.Add('\cell');
if Assigned(CrackTree.OnAfterColumnExport) then
CrackTree.OnAfterColumnExport(CrackTree, etRTF, Columns[I]);
end;
Buffer.Add('\row');
if Assigned(CrackTree.OnAfterHeaderExport) then
CrackTree.OnAfterHeaderExport(CrackTree, etRTF);
end;
// Now write the contents.
Run := Save;
while Assigned(Run) and not CrackTree.OperationCanceled do
begin
if (not CrackTree.CanExportNode(Run)) then
begin
Run := GetNextNode(Run);
Continue;
end;
if Assigned(CrackTree.OnBeforeNodeExport) then
CrackTree.OnBeforeNodeExport(CrackTree, etRTF, Run);
I := 0;
while not RenderColumns or (I < Length(Columns)) do
begin
if RenderColumns then
begin
Index := Columns[I].Index;
Alignment := Columns[I].Alignment;
BidiMode := Columns[I].BidiMode;
end
else
begin
Index := NoColumn;
Alignment := CrackTree.Alignment;
BidiMode := CrackTree.BidiMode;
end;
if not RenderColumns or (coVisible in Columns[I].Options) then
begin
// Get the text
lGetCellTextEventArgs.Node := Run;
lGetCellTextEventArgs.Column := Index;
CrackTree.DoGetText(lGetCellTextEventArgs);
Buffer.Add('\pard\intbl');
// Alignment is not supported with older RTF formats, however it will be ignored.
if BidiMode <> bdLeftToRight then
ChangeBidiModeAlignment(Alignment);
case Alignment of
taRightJustify:
Buffer.Add('\qr');
taCenter:
Buffer.Add('\qc');
end;
// Call back the application to know about font customization.
CrackTree.Canvas.Font.Assign(CrackTree.Font);
CrackTree.FFontChanged := False;
CrackTree.DoPaintText(Run, CrackTree.Canvas, Index, ttNormal);
if Index = CrackTree.Header.MainColumn then
begin
Level := CrackTree.GetNodeLevel(Run);
if Level <> LastLevel then
begin
LastLevel := Level;
Tabs := '';
for J := 0 to Level - 1 do
Tabs := Tabs + '\tab';
end;
if Level > 0 then
begin
Buffer.Add(Tabs);
Buffer.Add(' ');
TextPlusFont(lGetCellTextEventArgs.CellText, CrackTree.Canvas.Font);
end
else
begin
TextPlusFont(lGetCellTextEventArgs.CellText, CrackTree.Canvas.Font);
end;
end
else
begin
TextPlusFont(lGetCellTextEventArgs.CellText, CrackTree.Canvas.Font);
end;
if not lGetCellTextEventArgs.StaticText.IsEmpty and (toShowStaticText in TStringTreeOptions(CrackTree.TreeOptions).StringOptions) then
begin
CrackTree.DoPaintText(Run, CrackTree.Canvas, Index, ttStatic);
TextPlusFont(' ' + lGetCellTextEventArgs.StaticText, CrackTree.Canvas.Font);
end;//if static text
Buffer.Add('\cell');
end;
if not RenderColumns then
Break;
System.Inc(I);
end;
Buffer.Add('\row');
Buffer.AddNewLine;
if (Assigned(CrackTree.OnAfterNodeExport)) then
CrackTree.OnAfterNodeExport(CrackTree, etRTF, Run);
Run := GetNextNode(Run);
end;
Buffer.Add('\pard\par');
// Build lists with fonts and colors. They have to be at the start of the document.
S := '{\rtf1\ansi\ansicpg1252\deff0\deflang1043{\fonttbl';
for I := 0 to Fonts.Count - 1 do
S := S + Format('{\f%d %s;}', [I, Fonts[I]]);
S := S + '}';
S := S + '{\colortbl;';
for I := 0 to Colors.Count - 1 do
begin
J := ColorToRGB(TColor(Colors[I]));
S := S + Format('\red%d\green%d\blue%d;', [J and $FF, (J shr 8) and $FF, (J shr 16) and $FF]);
end;
S := S + '}';
if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMEASURE, @LocaleBuffer[0], Length(LocaleBuffer)) <> 0) and (LocaleBuffer[0] = '0'{metric}) then
S := S + '\paperw16840\paperh11907'// This sets A4 landscape format
else
S := S + '\paperw15840\paperh12240';//[JAM:marder] This sets US Letter landscape format
// Make sure a small margin is used so that a lot of the table fits on a paper. This defines a margin of 0.5"
S := S + '\margl720\margr720\margt720\margb720';
Result := S + Buffer.AsString + '}';
Fonts.Free;
Colors.Free;
CrackTree.RestoreFontChangeEvent(CrackTree.Canvas);
finally
CrackTree.EndOperation(TVTOperationKind.okExport);
Buffer.Free;
end;
end;
function ContentToUnicodeString(Tree: TCustomVirtualStringTree; Source: TVSTTextSourceType; const Separator: string): string;
// Renders the current tree content (depending on Source) as Unicode text.
// If an entry contains the separator char then it is wrapped with double quotation marks.
var
Buffer: TBufferedString;
procedure CheckQuotingAndAppend(const pText: string);
begin
// Wrap the text with quotation marks if it contains the separator character.
if Pos(Separator, pText) > 0 then
Buffer.Add(AnsiQuotedStr(pText, '"'))
else
Buffer.Add(pText);
end;
var
RenderColumns: Boolean;
Tabs: string;
GetNextNode: TGetNextNodeProc;
Run, Save: PVirtualNode;
Columns: TColumnsArray;
LastColumn: TVirtualTreeColumn;
Level, MaxLevel: Cardinal;
Index,
I: Integer;
CrackTree: TCustomVirtualStringTreeCracker;
lGetCellTextEventArgs: TVSTGetCellTextEventArgs;
begin
CrackTree := TCustomVirtualStringTreeCracker(Tree);
Buffer := TBufferedString.Create;
lGetCellTextEventArgs.ExportType := TVTExportType.etText;
CrackTree.StartOperation(TVTOperationKind.okExport);
try
Columns := nil;
RenderColumns := CrackTree.Header.UseColumns;
if RenderColumns then
Columns := CrackTree.Header.Columns.GetVisibleColumns;
CrackTree.GetRenderStartValues(Source, Run, GetNextNode);
Save := Run;
// The text consists of visible groups representing the columns, which are separated by one or more separator
// characters. There are always MaxLevel separator chars in a line (main column only). Either before the caption
// to ident it or after the caption to make the following column aligned.
MaxLevel := 0;
while Assigned(Run) and not CrackTree.OperationCanceled do
begin
Level := CrackTree.GetNodeLevel(Run);
if Level > MaxLevel then
MaxLevel := Level;
Run := GetNextNode(Run);
end;
Tabs := DupeString(Separator, MaxLevel);
// First line is always the header if used.
if RenderColumns then
begin
LastColumn := Columns[High(Columns)];
for I := 0 to High(Columns) do
begin
Buffer.Add(Columns[I].Text);
if Columns[I] <> LastColumn then
begin
if Columns[I].Index = CrackTree.Header.MainColumn then
begin
Buffer.Add(Tabs);
Buffer.Add(Separator);
end
else
Buffer.Add(Separator);
end;
end;
Buffer.AddNewLine;
end
else
LastColumn := nil;
Run := Save;
if RenderColumns then
begin
while Assigned(Run) and not CrackTree.OperationCanceled do
begin
for I := 0 to High(Columns) do
begin
if coVisible in Columns[I].Options then
begin
Index := Columns[I].Index;
lGetCellTextEventArgs.Node := Run;
lGetCellTextEventArgs.Column := Index;
CrackTree.DoGetText(lGetCellTextEventArgs);
if Index = CrackTree.Header.MainColumn then
Buffer.Add(Copy(Tabs, 1, Integer(CrackTree.GetNodeLevel(Run)) * Length(Separator)));
if not lGetCellTextEventArgs.StaticText.IsEmpty and (toShowStaticText in TStringTreeOptions(CrackTree.TreeOptions).StringOptions) then
CheckQuotingAndAppend(lGetCellTextEventArgs.CellText + ' ' + lGetCellTextEventArgs.StaticText)
else
CheckQuotingAndAppend(lGetCellTextEventArgs.CellText);
if Index = CrackTree.Header.MainColumn then
Buffer.Add(Copy(Tabs, 1, Integer(MaxLevel - CrackTree.GetNodeLevel(Run)) * Length(Separator)));
if Columns[I] <> LastColumn then
Buffer.Add(Separator);
end;
end;
Run := GetNextNode(Run);
Buffer.AddNewLine;
end;
end
else
begin
lGetCellTextEventArgs.Column := NoColumn;
while Assigned(Run) and not CrackTree.OperationCanceled do
begin
lGetCellTextEventArgs.Node := Run;
CrackTree.DoGetText(lGetCellTextEventArgs);
Level := CrackTree.GetNodeLevel(Run);
Buffer.Add(Copy(Tabs, 1, Integer(Level) * Length(Separator)));
Buffer.Add(lGetCellTextEventArgs.CellText);
Buffer.AddNewLine;
Run := GetNextNode(Run);
end;
end;
Result := Buffer.AsString;
finally
CrackTree.EndOperation(TVTOperationKind.okExport);
Buffer.Free;
end;
end;
function ContentToClipboard(Tree: TCustomVirtualStringTree; Format: Word; Source: TVSTTextSourceType): HGLOBAL;
// This method constructs a shareable memory object filled with string data in the required format. Supported are:
// CF_TEXT - plain ANSI text (Unicode text is converted using the user's current locale)
// CF_UNICODETEXT - plain Unicode text
// CF_CSV - comma separated plain ANSI text
// CF_VRTF + CF_RTFNOOBS - rich text (plain ANSI)
// CF_HTML - HTML text encoded using UTF-8
//
// Result is the handle to a globally allocated memory block which can directly be used for clipboard and drag'n drop
// transfers. The caller is responsible for freeing the memory. If for some reason the content could not be rendered
// the Result is 0.
//--------------- local function --------------------------------------------
procedure MakeFragment(var HTML: Utf8String);
// Helper routine to build a properly-formatted HTML fragment.
const
Version = 'Version:1.0'#13#10;
StartHTML = 'StartHTML:';
EndHTML = 'EndHTML:';
StartFragment = 'StartFragment:';
EndFragment = 'EndFragment:';
DocType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
HTMLIntro = '<html><head><META http-equiv=Content-Type content="text/html; charset=utf-8">' +
'</head><body><!--StartFragment-->';
HTMLExtro = '<!--EndFragment--></body></html>';
NumberLengthAndCR = 10;
// Let the compiler determine the description length.
DescriptionLength = Length(Version) + Length(StartHTML) + Length(EndHTML) + Length(StartFragment) +
Length(EndFragment) + 4 * NumberLengthAndCR;
var
Description: Utf8String;
StartHTMLIndex,
EndHTMLIndex,
StartFragmentIndex,
EndFragmentIndex: Integer;
begin
// The HTML clipboard format is defined by using byte positions in the entire block where HTML text and
// fragments start and end. These positions are written in a description. Unfortunately the positions depend on the
// length of the description but the description may change with varying positions.
// To solve this dilemma the offsets are converted into fixed length strings which makes it possible to know
// the description length in advance.
StartHTMLIndex := DescriptionLength; // position 0 after the description
StartFragmentIndex := StartHTMLIndex + Length(DocType) + Length(HTMLIntro);
EndFragmentIndex := StartFragmentIndex + Length(HTML);
EndHTMLIndex := EndFragmentIndex + Length(HTMLExtro);
Description := Version +
System.SysUtils.Format('%s%.8d', [StartHTML, StartHTMLIndex]) + #13#10 +