LazControls: Prevent overflow in T(Float)SpinEditEx.

git-svn-id: trunk@56666 -
This commit is contained in:
bart 2017-12-07 21:28:44 +00:00
parent 8fa91fbd06
commit ae0e9976a3
2 changed files with 49 additions and 5 deletions
components/lazcontrols

View File

@ -172,7 +172,10 @@ end;
procedure TSpinEditExBase.SetIncrement(const AIncrement: T);
begin
if AIncrement = FIncrement then Exit;
FIncrement := AIncrement;
if AIncrement > 0 then
FIncrement := AIncrement
else
FIncrement := -AIncrement;
end;
@ -274,9 +277,9 @@ begin
else
begin
if Up then
NewValue := GetLimitedValue(OldValue + Increment)
NewValue := GetLimitedValue(SafeInc(OldValue))
else
NewValue := GetLimitedValue(OldValue - Increment)
NewValue := GetLimitedValue(SafeDec(OldValue));
end;
end;
SetValue(NewValue);
@ -396,6 +399,22 @@ begin
{$endif}
end;
function TCustomFloatSpinEditEx.SafeInc(AValue: Double): Double;
begin
if ((AValue > 0) and (AValue > (MaxDouble-FIncrement))) then
Result := MaxDouble
else
Result := AValue + FIncrement;
end;
function TCustomFloatSpinEditEx.SafeDec(AValue: Double): Double;
begin
if (AValue < 0) and ((-MaxDouble + FIncrement) > AValue) then
Result := -MaxDouble
else
Result := AValue - FIncrement;
end;
procedure TCustomFloatSpinEditEx.SetDecimals(ADecimals: Integer);
begin
if FDecimals = ADecimals then Exit;
@ -403,6 +422,7 @@ begin
UpdateControl;
end;
function TCustomFloatSpinEditEx.ValueToStr(const AValue: Double): String;
begin
Result := FloatToStrF(GetLimitedValue(AValue), ffFixed, 20, DecimalPlaces, FFS);
@ -464,6 +484,22 @@ begin
if (Key = '-') and IsLimited and (MinValue >= 0) then Key := #0;
end;
function TCustomSpinEditEx.SafeInc(AValue: Int64): Int64;
begin
if ((AValue > 0) and (AValue > (High(Int64)-FIncrement))) then
Result := High(Int64)
else
Result := AValue + FIncrement;
end;
function TCustomSpinEditEx.SafeDec(AValue: Int64): Int64;
begin
if (AValue < 0) and ((Low(Int64) + FIncrement) > AValue) then
Result := Low(Int64)
else
Result := AValue - FIncrement;
end;
function TCustomSpinEditEx.TextIsNumber(const S: String; out ANumber: Int64
): Boolean;
var
@ -484,6 +520,7 @@ begin
{$endif}
end;
function TCustomSpinEditEx.ValueToStr(const AValue: Int64): String;
begin
Result := IntToStr(AValue);
@ -525,6 +562,5 @@ end;
constructor TCustomSpinEditEx.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
NumbersOnly := True;
end;

View File

@ -72,7 +72,7 @@ unit spinex;
interface
uses
Classes, SysUtils,
Classes, SysUtils, Math,
// LCL
LCLType, LCLProc, Controls, ClipBrd, ComCtrls, GroupedEdit;
@ -130,6 +130,8 @@ type
procedure EditKeyDown(var Key: word; Shift: TShiftState); override;
procedure EditMouseWheelUp(Shift: TShiftState; MousePos: TPoint; var Handled: Boolean); override;
procedure EditMouseWheelDown(Shift: TShiftState; MousePos: TPoint; var Handled: Boolean); override;
function SafeInc(AValue: T): T; virtual; abstract;
function SafeDec(AValue: T): T; virtual abstract;
procedure SetValue(const AValue: T); virtual;
procedure SetNullValue(AValue: T); virtual;
procedure SetMaxValue(const AValue: T); virtual;
@ -159,6 +161,8 @@ type
property Value: T read GetValue write SetValue;
end;
{ TCustomFloatSpinEditEx }
TCustomFloatSpinEditEx = class(specialize TSpinEditExBase<Double>)
private
FDecimals: Integer;
@ -168,6 +172,8 @@ type
protected
procedure EditKeyPress(var Key: char); override;
function TextIsNumber(const S: String; out ANumber: Double): Boolean; override;
function SafeInc(AValue: Double): Double; override;
function SafeDec(AValue: Double): Double; override;
procedure SetDecimals(ADecimals: Integer); virtual;
public
function ValueToStr(const AValue: Double): String; override;
@ -264,6 +270,8 @@ type
TCustomSpinEditEx = class(specialize TSpinEditExBase<Int64>)
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;