From 7319e69d775de83be99b27b60f8b0c7c9c9a208f Mon Sep 17 00:00:00 2001 From: maxim Date: Fri, 18 Sep 2015 22:47:14 +0000 Subject: [PATCH] Merged revision(s) 47800 #8f4b9db051 from trunk: TDateEdit: - refactor GetDate/SetDate - reduce the need for parsing Text for GetDate - force valid text in control when DirectInput = False ........ git-svn-id: branches/fixes_1_4@49846 - --- lcl/editbtn.pas | 117 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 32 deletions(-) diff --git a/lcl/editbtn.pas b/lcl/editbtn.pas index 38aa645725..4e23f7d4d8 100644 --- a/lcl/editbtn.pas +++ b/lcl/editbtn.pas @@ -162,7 +162,6 @@ type procedure SetButtonWidth(AValue: Integer); procedure SetCaretPos(AValue: TPoint); procedure SetCharCase(AValue: TEditCharCase); - procedure SetDirectInput(AValue: Boolean); procedure SetEchoMode(AValue: TEchoMode); procedure SetEditMask(AValue: String); procedure SetEditText(AValue: string); @@ -183,12 +182,14 @@ type procedure SetSelText(AValue: String); procedure SetSpacing(const Value: integer); procedure SetTabStop(AValue: Boolean); - procedure SetText(AValue: TCaption); protected class function GetControlClassDefaultSize: TSize; override; function CalcButtonVisible: Boolean; virtual; function GetDefaultGlyph: TBitmap; virtual; function GetDefaultGlyphName: String; virtual; + procedure SetDirectInput(AValue: Boolean); virtual; + procedure SetText(AValue: TCaption); virtual; + function GetEditPopupMenu: TPopupMenu; procedure CalculatePreferredSize(var PreferredWidth, @@ -743,16 +744,22 @@ type FOKCaption: TCaption; FCancelCaption: TCaption; FDateFormat: string; + FDate: TDateTime; + FUpdatingDate: Boolean; + function TextToDate(AText: String; ADefault: TDateTime): TDateTime; function GetDate: TDateTime; procedure SetDate(Value: TDateTime); procedure CalendarPopupReturnDate(Sender: TObject; const ADate: TDateTime); procedure CalendarPopupShowHide(Sender: TObject); procedure SetDateOrder(const AValue: TDateOrder); + function DateToText(Value: TDateTime): String; protected function GetDefaultGlyph: TBitmap; override; function GetDefaultGlyphName: String; override; procedure ButtonClick; override; procedure EditDblClick; override; + procedure SetDirectInput(AValue: Boolean); override; + procedure SetText(AValue: TCaption); override; procedure SetDateMask; virtual; public constructor Create(AOwner: TComponent); override; @@ -2321,6 +2328,8 @@ end; constructor TDateEdit.Create(AOwner: TComponent); begin inherited Create(AOwner); + FDate := NullDate; + FUpdatingDate := False; FDefaultToday := False; FDisplaySettings := [dsShowHeadings, dsShowDayNames]; OKCaption := 'OK'; @@ -2367,6 +2376,31 @@ begin ButtonClick; end; +procedure TDateEdit.SetDirectInput(AValue: Boolean); +var + Def: TDateTime; +begin + inherited SetDirectInput(AValue); + //Synchronize FDate and force valid text + FDate := TextToDate(Text, NullDate); + SetDate(FDate); +end; + +procedure TDateEdit.SetText(AValue: TCaption); +begin + if (not DirectInput) and not FUpdatingDate then + begin + //force a valid date and set FDate + debugln('TDateEdit.SetText: DirectInput = False'); + if FDefaultToday then + FDate := TextToDate(AValue, SysUtils.Date) + else + FDate := TextToDate(AValue, NullDate); + AValue := DateToText(FDate); + end; + inherited SetText(AValue); +end; + procedure TDateEdit.SetDateMask; Var @@ -2624,30 +2658,38 @@ begin Result := NullDate; end; +function TDateEdit.TextToDate(AText: String; ADefault: TDateTime): TDateTime; +begin + if Assigned(FOnCustomDate) then + FOnCustomDate(Self, AText); + if (DateOrder = doNone) then + begin + if not TryStrToDate(AText, Result) then + begin + Result := ParseDateNoPredefinedOrder(AText, DefaultFormatSettings); + if (Result = NullDate) then Result := ADefault; + end; + end + else + Result := ParseDate(AText,DateOrder,ADefault) +end; + function TDateEdit.GetDate: TDateTime; var ADate: string; Def: TDateTime; begin - if FDefaultToday then + //debugln(['TDateEdit.GetDate: FDate = ',DateToStr(FDate)]); + if (FDate = NullDate) and FDefaultToday then Def := SysUtils.Date else - Def := NullDate; + Def := FDate; ADate := Trim(Text); - if ADate <> '' then + //if not DirectInput then FDate matches the Text, so no need to parse it + if (ADate <> '') and DirectInput then begin - if Assigned(FOnCustomDate) then - FOnCustomDate(Self, ADate); - if (DateOrder = doNone) then - begin - if not TryStrToDate(ADate, Result) then - begin - Result := ParseDateNoPredefinedOrder(ADate, DefaultFormatSettings); - if (Result = NullDate) then Result := Def; - end; - end - else - Result := ParseDate(ADate,DateOrder,Result) + Result := TextToDate(ADate, Def); + FDate := Result; end else Result := Def; @@ -2655,21 +2697,19 @@ end; procedure TDateEdit.SetDate(Value: TDateTime); begin - if {not IsValidDate(Value) or }(Value = NullDate) then - begin - if DefaultToday then - Value := SysUtils.Date - else - Value := NullDate; - end; - if Value = NullDate then - Text := '' - else - begin - if (FDateOrder = doNone) or (FDateFormat = '') then - Text := DateToStr(Value) - else - Text := FormatDateTime(FDateFormat, Value) + FUpdatingDate := True; + try + if {not IsValidDate(Value) or }(Value = NullDate) then + begin + if DefaultToday then + Value := SysUtils.Date + else + Value := NullDate; + end; + FDate := Value; + Text := DateToText(FDate); + finally + FUpdatingDate := False; end; end; @@ -2704,6 +2744,19 @@ begin SetDateMask; end; +function TDateEdit.DateToText(Value: TDateTime): String; +begin + if Value = NullDate then + Result := '' + else + begin + if (FDateOrder = doNone) or (FDateFormat = '') then + Result := DateToStr(Value) + else + Result := FormatDateTime(FDateFormat, Value) + end; +end; + { TCalcEdit } function TCalcEdit.GetAsFloat: Double;