TDateEdit patch from Alexey Lagunov:

- clean code
- allow set of null date

git-svn-id: trunk@13310 -
This commit is contained in:
paul 2007-12-13 07:14:22 +00:00
parent ac89221fef
commit 6f0adb5206

View File

@ -31,6 +31,9 @@ uses
Graphics, Controls, Forms, FileUtil, Dialogs, StdCtrls, Buttons, Calendar, Graphics, Controls, Forms, FileUtil, Dialogs, StdCtrls, Buttons, Calendar,
ExtDlgs, CalendarPopup; ExtDlgs, CalendarPopup;
const
NullDate: TDateTime = 0;
type type
{ TCustomEditButton } { TCustomEditButton }
@ -311,24 +314,26 @@ type
TDateEdit = class(TCustomEditButton) TDateEdit = class(TCustomEditButton)
private private
FDefaultToday: Boolean;
FDialogTitle: TCaption; FDialogTitle: TCaption;
FDisplaySettings: TDisplaySettings; FDisplaySettings: TDisplaySettings;
FOnAcceptDate: TAcceptDateEvent; FOnAcceptDate: TAcceptDateEvent;
FOKCaption: TCaption; FOKCaption: TCaption;
FCancelCaption: TCaption; FCancelCaption: TCaption;
FDate: TDateTime; FDateFormat:string;
function GetDate: TDateTime;
function IsStoreTitle: boolean; function IsStoreTitle: boolean;
procedure SetDate(const Value: TDateTime); procedure SetDate(Value: TDateTime);
procedure CalendarPopupReturnDate(Sender: TObject; Const ADate: TDateTime); procedure CalendarPopupReturnDate(Sender: TObject; Const ADate: TDateTime);
protected protected
procedure DoButtonClick (Sender: TObject); override; procedure DoButtonClick (Sender: TObject); override;
procedure EditingDone; override;
procedure DblClick; override; procedure DblClick; override;
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
procedure DateFormatChanged; virtual; procedure DateFormatChanged; virtual;
property Date: TDateTime read FDate write SetDate; function GetDateFormat: string;
property Date: TDateTime Read GetDate Write SetDate;
property Button; property Button;
published published
property DialogTitle:TCaption read FDialogTitle write FDialogTitle Stored IsStoreTitle; property DialogTitle:TCaption read FDialogTitle write FDialogTitle Stored IsStoreTitle;
@ -337,6 +342,8 @@ type
property OKCaption:TCaption read FOKCaption write FOKCaption; property OKCaption:TCaption read FOKCaption write FOKCaption;
property CancelCaption:TCaption read FCancelCaption write FCancelCaption; property CancelCaption:TCaption read FCancelCaption write FCancelCaption;
property ReadOnly default true; property ReadOnly default true;
property DefaultToday: Boolean read FDefaultToday write FDefaultToday
default False;
property ButtonOnlyWhenFocused; property ButtonOnlyWhenFocused;
property ButtonWidth; property ButtonWidth;
@ -881,14 +888,20 @@ end;
{ TDateEdit } { TDateEdit }
function StrToDateDef(cDate: String; dDefault: TDateTime): TDateTime;
begin
try
Result := StrToDate(cDate)
except
Result := dDefault;
end;
end;
constructor TDateEdit.Create(AOwner: TComponent); constructor TDateEdit.Create(AOwner: TComponent);
var var
ABitmap: TBitmap; ABitmap: TBitmap;
begin begin
inherited Create(AOwner); inherited Create(AOwner);
// FDate:=Now;
Date:=trunc(Now);
// Text:=DateToStr(Date);
FDisplaySettings:=[dsShowHeadings, dsShowDayNames]; FDisplaySettings:=[dsShowHeadings, dsShowDayNames];
DialogTitle:=rsPickDate; DialogTitle:=rsPickDate;
OKCaption:='OK'; OKCaption:='OK';
@ -897,6 +910,7 @@ begin
Button.Glyph:=ABitmap; Button.Glyph:=ABitmap;
ABitmap.Free; ABitmap.Free;
Button.OnClick:= @DoButtonClick; Button.OnClick:= @DoButtonClick;
DateFormatChanged;
end; end;
destructor TDateEdit.Destroy; destructor TDateEdit.Destroy;
@ -906,7 +920,12 @@ end;
procedure TDateEdit.DateFormatChanged; procedure TDateEdit.DateFormatChanged;
begin begin
Text:=DateToStr(FDate); FDateFormat := LongDateFormat;
end;
function TDateEdit.GetDateFormat: string;
begin
Result := FDateFormat;
end; end;
procedure TDateEdit.DoButtonClick(Sender:TObject);//or onClick procedure TDateEdit.DoButtonClick(Sender:TObject);//or onClick
@ -919,37 +938,41 @@ begin
ShowCalendarPopup(PopupOrigin, Date, @CalendarPopupReturnDate); ShowCalendarPopup(PopupOrigin, Date, @CalendarPopupReturnDate);
end; end;
procedure TDateEdit.EditingDone;
var Datetmp: TDate;
begin
inherited EditingDone;
//debugln('TDateEdit.EditingDone Text="',Text,'"');
try
Datetmp:=StrToDate(Text);
// if this worked, then adjust it to current format
Date:=Datetmp;
except
Text:=DateToStr(Date);
// invalid date: keep the old;
end;
end;
procedure TDateEdit.DblClick; procedure TDateEdit.DblClick;
begin begin
inherited DblClick; inherited DblClick;
DoButtonClick(nil); DoButtonClick(nil);
end; end;
function TDateEdit.GetDate: TDateTime;
begin
if FDefaultToday then Result := SysUtils.Date
else Result := NullDate;
if Trim(Text)<>'' then
Result := StrToDateDef(Text, Result);
end;
function TDateEdit.IsStoreTitle: boolean; function TDateEdit.IsStoreTitle: boolean;
begin begin
Result:=DialogTitle<>rsPickDate; Result:=DialogTitle<>rsPickDate;
end; end;
procedure TDateEdit.SetDate(const Value:TDateTime); procedure TDateEdit.SetDate(Value:TDateTime);
var
D: TDateTime;
begin begin
FDate:=Value; if {not IsValidDate(Value) or }(Value = NullDate) then
Text:=DateToStr(FDate); begin
if DefaultToday then Value := SysUtils.Date
else Value := NullDate;
end;
D := Self.Date;
if Value = NullDate then
Text := ''
else
Text := DateToStr(Value);
if D <> Date then
Change;
end; end;
procedure TDateEdit.CalendarPopupReturnDate(Sender: TObject; procedure TDateEdit.CalendarPopupReturnDate(Sender: TObject;
@ -959,14 +982,12 @@ var
D:TDateTime; D:TDateTime;
begin begin
try try
FDate:=ADate;
Self.Date:=FDate;
D:=FDate;
B:=true; B:=true;
If Assigned(FOnAcceptDate) then D:=ADate;
FOnAcceptDate(Self,D,B); if Assigned(FOnAcceptDate) then
if B then FOnAcceptDate(Self, D, B);
Text:=DateToStr(FDate); if B then
Self.Date:=D;
except except
on E:Exception do on E:Exception do
MessageDlg(E.Message, mtError, [mbOK], 0); MessageDlg(E.Message, mtError, [mbOK], 0);