TMaskEdit: rename FCursorPos to FCharPos and make it 1-based instead of 0-based. No functional changes.

git-svn-id: trunk@64791 -
This commit is contained in:
bart 2021-03-12 17:36:13 +00:00
parent d4abf907f2
commit 427fdc1588

View File

@ -186,13 +186,13 @@ const
FRealMask : String; // Real mask inserted FRealMask : String; // Real mask inserted
FMask : TInternalMask; // Actual internal mask FMask : TInternalMask; // Actual internal mask
FMaskLength : Integer; // Length of internal mask FMaskLength : Integer; // Length of internal mask
FFirstFreePos : Integer; // First position where user can enter text (it is 1-based, in contrast to FCursorPos. Maybe adjust this? BB) FFirstFreePos : Integer; // First position where user can enter text (it is 1-based)
FMaskSave : Boolean; // Save mask as part of the data FMaskSave : Boolean; // Save mask as part of the data
FTrimType : TMaskEditTrimType; // Trim leading or trailing spaces in GetText FTrimType : TMaskEditTrimType; // Trim leading or trailing spaces in GetText
FSpaceChar : Char; // Char for space (default '_') FSpaceChar : Char; // Char for space (default '_')
FCurrentText : TCaption; // FCurrentText is our backup. See notes above! FCurrentText : TCaption; // FCurrentText is our backup. See notes above!
FTextOnEnter : String; // Text when user enters the control, used for Reset() FTextOnEnter : String; // Text when user enters the control, used for Reset()
FCursorPos : Integer; // Current caret position (so it is zero-based) FCharPos : Integer; // Current character position (1-based)
FChangeAllowed : Boolean; // We do not allow text changes by the OS (cut/clear via context menu) FChangeAllowed : Boolean; // We do not allow text changes by the OS (cut/clear via context menu)
FInitialText : String; // Text set in the formdesigner (must be handled in Loaded) FInitialText : String; // Text set in the formdesigner (must be handled in Loaded)
FInitialMask : String; // EditMask set in the formdesigner (must be handled in Loaded) FInitialMask : String; // EditMask set in the formdesigner (must be handled in Loaded)
@ -813,25 +813,25 @@ begin
//no need to do this when in designmode, it actually looks silly if we do //no need to do this when in designmode, it actually looks silly if we do
if not (csDesigning in ComponentState) then if not (csDesigning in ComponentState) then
begin begin
if FCursorPos < 0 then FCursorPos := 0 if FCharPos < 1 then FCharPos := 1
else if FCursorPos > FMaskLength then FCursorPos := FMaskLength; else if (FCharPos > FMaskLength + 1) then FCharPos := FMaskLength + 1;
if (FCursorPos + 1 > FMaskLength) or not Focused then if (FCharPos > FMaskLength) or not Focused then
SetSel(FCursorPos, FCursorPos) SetSel(FCharPos-1, FCharPos-1)
else else
SetSel(FCursorPos, FCursorPos + 1); SetSel(FCharPos-1, FCharPos);
end; end;
end; end;
//Move to next char, skip any mask-literals //Move to next char, skip any mask-literals
procedure TCustomMaskEdit.SelectNextChar; procedure TCustomMaskEdit.SelectNextChar;
begin begin
if (FCursorPos + 1) > FMaskLength then Exit; if (FCharPos) > FMaskLength then Exit;
Inc(FCursorPos); Inc(FCharPos);
While (FCursorPos + 1 < FMaskLength) and (IsLiteral(FCursorPos + 1)) do While (FCharPos < FMaskLength) and (IsLiteral(FCharPos)) do
begin begin
Inc(FCursorPos); Inc(FCharPos);
end; end;
if IsLiteral(FCursorPos + 1) then Inc(FCursorPos); if IsLiteral(FCharPos) then Inc(FCharPos);
SetCursorPos; SetCursorPos;
end; end;
@ -843,27 +843,27 @@ var
AStop: Integer; AStop: Integer;
begin begin
GetSel(AStart, AStop); GetSel(AStart, AStop);
if (FCursorPos = 0) and (AStop - AStart <= 1) then Exit; if (FCharPos = 1) and (AStop - AStart <= 1) then Exit;
P := FCursorPos; P := FCharPos;
Dec(FCursorPos); Dec(FCharPos);
While (FCursorPos > 0) and IsLiteral(FCursorPos + 1) do While (FCharPos > 1) and IsLiteral(FCharPos) do
begin begin
Dec(FCursorPos); Dec(FCharPos);
end; end;
if (FCursorPos = 0) and (P <> 0) and IsLiteral(FCursorPos + 1) then FCursorPos := P; if (FCharPos = 1) and (P <> 1) and IsLiteral(FCharPos) then FCharPos := P;
SetCursorPos; SetCursorPos;
end; end;
procedure TCustomMaskEdit.SelectFirstChar; procedure TCustomMaskEdit.SelectFirstChar;
begin begin
FCursorPos := 0; FCharPos := 1;
SetCursorPos; SetCursorPos;
end; end;
procedure TCustomMaskEdit.GotoEnd; procedure TCustomMaskEdit.GotoEnd;
begin begin
FCursorPos := FMaskLength; FCharPos := FMaskLength + 1;
SetCursorPos; SetCursorPos;
end; end;
@ -898,7 +898,7 @@ var
P, P2: Integer; P, P2: Integer;
begin begin
if not (Dot in [Period, Comma]) then Exit; if not (Dot in [Period, Comma]) then Exit;
P := MaskPos(Dot, FCursorPos + 1); P := MaskPos(Dot, FCharPos);
HasNextDot := P > 0; HasNextDot := P > 0;
If (Dot = Period) then If (Dot = Period) then
begin begin
@ -918,7 +918,7 @@ begin
CanJump := HasNextDot and (P < FMaskLength) and (not IsLiteral(P+1)); CanJump := HasNextDot and (P < FMaskLength) and (not IsLiteral(P+1));
if CanJump then if CanJump then
begin begin
FCursorPos := P; FCharPos := P+1;
SetCursorPos; SetCursorPos;
end; end;
end; end;
@ -1253,7 +1253,7 @@ Var
S: String; S: String;
i, SelectionStart, SelectionStop: Integer; i, SelectionStart, SelectionStop: Integer;
begin begin
if CanInsertChar(FCursorPos + 1, Ch) then if CanInsertChar(FCharPos, Ch) then
begin begin
S := inherited RealGetText; S := inherited RealGetText;
if HasSelection then if HasSelection then
@ -1264,7 +1264,7 @@ begin
GetSel(SelectionStart, SelectionStop); GetSel(SelectionStart, SelectionStop);
for i := SelectionStart + 1 to SelectionStop do SetCodePoint(S, i, ClearChar(i)); for i := SelectionStart + 1 to SelectionStop do SetCodePoint(S, i, ClearChar(i));
end; end;
SetCodePoint(S, FCursorPos + 1, Ch); SetCodePoint(S, FCharPos, Ch);
RealSetTextWhileMasked(S); RealSetTextWhileMasked(S);
SelectNextChar; SelectNextChar;
end end
@ -1375,13 +1375,13 @@ begin
if HasSelection then if HasSelection then
begin begin
DeleteSelected; DeleteSelected;
if IsLiteral(FCursorPos) then if IsLiteral(FCharPos) then
SelectNextChar; SelectNextChar;
end end
else else
begin begin
//cannot delete beyond length of string //cannot delete beyond length of string
if FCursorPos < FMaskLength then if (FCharPos < FMaskLength + 1) then
begin begin
//This will select the appropriate char in the control //This will select the appropriate char in the control
SetCursorPos; SetCursorPos;
@ -1395,13 +1395,13 @@ begin
if HasExtSelection then if HasExtSelection then
begin begin
DeleteSelected; DeleteSelected;
if IsLiteral(FCursorPos+1) then if IsLiteral(FCharPos) then
SelectNextChar; SelectNextChar;
end end
else else
begin begin
//cannot backspace if we are at beginning of string, or if all chars in front are MaskLiterals //cannot backspace if we are at beginning of string, or if all chars in front are MaskLiterals
if FCursorPos > FFirstFreePos - 1 then if FCharPos > FFirstFreePos then
begin begin
//This will select the previous character //This will select the previous character
//If there are MaskLiterals just in front of the current position, they will be skipped //If there are MaskLiterals just in front of the current position, they will be skipped
@ -1816,7 +1816,7 @@ begin
if IsMasked and (not ReadOnly) then if IsMasked and (not ReadOnly) then
begin begin
RealSetTextWhileMasked(FTextOnEnter); RealSetTextWhileMasked(FTextOnEnter);
FCursorPos := FFirstFreePos-1; FCharPos := FFirstFreePos;
SetCursorPos; SetCursorPos;
end; end;
end; end;
@ -1828,7 +1828,7 @@ begin
if IsMasked then if IsMasked then
begin begin
//debugln('TCustomMaskEdit.DoEnter: FValidationFailed = ',DbgS(FValidationFailed)); //debugln('TCustomMaskEdit.DoEnter: FValidationFailed = ',DbgS(FValidationFailed));
FCursorPos := GetSelStart; FCharPos := GetSelStart + 1;
//Only save FTextOnEnter if validation did not fail in last DoExit that occurred //Only save FTextOnEnter if validation did not fail in last DoExit that occurred
if not FValidationFailed then if not FValidationFailed then
FTextOnEnter := inherited RealGetText FTextOnEnter := inherited RealGetText
@ -1838,11 +1838,11 @@ begin
if (AutoSelect and not (csLButtonDown in ControlState)) then if (AutoSelect and not (csLButtonDown in ControlState)) then
begin begin
SelectAll; SelectAll;
FCursorPos := GetSelStart; FCharPos := GetSelStart + 1;
end end
else else
begin begin
if ((FCursorPos = 0) and (IsLiteral(1))) then if ((FCharPos = 1) and (IsLiteral(1))) then
//On entering select first editable char //On entering select first editable char
SelectNextChar SelectNextChar
else else
@ -1896,7 +1896,7 @@ begin
begin begin
Exit; Exit;
end; end;
FCursorPos := GetSelStart; FCharPos := GetSelStart + 1;
// shift and arrowkey -> old procedure // shift and arrowkey -> old procedure
if (ssShift in Shift) then if (ssShift in Shift) then
begin begin
@ -2049,9 +2049,9 @@ begin
begin begin
Exit; Exit;
end; end;
FCursorPos := GetSelStart; FCharPos := GetSelStart + 1;
//If the cursor is on a MaskLiteral then go to the next writable position if a key is pressed (Delphi compatibility) //If the cursor is on a MaskLiteral then go to the next writable position if a key is pressed (Delphi compatibility)
if IsLiteral(FCursorPos + 1) then if IsLiteral(FCharPos) then
begin begin
SelectNextChar; SelectNextChar;
Key := EmptyStr; Key := EmptyStr;
@ -2060,7 +2060,7 @@ begin
// Insert a char // Insert a char
if not ((Length(Key) = 1) and (Key[1] in [#0..#31])) then if not ((Length(Key) = 1) and (Key[1] in [#0..#31])) then
begin begin
if ((Key = Period) or (Key = Comma)) and not (CanInsertChar(FCursorPos + 1, Key)) then if ((Key = Period) or (Key = Comma)) and not (CanInsertChar(FCharPos, Key)) then
begin//Try to jump to next period or comma, if at all possible begin//Try to jump to next period or comma, if at all possible
JumpToNextDot(Key[1]); JumpToNextDot(Key[1]);
end end
@ -2103,7 +2103,7 @@ begin
inherited MouseUp(Button, Shift, X, Y); inherited MouseUp(Button, Shift, X, Y);
if IsMasked then if IsMasked then
begin begin
FCursorPos := GetSelStart; FCharPos := GetSelStart + 1;
if not HasSelection then SetCursorPos; if not HasSelection then SetCursorPos;
end; end;
end; end;
@ -2146,7 +2146,7 @@ begin
ClipText := ClipBoard.AsText; ClipText := ClipBoard.AsText;
if (Utf8Length(ClipText) > 0) then if (Utf8Length(ClipText) > 0) then
begin begin
P := FCursorPos + 1; P := FCharPos;
DeleteSelected; DeleteSelected;
S := inherited RealGetText; S := inherited RealGetText;
i := 1; i := 1;
@ -2195,7 +2195,7 @@ begin
S := ''; S := '';
for I := 1 To FMaskLength do S := S + ClearChar(I); for I := 1 To FMaskLength do S := S + ClearChar(I);
RealSetTextWhileMasked(S); RealSetTextWhileMasked(S);
FCursorPos := 0; FCharPos := 1;
SetCursorPos; SetCursorPos;
end end
else Inherited Clear; else Inherited Clear;