diff --git a/components/lazcontrols/spinex.inc b/components/lazcontrols/spinex.inc index 49b5e9a797..e309e0b900 100644 --- a/components/lazcontrols/spinex.inc +++ b/components/lazcontrols/spinex.inc @@ -120,6 +120,20 @@ begin UpdateControl; 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; begin {$ifdef debugspinex} @@ -376,6 +390,16 @@ begin {$endif} end; +function TSpinEditExBase.KeyAllowed(Key: Char): Boolean; +begin + Result := True; +end; + +function TSpinEditExBase.Utf8KeyAllowed(Key: TUTF8Char): Boolean; +begin + Result := True; +end; + procedure TSpinEditExBase.FinalizeWnd; begin GetValue; @@ -453,9 +477,8 @@ begin If FDecimals = 0, disallow decimalseparator also } if (Key in ['.',',']) then Key := FFS.Decimalseparator; - if not (Key in (Digits + AllowedControlChars + [FFS.DecimalSeparator,'-','+','e','E'])) then Key := #0; - if (Key = FFS.DecimalSeparator) and (FDecimals = 0) then Key := #0; - //if (Key = '-') and IsLimited and (MinValue >= 0) then Key := #0; + if not KeyAllowed(Key) then + Key := #0; end; function TCustomFloatSpinEditEx.TextIsNumber(const S: String; out ANumber: Double @@ -532,6 +555,19 @@ begin 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); begin @@ -593,15 +629,6 @@ begin UpdateControl; 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; begin @@ -646,3 +673,13 @@ begin if (FThousandSeparator <> '') then Result := InsertThousandSeparator(Result, FThousandSeparator); 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; diff --git a/components/lazcontrols/spinex.pp b/components/lazcontrols/spinex.pp index 0884ef8fcb..7befde34ba 100644 --- a/components/lazcontrols/spinex.pp +++ b/components/lazcontrols/spinex.pp @@ -148,6 +148,10 @@ type procedure FinalizeWnd; 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 Edit: TGEEdit read GetEdit; property UpDown: TUpDown read GetUpDown; @@ -158,7 +162,8 @@ type function GetLimitedValue(const AValue: T): T; virtual; function ValueToStr(const AValue: T): String; virtual; abstract; 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 property Increment: T read FIncrement write SetIncrement stored IncrementStored nodefault; property MinValue: T read FMinValue write SetMinValue; @@ -203,6 +208,7 @@ type procedure SetDecimals(ADecimals: Integer); virtual; public function ValueToStr(const AValue: Double): String; override; + function KeyAllowed(Key: Char): Boolean; override; constructor Create(TheOwner: TComponent); override; property DecimalSeparator: Char read GetDecimalSeparator write SetDecimalSeparator default DefDecimalSeparator; property DecimalPlaces: Integer read FDecimals write SetDecimals default DefDecimals; @@ -303,12 +309,12 @@ type FThousandSeparator: String; procedure SetThousandSeparator(AValue: String); protected - procedure EditKeyPress(var Key: char); override; function SafeInc(AValue: Int64): Int64; override; function SafeDec(AValue: Int64): Int64; override; function TextIsNumber(const S: String; out ANumber: Int64): Boolean; override; public function ValueToStr(const AValue: Int64): String; override; + function KeyAllowed(Key: Char): Boolean; override; public property Increment default 1; property ThousandSeparator: String read FThousandSeparator write SetThousandSeparator; //string so you can use Utf8