My favorites | Sign in
Project Home Source
Checkout   Browse   Changes  
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
unit grideditlinks;

// The editor links, instanciated by VirtualTree.CreateEditor

interface

uses
Windows, Forms, Graphics, Messages, VirtualTrees, ComCtrls, SysUtils, Classes,
StdCtrls, ExtCtrls, CheckLst, Controls, Types, Dialogs, Mask, DateUtils, Math,
dbconnection, mysql_structures, helpers, texteditor, bineditor;

type
// Radio buttons and checkboxes which do not pass <Enter> key to their parent control
// so a OnKeyDown event using <Enter> has the chance to end editing.
TAllKeysRadioButton = class(TRadioButton)
procedure WMGetDlgCode(var Msg: TMessage); message WM_GETDLGCODE;
end;
TAllKeysCheckBox = class(TCheckBox)
procedure WMGetDlgCode(var Msg: TMessage); message WM_GETDLGCODE;
end;

TBaseGridEditorLink = class(TInterfacedObject, IVTEditLink)
private
FParentForm: TWinControl; // A back reference to the main form
FTree: TVirtualStringTree; // A back reference to the tree calling.
FNode: PVirtualNode; // The node to be edited.
FColumn: TColumnIndex; // The column of the node.
FCellText: String; // Original cell text value
FCellFont: TFont; // Cosmetic
FCellBackground: TColor;
FMainControl: TWinControl; // The editor's most important component
FStopping: Boolean; // Set to True when the edit link requests stopping the edit action.
FLastKeyDown: Integer; // Set in OnKeyDown on the editor's main control
FLastShiftState: TShiftState;
FOldWindowProc: TWndMethod; // Temporary switched to TempWindowProc to be able to catch Tab key
FFullDatatype: TDBDatatype;
FModified: Boolean;
procedure TempWindowProc(var Message: TMessage);
procedure DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure DoEndEdit(Sender: TObject);
procedure DoCancelEdit(Sender: TObject);
function GetCellRect(InnerTextBounds: Boolean): TRect;
public
Connection: TDBConnection;
Datatype: TDBDatatypeIndex; // The data type of the cell being edited. Mostly used in data grids.
constructor Create; overload; // The original constructor, not used any more, throws an exception if you do
constructor Create(Tree: TVirtualStringTree); overload; virtual; // The right constructor, we need the Tree reference
destructor Destroy; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; virtual; stdcall;
function BeginEdit: Boolean; virtual; stdcall;
function CancelEdit: Boolean; virtual; stdcall;
function EndEdit: Boolean; virtual; stdcall; abstract;
function EndEditHelper(NewText: String): Boolean;
function GetBounds: TRect; virtual; stdcall; // Normally useless and unused
procedure ProcessMessage(var Message: TMessage); stdcall;
procedure SetBounds(R: TRect); virtual; stdcall; abstract;
end;

THexEditorLink = class(TBaseGridEditorLink)
private
FForm: TfrmBinEditor;
public
MaxLength: Integer;
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function CancelEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
end;

TDateTimeEditorLink = class(TBaseGridEditorLink)
private
FPanel: TPanel;
FMaskEdit: TMaskEdit;
FTimer: TTimer;
FModifyOffset: Integer;
FTimerCalls: Integer;
FUpDown: TUpDown;
FDefaultDateTime: String;
procedure DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure DoKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure UpDownChangingEx(Sender: TObject; var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
procedure UpDownMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure DoOnTimer(Sender: TObject);
procedure ModifyDate(Offset: Integer);
procedure TextChange(Sender: TObject);
public
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
property DefaultDateTime: String read FDefaultDateTime write FDefaultDateTime;
end;

TEnumEditorLink = class(TBaseGridEditorLink)
private
FCombo: TComboBox;
public
ValueList, DisplayList: TStringList;
AllowCustomText: Boolean;
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
end;

TSetEditorLink = class(TBaseGridEditorLink)
private
FPanel: TPanel;
FCheckList: TCheckListBox;
FBtnOK, FBtnCancel: TButton;
public
ValueList: TStringList;
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
procedure PanelResize(Sender: TObject);
end;

// Inplace editor with button
TInplaceEditorLink = class(TBaseGridEditorLink)
private
FPanel: TPanel;
FEdit: TEdit;
FButton: TButton;
FTextEditor: TfrmTextEditor;
FMaxLength: Integer;
procedure DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure ButtonClick(Sender: TObject);
public
ButtonVisible: Boolean;
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function CancelEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
property MaxLength: Integer read FMaxLength write FMaxLength;
end;

TColumnDefaultEditorLink = class(TBaseGridEditorLink)
private
FPanel: TPanel;
FRadioNothing, FRadioText, FRadioNULL, FRadioCurTS, FRadioAutoInc: TAllKeysRadioButton;
FCheckCurTS: TAllKeysCheckbox;
FMemoText: TMemo;
FBtnOK, FBtnCancel: TButton;
procedure RadioClick(Sender: TObject);
procedure TextChange(Sender: TObject);
public
DefaultType: TColumnDefaultType;
DefaultText: String;
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
end;

TDataTypeEditorLink = class(TBaseGridEditorLink)
private
FTreeSelect: TVirtualStringTree;
FMemoHelp: TMemo;
procedure DoTreeSelectGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
procedure DoTreeSelectInitNode(Sender: TBaseVirtualTree;
ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
procedure DoTreeSelectInitChildren(Sender: TBaseVirtualTree;
Node: PVirtualNode; var ChildCount: Cardinal);
procedure DoTreeSelectHotChange(Sender: TBaseVirtualTree; OldNode, NewNode: PVirtualNode);
procedure DoTreeSelectPaintText(Sender: TBaseVirtualTree;
const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType);
procedure DoTreeSelectFocusChanging(Sender: TBaseVirtualTree; OldNode, NewNode:
PVirtualNode; OldColumn, NewColumn: TColumnIndex; var Allowed: Boolean);
procedure DoTreeSelectFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
public
constructor Create(Tree: TVirtualStringTree); override;
destructor Destroy; override;
function BeginEdit: Boolean; override;
function EndEdit: Boolean; override;
function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; override;
procedure SetBounds(R: TRect); override;
end;



implementation


procedure TAllKeysRadioButton.WMGetDlgCode(var Msg: TMessage);
begin
inherited;
Msg.Result := Msg.Result or DLGC_WANTALLKEYS;
end;


procedure TAllKeysCheckBox.WMGetDlgCode(var Msg: TMessage);
begin
inherited;
Msg.Result := Msg.Result or DLGC_WANTALLKEYS;
end;


constructor TBaseGridEditorLink.Create;
begin
raise Exception.Create('Wrong constructor called: ' + Self.ClassName + '.Create.' + CRLF +
'Instead, please call the overloaded version ' + Self.ClassName + '.Create(VirtualStringTree).');
end;

constructor TBaseGridEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create;
FTree := Tree;
// Enable mouse scrolling, plus ensure the editor component
// is not partly hidden when it pops up in a bottom cell
FParentForm := GetParentForm(FTree);
// Avoid flicker
FParentForm.Repaint;
SendMessage(FParentForm.Handle, WM_SETREDRAW, 0, 0);
FModified := False;
end;

destructor TBaseGridEditorLink.Destroy;
var
NewColumn, FirstCol, LastCol: TColumnIndex;
NewNode: PVirtualNode;
DoPrev: Boolean;
begin
inherited;
if FLastKeyDown = VK_TAB then begin
DoPrev := ssShift in FLastShiftState;
// Advance to next/previous visible column/node.
NewNode := FNode;
NewColumn := FColumn;
FirstCol := FTree.Header.Columns.GetFirstVisibleColumn;
LastCol := FTree.Header.Columns.GetLastVisibleColumn;
while true do begin
// Find a column for the current node which can be focused.
if DoPrev then begin
if NewColumn = FirstCol then begin
NewColumn := LastCol;
NewNode := FTree.GetPreviousVisible(NewNode);
end else
NewColumn := FTree.Header.Columns.GetPreviousColumn(NewColumn);
end else begin
if NewColumn = LastCol then begin
NewColumn := FirstCol;
NewNode := FTree.GetNextVisible(NewNode);
end else
NewColumn := FTree.Header.Columns.GetNextColumn(NewColumn);
end;
if not Assigned(NewNode) then
Break;
FTree.FocusedNode := NewNode;
FTree.FocusedColumn := NewColumn;
if not FTree.EditNode(FTree.FocusedNode, FTree.FocusedColumn) then
FTree.SetFocus;
Break;
end;
end;
end;

function TBaseGridEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean;
var
FCellTextBounds: TRect;
i: Integer;
begin
Result := not FStopping;
if not Result then
Exit;
FNode := Node;
FColumn := Column;
FCellFont := TFont.Create;
FTree.GetTextInfo(FNode, FColumn, FCellFont, FCellTextBounds, FCellText);
// Not all editors have a connection assigned, e.g. session manager tree
if Assigned(Connection) then begin
for i:=0 to High(Connection.Datatypes) do begin
if Connection.Datatypes[i].Index = Datatype then begin
FFullDatatype := Connection.Datatypes[i];
break;
end;
end;
FCellFont.Color := DatatypeCategories[Integer(FFullDatatype.Category)].Color;
end;
FCellBackground := FTree.Header.Columns[FColumn].Color;
if Assigned(FMainControl) then begin
FOldWindowProc := FMainControl.WindowProc;
FMainControl.WindowProc := TempWindowProc;
end;
// Adjust editor position and allow repainting mainform
SetBounds(FCellTextBounds);
SendMessage(FParentForm.Handle, WM_SETREDRAW, 1, 0);
end;

function TBaseGridEditorLink.BeginEdit: Boolean;
begin
Result := not FStopping;
end;

function TBaseGridEditorLink.CancelEdit: Boolean;
begin
Result := not FStopping;
if Result then begin
FStopping := True;
FTree.CancelEditNode;
if FTree.CanFocus then
FTree.SetFocus;
end;
end;

function TBaseGridEditorLink.EndEditHelper(NewText: String): Boolean;
begin
Result := not FStopping;
if FStopping then Exit;
FStopping := True;
if FModified then
FTree.Text[FNode, FColumn] := NewText;
if FTree.CanFocus and (FLastKeyDown <> VK_TAB) then
FTree.SetFocus;
end;

procedure TBaseGridEditorLink.TempWindowProc(var Message: TMessage);
begin
case Message.Msg of
WM_CHAR: // Catch hotkeys
if not (TWMChar(Message).CharCode = VK_TAB) then
FOldWindowProc(Message);
WM_GETDLGCODE: // "WantTabs" mode for main control
Message.Result := Message.Result or DLGC_WANTARROWS or DLGC_WANTALLKEYS or DLGC_WANTTAB;
else begin
try
FOldWindowProc(Message);
except
// EAccessViolation occurring in some cases
end;
end;
end;
end;

procedure TBaseGridEditorLink.ProcessMessage(var Message: TMessage);
begin
if Assigned(FMainControl) then
FMainControl.WindowProc(Message);
end;

function TBaseGridEditorLink.GetBounds: TRect; stdcall;
begin
// Only important if the editor resizes itself.
Result := Rect(0, 0, 0, 0);
end;

function TBaseGridEditorLink.GetCellRect(InnerTextBounds: Boolean): TRect;
var
Text: String;
CellBounds, TextBounds: TRect;
Ghosted: Boolean;
ImageIndex: Integer;
f: TFont;
begin
// Return the cell's rectangle, relative to the parent form.
f := TFont.Create;
FTree.GetTextInfo(FNode, FColumn, f, TextBounds, Text);
CellBounds := FTree.GetDisplayRect(FNode, FColumn, False);

Inc(CellBounds.Left, Integer(FTree.GetNodeLevel(FNode)) * (Integer(FTree.Indent)+FTree.TextMargin));
if (toShowRoot in FTree.TreeOptions.PaintOptions)
and (FColumn = FTree.Header.MainColumn) then begin
// Reserve space for plus or minus button
Inc(CellBounds.Left, FTree.Indent);
end;
if Assigned(FTree.Images) and Assigned(FTree.OnGetImageIndex) then begin
// Reserve space for image
ImageIndex := -1;
FTree.OnGetImageIndex(FTree, FNode, ikNormal, FColumn, Ghosted, ImageIndex);
if ImageIndex > -1 then
Inc(CellBounds.Left, FTree.Images.Width+2);
end;
TextBounds.Left := CellBounds.Left + 2*FTree.TextMargin;

if InnerTextBounds then begin
// Inner bounds are considered to be relative to the outer cell bounds
Result := Rect(TextBounds.Left-CellBounds.Left,
TextBounds.Top-CellBounds.Top,
CellBounds.Right-CellBounds.Left, // Far right edge of cell, not of text
CellBounds.Bottom-CellBounds.Top
);
end else begin
// Recalculate top left corner of rectangle, so it is relative to the parent form (which is FParentForm)
Result := CellBounds;
OffsetRect(Result,
FTree.ClientOrigin.X - FParentForm.ClientOrigin.X,
FTree.ClientOrigin.Y - FParentForm.ClientOrigin.Y
);
Dec(Result.Bottom, 1);
end;
end;

procedure TBaseGridEditorLink.DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
FLastKeyDown := Key;
FLastShiftState := Shift;
case Key of
// Cancel by Escape
VK_ESCAPE: FTree.CancelEditNode;
// Apply changes and end editing by [Ctrl +] Enter or Tab
VK_RETURN, VK_TAB: FTree.EndEditNode;
end;
end;

procedure TBaseGridEditorLink.DoEndEdit(Sender: TObject);
begin
FTree.EndEditNode;
end;

procedure TBaseGridEditorLink.DoCancelEdit(Sender: TObject);
begin
FTree.CancelEditNode;
end;




constructor THexEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create(Tree);
end;

destructor THexEditorLink.Destroy;
begin
inherited;
FForm.Close;
FreeAndNil(FForm);
end;


function THexEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
begin
Result := inherited PrepareEdit(Tree, Node, Column);
if not Result then
Exit;

// Create the text editor form
FForm := TfrmBinEditor.Create(Ftree);
FForm.SetFont(FCellFont);
FForm.SetText(FCellText);
FForm.SetMaxLength(MaxLength);
end;


function THexEditorLink.BeginEdit: Boolean; stdcall;
begin
Result := inherited BeginEdit;
if Result then
FForm.ShowModal;
end;


function THexEditorLink.CancelEdit: Boolean;
begin
Result := inherited CancelEdit;
if Result then
FForm.Close;
end;


function THexEditorLink.EndEdit: Boolean; stdcall;
begin
FForm.Close;
FModified := FForm.Modified;
Result := EndEditHelper(FForm.GetText);
end;


procedure THexEditorLink.SetBounds(R: TRect); stdcall;
begin
// Not in use, form's position is centered on mainform
end;



{ DateTime editor }

constructor TDateTimeEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create(Tree);

FPanel := TPanel.Create(FParentForm);
FPanel.Parent := FParentForm;
FPanel.Hide;
FPanel.ParentBackground := False;
FPanel.BevelOuter := bvNone;
FPanel.OnExit := DoEndEdit;

FMaskEdit := TMaskEdit.Create(FPanel);
FMaskEdit.Parent := FPanel;
FMaskEdit.ParentColor := True;
FMaskEdit.BorderStyle := bsNone;
FMaskEdit.OnKeyDown := DoKeyDown;
FMaskEdit.OnKeyUp := DoKeyUp;
FMaskEdit.OnChange := TextChange;
FMainControl := FMaskEdit;

FUpDown := TUpDown.Create(FPanel);
FUpDown.Parent := FPanel;
FUpDown.OnChangingEx := UpDownChangingEx;
FUpDown.OnMouseUp := UpDownMouseUp;

FTimer := TTimer.Create(FMaskEdit);
FTimer.Interval := 50;
FTimer.OnTimer := DoOnTimer;
FTimer.Enabled := False;

FDefaultDateTime := '';
end;


destructor TDateTimeEditorLink.Destroy;
begin
OpenRegistry;
Mainreg.WriteInteger(REGPREFIX_DATEEDITOR_CURSOR+IntToStr(Integer(Datatype)), FMaskEdit.SelStart);
FreeAndNil(FTimer);
FreeAndNil(FUpDown);
FreeAndNil(FMaskEdit);
FreeAndNil(FPanel);
inherited;
end;


function TDateTimeEditorLink.BeginEdit: Boolean; stdcall;
begin
Result := inherited BeginEdit;
if Result then begin
FPanel.Show;
FMaskEdit.SetFocus;
// Focus very last segment of date
FMaskEdit.SelStart := GetRegValue(REGPREFIX_DATEEDITOR_CURSOR+IntToStr(Integer(Datatype)), Length(FMaskEdit.Text)-1);
FMaskEdit.SelLength := 1;
end;
end;


function TDateTimeEditorLink.EndEdit: Boolean;
begin
Result := EndEditHelper(Trim(FMaskEdit.Text));
end;


function TDateTimeEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
var
MinColWidth: Integer;
begin
Result := inherited PrepareEdit(Tree, Node, Column);
if not Result then
Exit;
case Datatype of
dtDate: FMaskEdit.EditMask := '0000-00-00;1; ';
dtDatetime, dtTimestamp: FMaskEdit.EditMask := '0000-00-00 00\:00\:00;1; ';
dtTime: begin
FMaskEdit.EditMask := '#900\:00\:00;1; ';
while Length(FCellText) < 10 do
FCellText := ' ' + FCellText;
end;
dtYear: FMaskEdit.EditMask := '0000;1; ';
end;
if FCellText = '' then begin
FMaskEdit.Text := FDefaultDateTime;
case DataType of
dtDate: FMaskEdit.Text := Copy(FMaskEdit.Text, 1, 10);
dtTime: FMaskEdit.Text := Copy(FMaskEdit.Text, 12, 8);
end;
end else
FMaskEdit.Text := FCellText;
FModified := False;
FMaskEdit.Font.Assign(FCellFont);
FPanel.Color := FCellBackground;
// Auto-enlarge current tree column so the text in the edit is not cut
MinColWidth := FTree.Canvas.TextWidth(FCellText) + FTree.TextMargin + FUpDown.Width + 5;
if FTree.Header.Columns[FColumn].Width < MinColWidth then
FTree.Header.Columns[FColumn].Width := MinColWidth;
end;


procedure TDateTimeEditorLink.SetBounds(R: TRect); stdcall;
var
EditRect: TRect;
OldSelStart, OldSelLen: Integer;
begin
FPanel.BoundsRect := GetCellRect(False);

FUpDown.Left := FPanel.Width - FUpDown.Width;
FUpDown.Height := FPanel.Height;

EditRect := GetCellRect(True);
EditRect.Right := FUpDown.Left;
FMaskEdit.BoundsRect := EditRect;

OldSelStart := FMaskEdit.SelStart;
OldSelLen := FMaskEdit.SelLength;
FMaskEdit.SelStart := 0;
FMaskEdit.SelStart := OldSelStart;
FMaskEdit.SelLength := OldSelLen;
end;


procedure TDateTimeEditorLink.DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
inherited DoKeyDown(Sender, Key, Shift);
if (Key in [VK_UP, VK_DOWN]) and (not FTimer.Enabled) then begin
if Key = VK_UP then FModifyOffset := 1
else FModifyOffset := -1;
FTimerCalls := 0;
DoOnTimer(Sender);
FTimer.Enabled := True;
end;
end;


procedure TDateTimeEditorLink.DoKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
FTimer.Enabled := False;
end;


procedure TDateTimeEditorLink.UpDownChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: SmallInt; Direction: TUpDownDirection);
begin
if FTimer.Enabled then
Exit;
if Direction = updUp then FModifyOffset := 1
else FModifyOffset := -1;
FTimerCalls := 0;
DoOnTimer(Sender);
FTimer.Enabled := True;
end;


procedure TDateTimeEditorLink.UpDownMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FTimer.Enabled := False;
end;


procedure TDateTimeEditorLink.DoOnTimer(Sender: TObject);
var
DelayCalls: Integer;
begin
Inc(FTimerCalls);
// short delay before counting up/down
DelayCalls := 350 Div FTimer.Interval;
if (FTimerCalls > DelayCalls) or (not (Sender is TTimer)) then
ModifyDate(FModifyOffset);
// Speed up counting in steps
if FTimerCalls in [DelayCalls*5, DelayCalls*10] then begin
if FModifyOffset > 0 then
Inc(FModifyOffset, 3)
else
Dec(FModifyOffset, 3);
end;
end;


procedure TDateTimeEditorLink.ModifyDate(Offset: Integer);
var
dt: TDateTime;
d: TDate;
i, MaxSeconds, MinSeconds: Int64;
text: String;
OldSelStart, OldSelLength: Integer;

function TimeToSeconds(Str: String): Int64;
var
Hours: String;
Seconds: Int64;
begin
Hours := Trim(Copy(Str, 1, 4));
Result := MakeInt(Hours) * 60 * 60;
Seconds := MakeInt(Copy(Str, 6, 2)) * 60 + MakeInt(Copy(Str, 9, 2));
if (Result < 0) or ((Result = 0) and (Hours[1]='-')) then
Dec(Result, Seconds)
else
Inc(Result, Seconds);
end;

function SecondsToTime(Seconds: Int64): String;
var
HoursNum, Minutes: Int64;
Hours: String;
begin
HoursNum := Abs(Seconds div (60*60));
Hours := IntToStr(HoursNum);
if Length(Hours) = 1 then
Hours := '0' + Hours;
if Seconds < 0 then
Hours := '-' + Hours;
Seconds := Abs(Seconds) mod (60*60);
Minutes := Seconds div 60;
Seconds := Seconds mod 60;
Result := Format('%4s:%.2u:%.2u', [Hours, Minutes, Seconds]);
end;
begin
try
case Datatype of
dtYear: begin
i := MakeInt(FMaskEdit.Text);
i := i + Offset;
text := IntToStr(i);
end;

dtDate: begin
d := StrToDate(FMaskEdit.Text);
// De- or increase focused date segment
case FMaskEdit.SelStart of
0..3: d := IncYear(d, Offset);
5,6: d := IncMonth(d, Offset);
8..10: d := IncDay(d, Offset);
end;
text := DateToStr(d);
end;

dtDateTime, dtTimestamp: begin
dt := StrToDateTime(FMaskEdit.Text);
case FMaskEdit.SelStart of
0..3: dt := IncYear(dt, Offset);
5,6: dt := IncMonth(dt, Offset);
8,9: dt := IncDay(dt, Offset);
11,12: dt := IncHour(dt, Offset);
14,15: dt := IncMinute(dt, Offset);
17..19: dt := IncSecond(dt, Offset);
end;
text := DateTimeToStr(dt);
if Length(text) = 10 then
text := text + ' 00:00:00';
end;

dtTime: begin
i := TimeToSeconds(FMaskEdit.Text);
case FMaskEdit.SelStart of
0..3: Inc(i, Offset*60*60);
5,6: Inc(i, Offset*60);
8,9: Inc(i, Offset);
end;
// Stop at max and min values. See http://dev.mysql.com/doc/refman/5.0/en/time.html
MaxSeconds := 839*60*60-1;
MinSeconds := -(MaxSeconds);
if i > MaxSeconds then
i := MaxSeconds;
if i < MinSeconds then
i := MinSeconds;
text := SecondsToTime(i);
end;

else text := '';
end;

if text <> '' then begin
OldSelStart := FMaskEdit.SelStart;
OldSelLength := FMaskEdit.SelLength;
FMaskEdit.Text := text;
FMaskEdit.SelStart := OldSelStart;
FMaskEdit.SelLength := OldSelLength;
end;
except
on E:EConvertError do begin
// Ignore any DateToStr exception. Should only appear in cases where the users
// enters invalid dates
end else
raise;
end;
end;


procedure TDateTimeEditorLink.TextChange;
begin
FModified := True;
end;



{ Enum editor }

constructor TEnumEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create(Tree);
AllowCustomText := False;
FCombo := TComboBox.Create(FParentForm);
FCombo.Hide;
FCombo.Parent := FParentForm;
FCombo.OnKeyDown := DoKeyDown;
FCombo.OnExit := DoEndEdit;
ValueList := TStringList.Create;
DisplayList := TStringList.Create;
FMainControl := FCombo;
end;


destructor TEnumEditorLink.Destroy;
begin
FCombo.Free;
inherited;
end;


function TEnumEditorLink.BeginEdit: Boolean; stdcall;
begin
Result := inherited BeginEdit;
if Result then begin
FCombo.Show;
FCombo.SetFocus;
end;
end;


function TEnumEditorLink.EndEdit: Boolean; stdcall;
var
NewText: String;
begin
if AllowCustomText then
NewText := FCombo.Text
else if (ValueList.Count > 0) and (FCombo.ItemIndex > -1) then
NewText := ValueList[FCombo.ItemIndex]
else
NewText := '';
FModified := NewText <> FCellText;
Result := EndEditHelper(NewText);
end;


function TEnumEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
var
i: Integer;
Items: TStringList;
begin
Result := inherited PrepareEdit(Tree, Node, Column);
if Result then begin
if DisplayList.Count = ValueList.Count then
Items := DisplayList
else
Items := ValueList;
for i:=0 to Items.Count - 1 do
FCombo.Items.Add(Items[i]);
if AllowCustomText then begin
FCombo.Style := csDropDown;
FCombo.Text := FCellText;
end else begin
// Set style to OwnerDraw, otherwise we wouldn't be able to adjust the combo's height
FCombo.Style := csOwnerDrawFixed;
FCombo.ItemIndex := ValueList.IndexOf(FCellText);
end;
end;
end;


procedure TEnumEditorLink.SetBounds(R: TRect); stdcall;
begin
FCombo.BoundsRect := GetCellRect(False);
end;




{ SET editor }

constructor TSetEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create(Tree);
ValueList := TStringList.Create;

FPanel := TPanel.Create(FParentForm);
FPanel.Hide;
FPanel.Parent := FParentForm;
FPanel.ParentBackground := False;
FPanel.Width := GetRegValue(REGNAME_SETEDITORWIDTH, DEFAULT_SETEDITORWIDTH);
FPanel.Height := GetRegValue(REGNAME_SETEDITORHEIGHT, DEFAULT_SETEDITORHEIGHT);
SetWindowSizeGrip(FPanel.Handle, True);
FPanel.OnResize := PanelResize;
FPanel.OnExit := DoEndEdit;

FCheckList := TCheckListBox.Create(FPanel);
FCheckList.Parent := FPanel;
FCheckList.OnKeyDown := DoKeyDown;
FMainControl := FCheckList;

FBtnOk := TButton.Create(FPanel);
FBtnOk.Parent := FPanel;
FBtnOk.Caption := 'OK';
FBtnOk.OnClick := DoEndEdit;

FBtnCancel := TButton.Create(FPanel);
FBtnCancel.Parent := FPanel;
FBtnCancel.Caption := 'Cancel';
FBtnCancel.OnClick := DoCancelEdit;
end;


destructor TSetEditorLink.Destroy;
begin
Mainreg.WriteInteger(REGNAME_SETEDITORWIDTH, FPanel.Width);
Mainreg.WriteInteger(REGNAME_SETEDITORHEIGHT, FPanel.Height);
FreeAndNil(FPanel);
inherited;
end;


function TSetEditorLink.BeginEdit: Boolean; stdcall;
begin
Result := inherited BeginEdit;
if Result then begin
FPanel.Show;
FCheckList.SetFocus;
end;
end;


function TSetEditorLink.EndEdit: Boolean; stdcall;
var
newtext: String;
i: Integer;
begin
Result := not FStopping;
if FStopping then Exit;
newText := '';
for i := 0 to FCheckList.Items.Count - 1 do
if FCheckList.Checked[i] then newText := newText + FCheckList.Items[i] + ',';
Delete(newText, Length(newText), 1);
FModified := newText <> FCellText;
Result := EndEditHelper(newText);
end;


function TSetEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
var
SelValues: TStringList;
begin
Result := inherited PrepareEdit(Tree, Node, Column);
if not Result then
Exit;

FCheckList.Font.Assign(FCellFont);
FCheckList.Items.Assign(ValueList);
SelValues := TStringList.Create;
SelValues.Delimiter := ',';
SelValues.StrictDelimiter := True;
SelValues.DelimitedText := FCellText;
ToggleCheckListBox(FCheckList, True, SelValues);
end;


procedure TSetEditorLink.SetBounds(R: TRect); stdcall;
const
margin = 5;
begin
R := GetCellRect(False);
FPanel.Top := R.Top;
FPanel.Left := R.Left;

FBtnOk.Width := (FPanel.Width - 3*margin) div 2;
FBtnOk.Left := margin;
FBtnOk.Height := 24;
FBtnOk.Top := FPanel.Height - 2*margin - FBtnOk.Height;

FBtnCancel.Width := FBtnOk.Width;
FBtnCancel.Left := 2*margin + FBtnOk.Width;
FBtnCancel.Height := FBtnOk.Height;
FBtnCancel.Top := FBtnOk.Top;

FCheckList.Top := margin;
FCheckList.Left := margin;
FCheckList.Width := FPanel.Width - 2*margin;
FCheckList.Height := FBtnOk.Top - margin - FCheckList.Top;
end;


procedure TSetEditorLink.PanelResize(Sender: TObject);
begin
SetBounds(FPanel.ClientRect);
end;


{ TInplaceEditorLink }

constructor TInplaceEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create(Tree);
ButtonVisible := false;
FTextEditor := nil;

FPanel := TPanel.Create(FParentForm);
FPanel.Parent := FParentForm;
FPanel.Hide;
FPanel.ParentBackground := False;
FPanel.BevelOuter := bvNone;
FPanel.OnExit := DoEndEdit;

FEdit := TEdit.Create(FPanel);
FEdit.Parent := FPanel;
FEdit.ParentColor := True;
FEdit.BorderStyle := bsNone;
FEdit.OnKeyDown := DoKeyDown;
FMainControl := FEdit;

FButton := TButton.Create(FPanel);
FButton.Parent := FPanel;
FButton.TabStop := False;
FButton.Caption := '…';
FButton.Hint := 'Edit text in popup editor ...';
FButton.ShowHint := True;
FButton.OnClick := ButtonClick;
end;

destructor TInplaceEditorLink.Destroy;
begin
if Assigned(FTextEditor) then
FTextEditor.Release;
if not (csDestroying in FPanel.ComponentState) then
FPanel.Free;
inherited;
end;

function TInplaceEditorLink.BeginEdit: Boolean;
begin
Result := inherited BeginEdit;
if Result then begin
FButton.Visible := ButtonVisible;
SetBounds(Rect(0, 0, 0, 0));
if (Length(FEdit.Text) > SIZE_KB*100) or (ScanLineBreaks(FEdit.Text) <> lbsNone) then
ButtonClick(FTree)
else begin
FPanel.Show;
FEdit.SetFocus;
end;
end;
end;

function TInplaceEditorLink.CancelEdit: Boolean;
begin
Result := inherited CancelEdit;
if Result then begin
if Assigned(FTextEditor) then
FTextEditor.Close;
end;
end;

function TInplaceEditorLink.EndEdit: Boolean;
var
NewText: String;
begin
Result := not FStopping;
if FStopping then Exit;
if Assigned(FTextEditor) then begin
NewText := FTextEditor.GetText;
FModified := FTextEditor.Modified;
FTextEditor.Close;
end else begin
NewText := FEdit.Text;
FModified := FEdit.Modified;
end;
Result := EndEditHelper(NewText);
end;

procedure TInplaceEditorLink.DoKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
inherited DoKeyDown(Sender, Key, Shift);
if Key = VK_F2 then
ButtonClick(FButton);
end;

procedure TInplaceEditorLink.ButtonClick(Sender: TObject);
begin
if not FButton.Visible then Exit; // Button was invisible, but hotkey was pressed
FTextEditor := TfrmTextEditor.Create(FTree);
FTextEditor.SetFont(FEdit.Font);
FTextEditor.SetText(FEdit.Text);
FTextEditor.Modified := FEdit.Modified;
FTextEditor.SetMaxLength(Self.FMaxLength);
FTextEditor.ShowModal;
end;

function TInplaceEditorLink.PrepareEdit(Tree: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex): Boolean;
begin
Result := inherited PrepareEdit(Tree, Node, Column);
if not Result then
Exit;

FEdit.Font.Assign(FCellFont);
FEdit.Font.Color := clWindowText;
FPanel.Color := FCellBackground;
if ScanNulChar(FCellText) then begin
MessageDialog(SContainsNulCharGrid, mtInformation, [mbOK]);
FEdit.Text := RemoveNulChars(FCellText);
end else
FEdit.Text := FCellText;
FEdit.Modified := False;
end;

procedure TInplaceEditorLink.SetBounds(R: TRect);
begin
if not FStopping then begin
// Position edit control according to cell text bounds
FPanel.BoundsRect := GetCellRect(False);
R := GetCellRect(True);
if FButton.Visible then
Dec(R.Right, 20);
FEdit.BoundsRect := R;

FButton.BoundsRect := Rect(FEdit.BoundsRect.Right, 0, FPanel.Width, FPanel.Height);
end;
end;



{ Column default editor }

constructor TColumnDefaultEditorLink.Create(Tree: TVirtualStringTree);
const
m = 3;
begin
inherited Create(Tree);

FPanel := TPanel.Create(FParentForm);
FPanel.Hide;
FPanel.Parent := FParentForm;
FPanel.OnExit := DoEndEdit;
FPanel.Width := 200;
FPanel.ParentBackground := False;
FPanel.Color := clWindow;
FPanel.BevelKind := bkFlat;
FPanel.BevelOuter := bvNone;
FMainControl := FPanel;

FRadioNothing := TAllKeysRadioButton.Create(FPanel);
FRadioNothing.Parent := FPanel;
FRadioNothing.Top := m;
FRadioNothing.Left := m;
FRadioNothing.Width := FRadioNothing.Parent.Width - 2 * FRadioNothing.Left;
FRadioNothing.OnClick := RadioClick;
FRadioNothing.OnKeyDown := DoKeyDown;
FRadioNothing.Caption := 'No default value';

FRadioText := TAllKeysRadioButton.Create(FPanel);
FRadioText.Parent := FPanel;
FRadioText.Top := FRadioNothing.Top + FRadioNothing.Height + m;;
FRadioText.Left := m;
FRadioText.Width := FRadioText.Parent.Width - 2 * FRadioText.Left;
FRadioText.OnClick := RadioClick;
FRadioText.OnKeyDown := DoKeyDown;
FRadioText.Caption := 'Custom:';

FMemoText := TMemo.Create(FPanel);
FMemoText.Parent := FPanel;
FMemoText.Top := FRadioText.Top + FRadioText.Height + m;
FMemoText.Left := 2*m;
FMemoText.Width := FMemoText.Parent.Width - FMemoText.Left - m;
FMemoText.Height := 40;
FMemoText.ScrollBars := ssVertical;
FMemoText.OnChange := TextChange;

FRadioNull := TAllKeysRadioButton.Create(FPanel);
FRadioNull.Parent := FPanel;
FRadioNull.Top := FMemoText.Top + FMemoText.Height + m;
FRadioNull.Left := m;
FRadioNull.Width := FRadioNull.Parent.Width - 2 * FRadioNull.Left;
FRadioNull.OnClick := RadioClick;
FRadioNull.OnKeyDown := DoKeyDown;
FRadioNull.Caption := 'NULL';

FRadioCurTS := TAllKeysRadioButton.Create(FPanel);
FRadioCurTS.Parent := FPanel;
FRadioCurTS.Top := FRadioNull.Top + FRadioNull.Height + m;
FRadioCurTS.Left := m;
FRadioCurTS.Width := FRadioCurTS.Parent.Width - 2 * FRadioCurTS.Left;
FRadioCurTS.OnClick := RadioClick;
FRadioCurTS.OnKeyDown := DoKeyDown;
FRadioCurTS.Caption := 'CURRENT_TIMESTAMP';

FCheckCurTS := TAllKeysCheckbox.Create(FPanel);
FCheckCurTS.Parent := FPanel;
FCheckCurTS.Top := FRadioCurTS.Top + FRadioCurTS.Height + m;
FCheckCurTS.Left := m;
FCheckCurTS.Width := FCheckCurTS.Parent.Width - 2 * FCheckCurTS.Left;
FCheckCurTS.OnClick := RadioClick;
FCheckCurTS.OnKeyDown := DoKeyDown;
FCheckCurTS.Caption := 'ON UPDATE CURRENT_TIMESTAMP';

FRadioAutoInc := TAllKeysRadioButton.Create(FPanel);
FRadioAutoInc.Parent := FPanel;
FRadioAutoInc.Top := FCheckCurTS.Top + FCheckCurTS.Height + m;
FRadioAutoInc.Left := m;
FRadioAutoInc.Width := FRadioAutoInc.Parent.Width - 2 * FRadioAutoInc.Left;
FRadioAutoInc.OnClick := RadioClick;
FRadioAutoInc.OnKeyDown := DoKeyDown;
FRadioAutoInc.Caption := 'AUTO_INCREMENT';

FBtnOk := TButton.Create(FPanel);
FBtnOk.Parent := FPanel;
FBtnOk.Width := 60;
FBtnOk.Top := FRadioAutoInc.Top + FRadioAutoInc.Height + m;
FBtnOk.Left := FPanel.Width - 2*m - 2*FBtnOk.Width;
FBtnOk.OnClick := DoEndEdit;
FBtnOk.Default := True;
FBtnOk.Caption := 'OK';

FBtnCancel := TButton.Create(FPanel);
FBtnCancel.Parent := FPanel;
FBtnCancel.Top := FBtnOk.Top;
FBtnCancel.Width := FBtnOk.Width;
FBtnCancel.Left := FBtnOk.Left + FBtnOk.Width + m;
FBtnCancel.OnClick := DoCancelEdit;
FBtnCancel.Cancel := True;
FBtnCancel.Caption := 'Cancel';

FPanel.Height := FBtnOk.Top + FBtnOk.Height + m;
FRadioNothing.Anchors := [akLeft, akTop, akRight];
FRadioText.Anchors := [akLeft, akTop, akRight];
FMemoText.Anchors := [akLeft, akTop, akRight, akBottom];
FRadioNull.Anchors := [akLeft, akBottom, akRight];
FRadioCurTS.Anchors := [akLeft, akBottom, akRight];
FCheckCurTS.Anchors := [akLeft, akBottom, akRight];
FRadioAutoInc.Anchors := [akLeft, akBottom, akRight];
FBtnOk.Anchors := [akBottom, akRight];
FBtnCancel.Anchors := FBtnOk.Anchors;
FPanel.Width := GetParentForm(FPanel).Canvas.TextWidth(FCheckCurTS.Caption) + 2*FCheckCurTS.Left + 24;
end;


destructor TColumnDefaultEditorLink.Destroy;
begin
FPanel.Free;
inherited;
end;


function TColumnDefaultEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
begin
inherited PrepareEdit(Tree, Node, Column);

// Check relevant radio button
FRadioNothing.Checked := DefaultType = cdtNothing;
FRadioText.Checked := DefaultType in [cdtText, cdtTextUpdateTS];
FRadioNull.Checked := DefaultType in [cdtNull, cdtNullUpdateTS];
FRadioCurTS.Checked := DefaultType in [cdtCurTS, cdtCurTSUpdateTS];
FCheckCurTS.Checked := DefaultType in [cdtTextUpdateTS, cdtNullUpdateTS, cdtCurTSUpdateTS];
FRadioAutoInc.Checked := DefaultType = cdtAutoInc;

if FRadioText.Checked then
FMemoText.Text := DefaultText;

// Disable non working default options per data type
// But leave checked option enabled, regardless of if that is a valid default option or not
FRadioCurTS.Enabled := FRadioCurTS.Checked or (FFullDataType.Index = dtTimestamp);
FCheckCurTS.Enabled := FCheckCurTS.Checked or FRadioCurTS.Enabled;
FRadioAutoInc.Enabled := FRadioAutoInc.Checked or (FFullDataType.Category = dtcInteger);

Result := True;
end;


procedure TColumnDefaultEditorLink.SetBounds(R: TRect); stdcall;
var
CellRect: TRect;
begin
CellRect := GetCellRect(False);
FPanel.Left := CellRect.Left;
FPanel.Top := CellRect.Top;
end;


function TColumnDefaultEditorLink.BeginEdit: Boolean; stdcall;
begin
Result := not FStopping;
if Result then begin
FPanel.Show;
if FRadioNothing.Checked then FRadioNothing.SetFocus
else if FRadioText.Checked then FRadioText.SetFocus
else if FRadioNull.Checked then FRadioNull.SetFocus
else if FRadioCurTS.Checked then FRadioCurTS.SetFocus
else if FRadioAutoInc.Checked then FRadioAutoInc.SetFocus;
end;
end;


function TColumnDefaultEditorLink.EndEdit: Boolean; stdcall;
var
newText: String;
newDefaultType: TColumnDefaultType;
begin
Result := not FStopping;
if Result then begin
FStopping := True;
if FRadioNothing.Checked then
newDefaultType := cdtNothing
else if FRadioText.Checked and FCheckCurTS.Checked then
newDefaultType := cdtTextUpdateTS
else if FRadioText.Checked then
newDefaultType := cdtText
else if FRadioNull.Checked and FCheckCurTS.Checked then
newDefaultType := cdtNullUpdateTS
else if FRadioNull.Checked then
newDefaultType := cdtNull
else if FRadioCurTS.Checked and FCheckCurTS.Checked then
newDefaultType := cdtCurTSUpdateTS
else if FRadioCurTS.Checked then
newDefaultType := cdtCurTS
else if FRadioAutoInc.Checked then
newDefaultType := cdtAutoInc
else
newDefaultType := cdtText;

case newDefaultType of
cdtNothing: newText := '';
cdtText, cdtTextUpdateTS: newText := FMemoText.Text;
cdtNull, cdtNullUpdateTS: newText := 'NULL';
cdtCurTS, cdtCurTSUpdateTS: newText := 'CURRENT_TIMESTAMP';
cdtAutoInc: newText := 'AUTO_INCREMENT';
end;
newText := IntToStr(Integer(newDefaultType)) + newText;
if newtext <> IntToStr(Integer(DefaultType)) + DefaultText then
FTree.Text[FNode, FColumn] := newtext;
if FTree.CanFocus then
FTree.SetFocus;
end;
end;


procedure TColumnDefaultEditorLink.RadioClick(Sender: TObject);
begin
if not FRadioText.Checked then
FMemoText.Color := clBtnFace
else begin
FMemoText.Color := clWindow;
if FMemoText.CanFocus then
FMemoText.SetFocus;
end;
FCheckCurTS.Enabled := not FRadioNothing.Checked;
FModified := True;
end;


procedure TColumnDefaultEditorLink.TextChange(Sender: TObject);
begin
FRadioText.Checked := True;
FModified := True;
end;



{ Datatype selector }
constructor TDataTypeEditorLink.Create(Tree: TVirtualStringTree);
begin
inherited Create(Tree);

FTreeSelect := TVirtualStringTree.Create(FParentForm);
FTreeSelect.Hide;
FTreeSelect.TreeOptions.PaintOptions := FTreeSelect.TreeOptions.PaintOptions
- [toShowTreeLines, toShowButtons, toShowRoot]
+ [toHotTrack, toUseExplorerTheme, toHideTreeLinesIfThemed];
FTreeSelect.TreeOptions.SelectionOptions := FTreeSelect.TreeOptions.SelectionOptions
+ [toFullRowSelect];
FTreeSelect.Header.Columns.Add;
FTreeSelect.Parent := FParentForm;
FTreeSelect.TextMargin := 0;
FTreeSelect.BorderStyle := bsNone;
FTreeSelect.BevelKind := bkFlat;
FTreeSelect.BevelInner := bvNone;
FTreeSelect.IncrementalSearch := isAll;
FTreeSelect.RootNodeCount := Length(DatatypeCategories);
FTreeSelect.OnGetText := DoTreeSelectGetText;
FTreeSelect.OnInitNode := DoTreeSelectInitNode;
FTreeSelect.OnInitChildren := DoTreeSelectInitChildren;
FTreeSelect.OnKeyDown := DoKeyDown;
FTreeSelect.OnHotChange := DoTreeSelectHotChange;
FTreeSelect.OnPaintText := DoTreeSelectPaintText;
FTreeSelect.OnExit := DoEndEdit;
// See further events in PrepareEdit
FixVT(FTreeSelect);
FMainControl := FTreeSelect;

FMemoHelp := TMemo.Create(FParentForm);
FMemoHelp.Hide;
FMemoHelp.Parent := FParentForm;
FMemoHelp.Color := clInfoBk;
FMemoHelp.Font.Color := clInfoText;
FMemoHelp.BorderStyle := bsNone;
FMemoHelp.BevelKind := bkFlat;
FMemoHelp.BevelInner := bvNone;
end;


destructor TDataTypeEditorLink.Destroy;
begin
FreeAndNil(FTreeSelect);
FreeAndNil(FMemoHelp);
inherited;
end;


function TDataTypeEditorLink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean;
var
dt: TDBDatatype;
CatNode, TypeNode: PVirtualNode;
begin
Result := inherited PrepareEdit(Tree, Node, Column);
if not Result then
Exit;
// Just use font name and size, avoid bold style and white color
FTreeSelect.Font.Name := FCellFont.Name;
FTreeSelect.Font.Size := FCellFont.Size;

// Find and select current datatype in tree
dt := Connection.GetDataTypeByName(FCellText);
CatNode := FTreeSelect.GetFirst;
while Assigned(CatNode) do begin
if CatNode.Index = Cardinal(dt.Category) then begin
TypeNode := FTreeSelect.GetFirstChild(CatNode);
while Assigned(TypeNode) do begin
if FTreeSelect.Text[TypeNode, 0] = FCellText then begin
FTreeSelect.FocusedNode := TypeNode;
FTreeSelect.Selected[TypeNode] := True;
break;
end;
TypeNode := FTreeSelect.GetNextSibling(TypeNode);
end;
end;
CatNode := FTreeSelect.GetNextSibling(CatNode);
end;
FTreeSelect.Header.AutoFitColumns(False, smaUseColumnOption, 0, 0);
if Assigned(FTreeSelect.FocusedNode) then
FTreeSelect.ScrollIntoView(FTreeSelect.FocusedNode, True);
FTreeSelect.OnFocusChanging := DoTreeSelectFocusChanging;
FTreeSelect.OnFocusChanged := DoTreeSelectFocusChanged;
FTreeSelect.OnClick := DoEndEdit;
end;


function TDataTypeEditorLink.BeginEdit: Boolean;
begin
Result := inherited BeginEdit;
if Result then begin
FTreeSelect.Show;
FTreeSelect.SetFocus;
end;
end;


function TDataTypeEditorLink.EndEdit: Boolean;
begin
if Assigned(FTreeSelect.FocusedNode) then
Result := EndEditHelper(FTreeSelect.Text[FTreeSelect.FocusedNode, 0])
else
Result := FTree.CancelEditNode;
end;


procedure TDataTypeEditorLink.SetBounds(R: TRect);
var
CellRect: TRect;
TreeHeight: Integer;
begin
// Set position of tree. As the tree's parent is mainform, not listcolumns, add listcolumn's x + y positions
CellRect := GetCellRect(False);
// Do not exceed lower edge of mainform, as that portion would be hidden
TreeHeight := Min(250, FParentForm.ClientHeight-CellRect.Top-10);
FTreeSelect.SetBounds(CellRect.Left,
CellRect.Top,
FTreeSelect.Header.Columns[0].Width + GetSystemMetrics(SM_CXVSCROLL) + 5,
TreeHeight);
end;


procedure TDataTypeEditorLink.DoTreeSelectInitNode(Sender: TBaseVirtualTree;
ParentNode, Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
begin
// First level nodes always expanded
if Sender.GetNodeLevel(Node) = 0 then
InitialStates := InitialStates + [ivsExpanded, ivsHasChildren];
end;


procedure TDataTypeEditorLink.DoTreeSelectInitChildren(Sender: TBaseVirtualTree;
Node: PVirtualNode; var ChildCount: Cardinal);
var
i: Integer;
begin
// Tell number of datatypes per category
ChildCount := 0;
if Sender.GetNodeLevel(Node) = 0 then for i:=0 to High(Connection.Datatypes) do begin
if Connection.Datatypes[i].Category = DatatypeCategories[Node.Index].Index then
Inc(ChildCount);
end;
end;


procedure TDataTypeEditorLink.DoTreeSelectGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
var
i: Integer;
Counter: Cardinal;
begin
// Get cell text
case Sender.GetNodeLevel(Node) of
0: CellText := DatatypeCategories[Node.Index].Name;
1: begin
Counter := 0;
for i:=0 to High(Connection.Datatypes) do begin
if Connection.Datatypes[i].Category = DatatypeCategories[Node.Parent.Index].Index then begin
Inc(Counter);
if Counter = Node.Index+1 then begin
CellText := Connection.Datatypes[i].Name;
break;
end;
end;
end;
end;
end;
end;


procedure TDataTypeEditorLink.DoTreeSelectHotChange(Sender: TBaseVirtualTree; OldNode, NewNode: PVirtualNode);
var
R: TRect;
NodeText: String;
bmp: TBitMap;
begin
// Display help box for hovered datatype
FMemoHelp.Clear;
if Assigned(NewNode) and (Sender.GetNodeLevel(NewNode) = 1) then begin
R := FTreeSelect.GetDisplayRect(NewNode, 0, False);
NodeText := FTreeSelect.Text[NewNode, 0];
FMemoHelp.Width := Min(250, FTreeSelect.Left);
FMemoHelp.Left := FTreeSelect.Left - FMemoHelp.Width + (Integer(FTreeSelect.Indent) Div 2);
FMemoHelp.Top := FTreeSelect.Top + R.Top + 3;
FMemoHelp.Text := Connection.GetDatatypeByName(NodeText).Description;
// Calc height of memo
bmp := TBitMap.Create;
bmp.Canvas.Font.Assign(FMemoHelp.Font);
R := Rect(0, 0, FMemoHelp.Width-10, 0);
DrawText(bmp.Canvas.Handle, PChar(FMemoHelp.Text), Length(FMemoHelp.Text), R, DT_WORDBREAK or DT_CALCRECT);
FreeAndNil(bmp);
FMemoHelp.Height := R.Bottom + 2;
FMemoHelp.Show;
end;
if FMemoHelp.GetTextLen = 0 then
FMemoHelp.Hide;
end;


procedure TDataTypeEditorLink.DoTreeSelectPaintText(Sender: TBaseVirtualTree;
const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType);
begin
// Give datatype column specific color, as set in preferences
case Sender.GetNodeLevel(Node) of
0: TargetCanvas.Font.Style := TargetCanvas.Font.Style + [fsBold];
1: if not (vsSelected in Node.States) then
TargetCanvas.Font.Color := DatatypeCategories[Node.Parent.Index].Color;
end;
end;


procedure TDataTypeEditorLink.DoTreeSelectFocusChanging(Sender: TBaseVirtualTree; OldNode, NewNode:
PVirtualNode; OldColumn, NewColumn: TColumnIndex; var Allowed: Boolean);
var
JumpToNode: PVirtualNode;
begin
// Allow only 2nd level datatypes to be focused, not their category
Allowed := Sender.GetNodeLevel(NewNode) = 1;
if not Allowed then begin
JumpToNode := nil;
if FLastKeyDown = VK_UP then
JumpToNode := Sender.GetPrevious(NewNode)
else if FLastKeyDown = VK_DOWN then
JumpToNode := Sender.GetNext(NewNode);
if Assigned(JumpToNode) then
Sender.FocusedNode := JumpToNode;
end;
end;


procedure TDataTypeEditorLink.DoTreeSelectFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
begin
FModified := True;
end;


end.

Change log

r4046 by ansgar.becker on Feb 7, 2012   Diff
Move modified flag to the right place.
Fixes  issue #2709 .
Go to: 

Older revisions

r4043 by ansgar.becker on Feb 6, 2012   Diff
Leave NULL date/time values in grid
untouched if user did not modify
default value. Fixes  issue #2708 .
r4008 by ansgar.becker on Dec 30, 2011   Diff
Follow up to r4002 and  issue #2656 :
Set focus to textbox only if it's
already shown, to avoid "Cannot focus
a hidden or disabled control".
r4002 by ansgar.becker on Dec 26, 2011   Diff
Focus textbox when user clicks
"Custom" in default-value selector.
Fixes  issue #2656 .
All revisions of this file

File info

Size: 50416 bytes, 1599 lines
Powered by Google Project Hosting