SpinEx: make it easier to override what is allowed to type in the control.

git-svn-id: trunk@63804 -
This commit is contained in:
bart 2020-08-22 16:23:16 +00:00
parent dbe82166d0
commit 0d5d33c3d7
2 changed files with 57 additions and 14 deletions

View File

@ -120,6 +120,20 @@ begin
UpdateControl; UpdateControl;
end; end;
procedure TSpinEditExBase.EditKeyPress(var Key: Char);
begin
inherited EditKeyPress(Key);
if not KeyAllowed(Key) then
Key := #0;
end;
procedure TSpinEditExBase.EditUtf8KeyPress(var UTF8Key: TUTF8Char);
begin
inherited EditUtf8KeyPress(UTF8Key);
if not Utf8KeyAllowed(Utf8Key) then
Utf8Key := '';
end;
procedure TSpinEditExBase.EditChange; procedure TSpinEditExBase.EditChange;
begin begin
{$ifdef debugspinex} {$ifdef debugspinex}
@ -376,6 +390,16 @@ begin
{$endif} {$endif}
end; end;
function TSpinEditExBase.KeyAllowed(Key: Char): Boolean;
begin
Result := True;
end;
function TSpinEditExBase.Utf8KeyAllowed(Key: TUTF8Char): Boolean;
begin
Result := True;
end;
procedure TSpinEditExBase.FinalizeWnd; procedure TSpinEditExBase.FinalizeWnd;
begin begin
GetValue; GetValue;
@ -453,9 +477,8 @@ begin
If FDecimals = 0, disallow decimalseparator also If FDecimals = 0, disallow decimalseparator also
} }
if (Key in ['.',',']) then Key := FFS.Decimalseparator; if (Key in ['.',',']) then Key := FFS.Decimalseparator;
if not (Key in (Digits + AllowedControlChars + [FFS.DecimalSeparator,'-','+','e','E'])) then Key := #0; if not KeyAllowed(Key) then
if (Key = FFS.DecimalSeparator) and (FDecimals = 0) then Key := #0; Key := #0;
//if (Key = '-') and IsLimited and (MinValue >= 0) then Key := #0;
end; end;
function TCustomFloatSpinEditEx.TextIsNumber(const S: String; out ANumber: Double function TCustomFloatSpinEditEx.TextIsNumber(const S: String; out ANumber: Double
@ -532,6 +555,19 @@ begin
end; end;
end; end;
function TCustomFloatSpinEditEx.KeyAllowed(Key: Char): Boolean;
begin
{
Disallow any key that is not a digit, decimalseparator or '+', '-' or 'E/e'
For ease of use translate any decimalpoint or comma to DecimalSeparator
Tab, BackSpace, Cut, Paste, Copy, Undo of course should be passed onto inherited KeyPress
If FDecimals = 0, disallow decimalseparator also
}
Result := (Key in (Digits + AllowedControlChars + [FFS.DecimalSeparator,'-','+','e','E']));
if Result and (Key = FFS.DecimalSeparator) and (FDecimals = 0) then
Result := False;
end;
constructor TCustomFloatSpinEditEx.Create(TheOwner: TComponent); constructor TCustomFloatSpinEditEx.Create(TheOwner: TComponent);
begin begin
@ -593,15 +629,6 @@ begin
UpdateControl; UpdateControl;
end; end;
procedure TCustomSpinEditEx.EditKeyPress(var Key: char);
begin
inherited EditKeyPress(Key);
{Disallow any key that is not a digit or - or (part of) FThousandSeparator
Tab, BackSpace, Cut, Paste, Copy, Undo of course should be passed onto inherited KeyPress
}
if not ((Key in (Digits + AllowedControlChars + ['-'])) or (Pos(Key, FThousandSeparator) > 0)) then Key := #0;
if (Key = '-') and IsLimited and (MinValue >= 0) then Key := #0;
end;
function TCustomSpinEditEx.SafeInc(AValue: Int64): Int64; function TCustomSpinEditEx.SafeInc(AValue: Int64): Int64;
begin begin
@ -646,3 +673,13 @@ begin
if (FThousandSeparator <> '') then if (FThousandSeparator <> '') then
Result := InsertThousandSeparator(Result, FThousandSeparator); Result := InsertThousandSeparator(Result, FThousandSeparator);
end; end;
function TCustomSpinEditEx.KeyAllowed(Key: Char): Boolean;
begin
{Disallow any key that is not a digit or - or (part of) FThousandSeparator
Tab, BackSpace, Cut, Paste, Copy, Undo of course should be passed onto inherited KeyPress
}
Result := ((Key in (Digits + AllowedControlChars + ['-'])) or (Pos(Key, FThousandSeparator) > 0));
if Result and (Key = '-') and IsLimited and (MinValue >= 0) then
Result := False;
end;

View File

@ -148,6 +148,10 @@ type
procedure FinalizeWnd; override; procedure FinalizeWnd; override;
procedure Loaded; override; procedure Loaded; override;
procedure EditEditingDone; override;
procedure EditKeyPress(var Key: Char); override;
procedure EditUtf8KeyPress(var UTF8Key: TUTF8Char); override;
property ArrowKeys: Boolean read FArrowKeys write FArrowKeys default True; property ArrowKeys: Boolean read FArrowKeys write FArrowKeys default True;
property Edit: TGEEdit read GetEdit; property Edit: TGEEdit read GetEdit;
property UpDown: TUpDown read GetUpDown; property UpDown: TUpDown read GetUpDown;
@ -158,7 +162,8 @@ type
function GetLimitedValue(const AValue: T): T; virtual; function GetLimitedValue(const AValue: T): T; virtual;
function ValueToStr(const AValue: T): String; virtual; abstract; function ValueToStr(const AValue: T): String; virtual; abstract;
function StrToValue(const S: String): T; virtual; function StrToValue(const S: String): T; virtual;
procedure EditEditingDone; override; function KeyAllowed({%H-}Key: Char): Boolean; virtual;
function Utf8KeyAllowed({%H-}Key: TUTF8Char): Boolean; virtual;
public public
property Increment: T read FIncrement write SetIncrement stored IncrementStored nodefault; property Increment: T read FIncrement write SetIncrement stored IncrementStored nodefault;
property MinValue: T read FMinValue write SetMinValue; property MinValue: T read FMinValue write SetMinValue;
@ -203,6 +208,7 @@ type
procedure SetDecimals(ADecimals: Integer); virtual; procedure SetDecimals(ADecimals: Integer); virtual;
public public
function ValueToStr(const AValue: Double): String; override; function ValueToStr(const AValue: Double): String; override;
function KeyAllowed(Key: Char): Boolean; override;
constructor Create(TheOwner: TComponent); override; constructor Create(TheOwner: TComponent); override;
property DecimalSeparator: Char read GetDecimalSeparator write SetDecimalSeparator default DefDecimalSeparator; property DecimalSeparator: Char read GetDecimalSeparator write SetDecimalSeparator default DefDecimalSeparator;
property DecimalPlaces: Integer read FDecimals write SetDecimals default DefDecimals; property DecimalPlaces: Integer read FDecimals write SetDecimals default DefDecimals;
@ -303,12 +309,12 @@ type
FThousandSeparator: String; FThousandSeparator: String;
procedure SetThousandSeparator(AValue: String); procedure SetThousandSeparator(AValue: String);
protected protected
procedure EditKeyPress(var Key: char); override;
function SafeInc(AValue: Int64): Int64; override; function SafeInc(AValue: Int64): Int64; override;
function SafeDec(AValue: Int64): Int64; override; function SafeDec(AValue: Int64): Int64; override;
function TextIsNumber(const S: String; out ANumber: Int64): Boolean; override; function TextIsNumber(const S: String; out ANumber: Int64): Boolean; override;
public public
function ValueToStr(const AValue: Int64): String; override; function ValueToStr(const AValue: Int64): String; override;
function KeyAllowed(Key: Char): Boolean; override;
public public
property Increment default 1; property Increment default 1;
property ThousandSeparator: String read FThousandSeparator write SetThousandSeparator; //string so you can use Utf8 property ThousandSeparator: String read FThousandSeparator write SetThousandSeparator; //string so you can use Utf8