-
Notifications
You must be signed in to change notification settings - Fork 5
/
VTEditors.pas
1593 lines (1412 loc) · 42.3 KB
/
VTEditors.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
{----------------------------------------------------------}
{ Simple Editors for VirtualStringTree }
{ by Constantine Yannakopoulos }
{----------------------------------------------------------}
{ This software is distributed "AS IS", }
{ WITHOUT WARRANTY OF ANY KIND, either express or implied. }
{----------------------------------------------------------}
unit VTEditors;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,
Buttons,
ExtCtrls,
ComCtrls,
Spin,
VirtualTrees;
{$I Compilers.inc}
type
TCustomEditLinkClass = class of TCustomEditLink;
TIntfOwnedPersistent = class(TPersistent, IUnknown)
protected {private}
FOwner: TPersistent;
FOwnerInterface: IUnknown;
FRefCount: Integer;
FManaged: Boolean;
protected
function GetOwner: TPersistent; override;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
constructor Create(AOwner: TPersistent); overload; virtual;
constructor Create(AManaged: Boolean = False); overload; // if AManaged = True it is a ref-counted object
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
property Owner: TPersistent read FOwner;
property RefCount: Integer read FRefCount;
property Managed: Boolean read FManaged;
end;
TVirtualStringTreeHacker = class(TVirtualStringTree);
TCustomEditLink = class(TIntfOwnedPersistent, IVTEditLink)
protected {private}
FEdit: TWinControl;
FTree: TVirtualStringTreeHacker;
FNode: PVirtualNode;
FColumn: Integer;
FTextBounds: TRect;
FOldEditText: string;
FStopping: Boolean;
FOldWndProc: TWndMethod;
function GetLink: IVTEditLink;
function GetTree: TCustomVirtualStringTree;
protected
function CreateEditControl: TWinControl; virtual; abstract;
function GetEditText: WideString; virtual;
procedure SetEditText(const Value: WideString); virtual;
procedure PrepareEditControl; virtual;
procedure CMAutoAdjust(var Message: TMessage); message CM_AUTOADJUST;
procedure CMExit(var Message: TMessage); message CM_EXIT;
procedure CMRelease(var Message: TMessage); message CM_RELEASE;
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
procedure WMChar(var Message: TWMChar); message WM_CHAR;
procedure WMDestroy(var Message: TWMDestroy); message WM_DESTROY;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
procedure EditWndProc(var Message: TMessage); virtual;
procedure AutoAdjustSize; virtual;
function IsMultiLine: Boolean; virtual;
procedure KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); virtual;
procedure DoExit(Sender: TObject);
function Modified: Boolean; virtual;
procedure StopEdit; virtual;
function GetHookableWindowProc: TWndMethod; virtual;
procedure SetHookableWindowProc(const Value: TWndMethod); virtual;
property Tree: TCustomVirtualStringTree read GetTree;
property EditControl: TWinControl read FEdit;
property Node: PVirtualNode read FNode;
property Column: Integer read FColumn;
property Stopping: Boolean read FStopping;
property HookableWindowProc: TWndMethod
read GetHookableWindowProc
write SetHookableWindowProc;
public
destructor Destroy; override;
class function GetName: string; virtual;
procedure DefaultHandler(var Message); override;
property Link: IVTEditLink read GetLink;
published
protected
{ IVTEditLink }
function BeginEdit: Boolean; stdcall;
function CancelEdit: Boolean; stdcall;
function EndEdit: Boolean; stdcall;
function GetBounds: TRect; stdcall;
function PrepareEdit(ATree: TBaseVirtualTree; ANode: PVirtualNode; AColumn: TColumnIndex): Boolean; stdcall;
procedure ProcessMessage(var Message: TMessage); stdcall;
procedure SetBounds(Rect: TRect); virtual; stdcall;
procedure AfterBeginEdit; virtual;
end;
TEditEditLink = class(TCustomEditLink)
protected {private}
FReadOnly: Boolean;
protected
function CreateEditControl: TWinControl; override;
procedure PrepareEditControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
procedure EditWndProc(var Message: TMessage); override;
procedure KeyPress(Sender: TObject; var Key: Char);
public
constructor Create(AReadOnly: Boolean = False); overload;
published
property ReadOnly: Boolean read FReadOnly write FReadOnly default False;
end;
TMemoEditLink = class(TCustomEditLink)
protected {private}
FReadOnly: Boolean;
FExtent: TPoint;
protected
function CreateEditControl: TWinControl; override;
procedure PrepareEditControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
function IsMultiLine: Boolean; override;
public
constructor Create(AReadOnly: Boolean = False); overload;
procedure SetBounds(R: TRect); override; stdcall;
published
property ReadOnly: Boolean read FReadOnly write FReadOnly default False;
property ExtentX: Integer read FExtent.X write FExtent.X default 0;
property ExtentY: Integer read FExtent.Y write FExtent.Y default 0;
end;
TSpinEditLink = class(TCustomEditLink)
protected {private}
FReadOnly: Boolean;
FEditorEnabled: Boolean;
FIncrement: Integer;
FMinValue: Integer;
FMaxValue: Integer;
procedure KeyPress(Sender: TObject; var Key: Char);
protected
function CreateEditControl: TWinControl; override;
procedure PrepareEditControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
public
constructor Create(AOwner: TPersistent); override;
constructor Create(AEditorEnabled: Boolean = True; AIncrement: Integer = 1; AMinValue: Integer = 0; AMaxValue: Integer = 0); overload;
published
property EditorEnabled: Boolean read FEditorEnabled write FEditorEnabled default True;
property Increment: Integer read FIncrement write FIncrement default 1;
property MinValue: Integer read FMinValue write FMinValue default 0;
property MaxValue: Integer read FMaxValue write FMaxValue default 0;
end;
TAutoCompleteMode = (acNone, acComplete, acLimit);
TComboEditLink = class;
IComboCustomDraw = interface ['{3B63A965-89BB-4C1E-988C-DE61726F0C82}']
procedure ComboDrawItem(Sender: TComboEditLink; Control: TComboBox; Index: Integer; Rect: TRect; State: TOwnerDrawState);
procedure ComboMeasureItem(Sender: TComboEditLink; Control: TComboBox; Index: Integer; var Height: Integer);
end;
TComboEditLink = class(TCustomEditLink)
protected {private}
FStyle: TComboBoxStyle;
FPickList: TStringList;
FAutoCompleteMode: TAutoCompleteMode;
FSorted: Boolean;
procedure EditKeyPress(Sender: TObject; var Key: Char);
procedure EditChange(Sender: TObject);
procedure DoAutoComplete(var Key: Char);
function GetPickList: TStrings;
procedure SetPickList(const Value: TStrings);
protected
function CreateEditControl: TWinControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
procedure PrepareEditControl; override;
procedure KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); override;
procedure ComboDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); virtual;
procedure ComboMeasureItem(Control: TWinControl; Index: Integer; var Height: Integer); virtual;
public
constructor Create(AOwner: TPersistent); override;
constructor Create(const AList: string; AStyle: TComboBoxStyle; ASorted: Boolean = False); overload;
constructor Create(APickList: TStrings; AStyle: TComboBoxStyle; ASorted: Boolean = False); overload;
destructor Destroy; override;
procedure SetBounds(R: TRect); override;
published
property Style: TComboBoxStyle read FStyle write FStyle default csDropDown;
property Sorted: Boolean read FSorted write FSorted default False;
property PickList: TStrings read GetPickList write SetPickList;
property AutoCompleteMode: TAutoCompleteMode read FAutoCompleteMode write FAutoCompleteMode default acNone;
end;
TcheckComboEditLink = class;
TcheckComboEditLink = class(TComboEditLink)
protected
function CreateEditControl: TWinControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
procedure PrepareEditControl; override;
procedure KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); override;
public
procedure SetBounds(R: TRect); override;
procedure PopupSetBounds;
end;
TDateEditLink = class(TCustomEditLink)
protected {private}
FDateFormat: string;
FDateSeparator: Char;
FKind: TDateTimeKind;
protected
function CreateEditControl: TWinControl; override;
procedure PrepareEditControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
public
constructor Create(AKind: TDateTimeKind; const ADateFormat: string = ''; ADateSeparator: Char = #0);
published
property DateFormat: string read FDateFormat write FDateFormat;
property DateSeparator: Char read FDateSeparator write FDateSeparator default #0;
property Kind: TDateTimeKind read FKind write FKind default dtkDate;
end;
TCheckEditLink = class(TCustomEditLink)
protected {private}
FAllowGrayed: Boolean;
FValueFalse: string;
FValueTrue: string;
FValueGrayed: string;
protected
protected
function CreateEditControl: TWinControl; override;
procedure PrepareEditControl; override;
function GetEditText: WideString; override;
procedure SetEditText(const Value: WideString); override;
public
constructor Create(AOwner: TPersistent); override;
published
property AllowGrayed: Boolean read FAllowGrayed write FAllowGrayed default False;
property ValueFalse: string read FValueFalse write FValueFalse;
property ValueTrue: string read FValueTrue write FValueTrue;
property ValueGrayed: string read FValueGrayed write FValueGrayed;
end;
procedure RegisterEditLink(ALinkClass: TCustomEditLinkClass);
procedure RevokeEditLink(ALinkClass: TCustomEditLinkClass);
function GetEditLinkClass(const Name: string): TCustomEditLinkClass;
procedure GetEditLinkClasses(Strings: TStrings);
implementation
uses
TypInfo,
Types,
Math,
JvCheckListBox,
JvComboBox;
var
LinkClasses: TStringList = nil;
{ TIntfOwnedPersistent }
constructor TIntfOwnedPersistent.Create(AOwner: TPersistent);
begin
inherited Create;
FOwner := AOwner;
FManaged := True;
end;
constructor TIntfOwnedPersistent.Create(AManaged: Boolean = False);
begin
Create(nil);
FManaged := AManaged;
end;
procedure TIntfOwnedPersistent.AfterConstruction;
begin
inherited;
if GetOwner <> nil then
GetOwner.GetInterface(IUnknown, FOwnerInterface);
if not FManaged then
InterlockedDecrement(FRefCount);
end;
procedure TIntfOwnedPersistent.BeforeDestruction;
begin
Assert(FManaged or (FRefCount = 0),
Format('%s instance has RefCount <> 0 during destruction.', [ClassName]));
end;
class function TIntfOwnedPersistent.NewInstance: TObject;
begin
Result := inherited NewInstance;
TIntfOwnedPersistent(Result).FRefCount := 1;
end;
function TIntfOwnedPersistent.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then Result := S_OK else Result := E_NOINTERFACE;
end;
function TIntfOwnedPersistent.GetOwner: TPersistent;
begin
Result := FOwner;
end;
function TIntfOwnedPersistent._AddRef: Integer;
begin
if FOwnerInterface <> nil then
Result := FOwnerInterface._AddRef
else if not FManaged then
Result := InterlockedIncrement(FRefCount)
else
Result := -1;
end;
function TIntfOwnedPersistent._Release: Integer;
begin
if FOwnerInterface <> nil then
Result := FOwnerInterface._Release
else if not FManaged then
begin
Result := InterlockedDecrement(FRefCount);
if Result <= 0 then Destroy;
end
else
Result := -1;
end;
{ TCustomEditLink }
type
TWinControlAccess = class(TWinControl);
procedure TCustomEditLink.DefaultHandler(var Message);
begin
FOldWndProc(TMessage(Message));
end;
destructor TCustomEditLink.Destroy;
begin
FreeAndNil(FEdit);
inherited Destroy;
end;
class function TCustomEditLink.GetName: string;
var
I: Integer;
begin
Result := ClassName;
if Result[1] = 'T' then Delete(Result, 1, 1);
I := Pos('EditLink', Result);
if I = Length(Result) - 7 then SetLength(Result, Length(Result) - 8);
end;
function TCustomEditLink.GetTree: TCustomVirtualStringTree;
begin
Result := FTree;
end;
function TCustomEditLink.IsMultiLine: Boolean;
begin
Result := Assigned(FNode) and (vsMultiline in FNode.States);
end;
procedure TCustomEditLink.AfterBeginEdit;
begin
{can be override}
end;
procedure TCustomEditLink.AutoAdjustSize;
begin
{can be override}
end;
function TCustomEditLink.BeginEdit: Boolean;
begin
Result := not FStopping;
if Result then
begin
FEdit.Show;
FEdit.SetFocus;
FOldWndProc := HookableWindowProc;
HookableWindowProc := EditWndProc;
FOldEditText := GetEditText;
AfterBeginEdit;
end;
end;
function TCustomEditLink.CancelEdit: Boolean;
begin
Result := not FStopping;
if Result then
try
FStopping := True;
FEdit.Hide;
FTree.CancelEditNode;
StopEdit;
finally
FStopping := False;
end;
end;
procedure TCustomEditLink.CMAutoAdjust(var Message: TMessage);
begin
AutoAdjustSize;
end;
procedure TCustomEditLink.CMExit(var Message: TMessage);
begin
if not FStopping then with FTree do begin
if (toAutoAcceptEditChange in TreeOptions.StringOptions) then
DoEndEdit
else
DoCancelEdit;
end else
inherited;
end;
procedure TCustomEditLink.CMRelease(var Message: TMessage);
begin
//is this ever called?
inherited;
end;
procedure TCustomEditLink.CNCommand(var Message: TWMCommand);
begin
if not FStopping and Assigned(FEdit) and Assigned(FTree) and (Message.NotifyCode = EN_UPDATE) and
not (toGridExtensions in FTree.TreeOptions.MiscOptions) and
not IsMultiLine then
// Instead directly calling AutoAdjustSize it is necessary on Win9x/Me to decouple this notification message
// and eventual resizing. Hence we use a message to accomplish that.
if Win32Platform = VER_PLATFORM_WIN32_NT then
AutoAdjustSize
else
PostMessage(FEdit.Handle, CM_AUTOADJUST, 0, 0);
end;
function TCustomEditLink.EndEdit: Boolean;
begin
Result := not FStopping;
if Result then
try
FStopping := True;
if Modified then FTree.Text[FNode, FColumn, True] := GetEditText;
//!!!FTree.EndEditNode;
StopEdit;
finally
FStopping := False;
end;
end;
function TCustomEditLink.GetBounds: TRect;
begin
Result := FEdit.BoundsRect;
end;
function TCustomEditLink.PrepareEdit(ATree: TBaseVirtualTree; ANode: PVirtualNode; AColumn: TColumnIndex): Boolean;
var
s: string;
begin
Result := not FStopping;
if Result then
begin
FTree := TVirtualStringTreeHacker(ATree as TCustomVirtualStringTree);
FNode := ANode;
FColumn := AColumn;
FreeAndNil(FEdit);
FEdit := CreateEditControl;
Result := Assigned(FEdit);
if Result then
with FEdit do
begin
Visible := False;
Parent := Tree;
TWinControlAccess(EditControl).OnKeyDown := KeyDown;
TWinControlAccess(EditControl).OnExit := DoExit;
FEdit.BoundsRect := FTree.GetDisplayRect(FNode, FColumn, False);
s := FTree.Text[FNode, FColumn, True];
FTree.GetTextInfo(FNode, FColumn, TWinControlAccess(EditControl).Font, FTextBounds, True, s);
PrepareEditControl;
SetEditText(s);
end;
end;
end;
procedure TCustomEditLink.ProcessMessage(var Message: TMessage);
begin
HookableWindowProc(Message);
end;
procedure TCustomEditLink.SetBounds(Rect: TRect);
var
L, R: Integer;
NodeRect: TRect;
begin
if not (toGridExtensions in TVirtualStringTree(FTree).TreeOptions.MiscOptions) then
begin
NodeRect := FTree.GetDisplayRect(FNode, FColumn, True);
Rect.Left := NodeRect.Left;
TVirtualStringTree(FTree).Header.Columns.GetColumnBounds(FColumn, L, R);
Rect.Right := R;
end;
FEdit.BoundsRect := Rect;
end;
function TCustomEditLink.GetEditText: WideString;
var
Len: Integer;
begin
Len := GetWindowTextLengthW(FEdit.Handle);
SetLength(Result, Len);
if Len > 0 then
GetWindowTextW(FEdit.Handle, @Result[1], Len);
end;
function TCustomEditLink.GetHookableWindowProc: TWndMethod;
begin
Result := FEdit.WindowProc;
end;
procedure TCustomEditLink.SetEditText(const Value: WideString);
begin
SetWindowTextW(FEdit.Handle, PWideChar(Value));
end;
procedure TCustomEditLink.SetHookableWindowProc(const Value: TWndMethod);
begin
FEdit.WindowProc := Value;
end;
procedure TCustomEditLink.PrepareEditControl;
begin
end;
procedure TCustomEditLink.EditWndProc(var Message: TMessage);
begin
Dispatch(Message);
end;
procedure TCustomEditLink.KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_ESCAPE:
FTree.CancelEditNode;
VK_RETURN:
if Shift = [] then
FTree.EndEditNode
else
Exit;
else
Exit;
end;
Key := 0;
end;
procedure TCustomEditLink.DoExit(Sender: TObject);
begin
if not FStopping then FTree.EndEditNode;
end;
function TCustomEditLink.Modified: Boolean;
begin
Result := GetEditText <> FOldEditText;
end;
procedure TCustomEditLink.StopEdit;
begin
if Assigned(FEdit) then
begin
FEdit.Hide;
HookableWindowProc := FOldWndProc;
FEdit.Parent := nil;
end;
end;
procedure TCustomEditLink.WMChar(var Message: TWMChar);
begin
if not (Message.CharCode in [VK_ESCAPE, VK_TAB]) then
inherited;
end;
procedure TCustomEditLink.WMDestroy(var Message: TWMDestroy);
begin
// If editing stopped by other means than accept or cancel then we have to do default processing for
// pending changes.
if not FStopping then
with FTree do
if (toAutoAcceptEditChange in TreeOptions.StringOptions) and Modified then
Text[FNode, FColumn, True] := GetEditText;
inherited;
end;
procedure TCustomEditLink.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
inherited;
Message.Result := Message.Result or DLGC_WANTALLKEYS or DLGC_WANTTAB or DLGC_WANTARROWS;
end;
procedure TCustomEditLink.WMKeyDown(var Message: TWMKeyDown);
// Handles some control keys.
var
Shift: TShiftState;
EndEdit: Boolean;
Tree: TVirtualStringTreeHacker;
begin
case Message.CharCode of
VK_ESCAPE:
begin
Tree := FTree;
Tree.DoCancelEdit;
Tree.SetFocus;
end;
VK_RETURN:
begin
EndEdit := not (vsMultiline in FNode.States);
if not EndEdit then begin
// If a multiline node is being edited the finish editing only if Ctrl+Enter was pressed,
// otherwise allow to insert line breaks into the text.
Shift := KeyDataToShiftState(Message.KeyData);
EndEdit := ssCtrl in Shift;
end;
if EndEdit then begin
Tree := FTree;
Tree.InvalidateNode(FNode);
Tree.DoEndEdit;
Tree.SetFocus;
end;
end;
VK_UP:
begin
if not IsMultiLine then
Message.CharCode := VK_LEFT;
inherited;
end;
VK_DOWN:
begin
if not IsMultiLine then
Message.CharCode := VK_RIGHT;
inherited;
end;
else
inherited;
end;
end;
function TCustomEditLink.GetLink: IVTEditLink;
begin
GetInterface(IVTEditLink, Result);
end;
{ TEditEditLink }
constructor TEditEditLink.Create(AReadOnly: Boolean = False);
begin
Create(nil);
FReadOnly := AReadOnly;
end;
function TEditEditLink.CreateEditControl: TWinControl;
begin
Result := TEdit.Create(nil);
end;
procedure TEditEditLink.EditWndProc(var Message: TMessage);
begin
case Message.Msg of
WM_KEYDOWN:
case TWMKeyDown(Message).CharCode of
VK_UP: TWMKeyDown(Message).CharCode := VK_LEFT;
VK_DOWN: TWMKeyDown(Message).CharCode := VK_RIGHT;
end;
end;
inherited;
end;
procedure TEditEditLink.KeyPress(Sender: TObject; var Key: Char);
begin
if Key in [#13, #27] then Key := #0; // Eliminate beep
end;
function TEditEditLink.GetEditText: WideString;
begin
Result := TEdit(FEdit).Text;
end;
procedure TEditEditLink.PrepareEditControl;
begin
inherited;
with TEdit(FEdit) do
begin
ReadOnly := FReadOnly;
OnKeyPress := KeyPress;
end;
end;
procedure TEditEditLink.SetEditText(const Value: WideString);
begin
TEdit(FEdit).Text := Value;
end;
type
TLinkComboBox = class(TComboBox)
end;
{ TComboEditLink }
constructor TComboEditLink.Create(AOwner: TPersistent);
begin
inherited Create(AOwner);
FPickList := TStringList.Create;
end;
constructor TComboEditLink.Create(const AList: string; AStyle: TComboBoxStyle;
ASorted: Boolean = False);
begin
Create(nil);
FSorted := ASorted;
FStyle := AStyle;
FPickList.CommaText := AList;
FAutoCompleteMode := acComplete;
end;
constructor TComboEditLink.Create(APickList: TStrings; AStyle: TComboBoxStyle;
ASorted: Boolean = False);
begin
Create(nil);
FSorted := ASorted;
FStyle := AStyle;
FAutoCompleteMode := acComplete;
if Assigned(APickList) then FPickList.AddStrings(APickList);
end;
function TComboEditLink.CreateEditControl: TWinControl;
begin
Result := TLinkComboBox.Create(nil);
with TLinkComboBox(Result) do begin
Ctl3D := False;
end;
end;
destructor TComboEditLink.Destroy;
begin
FreeAndNil(FPickList);
inherited;
end;
procedure TComboEditLink.KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_RETURN:
if (FStyle = csSimple) or not TLinkComboBox(EditControl).DroppedDown then
begin
Tree.EndEditNode;
Key := 0;
end;
VK_ESCAPE:
if (FStyle = csSimple) or not TLinkComboBox(EditControl).DroppedDown then
begin
Tree.CancelEditNode;
Key := 0;
end;
end;
end;
procedure TComboEditLink.ComboDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
I: IComboCustomDraw;
begin
if Supports(FOwnerInterface, IComboCustomDraw, I) then
I.ComboDrawItem(Self, TLinkComboBox(Control), Index, Rect, State);
end;
procedure TComboEditLink.ComboMeasureItem(Control: TWinControl; Index: Integer; var Height: Integer);
var
I: IComboCustomDraw;
begin
if Supports(FOwnerInterface, IComboCustomDraw, I) then
I.ComboMeasureItem(Self, TLinkComboBox(Control), Index, Height);
end;
function TComboEditLink.GetEditText: WideString;
begin
Result := TLinkComboBox(EditControl).Text;
end;
procedure TComboEditLink.PrepareEditControl;
begin
inherited;
with TLinkComboBox(EditControl) do
begin
Sorted := FSorted;
Style := FStyle;
Items := FPickList;
OnKeyPress := EditKeyPress;
OnChange := EditChange;
if Style in [csOwnerDrawFixed, csOwnerDrawVariable] then
begin
OnDrawItem := ComboDrawItem;
OnMeasureItem := ComboMeasureItem;
end;
end;
end;
procedure TComboEditLink.SetEditText(const Value: WideString);
begin
with TLinkComboBox(EditControl) do
if Style = csDropDownList then
ItemIndex := Items.IndexOf(Value)
else
Text := Value;
end;
procedure TComboEditLink.SetBounds(R: TRect);
begin
if FStyle = csSimple then
R.Bottom := R.Top + 104;
inherited SetBounds(R);
end;
procedure TComboEditLink.EditKeyPress(Sender: TObject; var Key: Char);
begin
DoAutoComplete(Key);
end;
procedure TComboEditLink.DoAutoComplete(var Key: Char);
var
S, S1: string;
I, St, L: Integer;
begin
if (FStyle in [csDropDown, csSimple]) and (Key in [#32 .. #255]) then
with TLinkComboBox(EditControl) do
case FAutoCompleteMode of
acComplete:
begin
St := SelStart;
S := Copy(Text, 1, St) + Key;
L := Length(S);
with FPickList do
for I := 0 to Count - 1 do
if AnsiSameText(Copy(Strings[I], 1, L), S) then
begin
S1 := S + Copy(Strings[I], Length(S) + 1, MaxInt);
TLinkComboBox(EditControl).Text := S1;
SelStart := St + 1;
SelLength := Length(S1) - St;
Key := #0;
Exit;
end;
end;
end;
end;
procedure TComboEditLink.EditChange(Sender: TObject);
var
S: string;
I, St, Sl, L: Integer;
List: TStringList;
begin
if (FStyle in [csDropDown, csSimple]) then
with TLinkComboBox(EditControl) do
case FAutoCompleteMode of
acLimit:
begin
S := Text;
L := Length(S);
St := SelStart;
Sl := SelLength;
List := TStringList.Create;
try
if L = 0 then
List.AddStrings(FPickList)
else
begin
with FPickList do
for I := 0 to Count - 1 do
if AnsiSameText(Copy(Strings[I], 1, L), S) then
List.Add(Strings[I]);
end;
if not Items.Equals(List) then Items.Assign(List);
TLinkComboBox(EditControl).Text := S;
SelStart := St;
SelLength := Sl;
finally
List.Free;
end;
end;
end;
end;
function TComboEditLink.GetPickList: TStrings;
begin
Result := FPickList;
end;
procedure TComboEditLink.SetPickList(const Value: TStrings);
begin
FPickList.Assign(Value);
end;
{ TDateEditLink }
constructor TDateEditLink.Create(AKind: TDateTimeKind; const ADateFormat: string = ''; ADateSeparator: Char = #0);
begin
FKind := AKind;
FDateFormat := ADateFormat;
FDateSeparator := ADateSeparator;
end;
function TDateEditLink.CreateEditControl: TWinControl;
begin
Result := TDateTimePicker.Create(nil);
end;
procedure TDateEditLink.PrepareEditControl;
begin
inherited;
TDateTimePicker(EditControl).Kind := FKind;
end;
{$IFDEF VER 220}
function TDateEditLink.GetEditText: WideString;
var
Sep: Char;
begin
Sep := SysUtils.DateSeparator;
try
if FDateSeparator <> #0 then SysUtils.DateSeparator := FDateSeparator;
if FDateFormat <> '' then
Result := FormatDateTime(FDateFormat, TDateTimePicker(EditControl).DateTime)
else
case FKind of
dtkDate: Result := DateToStr(TDateTimePicker(EditControl).DateTime);
dtkTime: Result := TimeToStr(TDateTimePicker(EditControl).DateTime);
end;
finally
SysUtils.DateSeparator := Sep;
end;
end;
procedure TDateEditLink.SetEditText(const Value: WideString);
var
DF: string;
Sep: Char;
begin
if Value <> '' then
try
DF := ShortDateFormat;
Sep := SysUtils.DateSeparator;
try
if FDateFormat <> '' then ShortDateFormat := FDateFormat;
if FDateSeparator <> #0 then SysUtils.DateSeparator := FDateSeparator;
case FKind of
dtkDate: TDateTimePicker(EditControl).Date := StrToDate(Value);
dtkTime: TDateTimePicker(EditControl).Time := StrToTime(Value);
end;
finally
ShortDateFormat := DF;
SysUtils.DateSeparator := Sep;
end;
except
end;
end;
{$ELSE VER220}
function TDateEditLink.GetEditText: WideString;
var
Sep: Char;