lcl: cleanup calendar code

git-svn-id: trunk@19083 -
This commit is contained in:
paul 2009-03-24 03:39:27 +00:00
parent b0f9fcfd3b
commit 7e563aa986

View File

@ -30,27 +30,29 @@ unit Calendar;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
{$IF defined(VER2_0_2) and defined(win32)}
// FPC <= 2.0.2 compatibility code
// WINDOWS define was added after FPC 2.0.2
{$define WINDOWS}
{$endif}
interface interface
uses uses
SysUtils, Classes, LCLType, LCLStrConsts, lMessages, Controls; SysUtils, Classes, LCLType, LCLStrConsts, lMessages, Controls;
Type type
TDisplaySetting = (
dsShowHeadings,
dsShowDayNames,
dsNoMonthChange,
dsShowWeekNumbers,
dsStartMonday
);
TDisplaySettings = set of TDisplaySetting;
const
DefaultDisplaySettings = [dsShowHeadings, dsShowDayNames];
type
EInvalidDate = class(Exception);
{ TCustomCalendar } { TCustomCalendar }
TDisplaySetting = (dsShowHeadings, dsShowDayNames, dsNoMonthChange,
dsShowWeekNumbers,dsStartMonday);
TDisplaySettings = set of TDisplaySetting;
EInvalidDate = class(Exception);
TCustomCalendar = class(TWinControl) TCustomCalendar = class(TWinControl)
private private
FDateAsString : String; FDateAsString : String;
@ -63,11 +65,10 @@ Type
FYearChanged: TNotifyEvent; FYearChanged: TNotifyEvent;
FPropsChanged: boolean; FPropsChanged: boolean;
function GetDateTime: TDateTime; function GetDateTime: TDateTime;
function ReadOnlyIsStored: boolean;
procedure SetDateTime(const AValue: TDateTime); procedure SetDateTime(const AValue: TDateTime);
procedure SetReadOnly(const AValue: Boolean); procedure SetReadOnly(const AValue: Boolean);
Procedure GetProps; procedure GetProps;
Procedure SetProps; procedure SetProps;
function GetDisplaySettings: TDisplaySettings; function GetDisplaySettings: TDisplaySettings;
procedure SetDisplaySettings(const AValue: TDisplaySettings); procedure SetDisplaySettings(const AValue: TDisplaySettings);
function GetDate: String; function GetDate: String;
@ -80,20 +81,19 @@ Type
class function GetControlClassDefaultSize: TPoint; override; class function GetControlClassDefaultSize: TPoint; override;
procedure Loaded; override; procedure Loaded; override;
public public
constructor Create(TheOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure InitializeWnd; override; procedure InitializeWnd; override;
property Date: String read GetDate write SetDate stored false; property Date: String read GetDate write SetDate stored False;
property DateTime: TDateTime read GetDateTime write SetDateTime; property DateTime: TDateTime read GetDateTime write SetDateTime;
property DisplaySettings: TDisplaySettings read GetDisplaySettings write SetDisplaySettings; property DisplaySettings: TDisplaySettings read GetDisplaySettings
property ReadOnly: Boolean read FReadOnly write SetReadOnly stored ReadOnlyIsStored; write SetDisplaySettings default DefaultDisplaySettings;
property ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
property OnChange: TNotifyEvent read FOnChange write FOnChange; property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnDayChanged: TNotifyEvent read FDayChanged write FDayChanged; property OnDayChanged: TNotifyEvent read FDayChanged write FDayChanged;
property OnMonthChanged: TNotifyEvent read FMonthChanged write FMonthChanged; property OnMonthChanged: TNotifyEvent read FMonthChanged write FMonthChanged;
property OnYearChanged: TNotifyEvent read FYearChanged write FYearChanged; property OnYearChanged: TNotifyEvent read FYearChanged write FYearChanged;
end; end;
{ TCalendar } { TCalendar }
TCalendar = class(TCustomCalendar) TCalendar = class(TCustomCalendar)
@ -126,11 +126,11 @@ Type
property OnYearChanged; property OnYearChanged;
property PopupMenu; property PopupMenu;
property ReadOnly; property ReadOnly;
property Tabstop; property ShowHint;
property TabStop;
property Visible; property Visible;
end; end;
procedure Register; procedure Register;
implementation implementation
@ -145,21 +145,17 @@ end;
{ TCustomCalendar } { TCustomCalendar }
constructor TCustomCalendar.Create(TheOwner: TComponent); constructor TCustomCalendar.Create(AOwner: TComponent);
begin begin
inherited Create(TheOwner); inherited Create(AOwner);
fCompStyle := csCalendar; fCompStyle := csCalendar;
SetInitialBounds(0,0,GetControlClassDefaultSize.X,GetControlClassDefaultSize.Y); SetInitialBounds(0,0,GetControlClassDefaultSize.X,GetControlClassDefaultSize.Y);
fDisplaySettings := [dsShowHeadings, dsShowDayNames]; FReadOnly := False;
FDisplaySettings := DefaultDisplaySettings;
ControlStyle:=ControlStyle-[csTripleClicks,csQuadClicks,csAcceptsControls,csCaptureMouse]; ControlStyle:=ControlStyle-[csTripleClicks,csQuadClicks,csAcceptsControls,csCaptureMouse];
DateTime := Now; DateTime := Now;
end; end;
destructor TCustomCalendar.Destroy;
begin
Inherited Destroy;
end;
procedure TCustomCalendar.Loaded; procedure TCustomCalendar.Loaded;
begin begin
inherited Loaded; inherited Loaded;
@ -183,24 +179,12 @@ procedure TCustomCalendar.SetDate(const AValue: String);
var var
NewDate: TDateTime; NewDate: TDateTime;
begin begin
if FDateAsString=AValue then exit; if FDateAsString = AValue then Exit;
try
{$IFDEF VerboseCalenderSetDate} NewDate:=StrToDate(AValue); //test to see if date valid ....
DebugLn('TCustomCalendar.SetDate A AValue=',AValue,' FDateAsString=',FDateAsString,' FDate=',FDate,' ShortDateFormat=',ShortDateFormat); // no exception => set valid date
{$ENDIF} FDateAsString := AValue;
NewDate:=StrToDate(AValue); //test to see if date valid .... FDate := NewDate;
// no exception => set valid date
FDateAsString := AValue;
FDate := NewDate;
except
// TODO: remove test for csLoading after fpc 2.0.4 has been released
// The Date property is not supposed to be stored, but earlier fpc version
// did this anyway.
if not (csLoading in ComponentState) then
raise EInvalidDate.CreateFmt(rsInvalidDate, [AValue])
else
exit;
end;
SetProps; SetProps;
end; end;
@ -225,11 +209,6 @@ begin
end; end;
end; end;
function TCustomCalendar.ReadOnlyIsStored: boolean;
begin
Result:=FReadOnly;
end;
function TCustomCalendar.GetDateTime: TDateTime; function TCustomCalendar.GetDateTime: TDateTime;
begin begin
GetProps; GetProps;
@ -327,5 +306,4 @@ begin
if Assigned(OnChange) then OnChange(self); if Assigned(OnChange) then OnChange(self);
end; end;
end. end.