Repository navigation
Expand file tree
/
Copy pathSCMaskEdit.pas
More file actions
1672 lines (1430 loc) · 40.3 KB
/
Copy pathSCMaskEdit.pas
File metadata and controls
1672 lines (1430 loc) · 40.3 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
{*******************************************************}
{ }
{ CA SweetControls Component Library }
{ }
{ Copyright (c) 1996,2004 CodeAccelerate Corporation }
{ }
{*******************************************************}
unit SCMaskEdit;
{$I SweetControls.inc}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SCConsts, SCResStrs, SCCommon, SCControl, SCEdits, ImgList, Clipbrd;
type
TSCMaskType = (scmtNone, scmtLiteral, scmtAnyChar, scmtAnyCharOpt,
scmtAlpha, scmtAlphaOpt, scmtAlphaNumber, scmtAlphaNumberOpt,
scmtNumeric, scmtNumericOpt, scmtNumericOrSignOpt, scmtDateSeparator,
scmtTimeSeparator, scmtFieldSeparator, scmtReverse, scmtUpperCase,
scmtLowerCase);
TSCMaskCase = (scmcAny, scmcUpperCase, scmcLowerCase);
ESCMaskEditError = class(Exception);
TSCMaskSheme = record
MaskType: TSCMaskType;
MaskCase: TSCMaskCase;
Literal : Char;
LeadingBlanks: Boolean;
end;
PSCMaskSheme = ^TSCMaskSheme;
TSCCustomMaskEdit = class(TSCCustomEdit)
private
FEditMask: String;
FShemeList: TList;
FInUndo: Boolean;
FSettingMask: Integer;
FRaiseMaskError: Boolean;
function GetBlankChar: Char;
procedure SetBlankChar(Value: Char);
procedure SetEditMask(const Value: String);
function GetMasked: Boolean;
function GetSaveLiterals: Boolean;
procedure SetSaveLiterals(Value: Boolean);
function GetSettingMask: Boolean;
procedure SwitchPos(var P1, P2: Integer);
procedure CMWantSpecialKey(var Message: TCMWantSpecialKey); message CM_WANTSPECIALKEY;
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
protected
procedure SetText(Value: TCaption); override;
procedure SetMaxLength(Value: Integer); override;
function AllowSelectionDrag: Boolean; override;
procedure CheckKeyDown(var Key: Word; Shift: TShiftState); dynamic;
procedure SetCursorPos(Pt: Integer; Prev: Boolean = False); dynamic;
procedure DoArrowMove(var Key: Word; Shift: TShiftState); dynamic;
procedure DeleteChar(Prev: Boolean); override;
procedure DeleteText(Prev: Boolean); override;
procedure InsertChar(Ch: Char); override;
procedure OverwriteChar(Ch: Char); override;
procedure InsertText(AText: String); override;
function AutoCompleteText: String; override;
procedure Reset; virtual;
procedure ValidateEdit; override;
function CanRaiseMaskError: Boolean; virtual;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
function FocusInvalidChar: Boolean;
procedure ValidateError; virtual;
procedure MaskChanged; dynamic;
function IsMaskStored: Boolean; virtual;
property AutoComplete default False;
property HideSelection default True;
property EditMask: String read FEditMask write SetEditMask stored IsMaskStored;
property BlankChar: Char read GetBlankChar write SetBlankChar;
property SaveLiterals: Boolean read GetSaveLiterals write SetSaveLiterals;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Undo; override;
procedure Redo; override;
procedure ClearSelection; override;
function ShemeCount: Integer;
function GetSheme(Index: Integer): TSCMaskSheme;
function ConvertToValidChar(C: Char; Index: Integer): Char;
function IsValidText(S: string; AcceptBlank: Boolean = True): Boolean;
function IsValidChar(C: Char; Index: Integer; AcceptBlank: Boolean = True): Boolean;
property IsMasked: Boolean read GetMasked;
property Text;
property RaiseMaskError: Boolean read FRaiseMaskError write FRaiseMaskError default True;
property SettingMask: Boolean read GetSettingMask;
end;
TSCMaskEdit = class(TSCCustomMaskEdit)
public
property CaretPos;
property SelLength;
property SelStart;
property SelText;
property BlankChar;
property SaveLiterals;
published
property Align;
property Alignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BiDiMode;
property BorderProps;
property CanSelect;
property CharCase;
property Checkbox;
property Color;
property Constraints;
property Description;
property DescriptionColor;
property DescriptionMode;
property DisabledColor;
property DisabledTextColor;
property DragCursor;
property DragKind;
property DragMode;
property EditCursor;
property EditMask;
property Enabled;
property EnterAsTab;
property FocusColor;
property FocusTextColor;
property Font;
property HideSelection;
property HideSelectionColor;
property HideSelectionTextColor;
property HighlightColor;
property HighlightTextColor;
property ImageIndex;
property Images;
property ImeMode;
property ImeName;
property LineSpace;
property MaxLength;
{$IFDEF SC_DELPHI2009_EARLY}
property OEMConvert;
{$ENDIF}
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PasswordImage;
property PasswordStyle;
property Picture;
property PictureOrient;
property PictureIndent;
property PictureTopIndent;
property PictureTransparent;
property PopupMenu;
property RaiseMaskError;
property ReadOnly;
property ShowHint;
property ShowPicture;
property TabOrder;
property TabStop;
property Text;
property Transparent;
property UseCaret;
property UseImage;
property UseUndo;
property UndoLimit;
property Visible;
property OnCaretMove;
property OnCanResize;
property OnChange;
property OnCheckboxChange;
property OnClick;
property OnClipCut;
property OnClipCopy;
property OnClipPaste;
property OnConstrainedResize;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnFocusChanged;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
end;
TSCShemeList = class(TList)
private
FMask: String;
FMaskText: String;
FBlankChar: Char;
FSaveLiterals: Boolean;
procedure SetBlankChar(const Value: Char);
procedure SetMask(const Value: String);
function GetSheme(Index: Integer): PSCMaskSheme;
procedure SetSaveLiterals(Value: Boolean);
function AddSheme: PSCMaskSheme;
procedure RefreshMask;
function CheckSaveLiteralsAndBlankChar(const S: string;
var DoSaveLiterals: Boolean; var ABlankChar: Char): string;
procedure ResetSheme(P: PSCMaskSheme);
function ShemeToMaskText: string;
public
constructor Create; virtual;
procedure Clear; override;
function ConvertToValidChar(C: Char; ShemePos: Integer): Char;
function IsValidText(S: string; AcceptBlank: Boolean = True): Boolean;
function IsValidChar(C: Char; ShemePos: Integer; AcceptBlank: Boolean = True): Boolean;
function MaskTypeToStr(M: TSCMaskType; C: Char): string;
function CharToMaskType(C: Char): TSCMaskType;
function MaskLength: Integer;
function ArrangeMaskedText(S: string): string;
function ApplyMaskToText(S: string): string;
function GetEditMask(DefBlank: Boolean): string;
property BlankChar: Char read FBlankChar write SetBlankChar;
property Mask: string read FMask write SetMask;
property MaskText: string read FMaskText;
property Sheme[Index: Integer]: PSCMaskSheme read GetSheme;
property SaveLiterals: Boolean read FSaveLiterals write SetSaveLiterals;
end;
const
scMskDefaultBlank = '_';
scMskFieldSeparator = ';';
scMskLitNoSave = '0';
scMskReverse = '!'; { removes leading blanks if true, else trailing blanks}
scMskUpperCase = '>'; { all chars that follow to upper case }
scMskLowerCase = '<'; { all chars that follow to lower case }
{ '<>' means remove casing directive }
scMskLiteral = '\'; { char that immediately follows is a literal }
scMskAlpha = 'L'; { in US = A-Z,a-z }
scMskAlphaOpt = 'l';
scMskAlphaNum = 'A'; { in US = A-Z,a-z,0-9 }
scMskAlphaNumOpt = 'a';
scMskAscii = 'C'; { any character}
scMskAsciiOpt = 'c';
scMskNumeric = '0'; { 0-9, no plus or minus }
scMskNumericOpt = '9';
scMskNumOrSign = '#'; { 0-9, plus and minus }
{ intl literals }
scMskTimeSeparator = ':';
scMskDateSeparator = '/';
implementation
{ TSCShemeList }
function TSCShemeList.AddSheme: PSCMaskSheme;
begin
New(Result);
Add(Result);
ResetSheme(Result);
end;
function TSCShemeList.ArrangeMaskedText(S: string): string;
var
C: Char;
I: Integer;
begin
if Count = 0 then
Result := S
else begin
if S = '' then
begin
Result := FMaskText;
Exit;
end;
Result := '';
for I := 0 to Count - 1 do
begin
C := #0;
if I + 1 <= Length(S) then
C := S[I + 1];
Result := Result + ConvertToValidChar(C, I);
end;
end;
end;
function TSCShemeList.CharToMaskType(C: Char): TSCMaskType;
begin
Result := scmtNone;
if C = scMskFieldSeparator then // ';'
Result := scmtFieldSeparator
else if C = scMskReverse then // '!'
Result := scmtReverse
else if C = scMskUpperCase then // '>'
Result := scmtUpperCase
else if C = scMskLowerCase then // '<'
Result := scmtLowerCase
else if C = scMskLiteral then // '\'
Result := scmtLiteral
else if C = scMskAlpha then // 'L'
Result := scmtAlpha
else if C = scMskAlphaOpt then // 'l'
Result := scmtAlphaOpt
else if C = scMskAlphaNum then // 'A'
Result := scmtAlphaNumber
else if C = scMskAlphaNumOpt then // 'a'
Result := scmtAlphaNumberOpt
else if C = scMskAscii then // 'C'
Result := scmtAnyChar
else if C = scMskAsciiOpt then // 'c'
Result := scmtAnyCharOpt
else if C = scMskNumeric then // '0'
Result := scmtNumeric
else if C = scMskNumericOpt then // '9'
Result := scmtNumericOpt
else if C = scMskNumOrSign then // '#'
Result := scmtNumericOrSignOpt
else if C = scMskTimeSeparator then // ':'
Result := scmtTimeSeparator
else if C = scMskDateSeparator then // ':'
Result := scmtDateSeparator;
end;
procedure TSCShemeList.Clear;
var
I: Integer;
P: PSCMaskSheme;
begin
for I := 0 to Count-1 do
begin
P := Items[I];
Dispose(P);
end;
inherited Clear;
end;
constructor TSCShemeList.Create;
begin
inherited Create;
FBlankChar := scMskDefaultBlank;
FSaveLiterals := True;
FMaskText := '';
end;
function TSCShemeList.CheckSaveLiteralsAndBlankChar(const S: string;
var DoSaveLiterals: Boolean; var ABlankChar: Char): string;
var
I: Integer;
BlankPos, LitPos: Integer;
begin
ABlankChar := scMskDefaultBlank;
DoSaveLiterals := True;
Result := S;
if Length(Result) >= 4 then
begin
BlankPos := -1;
LitPos := -1;
I := Length(Result);
while I > 0 do
begin
if CharToMaskType(Result[I]) = scmtFieldSeparator then
begin
if BlankPos < 0 then
BlankPos := I
else
LitPos := I;
end;
Dec(I);
end;
if LitPos < 0 then
begin
LitPos := BlankPos;
BlankPos := -1;
end;
if LitPos > 0 then
begin
DoSaveLiterals := not ((LitPos < Length(Result)) and
(Result[LitPos + 1] = scMskLitNoSave));
System.Delete(Result, LitPos, 2);
Dec(BlankPos, 2);
if (BlankPos > 0) and (BlankPos <= Length(Result)) then
begin
ABlankChar := #0;
if BlankPos < Length(Result) then
ABlankChar := Result[BlankPos + 1];
if ABlankChar = #0 then
ABlankChar := scMskDefaultBlank;
System.Delete(Result, BlankPos, 2);
end;
end;
end;
end;
function TSCShemeList.GetSheme(Index: Integer): PSCMaskSheme;
begin
Result := nil;
if (Index > -1) and (Index < Count) then
Result := Items[Index];
end;
function TSCShemeList.MaskLength: Integer;
begin
Result := Length(FMaskText);
end;
procedure TSCShemeList.ResetSheme(P: PSCMaskSheme);
begin
if P <> nil then
begin
P^.MaskType := scmtNone;
P^.MaskCase := scmcAny;
P^.Literal := #0;
P^.LeadingBlanks := False;
end;
end;
procedure TSCShemeList.SetBlankChar(const Value: Char);
begin
FBlankChar := Value;
RefreshMask;
end;
procedure TSCShemeList.SetMask(const Value: String);
var
I: Integer;
P: PSCMaskSheme;
S: String;
LeadingBlanks: Boolean;
OldMask, MaskType: TSCMaskType;
MaskCase: TSCMaskCase;
begin
if Value = FMask then
Exit;
FMask := Value;
Clear;
FMaskText := '';
FBlankChar := scMskDefaultBlank;
FSaveLiterals := True;
if Value = '' then
Exit;
S := CheckSaveLiteralsAndBlankChar(Value, FSaveLiterals, FBlankChar);
LeadingBlanks := False;
MaskType := scmtNone;
MaskCase := scmcAny;
P := nil;
I := 0;
while I < Length(S) do
begin
Inc(I);
if MaskType = scmtLiteral then
begin
MaskType := scmtNone;
P := AddSheme;
P^.MaskType := scmtLiteral;
P^.MaskCase := MaskCase;
P^.Literal := S[I];
P^.LeadingBlanks := LeadingBlanks;
LeadingBlanks:= False;
Continue;
end;
OldMask := MaskType;
MaskType := CharToMaskType(S[I]);
case MaskType of
scmtReverse: // '!'
begin
LeadingBlanks := True;
end;
scmtUpperCase: // '>'
begin
MaskCase := scmcUpperCase;
if OldMask = scmtLowerCase then
MaskCase := scmcAny;
end;
scmtLowerCase: // '<'
begin
MaskCase := scmcLowerCase;
end;
scmtLiteral: // '\'
begin
MaskType := scmtNone;
if (P = nil) or (P^.MaskType <> scmtLiteral) or
(P^.Literal <> scMskLiteral) then
MaskType := scmtLiteral;
end;
else begin
P := AddSheme;
P^.MaskType := MaskType;
P^.MaskCase := MaskCase;
P^.LeadingBlanks := LeadingBlanks;
LeadingBlanks:= False;
if MaskType in [scmtNone, scmtFieldSeparator] then
P^.Literal := S[I]
else if MaskType = scmtDateSeparator then
P^.Literal := {$IFDEF SC_DELPHI10_0_UP}FormatSettings.{$ENDIF}DateSeparator
else if MaskType = scmtTimeSeparator then
P^.Literal := {$IFDEF SC_DELPHI10_0_UP}FormatSettings.{$ENDIF}TimeSeparator;
end;
end;
end;
FMaskText := ShemeToMaskText;
end;
procedure TSCShemeList.SetSaveLiterals(Value: Boolean);
begin
FSaveLiterals := Value;
RefreshMask;
end;
procedure TSCShemeList.RefreshMask;
var
Bc: Char;
Sl: Boolean;
begin
FMask := CheckSaveLiteralsAndBlankChar(FMask, Sl, Bc);
FMask := FMask + ';' + IntToStr(Integer(FSaveLiterals));
if FBlankChar <> #0 then
FMask := FMask + ';' + FBlankChar;
end;
function TSCShemeList.ShemeToMaskText: string;
var
I: Integer;
P: PSCMaskSheme;
C, Blank: Char;
begin
Result := '';
Blank := BlankChar;
for I := 0 to Count-1 do
begin
P := Items[I];
C := #0;
if P^.MaskType in [scmtNone, scmtFieldSeparator,
scmtLiteral, scmtDateSeparator, scmtTimeSeparator] then
C := P^.Literal;
if C = #0 then
C := Blank;
Result := Result + C;
end;
end;
function TSCShemeList.IsValidChar(C: Char; ShemePos: Integer;
AcceptBlank: Boolean): Boolean;
var
P: PSCMaskSheme;
begin
Result := False;
if (ShemePos > -1) and (ShemePos < Count) then
begin
P := Items[ShemePos];
case P^.MaskType of
scmtNone, scmtLiteral:
Result := C = P^.Literal;
scmtAlpha, scmtAlphaOpt:
Result := (AcceptBlank and (C = BlankChar)) or
(C in ['A'..'Z']) or (C in ['a'..'z']);
scmtAlphaNumber, scmtAlphaNumberOpt:
Result := (AcceptBlank and (C = BlankChar)) or
(C in ['A'..'Z']) or (C in ['a'..'z']) or (C in ['0'..'9']);
scmtNumeric, scmtNumericOpt:
Result := (AcceptBlank and (C = BlankChar)) or
(C in ['0'..'9']);
scmtNumericOrSignOpt:
Result := (AcceptBlank and (C = BlankChar)) or
(C = '-') or (C = '+') or (C in ['0'..'9']);
scmtDateSeparator:
Result := C = {$IFDEF SC_DELPHI10_0_UP}FormatSettings.{$ENDIF}DateSeparator;
scmtTimeSeparator:
Result := C = {$IFDEF SC_DELPHI10_0_UP}FormatSettings.{$ENDIF}TimeSeparator;
end;
end;
end;
function TSCShemeList.ConvertToValidChar(C: Char; ShemePos: Integer): Char;
var
P: PSCMaskSheme;
begin
Result := #0;
if (ShemePos > -1) and (ShemePos < Count) then
begin
P := Items[ShemePos];
Result := C;
case P^.MaskType of
scmtNone, scmtLiteral:
Result := P^.Literal;
scmtFieldSeparator:
Result := scMskFieldSeparator;
scmtAlpha, scmtAlphaOpt:
if not ((C = BlankChar) or (C in ['A'..'Z']) or (C in ['a'..'z'])) then
Result := #0;
scmtAlphaNumber, scmtAlphaNumberOpt:
if not ((C = BlankChar) or (C in ['A'..'Z']) or (C in ['a'..'z']) or (C in ['0'..'9'])) then
Result := #0;
scmtNumeric, scmtNumericOpt:
if not ((C = BlankChar) or (C in ['0'..'9'])) then
Result := #0;
scmtNumericOrSignOpt:
if not ((C = BlankChar) or (C = '-') or (C = '+') or (C in ['0'..'9'])) then
Result := #0;
scmtDateSeparator:
Result := {$IFDEF SC_DELPHI10_0_UP}FormatSettings.{$ENDIF}DateSeparator;
scmtTimeSeparator:
Result := {$IFDEF SC_DELPHI10_0_UP}FormatSettings.{$ENDIF}TimeSeparator;
end;
if Result = #0 then
Result := BlankChar;
if P^.MaskCase = scmcUpperCase then
Result := UpCase(Result)
else if (P^.MaskCase = scmcLowerCase) and
(Result >= 'A') and (Result <= 'Z') then
Result := Char(Ord(Result) + 32);
end;
end;
function TSCShemeList.IsValidText(S: string; AcceptBlank: Boolean): Boolean;
var
I: Integer;
begin
Result := False;
if (S = '') or (Count = 0) then
Result := True
else
if Length(S) = Count then
begin
if S <> FMaskText then
for I := 0 to Count-1 do
if not IsValidChar(S[I + 1], I, AcceptBlank) then
Exit;
Result := True;
end;
end;
function TSCShemeList.GetEditMask(DefBlank: Boolean): string;
var
I: Integer;
P: PSCMaskSheme;
begin
Result := '';
for I := 0 to Count-1 do
begin
P := Items[I];
if P^.LeadingBlanks then
Result := Result + '!';
Result := Result + MaskTypeToStr(P^.MaskType, P^.Literal);
end;
if FSaveLiterals then
Result := Result + ';1'
else if (FBlankChar <> #0) or DefBlank then
Result := Result + ';0';
if FBlankChar <> #0 then
Result := Result + ';' + FBlankChar
else if DefBlank then
Result := Result + ';' + scMskDefaultBlank;
end;
function TSCShemeList.MaskTypeToStr(M: TSCMaskType; C: Char): string;
begin
Result := C;
if M = scmtFieldSeparator then // ';'
Result := scMskFieldSeparator
else if M = scmtReverse then // '!'
Result := scMskReverse
else if M = scmtUpperCase then // '>'
Result := scMskUpperCase
else if M = scmtLowerCase then // '<'
Result := scMskLowerCase
else if M = scmtLiteral then // '\'
Result := scMskLiteral + C
else if M = scmtAlpha then // 'L'
Result := scMskAlpha
else if M = scmtAlphaOpt then // 'l'
Result := scMskAlphaOpt
else if M = scmtAlphaNumber then // 'A'
Result := scMskAlphaNum
else if M = scmtAlphaNumberOpt then // 'a'
Result := scMskAlphaNumOpt
else if M = scmtAnyChar then // 'C'
Result := scMskAscii
else if M = scmtAnyCharOpt then // 'c'
Result := scMskAsciiOpt
else if M = scmtNumeric then // '0'
Result := scMskNumeric
else if M = scmtNumericOpt then // '9'
Result := scMskNumericOpt
else if M = scmtNumericOrSignOpt then // '#'
Result := scMskNumOrSign
else if M = scmtTimeSeparator then // ':'
Result := scMskTimeSeparator
else if M = scmtDateSeparator then // ':'
Result := scMskDateSeparator;
end;
function TSCShemeList.ApplyMaskToText(S: string): string;
var
C: Char;
I, J: Integer;
P: PSCMaskSheme;
begin
Result := '';
I := 0; J := 1;
while I < Count do
begin
P := Items[I];
Inc(I);
C := #0;
if P^.MaskType in [scmtNone, scmtFieldSeparator,
scmtLiteral, scmtDateSeparator, scmtTimeSeparator] then
C := P^.Literal;
if (C = #0) and (J <= Length(S)) then
begin
C := S[J];
Inc(J);
end;
if C <> #0 then
Result := Result + C;
end;
end;
{ TSCCustomMaskEdit }
function TSCCustomMaskEdit.AllowSelectionDrag: Boolean;
begin
Result := FEditMask = '';
end;
procedure TSCCustomMaskEdit.Assign(Source: TPersistent);
begin
if Source is TSCCustomMaskEdit then
begin
with TSCCustomMaskEdit(Source) do
begin
Self.EditMask := EditMask;
end;
end;
inherited Assign(Source);
end;
function TSCCustomMaskEdit.AutoCompleteText: String;
begin
Result := '';
end;
function TSCCustomMaskEdit.CanRaiseMaskError: Boolean;
begin
Result := True;
end;
procedure TSCCustomMaskEdit.CheckKeyDown(var Key: Word;
Shift: TShiftState);
begin
if IsMasked and (Key <> 0) then
begin
//
end;
end;
procedure TSCCustomMaskEdit.ClearSelection;
var
S: String;
C, Blank: Char;
P: PSCMaskSheme;
I, Start, Stop: Integer;
begin
if FInUndo or not IsMasked then
inherited ClearSelection
else
if HasSelection then
begin
Stop := CaretPos.x;
Start := SelStart.x;
if Start > Stop then
SwitchPos(Start, Stop);
S := '';
Blank := TSCShemeList(FShemeList).BlankChar;
if Stop > FShemeList.Count-1 then
Stop := FShemeList.Count-1;
for I := Start to Stop do
begin
C := Blank;
P := TSCShemeList(FShemeList).GetSheme(I);
if (P <> nil) and (P^.Literal <> #0) and
(P^.MaskType in [scmtNone, scmtLiteral,
scmtFieldSeparator, scmtDateSeparator, scmtTimeSeparator]) then
C := P^.Literal;
S := S + C;
end;
CatchUndo;
try
inherited ClearSelection;
inherited InsertText(S);
SetCursorPos(Start);
finally
ReleaseUndoCatch;
end;
end;
end;
procedure TSCCustomMaskEdit.CMWantSpecialKey(var Message: TCMWantSpecialKey);
begin
inherited;
if (Message.CharCode = VK_ESCAPE) and IsMasked and Modified then
Message.Result := 1;
end;
function TSCCustomMaskEdit.ConvertToValidChar(C: Char; Index: Integer): Char;
begin
Result := C;
if IsMasked then
Result := TSCShemeList(FShemeList).ConvertToValidChar(C, Index);
end;
constructor TSCCustomMaskEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
AutoComplete := False;
FShemeList := TSCShemeList.Create;
HideSelection := True;
FRaiseMaskError := True;
end;
procedure TSCCustomMaskEdit.DeleteChar(Prev: Boolean);
var
C: Char;
P: PSCMaskSheme;
SelLen, Start, Stop: Integer;
begin
if ReadOnly then
Exit;
SelLen := SelLength;
if FInUndo or not IsMasked or (SelLen > 1) then
inherited DeleteChar(Prev)
else begin
Start := SelStart.X;
Stop := CaretPos.X;
if Start > Stop then
SwitchPos(Start, Stop);
if (SelLen = 0) and (Prev and (Start = 0)) or
((Start = MaxLength) and not Prev) then
Exit;
if Prev and (SelLen = 0) then
Dec(Start);
with TSCShemeList(FShemeList) do
begin
C := BlankChar;
P := Items[Start];
end;
if (P <> nil) and (P^.Literal <> #0) and
(P^.MaskType in [scmtNone, scmtLiteral,
scmtFieldSeparator, scmtDateSeparator, scmtTimeSeparator]) then
C := P^.Literal;
CatchUndo;
try
inherited DeleteChar(Prev);
if SelLen = 0 then
inherited InsertChar(C)
else
if Prev then
Dec(Start);
SetCursorPos(Start, Prev);
finally
ReleaseUndoCatch;
end;
end;
end;
procedure TSCCustomMaskEdit.DeleteText(Prev: Boolean);
begin
if not (SettingMask or IsMasked) then
inherited DeleteText(Prev)
else
DeleteChar(Prev);
end;
destructor TSCCustomMaskEdit.Destroy;
begin
FShemeList.Free;
inherited Destroy;
end;
procedure TSCCustomMaskEdit.DoArrowMove(var Key: Word; Shift: TShiftState);
var
P: PSCMaskSheme;
Start, Stop: Integer;
begin
if IsMasked and (Key <> 0) and not (ssAlt in Shift) then
begin
Stop := CaretPos.X;
Start := SelStart.X;
case Key of
VK_LEFT, VK_RIGHT:
begin
if Stop = MaxLength then
begin
if Start = Stop - 1 then
begin
P := TSCShemeList(FShemeList).Items[Start];
if (P <> nil) and (P^.Literal <> #0) and
(P^.MaskType in [scmtNone, scmtLiteral,