diff options
author | Jean-Marc Levecque <jean-marc.levecque@jmlesite.fr> | 2013-01-03 16:45:03 +0000 |
---|---|---|
committer | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2013-01-03 16:45:03 +0000 |
commit | d08853f0a57903653fea0d39e57a6a7c1b98320e (patch) | |
tree | 1cb004ea7cc44956cde7cb357496ce483538b162 /src/gui | |
parent | 9400a31c67e03e29f355c694b530258308c4c784 (diff) | |
download | fpGUI-d08853f0a57903653fea0d39e57a6a7c1b98320e.tar.xz |
needed of values limits on integer edits.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/fpg_edit.pas | 266 | ||||
-rw-r--r-- | src/gui/fpg_spinedit.pas | 29 |
2 files changed, 237 insertions, 58 deletions
diff --git a/src/gui/fpg_edit.pas b/src/gui/fpg_edit.pas index dce1fda8..f66a5859 100644 --- a/src/gui/fpg_edit.pas +++ b/src/gui/fpg_edit.pas @@ -242,9 +242,15 @@ type TfpgEditInteger = class(TfpgBaseNumericEdit) + private + FLimit: Boolean; + FMaxValue: integer; + FMinValue: integer; protected function GetValue: integer; virtual; procedure SetValue(const AValue: integer); virtual; + procedure SetMaxValue(const AValue: integer); virtual; + procedure SetMinValue(const AValue: integer); virtual; procedure HandleKeyChar(var AText: TfpgChar; var shiftstate: TShiftState; var consumed: Boolean); override; procedure HandleSetFocus; override; procedure HandleKillFocus; override; @@ -258,6 +264,8 @@ type property CustomThousandSeparator; property Enabled; property Hint; + property MaxValue: integer read FMaxValue write SetMaxValue; + property MinValue: integer read FMinValue write SetMinValue; property NegativeColor; property ParentShowHint; property ReadOnly; @@ -279,12 +287,17 @@ type TfpgEditFloat = class(TfpgBaseNumericEdit) private - FFixedDecimals: boolean; + FFixedDecimals: integer; + FLimit: Boolean; + FMaxValue: extended; + FMinValue: extended; protected function GetValue: extended; virtual; procedure SetValue(const AValue: extended); virtual; + procedure SetMaxValue(const AValue: extended); virtual; + procedure SetMinValue(const AValue: extended); virtual; procedure SetDecimals(const AValue: integer); - procedure SetFixedDecimals(const AValue: boolean); + procedure SetFixedDecimals(const AValue: integer); procedure HandleKeyChar(var AText: TfpgChar; var shiftstate: TShiftState; var consumed: Boolean); override; procedure HandleSetFocus; override; procedure HandleKillFocus; override; @@ -298,9 +311,11 @@ type property CustomDecimalSeparator; property CustomThousandSeparator; property Decimals: integer read FDecimals write SetDecimals default -1; + property FixedDecimals: integer read FFixedDecimals write SetFixedDecimals default -1; property Enabled; - property FixedDecimals: boolean read FFixedDecimals write SetFixedDecimals default False; property Hint; + property MaxValue: extended read FMaxValue write SetMaxValue; + property MinValue: extended read FMinValue write SetMinValue; property NegativeColor; property ParentShowHint; property ReadOnly; @@ -321,9 +336,15 @@ type TfpgEditCurrency = class(TfpgBaseNumericEdit) + private + FLimit: Boolean; + FMaxValue: Currency; + FMinValue: Currency; protected function GetValue: Currency; virtual; procedure SetValue(const AValue: Currency); virtual; + procedure SetMaxValue(const AValue: Currency); virtual; + procedure SetMinValue(const AValue: Currency); virtual; procedure SetDecimals(AValue: integer); procedure HandleKeyChar(var AText: TfpgChar; var shiftstate: TShiftState; var consumed: Boolean); override; procedure HandleKeyPress(var keycode: word; var shiftstate: TShiftState; var consumed: Boolean); override; @@ -341,6 +362,8 @@ type property Decimals: integer read FDecimals write SetDecimals default 2; property Enabled; property Hint; + property MaxValue: Currency read FMaxValue write SetMaxValue; + property MinValue: Currency read FMinValue write SetMinValue; property NegativeColor; property ParentShowHint; property ReadOnly; @@ -365,7 +388,7 @@ function CreateEditInteger(AOwner: TComponent; x, y, w, h: TfpgCoord; AShowThousand: boolean= True): TfpgEditInteger; function CreateEditFloat(AOwner: TComponent; x, y, w, h: TfpgCoord; - AShowThousand: boolean= True; ADecimals: Integer= -1; AFixedDecimals: boolean= False): TfpgEditFloat; + AShowThousand: boolean= True; ADecimals: Integer= -1; AFixedDecimals: integer= -1): TfpgEditFloat; function CreateEditCurrency(AOwner: TComponent; x, y, w, h: TfpgCoord; AShowThousand: boolean= True; ADecimals: Integer= 2): TfpgEditCurrency; @@ -388,7 +411,6 @@ const cPasswordChar = #$E2#$97#$8F; // U+25CF BLACK CIRCLE - function CreateEdit(AOwner: TComponent; x, y, w, h: TfpgCoord): TfpgEdit; begin Result := TfpgEdit.Create(AOwner); @@ -418,7 +440,7 @@ begin end; function CreateEditFloat(AOwner: TComponent; x, y, w, h: TfpgCoord; AShowThousand: boolean= True; - ADecimals: Integer= -1; AFixedDecimals: boolean= False): TfpgEditFloat; + ADecimals: Integer= -1; AFixedDecimals: integer= -1): TfpgEditFloat; begin Result := TfpgEditFloat.Create(AOwner); Result.Left := x; @@ -426,7 +448,7 @@ begin Result.Width := w; Result.ShowThousand:= AShowThousand; Result.Decimals := ADecimals; - Result.FFixedDecimals:= AFixedDecimals; + Result.FixedDecimals := AFixedDecimals; if h < TfpgEditFloat(Result).FFont.Height + 4 + (Result.FHeightMargin * 2) then Result.Height := TfpgEditFloat(Result).FFont.Height + 4 + (Result.FHeightMargin * 2) else @@ -1880,6 +1902,19 @@ begin begin try Result := StrToInt(fText); + if FLimit then + begin + if Result> FMaxValue then + begin + SetValue(FMaxValue); + Result:= FMaxValue; + end; + if Result< FMinValue then + begin + SetValue(FMinValue); + Result:= FMinValue; + end; + end; except on E: EConvertError do begin @@ -1896,13 +1931,43 @@ end; procedure TfpgEditInteger.SetValue(const AValue: integer); begin - try - Text := IntToStr(AValue); - FormatEdit; - except - on E: EConvertError do - Text := ''; - end; + if FLimit then + if (AValue <= FMaxValue) and (AValue >= FMinValue) then + try + Text := IntToStr(AValue); + FormatEdit; + except + on E: EConvertError do + Text := ''; + end + else + Exit + else + try + Text := IntToStr(AValue); + FormatEdit; + except + on E: EConvertError do + Text := ''; + end; +end; + +procedure TfpgEditInteger.SetMaxValue(const AValue: integer); +begin + if AValue > FMinValue then + FMaxValue:= AValue + else + FMaxValue := FMinValue; + FLimit:= True; +end; + +procedure TfpgEditInteger.SetMinValue(const AValue: integer); +begin + if AValue < FMaxValue then + FMinValue:= AValue + else + FMinValue := FMaxValue; + FLimit:= True; end; procedure TfpgEditInteger.HandleKeyChar(var AText: TfpgChar; @@ -1960,6 +2025,7 @@ begin inherited Create(AOwner); FShowThousand := True; FDecimals := 0; + FLimit := False; end; { TfpgEditFloat } @@ -1978,6 +2044,10 @@ begin if FDecimals = 0 then if UTF8Pos(FDecimalSeparator, fText) > 0 then fText := UTF8Copy(fText, 1, UTF8Length(fText) - 1); + if FFixedDecimals > -1 then + if UTF8Pos(FDecimalSeparator, fText) > 0 then + if UTF8Length(fText)-UTF8Pos(FDecimalSeparator, fText) > FFixedDecimals then + fText := UTF8Copy(fText, 1, UTF8Length(fText) - 1); if ShowThousand then begin @@ -2005,7 +2075,20 @@ begin begin try Result := StrToFloat(fText); - except + if FLimit then + begin + if Result> FMaxValue then + begin + SetValue(FMaxValue); + Result:= FMaxvalue; + end; + if Result< FMinValue then + begin + SetValue(FMinValue); + Result:= FMinValue; + end; + end; + except on E: EConvertError do begin Result := 0; @@ -2021,16 +2104,51 @@ end; procedure TfpgEditFloat.SetValue(const AValue: extended); begin - try - if FFixedDecimals then - Text := FloatToStrF(AValue, ffFixed, 18, FDecimals) + if FLimit then + if (AValue <= FMaxValue) and (AValue >= FMinValue) then + try + Text := FloatToStr(AValue); + if FFixedDecimals > -1 then + if UTF8Pos(FDecimalSeparator, Text) > 0 then + while UTF8Length(Text)-UTF8Pos(FDecimalSeparator, Text) < FFixedDecimals do + Text := Text + '0'; + FormatEdit; + except + on E: EConvertError do + Text := ''; + end else + Exit + else + try Text := FloatToStr(AValue); - FormatEdit; - except - on E: EConvertError do - Text := ''; - end; + if FFixedDecimals > -1 then + if UTF8Pos(FDecimalSeparator, Text) > 0 then + while UTF8Length(Text)-UTF8Pos(FDecimalSeparator, Text) < FFixedDecimals do + Text := Text + '0'; + FormatEdit; + except + on E: EConvertError do + Text := ''; + end; +end; + +procedure TfpgEditFloat.SetMaxValue(const AValue: extended); +begin + if AValue > FMinValue then + FMaxValue:= AValue + else + FMaxValue := FMinValue; + FLimit:= True; +end; + +procedure TfpgEditFloat.SetMinValue(const AValue: extended); +begin + if AValue < FMaxValue then + FMinValue:= AValue + else + FMinValue := FMaxValue; + FLimit:= True; end; procedure TfpgEditFloat.SetDecimals(const AValue: integer); @@ -2038,13 +2156,21 @@ begin if AValue < -1 then Exit; // => if FDecimals <> AValue then - FDecimals := AValue + begin + FDecimals := AValue; + FFixedDecimals := -1; + end; end; -procedure TfpgEditFloat.SetFixedDecimals(const AValue: boolean); +procedure TfpgEditFloat.SetFixedDecimals(const AValue: integer); begin + if AValue < -1 then + Exit; // => if FFixedDecimals <> AValue then + begin FFixedDecimals := AValue; + FDecimals := -1; + end; end; procedure TfpgEditFloat.HandleKeyChar(var AText: TfpgChar; @@ -2065,17 +2191,21 @@ procedure TfpgEditFloat.HandleSetFocus; begin try if GetValue = 0 then - Text := '' + fText := '' else begin - if FFixedDecimals then - Text := FloatToStrF(GetValue, ffFixed, 18, FDecimals) - else - Text := FloatToStr(GetValue); + fText := FloatToStr(GetValue); + if FFixedDecimals > -1 then + begin + if UTF8Pos(FDecimalSeparator, fText) = 0 then + fText := fText + FDecimalSeparator; + while (UTF8Length(fText) - (UTF8Pos(FDecimalSeparator, fText)) < FFixedDecimals) do + fText := fText +'0'; + end; end; except on E: EConvertError do - Text := ''; + fText := ''; end; inherited HandleSetFocus; end; @@ -2083,14 +2213,18 @@ end; procedure TfpgEditFloat.HandleKillFocus; begin try - if FFixedDecimals then - Text := FloatToStrF(GetValue, ffFixed, 18, FDecimals) - else - Text := FloatToStr(GetValue); + fText := FloatToStr(GetValue); + if FFixedDecimals > -1 then + begin + if UTF8Pos(FDecimalSeparator, fText) = 0 then + fText := fText + FDecimalSeparator; + while (UTF8Length(fText) - (UTF8Pos(FDecimalSeparator, fText)) < FFixedDecimals) do + fText := fText +'0'; + end; FormatEdit; except on E: EConvertError do - Text := ''; + fText := ''; end; inherited HandleKillFocus; end; @@ -2110,8 +2244,8 @@ constructor TfpgEditFloat.Create(AOwner: TComponent); begin inherited Create(AOwner); FDecimals := -1; - FFixedDecimals := False; FShowThousand := True; + FLimit := False; end; { TfpgEditCurrency } @@ -2147,6 +2281,19 @@ begin if fText > '' then try Result := StrToCurr(fText); + if FLimit then + begin + if Result> FMaxValue then + begin + SetValue(FMaxValue); + Result:= FMaxvalue; + end; + if Result< FMinValue then + begin + SetValue(FMinValue); + Result:= FMinValue; + end; + end; except on E: EConvertError do begin @@ -2161,13 +2308,43 @@ end; procedure TfpgEditCurrency.SetValue(const AValue: Currency); begin - try - Text := FloatToStrF(AValue, ffFixed, -1, FDecimals); - FormatEdit; - except - on E: EConvertError do - Text := ''; - end; + if FLimit then + if (AValue <= FMaxValue) and (AValue >= FMinValue) then + try + Text := FloatToStrF(AValue, ffFixed, -1, FDecimals); + FormatEdit; + except + on E: EConvertError do + Text := ''; + end + else + Exit + else + try + Text := FloatToStrF(AValue, ffFixed, -1, FDecimals); + FormatEdit; + except + on E: EConvertError do + Text := ''; + end; +end; + +procedure TfpgEditCurrency.SetMaxValue(const AValue: Currency); +begin + if AValue > FMinValue then + FMaxValue:= AValue + else + FMaxValue := FMinValue; + FLimit:= True; +end; + +procedure TfpgEditCurrency.SetMinValue(const AValue: Currency); +begin + if AValue < FMaxValue then + FMinValue:= AValue + else + FMinValue := FMaxValue; + FLimit:= True; end; procedure TfpgEditCurrency.SetDecimals(AValue: integer); @@ -2256,6 +2433,7 @@ begin inherited Create(AOwner); FDecimals := 2; FShowThousand := True; + FLimit := False; end; diff --git a/src/gui/fpg_spinedit.pas b/src/gui/fpg_spinedit.pas index 5c00c91b..444fa2c0 100644 --- a/src/gui/fpg_spinedit.pas +++ b/src/gui/fpg_spinedit.pas @@ -107,7 +107,7 @@ type function GetNegativeColor: TfpgColor; function GetFontDesc: string; function GetDecimals: integer; - function GetFixedDecimals: Boolean; + function GetFixedDecimals: integer; procedure ResizeChildren; override; procedure SetEditBackgroundColor(const AValue: Tfpgcolor); procedure SetTextColor(const AValue: Tfpgcolor); override; @@ -119,7 +119,7 @@ type procedure SetLargeIncrement(const AValue: extended); procedure SetValue(const AValue: extended); procedure SetDecimals(const AValue: integer); - procedure SetFixedDecimals(const AValue: Boolean); + procedure SetFixedDecimals(const AValue: integer); procedure SetHint(const AValue: TfpgString); override; procedure ButtonUpClick(Sender: TObject); procedure ButtonDownClick(Sender: TObject); @@ -150,7 +150,7 @@ type property LargeIncrement: extended read FLargeIncrement write SetLargeIncrement; property Value: extended read FValue write SetValue; property Decimals: integer read GetDecimals write SetDecimals; - property FixedDecimals: Boolean read GetFixedDecimals write SetFixedDecimals; + property FixedDecimals: integer read GetFixedDecimals write SetFixedDecimals; property Hint; property TabOrder; property OnChange; @@ -235,7 +235,7 @@ type function CreateSpinEditFloat(AOwner: TComponent; x, y, w, h: TfpgCoord; AMinValue: extended = 0; AMaxValue: extended = 100; AIncrement: extended = 1; ALargeIncrement: extended = 10.0; - ADecimals: integer = 1; AValue: extended = 0; AFixedDecimals: boolean= False): TfpgSpinEditFloat; + AFixedDecimals: integer = 1; AValue: extended = 0; ADecimals: integer = -1): TfpgSpinEditFloat; function CreateSpinEdit(AOwner: TComponent; x, y, w, h: TfpgCoord; AMinValue: integer = 0; AMaxValue: integer = 100; AIncrement: integer = 1; ALargeIncrement: integer = 10; AValue: integer = 0): TfpgSpinEdit; @@ -244,12 +244,14 @@ function CreateSpinEdit(AOwner: TComponent; x, y, w, h: TfpgCoord; AMinValue: in implementation uses - fpg_extgraphics, math; + fpg_extgraphics, + fpg_stringutils, + math; function CreateSpinEditFloat(AOwner: TComponent; x, y, w, h: TfpgCoord; AMinValue: extended = 0; AMaxValue: extended = 100; AIncrement: extended = 1; ALargeIncrement: extended = 10.0; - ADecimals: integer = 1; AValue: extended = 0; AFixedDecimals: boolean= False): TfpgSpinEditFloat; + AFixedDecimals: integer = 1; AValue: extended = 0; ADecimals: integer = -1): TfpgSpinEditFloat; var newh: TfpgCoord; begin @@ -267,8 +269,8 @@ begin end; Result.Increment := AIncrement; Result.LargeIncrement := ALargeIncrement; - Result.FEdit.Decimals := ADecimals; Result.FEdit.FixedDecimals := AFixedDecimals; + Result.FEdit.Decimals := ADecimals; if (AValue <= Result.MaxValue) and (AValue >= Result.MinValue) then Result.Value := AValue; end; @@ -502,7 +504,7 @@ begin Result := FEdit.Decimals; end; -function TfpgSpinEditFloat.GetFixedDecimals: Boolean; +function TfpgSpinEditFloat.GetFixedDecimals: integer; begin Result := FEdit.FixedDecimals; end; @@ -607,13 +609,12 @@ begin FEdit.Decimals := AValue; end; -procedure TfpgSpinEditFloat.SetFixedDecimals(const AValue: Boolean); +procedure TfpgSpinEditFloat.SetFixedDecimals(const AValue: integer); begin + if AValue < 0 then + Exit; // => if FEdit.FixedDecimals <> AValue then - begin FEdit.FixedDecimals := AValue; - FEdit.Text:= FloatToStrF(FEdit.Value, ffFixed, 18, FEdit.Decimals); - end; end; procedure TfpgSpinEditFloat.SetHint(const AValue: TfpgString); @@ -878,9 +879,9 @@ begin FUp := False; FDown := False; - FEdit.Decimals := 1; + FEdit.Decimals := -1; + FEdit.FixedDecimals := 1; FEdit.Value := FValue; - FEdit.FixedDecimals := False; FButtonUp.OnClick := @ButtonUpClick; FButtonDown.OnClick := @ButtonDownClick; |