-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDTagEditor.pas
More file actions
1388 lines (1239 loc) · 34.7 KB
/
DTagEditor.pas
File metadata and controls
1388 lines (1239 loc) · 34.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit DTagEditor;
{
TDTagEditor Component
First Version (2014) by: Andreas Rejbrand. (https://specials.rejbrand.se/dev/controls/tageditor/)
Modified and improved version (2021) by Daniel C. Dávila - daniel@cdavila.net
}
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Forms, Graphics,
Types, Menus, Vcl.Themes, Dialogs, StrUtils;
type
TClickInfo = cardinal;
GetTagIndex = word;
const
TAG_LOW = 0;
const
TAG_HIGH = MAXWORD - 2;
const
EDITOR = MAXWORD - 1;
const
NOWHERE = MAXWORD;
const
PART_BODY = $00000000;
const
PART_REMOVE_BUTTON = $00010000;
function GetTagPart(ClickInfo: TClickInfo): cardinal;
type
TTagItemConfig = record
CanDeleteTag: Boolean;
TagBgColor: TColor;
TagBorderColor: TColor;
TagValue: Variant;
TextColor: TColor;
end;
TTagClickEvent = procedure(Sender: TObject; TagIndex: integer;
const TagCaption: string) of object;
TRemoveConfirmEvent = procedure(Sender: TObject; TagIndex: integer;
const TagCaption: string; var CanRemove: Boolean) of object;
TTagRemoved = procedure(Sender: TObject; TagIndex: integer) of object;
TBeforeTagRemove = procedure(Sender: TObject; TagIndex: integer) of object;
TTags = class;
TDTagEditor = class;
TTagItem = class(TCollectionItem)
private
FCanDeleteTag: Boolean;
FTagBgColor: TColor;
FTagBorderColor: TColor;
FTagValue: Variant;
FTextColor: TColor;
FText: string;
function GetTagEditor: TDTagEditor;
procedure SetCanDeleteTag(const Value: Boolean);
procedure SetTagBgColor(const Value: TColor);
procedure SetTagBorderColor(const Value: TColor);
procedure SetTextColor(const Value: TColor);
published
property CanDeleteTag: Boolean read FCanDeleteTag write SetCanDeleteTag;
property TagBgColor: TColor read FTagBgColor write SetTagBgColor;
property TagBorderColor: TColor read FTagBorderColor
write SetTagBorderColor;
property TagValue: Variant read FTagValue write FTagValue;
property Text: String read FText write FText;
property TextColor: TColor read FTextColor write SetTextColor;
public
procedure Assign(Source: TPersistent); override;
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
end;
TTagItemCLass = class of TTagItem;
{$HINTS OFF}
TCollectionHack = class(TPersistent)
private
FItemClass: TCollectionItemClass;
FItems: TList;
end;
{$HINTS ON}
TTags = class(TCollection)
private
FTagEditor: TDTagEditor;
function GetTagItem(Index: integer): TTagItem;
procedure SetTagItem(Index: integer; const Value: TTagItem);
protected
function GetOwner: TPersistent; override;
public
function IndexOf(AText: string): integer;
function DelimitedText: String;
function Add(AItemConfig: TTagItemConfig; AText: String = '')
: TTagItem; overload;
function Add(AText: String = ''): TTagItem; overload;
procedure DeleteAll;
procedure Move(CurIndex, NewIndex: integer);
constructor Create(ATagEditor: TDTagEditor; ATagsItemClass: TTagItemCLass);
property Items[Index: integer]: TTagItem read GetTagItem
write SetTagItem; default;
property TagEditor: TDTagEditor read FTagEditor;
end;
TDTagEditor = class(TCustomControl)
private
{ Private declarations }
FActualTagHeight: integer;
FAllowDuplicates: Boolean;
FAutoHeight: Boolean;
FBeforeTagRemove: TBeforeTagRemove;
FBgColor: TColor;
FBorderColor: TColor;
FCanDragTags: Boolean;
FCaretVisible: Boolean;
FLefts, FRights, FWidths, FTops, FBottoms: array of integer;
FCloseBtnLefts, FCloseBtnTops: array of integer;
FCloseBtnWidth: integer;
FCommaAccepts: Boolean;
FDeleteButtonIcon: TIcon;
FDeleteTagButton: Boolean;
FDesiredHeight: integer;
FDragging: Boolean;
FEdit: TEdit;
FEditorColor: TColor;
FEditPos: TPoint;
FMaxHeight: integer;
FMaxTags: integer;
FMouseDownClickInfo: TClickInfo;
FMultiLine: Boolean;
FNoLeadingSpaceInput: Boolean;
FTags: TTags;
FNumRows: integer;
FOnChange: TNotifyEvent;
FOnDblClick: TNotifyEvent;
FOnRemoveConfirm: TRemoveConfirmEvent;
FPopupMenu: TPopupMenu;
FPrevScrollPos: integer;
FReadOnly: Boolean;
FSavedReadOnly: Boolean;
FScrollBarVisible: Boolean;
FScrollInfo: TScrollInfo;
FSemicolonAccepts: Boolean;
FShrunk: Boolean;
FSpaceAccepts: Boolean;
FSpacing: integer;
FTagAdded: TNotifyEvent;
FTagBgColor: TColor;
FTagBorderColor: TColor;
FTagClickEvent: TTagClickEvent;
FTagHeight: integer;
FTagRemoved: TTagRemoved;
FTagRoundBorder: integer;
FTextColor: TColor;
FTrimInput: Boolean;
function Accept: Boolean;
function GetClickInfoAt(X, Y: integer): TClickInfo;
function GetReadOnly: Boolean;
function GetSeparatorIndexAt(X, Y: integer): integer;
function GetShrunkClientRect(const Amount: integer): TRect;
function IsFirstOnRow(TagIndex: integer): Boolean; inline;
function IsLastOnRow(TagIndex: integer): Boolean;
procedure CreateCaret;
procedure DestroyCaret;
procedure DrawFocusRect;
procedure EditEnter(Sender: TObject);
procedure EditExit(Sender: TObject);
procedure EditKeyPress(Sender: TObject; var Key: Char);
procedure FixPosAndScrollWindow;
procedure HideEditor;
procedure mnuDeleteItemClick(Sender: TObject);
procedure SetAutoHeight(const Value: Boolean);
procedure SetBgColor(const Value: TColor);
procedure SetBorderColor(const Value: TColor);
procedure SetButtonIcon(const Value: TIcon);
procedure SetCanDragTags(const Value: Boolean);
procedure SetCloseTagButton(const Value: Boolean);
procedure SetMaxHeight(const Value: integer);
procedure SetMultiLine(const Value: Boolean);
procedure SetPasteText(AText: string);
procedure SetTags(const Value: TTags);
procedure SetReadOnly(const Value: Boolean);
procedure SetSpacing(const Value: integer);
procedure SetTagBgColor(const Value: TColor);
procedure SetTagBorderColor(const Value: TColor);
procedure SetTagHeight(const Value: integer);
procedure SetTagRoundBorder(const Value: integer);
procedure SetTextColor(const Value: TColor);
procedure ShowEditor;
procedure TagChange(Sender: TObject);
procedure UpdateMetrics;
procedure UpdateScrollBars;
protected
{ Protected declarations }
function CreateTags(ATagEditor: TDTagEditor): TTags; dynamic;
function GetFontEdit: TFont;
procedure CreateParams(var Params: TCreateParams); override;
procedure DblClick;
procedure KeyDown(var Key: word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure Loaded; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: integer;
Y: integer); override;
procedure MouseMove(Shift: TShiftState; X: integer; Y: integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: integer;
Y: integer); override;
procedure Paint; override;
procedure WndProc(var Message: TMessage); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Align;
property AllowDuplicates: Boolean read FAllowDuplicates
write FAllowDuplicates default false;
property Anchors;
property AutoHeight: Boolean read FAutoHeight write SetAutoHeight;
property BgColor: TColor read FBgColor write SetBgColor;
property BorderColor: TColor read FBorderColor write SetBorderColor;
property CanDragTags: Boolean read FCanDragTags write SetCanDragTags
default True;
property Color;
property CommaAccepts: Boolean read FCommaAccepts write FCommaAccepts
default True;
property Cursor;
property DeleteButtonIcon: TIcon read FDeleteButtonIcon write SetButtonIcon;
property DeleteTagButton: Boolean read FDeleteTagButton
write SetCloseTagButton default True;
property EditorColor: TColor read FEditorColor write FEditorColor
default clWindow;
property MaxHeight: integer read FMaxHeight write SetMaxHeight default 512;
property MaxTags: integer read FMaxTags write FMaxTags default 0;
property MultiLine: Boolean read FMultiLine write SetMultiLine
default false;
property NoLeadingSpaceInput: Boolean read FNoLeadingSpaceInput
write FNoLeadingSpaceInput default True;
property Tags: TTags read FTags write SetTags;
[Default (True)]
property OnBeforeTagRemove: TBeforeTagRemove read FBeforeTagRemove
write FBeforeTagRemove;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
property OnRemoveConfirm: TRemoveConfirmEvent read FOnRemoveConfirm
write FOnRemoveConfirm;
property OnTagAdded: TNotifyEvent read FTagAdded write FTagAdded;
property OnTagClick: TTagClickEvent read FTagClickEvent
write FTagClickEvent;
property OnTagRemoved: TTagRemoved read FTagRemoved write FTagRemoved;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly default false;
property SemicolonAccepts: Boolean read FSemicolonAccepts
write FSemicolonAccepts default True;
property SpaceAccepts: Boolean read FSpaceAccepts write FSpaceAccepts
default True;
property Spacing: integer read FSpacing write SetSpacing;
property TabOrder;
property TabStop;
property Tag;
property TagBgColor: TColor read FTagBgColor write SetTagBgColor;
property TagBorderColor: TColor read FTagBorderColor
write SetTagBorderColor;
property TagHeight: integer read FTagHeight write SetTagHeight default 32;
property TagRoundBorder: integer read FTagRoundBorder
write SetTagRoundBorder;
property TextColor: TColor read FTextColor write SetTextColor;
property TrimInput: Boolean read FTrimInput write FTrimInput default True;
end;
procedure Register;
implementation
uses Math, Clipbrd;
procedure Register;
begin
RegisterComponents('TagEditor', [TDTagEditor]);
end;
function IsKeyDown(const VK: integer): Boolean;
begin
IsKeyDown := GetKeyState(VK) and $8000 <> 0;
end;
function GetTagPart(ClickInfo: TClickInfo): cardinal;
begin
result := ClickInfo and $FFFF0000;
end;
procedure SafeDrawFocusRect(hDC: hDC; const R: TRect);
var
oldBkColor, oldTextColor: COLORREF;
begin
oldBkColor := Windows.SetBkColor(hDC, clWhite);
oldTextColor := Windows.SetTextColor(hDC, clBlack);
Windows.DrawFocusRect(hDC, R);
if oldBkColor <> CLR_INVALID then
Windows.SetBkColor(hDC, oldBkColor);
if oldTextColor <> CLR_INVALID then
Windows.SetTextColor(hDC, oldTextColor);
end;
{ TTagEditor }
constructor TDTagEditor.Create(AOwner: TComponent);
var
mnuItem: TMenuItem;
begin
inherited;
FEdit := TEdit.Create(Self);
FEdit.Parent := Self;
FEdit.BorderStyle := bsNone;
FEdit.Visible := false;
FEdit.OnKeyPress := EditKeyPress;
FEdit.OnEnter := EditEnter;
FEdit.OnExit := EditExit;
FTags := CreateTags(Self);
FBgColor := clWindow;
FBorderColor := clWindowFrame;
FTagBgColor := clSkyBlue;
FTagBorderColor := clNavy;
FSpacing := 8;
FTextColor := clWhite;
FSpaceAccepts := True;
FCommaAccepts := True;
FSemicolonAccepts := True;
FTrimInput := True;
FNoLeadingSpaceInput := True;
FAllowDuplicates := false;
FMultiLine := false;
FTagHeight := 32;
FShrunk := false;
FEditorColor := clWindow;
FMaxHeight := 512;
FCaretVisible := false;
FDragging := false;
FPrevScrollPos := 0;
FScrollInfo.cbSize := sizeof(FScrollInfo);
FScrollBarVisible := false;
FDeleteTagButton := True;
FPopupMenu := TPopupMenu.Create(Self);
mnuItem := TMenuItem.Create(PopupMenu);
mnuItem.Caption := 'Deletar';
mnuItem.OnClick := mnuDeleteItemClick;
mnuItem.Hint := 'Deleta a tag selecionada.';
FPopupMenu.Items.Add(mnuItem);
FCanDragTags := True;
TabStop := True;
FDeleteButtonIcon := TIcon.Create;
end;
procedure TDTagEditor.EditEnter(Sender: TObject);
begin
if FEditPos.Y + FEdit.Height > FScrollInfo.nPos + ClientHeight then
FScrollInfo.nPos := FEditPos.Y + ClientHeight - FEdit.Height;
FixPosAndScrollWindow;
end;
procedure TDTagEditor.EditExit(Sender: TObject);
begin
if FEdit.Text <> '' then
Accept
else
HideEditor;
end;
procedure TDTagEditor.mnuDeleteItemClick(Sender: TObject);
begin
if Sender is TMenuItem then
begin
if Assigned(FBeforeTagRemove) then
FBeforeTagRemove(Self, TMenuItem(Sender).Tag);
FTags.Delete(TMenuItem(Sender).Tag);
if Assigned(FTagRemoved) then
FTagRemoved(Self, TMenuItem(Sender).Tag);
end;
end;
procedure TDTagEditor.TagChange(Sender: TObject);
begin
UpdateMetrics;
Invalidate;
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TDTagEditor.WndProc(var Message: TMessage);
begin
inherited;
case Message.Msg of
WM_LBUTTONDBLCLK:
begin
if csCaptureMouse in ControlStyle then
MouseCapture := True;
if csClickEvents in ControlStyle then
DblClick;
end;
WM_SETFOCUS:
Invalidate;
WM_KILLFOCUS:
begin
if FCaretVisible then
DestroyCaret;
FDragging := false;
Invalidate;
end;
WM_COPY:
Clipboard.AsText := FTags.DelimitedText;
WM_CLEAR:
FTags.Clear;
WM_CUT:
begin
Clipboard.AsText := FTags.DelimitedText;
FTags.DeleteAll;
end;
WM_PASTE:
begin
if Clipboard.HasFormat(CF_TEXT) then
SetPasteText(Clipboard.AsText);
end;
WM_SIZE:
begin
UpdateMetrics;
Invalidate;
Message.result := 0;
end;
WM_VSCROLL:
begin
FScrollInfo.fMask := SIF_ALL;
GetScrollInfo(Handle, SB_VERT, FScrollInfo);
case Message.WParamLo of
SB_TOP:
FScrollInfo.nPos := FScrollInfo.nMin;
SB_BOTTOM:
FScrollInfo.nPos := FScrollInfo.nMax;
SB_PAGEUP:
Dec(FScrollInfo.nPos, FScrollInfo.nPage);
SB_PAGEDOWN:
Inc(FScrollInfo.nPos, FScrollInfo.nPage);
SB_LINEUP:
Dec(FScrollInfo.nPos, FTagHeight);
SB_LINEDOWN:
Inc(FScrollInfo.nPos, FTagHeight);
SB_THUMBTRACK:
FScrollInfo.nPos := FScrollInfo.nTrackPos;
end;
FixPosAndScrollWindow;
Message.result := 0;
end;
end;
end;
procedure TDTagEditor.FixPosAndScrollWindow;
begin
FScrollInfo.fMask := SIF_POS;
SetScrollInfo(Handle, SB_VERT, FScrollInfo, True);
GetScrollInfo(Handle, SB_VERT, FScrollInfo);
if FScrollInfo.nPos <> FPrevScrollPos then
begin
ScrollWindowEx(Handle, 0, FPrevScrollPos - FScrollInfo.nPos,
GetShrunkClientRect(3), GetShrunkClientRect(3), 0, nil, SW_INVALIDATE);
FPrevScrollPos := FScrollInfo.nPos;
Update;
end;
end;
procedure TDTagEditor.UpdateScrollBars;
begin
FScrollInfo.fMask := SIF_RANGE or SIF_PAGE;
FScrollInfo.nMin := 0;
FScrollInfo.nMax := FDesiredHeight - 1;
FScrollInfo.nPage := ClientHeight;
SetScrollInfo(Handle, SB_VERT, FScrollInfo, True);
FixPosAndScrollWindow;
end;
function TDTagEditor.Accept: Boolean;
begin
if (FTags.Count = FMaxTags) and (FMaxTags > 0) then
Exit(false);
Assert(FEdit.Visible);
result := false;
if FTrimInput then
FEdit.Text := Trim(FEdit.Text);
if (FEdit.Text = '') or ((not AllowDuplicates) and
(FTags.IndexOf(FEdit.Text) <> -1)) then
begin
beep;
Exit;
end;
FTags.Add(FEdit.Text);
UpdateMetrics;
result := True;
HideEditor;
if Assigned(FTagAdded) then
FTagAdded(Self);
Invalidate;
end;
procedure TDTagEditor.EditKeyPress(Sender: TObject; var Key: Char);
begin
if (Key = chr(VK_SPACE)) and (FEdit.Text = '') and FNoLeadingSpaceInput then
begin
Key := #0;
Exit;
end;
if ((Key = chr(VK_SPACE)) and FSpaceAccepts) or
((Key = ',') and FCommaAccepts) or ((Key = ';') and FSemicolonAccepts) then
Key := chr(VK_RETURN);
case ord(Key) of
VK_RETURN:
begin
Accept;
ShowEditor;
Key := #0;
end;
VK_BACK:
begin
if (FEdit.Text = '') and (FTags.Count > 0) then
begin
if Assigned(FBeforeTagRemove) then
FBeforeTagRemove(Sender, FTags.Count - 1);
FTags.Delete(FTags.Count - 1);
if Assigned(FTagRemoved) then
FTagRemoved(Sender, FTags.Count - 1);
UpdateMetrics;
Paint;
end;
end;
VK_ESCAPE:
begin
HideEditor;
Self.SetFocus;
Key := #0;
end;
end;
end;
procedure TDTagEditor.DblClick;
begin
if Assigned(FOnDblClick) then
FOnDblClick(Self);
end;
destructor TDTagEditor.Destroy;
begin
FTags.Free;
FTags := nil;
FPopupMenu.Free;
FEdit.Free;
FDeleteButtonIcon.Free;
inherited;
end;
procedure TDTagEditor.HideEditor;
begin
FEdit.Text := '';
FEdit.Hide;
// SetFocus;
end;
procedure TDTagEditor.KeyDown(var Key: word; Shift: TShiftState);
begin
inherited;
case Key of
VK_END:
ShowEditor;
VK_DELETE:
Perform(WM_CLEAR, 0, 0);
VK_INSERT:
Perform(WM_PASTE, 0, 0);
end;
end;
procedure TDTagEditor.KeyPress(var Key: Char);
begin
inherited;
case Key of
^C:
begin
Perform(WM_COPY, 0, 0);
Key := #0;
Exit;
end;
^X:
begin
Perform(WM_CUT, 0, 0);
Key := #0;
Exit;
end;
^V:
begin
Perform(WM_PASTE, 0, 0);
Key := #0;
Exit;
end;
end;
ShowEditor;
FEdit.Perform(WM_CHAR, ord(Key), 0);
end;
procedure TDTagEditor.Loaded;
begin
inherited;
UpdateMetrics;
end;
function TDTagEditor.GetClickInfoAt(X, Y: integer): TClickInfo;
var
i: integer;
begin
result := NOWHERE;
if (X >= FEditPos.X) and (Y >= FEditPos.Y) then
Exit(EDITOR);
for i := 0 to FTags.Count - 1 do
if InRange(X, FLefts[i], FRights[i]) and InRange(Y, FTops[i], FBottoms[i])
then
begin
result := i;
if InRange(X, FCloseBtnLefts[i], FCloseBtnLefts[i] + FCloseBtnWidth) and
InRange(Y, FCloseBtnTops[i], FCloseBtnTops[i] + FActualTagHeight) and
not FShrunk then
result := result or PART_REMOVE_BUTTON;
break;
end;
end;
function TDTagEditor.GetFontEdit: TFont;
begin
result := FEdit.Font;
end;
function TDTagEditor.GetReadOnly: Boolean;
begin
result := FReadOnly;
end;
function TDTagEditor.IsFirstOnRow(TagIndex: integer): Boolean;
begin
result := (TagIndex = 0) or (FTops[TagIndex] > FTops[TagIndex - 1]);
end;
function TDTagEditor.IsLastOnRow(TagIndex: integer): Boolean;
begin
result := (TagIndex = FTags.Count - 1) or
(FTops[TagIndex] < FTops[TagIndex + 1]);
end;
function TDTagEditor.GetSeparatorIndexAt(X, Y: integer): integer;
var
i: integer;
begin
result := FTags.Count;
Y := Max(Y, FSpacing + 1);
for i := FTags.Count - 1 downto 0 do
begin
if Y < FTops[i] then
Continue;
if (IsLastOnRow(i) and (X >= FRights[i])) or
((X < FRights[i]) and (IsFirstOnRow(i) or (FRights[i - 1] < X))) then
begin
result := i;
if (IsLastOnRow(i) and (X >= FRights[i])) then
Inc(result);
Exit;
end;
end;
end;
procedure TDTagEditor.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer);
begin
Inc(Y, FScrollInfo.nPos);
FMouseDownClickInfo := GetClickInfoAt(X, Y);
if GetTagIndex(FMouseDownClickInfo) <> EDITOR then
SetFocus;
end;
procedure TDTagEditor.CreateCaret;
begin
if not FCaretVisible then
FCaretVisible := Windows.CreateCaret(Handle, 0, 0, FActualTagHeight);
end;
procedure TDTagEditor.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style or WS_VSCROLL;
end;
function TDTagEditor.CreateTags(ATagEditor: TDTagEditor): TTags;
begin
result := TTags.Create(ATagEditor, TTagItem);
end;
procedure TDTagEditor.DestroyCaret;
begin
if not FCaretVisible then
Exit;
Windows.DestroyCaret;
FCaretVisible := false;
end;
procedure TDTagEditor.MouseMove(Shift: TShiftState; X, Y: integer);
var
SepIndex: integer;
begin
inherited;
Inc(Y, FScrollInfo.nPos);
if IsKeyDown(VK_LBUTTON) and InRange(GetTagIndex(FMouseDownClickInfo),
TAG_LOW, TAG_HIGH) and (FCanDragTags) then
begin
FDragging := True;
Screen.Cursor := crDrag;
SepIndex := GetSeparatorIndexAt(X, Y);
CreateCaret;
if SepIndex = FTags.Count then
SetCaretPos(FLefts[SepIndex - 1] + FWidths[SepIndex - 1] + FSpacing div 2,
FTops[SepIndex - 1] - FScrollInfo.nPos)
else
SetCaretPos(FLefts[SepIndex] - FSpacing div 2,
FTops[SepIndex] - FScrollInfo.nPos);
ShowCaret(Handle);
Exit;
end;
case GetTagIndex(GetClickInfoAt(X, Y)) of
NOWHERE:
Cursor := crArrow;
EDITOR:
Cursor := crIBeam;
TAG_LOW .. TAG_HIGH:
Cursor := crHandPoint;
end;
end;
procedure TDTagEditor.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: integer);
var
pnt: TPoint;
CanRemove: Boolean;
ClickInfo: TClickInfo;
i: word;
p: cardinal;
SepIndex: integer;
begin
inherited;
Inc(Y, FScrollInfo.nPos);
if FDragging then
begin
DestroyCaret;
FDragging := false;
Screen.Cursor := crDefault;
SepIndex := GetSeparatorIndexAt(X, Y);
if not InRange(SepIndex, GetTagIndex(FMouseDownClickInfo),
GetTagIndex(FMouseDownClickInfo) + 1) then
begin
FTags.Move(GetTagIndex(FMouseDownClickInfo),
SepIndex - IfThen(SepIndex > GetTagIndex(FMouseDownClickInfo), 1, 0));
UpdateMetrics;
Paint;
end;
Exit;
end;
ClickInfo := GetClickInfoAt(X, Y);
if ClickInfo <> FMouseDownClickInfo then
Exit;
i := GetTagIndex(ClickInfo);
p := GetTagPart(ClickInfo);
case i of
EDITOR:
ShowEditor;
NOWHERE:
;
else
case Button of
mbLeft:
begin
case p of
PART_BODY:
if Assigned(FTagClickEvent) then
FTagClickEvent(Self, i, FTags.Items[i].Text);
PART_REMOVE_BUTTON:
begin
if not FDeleteTagButton then
Exit;
if Assigned(FOnRemoveConfirm) then
begin
CanRemove := false;
FOnRemoveConfirm(Self, i, FTags.Items[i].Text, CanRemove);
if not CanRemove then
Exit;
end;
if Assigned(FBeforeTagRemove) then
FBeforeTagRemove(Self, i);
FTags.Delete(i);
if Assigned(FTagRemoved) then
FTagRemoved(Self, i);
UpdateMetrics;
Paint;
end;
end;
end;
mbRight:
begin
FPopupMenu.Items[0].Tag := i;
pnt := ClientToScreen(Point(X, Y));
FPopupMenu.Items[0].Caption := 'Delete tag "' + FTags.Items[i]
.Text + '"';
FPopupMenu.Popup(pnt.X, pnt.Y - FScrollInfo.nPos);
end;
end;
end;
end;
procedure TDTagEditor.UpdateMetrics;
var
i: integer;
X, Y: integer;
MeanWidth: integer;
AdjustedFDesiredHeight: integer;
begin
SetLength(FLefts, FTags.Count);
SetLength(FRights, FTags.Count);
SetLength(FTops, FTags.Count);
SetLength(FBottoms, FTags.Count);
SetLength(FWidths, FTags.Count);
SetLength(FCloseBtnLefts, FTags.Count);
SetLength(FCloseBtnTops, FTags.Count);
FCloseBtnWidth := Canvas.TextWidth('×');
FShrunk := false;
FNumRows := 1;
if FMultiLine then
begin
FActualTagHeight := FTagHeight;
X := FSpacing;
Y := FSpacing;
for i := 0 to FTags.Count - 1 do
begin
FWidths[i] := Canvas.TextWidth(FTags.Items[i].Text +
IfThen(DeleteTagButton, ' ×', '')) + 2 * FSpacing;
FLefts[i] := X;
FRights[i] := X + FWidths[i];
FTops[i] := Y;
FBottoms[i] := Y + FTagHeight;
if X + FWidths[i] + FSpacing > ClientWidth then
begin
X := FSpacing;
Inc(Y, FTagHeight + FSpacing);
Inc(FNumRows);
FLefts[i] := X;
FRights[i] := X + FWidths[i];
FTops[i] := Y;
FBottoms[i] := Y + FTagHeight;
end;
FCloseBtnLefts[i] := X + FWidths[i] - FCloseBtnWidth - FSpacing;
FCloseBtnTops[i] := Y;
Inc(X, FWidths[i] + FSpacing);
end;
end
else
begin
FActualTagHeight := ClientHeight - 2 * FSpacing;
X := FSpacing;
Y := FSpacing;
for i := 0 to FTags.Count - 1 do
begin
FWidths[i] := Canvas.TextWidth(FTags.Items[i].Text +
IfThen(DeleteTagButton, ' ×', '')) + 2 * FSpacing;
FLefts[i] := X;
FRights[i] := X + FWidths[i];
FTops[i] := Y;
FBottoms[i] := Y + FActualTagHeight;
Inc(X, FWidths[i] + FSpacing);
FCloseBtnLefts[i] := FRights[i] - FCloseBtnWidth - FSpacing;
FCloseBtnTops[i] := Y;
end;
FShrunk := X + 64 { FEdit } > ClientWidth;
if FShrunk then
begin
X := FSpacing;
Y := FSpacing;
for i := 0 to FTags.Count - 1 do
begin
FWidths[i] := Canvas.TextWidth(FTags.Items[i].Text) + 2 * FSpacing;
FLefts[i] := X;
FRights[i] := X + FWidths[i];
FTops[i] := Y;
FBottoms[i] := Y + FActualTagHeight;
Inc(X, FWidths[i] + FSpacing);
FCloseBtnLefts[i] := FRights[i] - FCloseBtnWidth - FSpacing;
FCloseBtnTops[i] := Y;
end;
if X + 64 { FEdit } > ClientWidth then
begin
MeanWidth := (ClientWidth - 2 * FSpacing - 64 { FEdit } )
div FTags.Count - FSpacing;
X := FSpacing;
for i := 0 to FTags.Count - 1 do
begin
FWidths[i] := Min(FWidths[i], MeanWidth);
FLefts[i] := X;
FRights[i] := X + FWidths[i];
Inc(X, FWidths[i] + FSpacing);
end;
end;
end;
end;
FEditPos := Point(FSpacing,
FSpacing + (FActualTagHeight - FEdit.Height) div 2);
if FTags.Count > 0 then
FEditPos := Point(FRights[FTags.Count - 1] + FSpacing,
FTops[FTags.Count - 1] + (FActualTagHeight - FEdit.Height) div 2);
if FMultiLine and (FEditPos.X + 64 > ClientWidth) and (FTags.Count > 0) then
begin
FEditPos := Point(FSpacing, FTops[FTags.Count - 1] + FTagHeight + FSpacing +
(FActualTagHeight - FEdit.Height) div 2);
Inc(FNumRows);
end;
FDesiredHeight := FSpacing + FNumRows * (FTagHeight + FSpacing);
AdjustedFDesiredHeight := Min(FDesiredHeight, FMaxHeight);
if FMultiLine and FAutoHeight and (ClientHeight <> AdjustedFDesiredHeight)
then
ClientHeight := AdjustedFDesiredHeight;
UpdateScrollBars;
end;
procedure TDTagEditor.Paint;
var
i: integer;
w: integer;
X, Y: integer;
R: TRect;
S: string;
clip: HRGN;
Rgn: HRGN;
begin
inherited;
Canvas.Brush.Color := FBgColor;
Canvas.Pen.Color := FBorderColor;
Canvas.Rectangle(ClientRect);
Canvas.Font.Assign(Self.Font);
clip := CreateRectRgnIndirect(GetShrunkClientRect((3)));
SelectClipRgn(Canvas.Handle, clip);
DeleteObject(clip);
for i := 0 to FTags.Count - 1 do
begin
X := FLefts[i];
Y := FTops[i] - FScrollInfo.nPos;