-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadot.VCL.Tools.pas
1191 lines (1035 loc) · 34.8 KB
/
adot.VCL.Tools.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit adot.VCL.Tools;
{ Definition of classes/record types:
TAppActions = class
Get all "executable" components of form (TMenu/TAction/...).
Used by automated testing framework & quick search window (available by F11).
TCanvasUtils = class
MaxFitLength / BlendRectangle and other graphic functions specific to VCL.
TControlUtils = class
ForEach, FindForm, GetShortCaption and other.
TMDIHelper<T: class> = class
FindMDIChild etc.
TVCLFileUtils = class
CopyFile without locking UI etc.
TVCLStreamUtils = class
Copy streams without locking UI etc.
}
interface
uses
adot.Types,
Winapi.ShellAPI,
Winapi.Windows,
Winapi.Messages,
Vcl.ActnList,
Vcl.ExtCtrls,
Vcl.Forms,
Vcl.Graphics,
Vcl.Menus,
Vcl.Controls,
System.Classes,
System.SysUtils,
System.Math,
System.Character,
System.Generics.Collections,
System.Generics.Defaults,
System.StrUtils,
System.Types,
System.DateUtils,
System.SyncObjs;
type
{
Screen object has event OnActiveControlChange, but it doesn't support multiple handlers.
Chaining of handlers for such event is not safe.
Objects can install handlers in one sequence and then uninstall it in different sequence, restoring
"dead" handlers as active.
To avoid of errors we use list for chaining of handlers.
}
TActiveControlListeners = class
private
class var
FList: TList<TNotifyEvent>;
FOldOnActiveControlChange: TNotifyEvent;
class property List: TList<TNotifyEvent> read FList;
class procedure ActiveControlChange(Data, Sender: TObject); static;
public
class constructor Create;
class destructor Destroy;
class function Add(AHandler: TNotifyEvent): boolean; static;
class function Exists(AHandler: TNotifyEvent): boolean; static;
class function Remove(AHandler: TNotifyEvent): boolean; static;
end;
{ Simple interface to search for MDI form of specific type. Example:
var
MVA: TMVAAvstemmingForm;
begin
MVA := TMDIHelper<TMVAAvstemmingForm>.FindMDIChild;
end; }
{ FindMDIChild etc }
TMDIHelper = class
public
class function FindMDIChild<T: TCustomForm>(ParentForm: TForm; BringToFront : Boolean = True; Sjekk: TFunc<TForm,Boolean> = nil):T; overload; static;
class function FindMDIChild<T: TCustomForm>(BringToFront: Boolean = True; Sjekk: TFunc<TForm,Boolean> = nil): T; overload; static;
class function MdiClientArea(MainForm: TForm; ScreenCoordinates: boolean): TRect; static;
end;
{ Get all "executable" components of form (TMenu/TAction/...).
Used by automated testing framework & quick search window (available by F11). }
TAppActions = class
public
type
TVerb = record
Text: string;
Obj: TComponent;
constructor Create(const AText: string; AObj: TComponent); overload;
constructor Create(const AText: string); overload;
end;
private
class function GetIntInPos(const s: string; n: integer): string; static;
class function FilterAccept(
Caption, SoekeTekst : String;
InnholdSoek : Boolean;
var Tekst : string;
var Indeks : integer;
AddedItems : TDictionary<string, boolean>
): Boolean; static;
class function IsInteger(const s: string): Boolean; static;
class function GetActionObjComparer(const ASoeketekst: string): TDelegatedComparer<TAppActions.TVerb>; static;
public
{ Both chars belong to same type (printable char, digit, space, or special char) }
class function SameCharCategory(const a, b: Char): Boolean; static;
{ TMenu, TButton, ... -> TBasicAction }
class function ComponentAction(aComponent: TComponent): TBasicAction; static;
{ Component (TMenu, TButton, ...) and/or assigned action are visible }
class function ComponentVisible(aComponent: TComponent): Boolean; static;
{ Component (TMenu, TButton, ...) and/or assigned action are enabled }
class function ComponentEnabled(aComponent: TComponent): Boolean; static;
{ Parent of TMenuItem is TMenuITem, parent of TControl is TControl, etc }
class function ComponentParent(aComponent: TComponent): TComponent; static;
{ Component (TMenu, TAction) has assigned OnClick/OnExecute }
class function ComponentExecutable(aComponent: TComponent): Boolean; static;
{ Component is enabled&visible + all parents are visible }
class function ComponentAccessible(aComponent: TComponent): Boolean; static;
{ Execute corresponding OnClick/Action.OnExecute/... for component }
class function ExecuteComponent(aComponent: TComponent): Boolean; static;
{ find all objects which can be executed (actions, menus, ...) }
class function FindFormActions(
AForm : TForm;
AExtraComponents : TStrings;
ASoeketekst : string;
AAutoAppendMenuItemsTag : integer;
AAutoAppendActionsTag : integer;
ATest : TFunc<TComponent,Boolean>
): TList<TAppActions.TVerb>;
class function MixCaptionAndHint(C: TAction): string; static;
end;
{ MaxFitLength / BlendRectangle and other graphic functions specific to VCL }
TCanvasUtils = class
public
class procedure BlendRectangle(Dst: TCanvas; const R: TRect; C: TColor; MixPercent: Byte); static;
class function MaxFitLength(const Src: string; Dst: TCanvas; WidthPixels: integer): integer; overload; static;
class function MaxFitLength(const Src: string; StartIndex,Count: integer; Dst: TCanvas; WidthPixels: integer): integer; overload; static;
end;
{ ForEach, FindForm, GetShortCaption and other }
TControlUtils = class
public
type
TControlBrkProc = reference to procedure(AControl: TControl; var ABreak: boolean);
TControlProc = reference to procedure(AControl: TControl);
class procedure SetEnabled(C: TControl; Value: Boolean; Recursive: Boolean = True); static;
class function FindParentOfClass(AControl: TControl; const AParentClass: TClass): TControl; static;
class function FindForm(C: TControl): TForm; static;
{ "file://c:\aaaa\sss\ddddddd\file.zip" -> "file://c:\aaa...\file.zip" }
class function GetShortCaption(
const Caption : string;
FixedHeadChars : integer;
FixedTailChars : integer;
Dst : TCanvas;
WidthPixels : integer;
Separator : string = '...'): string; static;
{ returns True if enumeration complete, False if canceled }
class function ForEach(AStart: TControl; ACallback: TControlBrkProc): Boolean; overload; static;
class procedure ForEach(AStart: TControl; ACallback: TControlProc); overload; static;
class function GetAll(AStart: TControl): TArray<TControl>; static;
class function FindControlAtPos(AScreenPos: TPoint): TControl; static;
class function FindControlAtMousePos: TControl; static;
class procedure HideBorder(c: TPanel); static;
end;
TWinControlUtils = class
public
class function GetReadableId(C: TWinControl): string; static;
end;
{ Copy streams without locking UI etc }
TVCLStreamUtils = class
public
{ Copy stream functions (from simple to feature-rich):
1. TStreamUtils.Copy:
uses standard Delphi streams, UI will freeze until operation is complete.
2. TVCLStreamUtils.Copy:
uses standard Delphi streams, UI will not freeze. }
class function Copy(Src,Dst: TStream; Count,BufSize: integer; ProgressProc: TCopyStreamProgressProc): int64; overload; static;
class function Copy(Src,Dst: TStream; ProgressProc: TCopyStreamProgressProc): int64; overload; static;
class function Copy(Src,Dst: TStream): int64; overload; static;
end;
{ CopyFile without locking UI etc }
TVCLFileUtils = class
public
{ Copy file functions (from simple to feature-rich):
1. TFileUtils.CopyFile:
uses standard Delphi streams, UI will freeze until operation is complete.
2. TWinFileUtils.CopyFile:
uses Windows function CopyFileEx, UI will freeze until operation is complete.
3. TVCLFileUtils.Copyfile:
uses standard Delphi streams, UI will not freeze.
4. TCopyFileProgressDlg.CopyFile:
uses either Delphi streams or CopyFileEx, UI will not freeze,
progress bar with cancel command will be available for long operations). }
class function CopyFile(
const SrcFileName,DstFileName : string;
out ErrorMessage : string;
ProgressProc : TCopyFileProgressProc): boolean; overload;
class function CopyFile(
const SrcFileName,DstFileName : string;
out ErrorMessage : string): boolean; overload;
end;
{ Debouncing of event with interval N ms means, that time between triggering of SourceEvent will at least N ms.
Most common use case: when user is editing a filter, data should not be updated too often }
TDebouncedEvent = class(TComponent)
private
class var
FInstCount: integer;
var
FSrcEvent: TNotifyEvent;
FMinInterval: integer;
FTimer: TTimer;
FSender: TObject;
FLastCallTime: TDateTime;
procedure OnTimer(Sender: TObject);
procedure EventHandler(Sender: TObject);
function GetDebouncedEvent: TNotifyEvent;
procedure CallSrcHandler(Sender: TObject);
public
constructor Create(AOwner: TComponent; AEventToDebounce: TNotifyEvent; AMinIntervalMs: integer); reintroduce;
destructor Destroy; override;
class function Debounce(AOwner: TComponent; AEventToDebounce: TNotifyEvent; AMinIntervalMs: integer): TNotifyEvent; static;
property DebouncedEvent: TNotifyEvent read GetDebouncedEvent;
property SourceEvent: TNotifyEvent read FSrcEvent;
class property InstanceCount: integer read FInstCount;
end;
TFormUtils = class
public
class procedure Activate(AForm: TForm); static;
end;
implementation
uses
adot.Collections,
adot.Tools,
adot.Tools.IO,
adot.Win.Tools;
{ MDIHelper }
class function TMDIHelper.FindMDIChild<T>(ParentForm: TForm; BringToFront: Boolean = True; Sjekk: TFunc<TForm,Boolean> = nil): T;
var
I: integer;
begin
for I := 0 to ParentForm.MDIChildcount-1 do
if (ParentForm.MDIChildren[I] is T) and (not Assigned(Sjekk) or Sjekk(ParentForm.MDIChildren[I])) then
begin
result := ParentForm.MDIChildren[I] as T;
if BringToFront then
begin
(result as TForm).Show;
(result as TForm).BringToFront;
end;
exit;
end;
result := nil;
end;
class function TMDIHelper.FindMDIChild<T>(BringToFront : Boolean = True; Sjekk: TFunc<TForm,Boolean> = nil):T;
begin
Result := FindMDIChild<T>(Application.MainForm, BringToFront, Sjekk);
end;
class function TMDIHelper.MdiClientArea(MainForm: TForm; ScreenCoordinates: boolean): TRect;
var
P1,P2: TPoint;
begin
if (MainForm = nil) or not Winapi.Windows.GetClientRect(MainForm.ClientHandle, Result) then
Exit(TRect.Empty);
P1 := TPoint.Create(0,0);
Winapi.Windows.ClientToScreen(MainForm.ClientHandle, P1);
if ScreenCoordinates then
result.Offset(P1.X, P1.Y)
else
begin
P2 := TPoint.Create(0,0);
Winapi.Windows.ClientToScreen(MainForm.Handle, P2);
result.Offset(P1.X-P2.X, P1.Y-P2.Y);
end;
end;
{ TVerb }
constructor TAppActions.TVerb.Create(const AText: string; AObj: TComponent);
begin
Text := AText;
Obj := AObj;
end;
constructor TAppActions.TVerb.Create(const AText: string);
begin
Create(AText, nil);
end;
{ TAppActions }
class function TAppActions.ComponentAction (aComponent : TComponent):TBasicAction;
begin
if aComponent is TMenuItem then
Result := TMenuItem(aComponent).Action
else if aComponent is TAction then
Result := TBasicAction(aComponent)
else if aComponent is TControl then
Result := TControl(aComponent).Action
else
Result := nil;
end;
class function TAppActions.ComponentVisible (aComponent : TComponent):Boolean;
begin
if aComponent is TMenuItem then
Result := TMenuItem(aComponent).Visible
else if aComponent is TAction then
Result := TAction(aComponent).Visible
else if aComponent is TControl then
Result := TControl(aComponent).Visible
else
Result := True;
end;
class function TAppActions.ComponentEnabled (aComponent : TComponent):Boolean;
begin
if aComponent is TMenuItem then
Result := TMenuItem(aComponent).Enabled
else if aComponent is TAction then
Result := TAction(aComponent).Enabled
else if aComponent is TControl then
Result := TControl(aComponent).Enabled
else
Result := True;
end;
class function TAppActions.ComponentParent (aComponent : TComponent):TComponent;
begin
if aComponent is TMenuItem then
Result := TMenuItem(aComponent).Parent
else if aComponent is TControl then
Result := TControl(aComponent).Parent
else
Result := nil;
end;
type
TControlHack = class (TControl);
class function TAppActions.ComponentExecutable (aComponent : TComponent) : Boolean;
begin
if aComponent is TMenuItem then
Result := Assigned (TMenuItem(aComponent).OnClick)
else if aComponent is TAction then
Result := Assigned (TAction(aComponent).OnExecute)
else if aComponent is TControl then
Result := Assigned (TControlHack(aComponent).OnClick)
else
Result := False;
end;
class function TAppActions.ComponentAccessible(aComponent: TComponent): Boolean;
var
Cmp:TComponent;
begin
Result:= False;
if aComponent = nil then
Exit;
{* Kontrollerer aComponent (enabled og visible)*}
Cmp:= aComponent;
if ComponentAction(Cmp) <> nil then
ComponentAction(Cmp).Update;
if (not ComponentEnabled(cmp)) or (not ComponentVisible(Cmp)) then
Exit;
{* Kontrollerer parents (visible) *}
Cmp:= ComponentParent (Cmp);
while Cmp <> nil do
begin
if ComponentAction(Cmp) <> nil then
ComponentAction(Cmp).Update;
if not ComponentVisible(Cmp) then
Exit;
Cmp:= ComponentParent (Cmp);
end;
Result:= True;
end;
class function TAppActions.ExecuteComponent(aComponent : TComponent): Boolean;
begin
result := Assigned(aComponent);
if result then
if (aComponent is TMenuItem) and Assigned(TMenuItem(aComponent).OnClick) then
TMenuItem(aComponent).OnClick(aComponent)
else if aComponent is TBasicAction then
TBasicAction(aComponent).Execute
else if aComponent is TControl then
TControlHack (aComponent).Click
else
result := false;
end;
class function TAppActions.SameCharCategory(const a,b: Char): Boolean;
var
f1, f2: integer;
begin
f1 :=
IfThen(a.IsWhiteSpace, 1, 0) +
IfThen(a.IsSeparator, 2, 0) +
IfThen(a.IsDigit, 4, 0) +
IfThen(a.IsLetter, 8, 0) +
IfThen(a.IsPunctuation, 16, 0) +
IfThen(a.IsSymbol, 32, 0);
f2 :=
IfThen(b.IsWhiteSpace, 1, 0) +
IfThen(b.IsSeparator, 2, 0) +
IfThen(b.IsDigit, 4, 0) +
IfThen(b.IsLetter, 8, 0) +
IfThen(b.IsPunctuation, 16, 0) +
IfThen(b.IsSymbol, 32, 0);
result := f1=f2;
end;
class function TAppActions.GetIntInPos(const s: string; n: integer): string;
var
i: Integer;
begin
for i := n to High(s) do
if not s[i].IsDigit then
begin
result := s.Substring(n, i-n);
exit;
end;
result := s.Substring(n);
end;
class function TAppActions.IsInteger(const s: string): Boolean;
var
n: integer;
begin
result := TryStrToInt(Trim(s), n);
end;
class function TAppActions.FilterAccept(
Caption, SoekeTekst : String;
InnholdSoek : Boolean;
var Tekst : string;
var Indeks : integer;
AddedItems : TDictionary<string, boolean>
): Boolean;
var
i: Integer;
RF,Num: Boolean;
b: Boolean;
begin
//Tekstlengde:= Length(Soeketekst);
Tekst:= StripHotKey(Caption);
// Sørger for at vi ikke får like elementer
if AddedItems.TryGetValue(AnsiLowerCase(Tekst), b) then
//if ListBox.Items.IndexOf(Tekst) <> -1 then
begin
Result := False;
Exit;
end;
Indeks:= Pos(AnsiLowerCase(Soeketekst), AnsiLowerCase(Tekst));
// RF:= (Indeks in [3,4]) and ErHeltall(AnsiLowercase(Soeketekst), True);
Num := IsInteger(SoekeTekst);
RF := false;
if Indeks>0 then
if Num then
RF := Pos(AnsiLowerCase('rf-'+Soeketekst), AnsiLowerCase(Tekst))>0
else
if (Pos('rf-',AnsiLowercase(Soeketekst))>0) then
RF := true;
Result :=
InnholdSoek and (Indeks <> 0) //and ((Tekstlengde > 0) or Num)
or ((not Innholdsoek) and (Indeks = 1))
or ((not Innholdsoek) and RF)
or (Indeks>1) and // example: searcrh "kost" should find "10 Kostnader"
((Tekst[Indeks-1]<'0') or (Tekst[Indeks-1]>'9')) and
TryStrToInt(Trim(Copy(Tekst,1,Indeks-1)), i)
or (Soeketekst = '');
end;
class function TAppActions.GetActionObjComparer(const ASoeketekst: string):TDelegatedComparer<TAppActions.TVerb>;
begin
// priority of items ordering:
// - items where Query and Result have same category (digit or nondigit)
// - both items starts from number - compare as numbers
// otherwise compare case insensitively as strings
result := TDelegatedComparer<TAppActions.TVerb>.Create(
function(const A,B: TAppActions.TVerb):Integer
var
c1,c2: Boolean;
n1,n2: integer;
begin
// category
c1 := (ASoeketekst<>'') and (A.Text<>'') and SameCharCategory(A.Text[Low(A.Text)], ASoeketekst[Low(ASoeketekst)]);
c2 := (ASoeketekst<>'') and (B.Text<>'') and SameCharCategory(B.Text[Low(B.Text)], ASoeketekst[Low(ASoeketekst)]);
if (c1 or c2) and (c1<>c2) then
begin
result := IfThen(c1, -1, 1);
exit;
end;
// number
if (A.Text<>'') and (B.Text<>'') and
A.Text[Low(A.Text)].IsDigit and B.Text[Low(B.Text)].IsDigit and
TryStrToInt(GetIntInPos(A.Text,Low(A.Text)), n1) and
TryStrToInt(GetIntInPos(B.Text,Low(B.Text)), n2)
then
result := n1-n2
else
result := AnsiCompareText(A.Text, B.Text);
end
);
end;
class function TAppActions.FindFormActions(
AForm : TForm;
AExtraComponents : TStrings;
ASoeketekst : string;
AAutoAppendMenuItemsTag : integer;
AAutoAppendActionsTag : integer;
ATest : TFunc<TComponent,Boolean>
): TList<TAppActions.TVerb>;
var
AutoFreeCollection: TAutoFreeCollection;
AddedItems: TDictionary<string, boolean>;
InnholdSoek: Boolean;
i: Integer;
Cmp: TComponent;
Tekst: String;
Indeks: Integer;
S: string;
begin
Assert(AForm <> nil, 'HostForm ikke satt under init.');
AddedItems := AutoFreeCollection.Add( TDictionary<string, boolean>.Create );
InnholdSoek := Trim(ASoeketekst).StartsWith('*');
while Trim(ASoeketekst).StartsWith('*') do
ASoeketekst := ASoeketekst.Replace('*', '');
result := TList<TAppActions.TVerb>.Create(GetActionObjComparer(ASoeketekst));
{ manually added components }
if AExtraComponents<>nil then
for i := 0 to AExtraComponents.Count - 1 do
if AExtraComponents.Objects[i]<>nil then
begin
Cmp := TComponent(AExtraComponents.Objects[i]);
if TAppActions.ComponentExecutable(Cmp) and
(TAppActions.ComponentAccessible(Cmp) or Assigned(ATest) and ATest(Cmp)) and
FilterAccept(AExtraComponents[i], ASoeketekst, InnholdSoek, Tekst, Indeks, AddedItems)
then
begin
result.Add(TAppActions.TVerb.Create(Tekst, AExtraComponents.Objects[i] as TComponent));
AddedItems.Add(AnsiLowerCase(Tekst), False);
end;
end;
{ components from HostForm }
if AForm<>nil then
for i:= 0 to AForm.ComponentCount-1 do
begin
Cmp := AForm.components[i];
if (AAutoAppendMenuItemsTag<>0) and (Cmp is TMenuItem) then
if (Cmp.tag = AAutoAppendMenuItemsTag) and
TAppActions.ComponentExecutable(Cmp) and
(
TAppActions.ComponentAccessible(Cmp) or
Assigned(ATest) and ATest(Cmp)
) and
FilterAccept(TMenuItem(Cmp).Caption, ASoeketekst, InnholdSoek, Tekst, Indeks, AddedItems)
then
begin
result.Add(TAppActions.TVerb.Create(Tekst, Cmp));
AddedItems.Add(AnsiLowerCase(Tekst), False);
end;
if (AAutoAppendActionsTag<>0) and (Cmp is TAction) then
if (Cmp.tag = AAutoAppendActionsTag) and
TAppActions.ComponentExecutable(Cmp) and
(
TAppActions.ComponentAccessible(Cmp) or
Assigned(ATest) and ATest(Cmp)
)
then
begin
{Hint/Caption can be modified inside of TAction.Update. That is why we call ComponentAccessible
first (it will trigger TAction.Update) and only after that we read Caption/Hint for processing }
S := MixCaptionAndHint(TAction(Cmp));
if FilterAccept(S, ASoeketekst, InnholdSoek, Tekst, Indeks, AddedItems) then
begin
result.Add(TAppActions.TVerb.Create(Tekst, AForm.components[i]));
AddedItems.Add(AnsiLowerCase(Tekst), False);
end;
end;
end;
Result.Sort;
end;
class function TAppActions.MixCaptionAndHint(C: TAction): string;
var I: integer;
begin
Result := C.Caption;
if TryStrToInt(Result, I) then
Result := Trim(Result + ' ' + C.Hint);
end;
{ TCanvasUtils }
class procedure TCanvasUtils.BlendRectangle(Dst: TCanvas; const R: TRect; C: TColor; MixPercent: Byte);
var
Bmp: TBitmap;
Blend: TBlendFunction;
begin
Bmp := TBitmap.Create;
try
Bmp.Width := 1;
Bmp.Height := 1;
Bmp.Canvas.Pixels[0,0] := C;
Blend.BlendOp := AC_SRC_OVER;
Blend.BlendFlags := 0;
Blend.SourceConstantAlpha := (50 + 255*MixPercent) Div 100;
Blend.AlphaFormat := 0;
AlphaBlend(Dst.Handle, R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top, Bmp.Canvas.Handle, 0, 0, 1, 1, Blend);
finally
Bmp.Free;
end;
end;
class function TCanvasUtils.MaxFitLength(const Src: string; StartIndex, Count: integer; Dst: TCanvas; WidthPixels: integer): integer;
var
l,r,w: integer;
begin
result := 0;
if Count <= 0 then
Exit;
l := StartIndex + 1;
r := StartIndex + Count;
while r-l>=2 do
begin
result := (r+l) shr 1;
w := Dst.TextWidth(Src.Substring(0,result));
if w=WidthPixels then
Break;
if w<WidthPixels then
l := result
else
r := result;
end;
if not (r-l>=2) then
if Dst.TextWidth(Src.Substring(0,r)) <= WidthPixels then
result := r
else
result := l;
if Dst.TextWidth(Src.Substring(0,result)) > WidthPixels then
dec(result);
if result<0 then
result := 0;
end;
class function TCanvasUtils.MaxFitLength(const Src: string; Dst: TCanvas; WidthPixels: integer): integer;
begin
result := MaxFitLength(Src, 0, Length(Src), Dst, WidthPixels);
end;
{ TControlUtils }
class procedure TControlUtils.SetEnabled(C: TControl; Value, Recursive: Boolean);
var
I: Integer;
begin
C.Enabled := Value;
if Recursive and (C is TWinControl) then
for I := 0 to TWinControl(C).ControlCount-1 do
SetEnabled(TWinControl(C).Controls[I], Value, Recursive);
end;
class function TControlUtils.ForEach(AStart: TControl; ACallback: TControlBrkProc): Boolean;
var
i: Integer;
begin
if AStart<>nil then
begin
{ check if canceled by callback function }
result := False;
ACallback(AStart, result);
if result then
Exit(False);
{ check if any subsearch canceled }
if AStart is TWinControl then
for i := TWinControl(AStart).ControlCount-1 downto 0 do
if not ForEach(TWinControl(AStart).Controls[i], ACallback) then
Exit(False);
end;
result := True;
end;
class function TControlUtils.FindControlAtMousePos: TControl;
begin
Result := FindControlAtPos(Mouse.CursorPos);
end;
function FindSubcontrolAtPos(AControl: TControl; AScreenPos, AClientPos: TPoint): TControl;
var
i: Integer;
C: TControl;
begin
Result := nil;
C := AControl;
if (C=nil) or not C.Visible or not TRect.Create(C.Left, C.Top, C.Left+C.Width, C.Top+C.Height).Contains(AClientPos) then
Exit;
Result := AControl;
if AControl is TWinControl then
for i := 0 to TWinControl(AControl).ControlCount-1 do
begin
C := FindSubcontrolAtPos(TWinControl(AControl).Controls[i], AScreenPos, AControl.ScreenToClient(AScreenPos));
if C<>nil then
Result := C;
end;
end;
class function TControlUtils.FindControlAtPos(AScreenPos: TPoint): TControl;
var
i: Integer;
f,m: TForm;
p: TPoint;
r: TRect;
begin
Result := nil;
for i := Screen.FormCount-1 downto 0 do
begin
f := Screen.Forms[i];
if f.Visible and (f.Parent=nil) and (f.FormStyle<>fsMDIChild) and
TRect.Create(f.Left, f.Top, f.Left+f.Width, f.Top+f.Height).Contains(AScreenPos)
then
Result := f;
end;
Result := FindSubcontrolAtPos(Result, AScreenPos, AScreenPos);
if (Result is TForm) and (TForm(Result).ClientHandle<>0) then
begin
WinAPI.Windows.GetWindowRect(TForm(Result).ClientHandle, r);
p := TPoint.Create(AScreenPos.X-r.Left, AScreenPos.Y-r.Top);
m := nil;
for i := TForm(Result).MDIChildCount-1 downto 0 do
begin
f := TForm(Result).MDIChildren[i];
if TRect.Create(f.Left, f.Top, f.Left+f.Width, f.Top+f.Height).Contains(p) then
m := f;
end;
if m<>nil then
Result := FindSubcontrolAtPos(m, AScreenPos, p);
end;
end;
class function TControlUtils.FindForm(C: TControl): TForm;
begin
Result := TForm(FindParentOfClass(C, TForm));
end;
class function TControlUtils.FindParentOfClass(AControl: TControl; const AParentClass: TClass): TControl;
begin
result := AControl;
while (result<>nil) and not (result is AParentClass) do
result := result.Parent;
end;
class procedure TControlUtils.ForEach(AStart: TControl; ACallback: TControlProc);
var
i: Integer;
begin
if AStart=nil then
Exit;
ACallback(AStart);
if AStart is TWinControl then
for i := TWinControl(AStart).ControlCount-1 downto 0 do
ForEach(TWinControl(AStart).Controls[i], ACallback);
end;
class function TControlUtils.GetAll(AStart: TControl): TArray<TControl>;
var
Dst: TArray<TControl>;
Count: integer;
begin
Count := 0;
SetLength(Dst, 1000);
ForEach(AStart,
procedure(C: TControl)
begin
if Count>=Length(Dst) then
SetLength(Dst, Length(Dst)*2);
Dst[Count] := C;
inc(Count);
end);
SetLength(Dst, Count);
Result := Dst;
end;
class function TControlUtils.GetShortCaption(
const Caption : string;
FixedHeadChars : integer;
FixedTailChars : integer;
Dst : TCanvas;
WidthPixels : integer;
Separator : string = '...'): string;
var
L,FixedLen: Integer;
begin
{ full caption is ok }
Result := Caption;
if Dst.TextWidth(Result) <= WidthPixels then
Exit;
{ only fixed part may fit }
Result :=
Caption.Substring(0,FixedHeadChars) +
Separator +
Caption.Substring(Length(Caption)-FixedTailChars,FixedTailChars);
FixedLen := Dst.TextWidth(Result);
if Dst.TextWidth(Result) >= WidthPixels then
Exit;
{ we have to cut some part }
Result := Caption.Substring(FixedHeadChars, Length(Caption) - FixedHeadChars - FixedTailChars);
L := TCanvasUtils.MaxFitLength(Result, Dst, WidthPixels-FixedLen);
result :=
Caption.Substring(0, FixedHeadChars+L) +
Separator +
Caption.Substring(Length(Caption)-FixedTailChars,FixedTailChars);
end;
class procedure TControlUtils.HideBorder(c: TPanel);
begin
c.BevelInner := bvNone;
c.BevelOuter := bvNone;
c.BevelKind := bkNone;
c.BorderStyle := bsNone;
end;
type
TCopyStreamsForm = class(TForm)
protected
FSrc, FDst: TStream;
FCount, FBufSize: integer;
FProgressProc: TCopyStreamProgressProc;
FLocked: boolean;
FTransferred: int64;
procedure FormShow(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure CreateParams(var Params:TCreateParams); override;
public
constructor Create(Src, Dst: TStream; Count, BufSize: integer; ProgressProc: TCopyStreamProgressProc); reintroduce;
property Transferred: int64 read FTransferred;
end;
{ TCopyStreamsForm }
constructor TCopyStreamsForm.Create(Src, Dst: TStream; Count, BufSize: integer; ProgressProc: TCopyStreamProgressProc);
begin
inherited CreateNew(nil);
{ form properties (we will try to make the form invisible/transparent) }
BorderStyle := bsNone;
Left := 0;
Top := 0;
Width := 0;
Height := 0;
OnShow := FormShow;
OnCloseQuery := FormCloseQuery;
FSrc := Src;
FDst := Dst;
FCount := Count;
FBufSize := BufSize;
FProgressProc := ProgressProc;
FLocked := True;
end;
procedure TCopyStreamsForm.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := WS_EX_TRANSPARENT or WS_EX_TOPMOST;
end;
procedure TCopyStreamsForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := not FLocked;
end;
procedure TCopyStreamsForm.FormShow(Sender: TObject);
begin
FLocked := True;
try
FTransferred := TStreamUtils.Copy(FSrc, FDst, FCount, FBufSize,
procedure(const Transferred: int64; var Cancel: boolean)
begin
{ user defined callback }
if Assigned(FProgressProc) then
FProgressProc(Transferred, Cancel);
{ to keep ui alive ()}
Application.ProcessMessages;
end);
finally
FLocked := False;
ModalResult := mrOk;
{ .Close mwthod will not close the form from FormShow, so we send wm_close message instead }
PostMessage(Handle, wm_close,0,0);
end;
end;
{ TVCLStreamUtils }
class function TVCLStreamUtils.Copy(Src, Dst: TStream; Count, BufSize: integer; ProgressProc: TCopyStreamProgressProc): int64;
var
LockUIForm: TCopyStreamsForm;
begin
LockUIForm := TCopyStreamsForm.Create(Src, Dst, Count, BufSize, ProgressProc);
try
LockUIForm.ShowModal;
result := LockUIForm.Transferred;
finally
LockUIForm.Free;
end;
end;
class function TVCLStreamUtils.Copy(Src, Dst: TStream; ProgressProc: TCopyStreamProgressProc): int64;
begin
result := Copy(Src, Dst, 0,0,ProgressProc);
end;
class function TVCLStreamUtils.Copy(Src, Dst: TStream): int64;
begin
result := Copy(Src, Dst, 0,0,nil);
end;
{ TVCLFileUtils }
class function TVCLFileUtils.CopyFile(
const SrcFileName, DstFileName : string;
out ErrorMessage : string;
ProgressProc : TCopyFileProgressProc): boolean;
var
AutoFreeCollection: TAutoFreeCollection;