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 -
This commit is contained in:
maxim 2015-09-18 22:47:14 +00:00
parent 508f0453cc
commit 7319e69d77

View File

@ -162,7 +162,6 @@ type
procedure SetButtonWidth(AValue: Integer); procedure SetButtonWidth(AValue: Integer);
procedure SetCaretPos(AValue: TPoint); procedure SetCaretPos(AValue: TPoint);
procedure SetCharCase(AValue: TEditCharCase); procedure SetCharCase(AValue: TEditCharCase);
procedure SetDirectInput(AValue: Boolean);
procedure SetEchoMode(AValue: TEchoMode); procedure SetEchoMode(AValue: TEchoMode);
procedure SetEditMask(AValue: String); procedure SetEditMask(AValue: String);
procedure SetEditText(AValue: string); procedure SetEditText(AValue: string);
@ -183,12 +182,14 @@ type
procedure SetSelText(AValue: String); procedure SetSelText(AValue: String);
procedure SetSpacing(const Value: integer); procedure SetSpacing(const Value: integer);
procedure SetTabStop(AValue: Boolean); procedure SetTabStop(AValue: Boolean);
procedure SetText(AValue: TCaption);
protected protected
class function GetControlClassDefaultSize: TSize; override; class function GetControlClassDefaultSize: TSize; override;
function CalcButtonVisible: Boolean; virtual; function CalcButtonVisible: Boolean; virtual;
function GetDefaultGlyph: TBitmap; virtual; function GetDefaultGlyph: TBitmap; virtual;
function GetDefaultGlyphName: String; virtual; function GetDefaultGlyphName: String; virtual;
procedure SetDirectInput(AValue: Boolean); virtual;
procedure SetText(AValue: TCaption); virtual;
function GetEditPopupMenu: TPopupMenu; function GetEditPopupMenu: TPopupMenu;
procedure CalculatePreferredSize(var PreferredWidth, procedure CalculatePreferredSize(var PreferredWidth,
@ -743,16 +744,22 @@ type
FOKCaption: TCaption; FOKCaption: TCaption;
FCancelCaption: TCaption; FCancelCaption: TCaption;
FDateFormat: string; FDateFormat: string;
FDate: TDateTime;
FUpdatingDate: Boolean;
function TextToDate(AText: String; ADefault: TDateTime): TDateTime;
function GetDate: TDateTime; function GetDate: TDateTime;
procedure SetDate(Value: TDateTime); procedure SetDate(Value: TDateTime);
procedure CalendarPopupReturnDate(Sender: TObject; const ADate: TDateTime); procedure CalendarPopupReturnDate(Sender: TObject; const ADate: TDateTime);
procedure CalendarPopupShowHide(Sender: TObject); procedure CalendarPopupShowHide(Sender: TObject);
procedure SetDateOrder(const AValue: TDateOrder); procedure SetDateOrder(const AValue: TDateOrder);
function DateToText(Value: TDateTime): String;
protected protected
function GetDefaultGlyph: TBitmap; override; function GetDefaultGlyph: TBitmap; override;
function GetDefaultGlyphName: String; override; function GetDefaultGlyphName: String; override;
procedure ButtonClick; override; procedure ButtonClick; override;
procedure EditDblClick; override; procedure EditDblClick; override;
procedure SetDirectInput(AValue: Boolean); override;
procedure SetText(AValue: TCaption); override;
procedure SetDateMask; virtual; procedure SetDateMask; virtual;
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
@ -2321,6 +2328,8 @@ end;
constructor TDateEdit.Create(AOwner: TComponent); constructor TDateEdit.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FDate := NullDate;
FUpdatingDate := False;
FDefaultToday := False; FDefaultToday := False;
FDisplaySettings := [dsShowHeadings, dsShowDayNames]; FDisplaySettings := [dsShowHeadings, dsShowDayNames];
OKCaption := 'OK'; OKCaption := 'OK';
@ -2367,6 +2376,31 @@ begin
ButtonClick; ButtonClick;
end; 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; procedure TDateEdit.SetDateMask;
Var Var
@ -2624,30 +2658,38 @@ begin
Result := NullDate; Result := NullDate;
end; 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; function TDateEdit.GetDate: TDateTime;
var var
ADate: string; ADate: string;
Def: TDateTime; Def: TDateTime;
begin begin
if FDefaultToday then //debugln(['TDateEdit.GetDate: FDate = ',DateToStr(FDate)]);
if (FDate = NullDate) and FDefaultToday then
Def := SysUtils.Date Def := SysUtils.Date
else else
Def := NullDate; Def := FDate;
ADate := Trim(Text); 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 begin
if Assigned(FOnCustomDate) then Result := TextToDate(ADate, Def);
FOnCustomDate(Self, ADate); FDate := Result;
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)
end end
else else
Result := Def; Result := Def;
@ -2655,21 +2697,19 @@ end;
procedure TDateEdit.SetDate(Value: TDateTime); procedure TDateEdit.SetDate(Value: TDateTime);
begin begin
if {not IsValidDate(Value) or }(Value = NullDate) then FUpdatingDate := True;
begin try
if DefaultToday then if {not IsValidDate(Value) or }(Value = NullDate) then
Value := SysUtils.Date begin
else if DefaultToday then
Value := NullDate; Value := SysUtils.Date
end; else
if Value = NullDate then Value := NullDate;
Text := '' end;
else FDate := Value;
begin Text := DateToText(FDate);
if (FDateOrder = doNone) or (FDateFormat = '') then finally
Text := DateToStr(Value) FUpdatingDate := False;
else
Text := FormatDateTime(FDateFormat, Value)
end; end;
end; end;
@ -2704,6 +2744,19 @@ begin
SetDateMask; SetDateMask;
end; 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 } { TCalcEdit }
function TCalcEdit.GetAsFloat: Double; function TCalcEdit.GetAsFloat: Double;