fixed TCalendar resizing problems from Salvatore

git-svn-id: trunk@6842 -
This commit is contained in:
mattias 2005-02-25 18:16:41 +00:00
parent 383be2bf12
commit 0c61b784a6
3 changed files with 171 additions and 312 deletions

View File

@ -139,7 +139,7 @@ constructor TCustomCalendar.Create(TheOwner: TComponent);
begin begin
inherited Create(TheOwner); inherited Create(TheOwner);
fCompStyle := csCalendar; fCompStyle := csCalendar;
SetInitialBounds(0,0,250,150); SetInitialBounds(0,0,190,153);
fDisplaySettings := [dsShowHeadings, dsShowDayNames]; fDisplaySettings := [dsShowHeadings, dsShowDayNames];
ControlStyle:=ControlStyle-csMultiClicks-[csAcceptsControls]; ControlStyle:=ControlStyle-csMultiClicks-[csAcceptsControls];
Date := FormatDateTime(ShortDateFormat,Now); Date := FormatDateTime(ShortDateFormat,Now);

View File

@ -290,71 +290,31 @@ type
TAcceptDateEvent = Procedure (Sender : TObject; Var ADate : TDateTime; TAcceptDateEvent = Procedure (Sender : TObject; Var ADate : TDateTime;
Var AcceptDate: Boolean) of Object; Var AcceptDate: Boolean) of Object;
TDateEdit = Class(TCustomEditButton)
TDateEdit=class(TEditButton)
private private
FDialogTitle: String; FDialogTitle:TCaption;
FDisplaySettings: TDisplaySettings; FDisplaySettings: TDisplaySettings;
FOnAcceptDate: TAcceptDateEvent; FOnAcceptDate: TAcceptDateEvent;
function GetDate: TDateTime;
procedure SetDate(const AValue: TDateTime); FOKCaption:TCaption;
Function StoreTitle : Boolean; FCancelCaption:TCaption;
Protected FDate: TDateTime;
Function CreateDateBitmap : TBitmap; Virtual; function IsStoreTitle: boolean;
procedure SetDate(Value:TDateTime);
protected
procedure DoButtonClick (Sender: TObject); override; procedure DoButtonClick (Sender: TObject); override;
Procedure RunDialog; Virtual; procedure Change; override;
Public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
Destructor Destroy; override; destructor Destroy; override;
Property Date : TDateTime Read GetDate Write SetDate; published
Published property DialogTitle:TCaption Read FDialogTitle Write FDialogTitle Stored IsStoreTitle;
// TDateEdit settings
Property CalendarDisplaySettings : TDisplaySettings Read FDisplaySettings Write FDisplaySettings; Property CalendarDisplaySettings : TDisplaySettings Read FDisplaySettings Write FDisplaySettings;
Property OnAcceptDate : TAcceptDateEvent Read FOnAcceptDAte Write FOnAcceptDate; Property OnAcceptDate : TAcceptDateEvent Read FOnAcceptDAte Write FOnAcceptDate;
Property DialogTitle : String Read FDialogTitle Write FDialogTitle Stored StoreTitle; property OKCaption:TCaption Read FOKCaption Write FOKCaption;
// TEditButton properties. property CancelCaption:TCaption Read FCancelCaption Write FCancelCaption;
Property ButtonWidth; property Date: TDateTime Read FDate Write SetDate;
Property DirectInput;
Property ButtonOnlyWhenFocused;
// property Glyph;
property NumGlyphs;
Property Flat;
// Other properties
property Align;
property Anchors;
property BorderSpacing;
property AutoSize;
property Color;
property Ctl3D;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property MaxLength;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end; end;
@ -833,9 +793,19 @@ end;
constructor TDateEdit.Create(AOwner: TComponent); constructor TDateEdit.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FDate:=Now;
Date:=trunc(FDate);
Text:=DateToStr(Date);
FDisplaySettings:=[dsShowHeadings, dsShowDayNames]; FDisplaySettings:=[dsShowHeadings, dsShowDayNames];
FDialogTitle:=rsPickDate; DialogTitle:='Select a date';
Glyph:=CreateDateBitmap; OKCaption:='OK';
CancelCaption:='Cancel';
ReadOnly:=true;
Color:=clBtnFace;
Button.Glyph:=CreateDateGlyph;//.LoadFromLazarusResource('DateEditGlyph'); //GlyphFromBitmapOrResource(DateGlyph,ResBtnCalendar)
Button.OnClick:= @DoButtonClick;
// OnChange:=@Change;
OnDblClick:=@DoButtonClick;
end; end;
destructor TDateEdit.Destroy; destructor TDateEdit.Destroy;
@ -843,73 +813,62 @@ begin
inherited Destroy; inherited Destroy;
end; end;
function TDateEdit.GetDate: TDateTime; procedure TDateEdit.DoButtonClick(Sender:TObject);//or onClick
var CD:TCalendarDialog;
begin
Result:=0;
Try
Result:=StrToDate(Text)
Except
// Catch errors.
end;
end;
procedure TDateEdit.SetDate(const AValue: TDateTime);
begin
Text:=DateToStr(AValue);
end;
function TDateEdit.StoreTitle: Boolean;
begin
Result:=FDialogTitle<>rsPickDate;
end;
function TDateEdit.CreateDateBitmap: TBitmap;
begin
Result:=CreateDateGlyph;
end;
procedure TDateEdit.DoButtonClick(Sender: TObject);
begin
inherited DoButtonClick(Sender);
RunDialog;
end;
procedure TDateEdit.RunDialog;
Var
FCalendar : TCalendarDialogForm;
B:Boolean; B:Boolean;
D:TDateTime; D:TDateTime;
P : TPoint; begin
inherited DoButtonClick(Sender);
begin CD:=TCalendarDialog.Create(Self);
FCalendar:=CreateCalendarForm(Self,0); with CD do begin
Try Date:=Self.Date;
P.X:=Self.Left; CancelCaption:=Self.CancelCaption;
P.Y:=Self.top; OKCaption:=Self.OKCaption;
P:=ClientToScreen(P); DialogTitle:=Self.DialogTitle;
P.Y:=P.Y+Self.Height; DisplaySettings:=Self.CalendarDisplaySettings;
FCalendar.Top:=P.Y; end;
FCalendar.Left:=P.X; try
FCalendar.DisplaySettings:=FDisplaySettings; if CD.Execute then begin
FCalendar.Date:=Date; FDate:=CD.Date;
FCalendar.Caption:=DialogTitle; Self.Date:=FDate;
If FCalendar.ShowModal=mrOK then D:=FDate;
begin B:=true;
D:=FCalendar.Date;
B:=True;
If Assigned(FOnAcceptDate) then If Assigned(FOnAcceptDate) then
FOnAcceptDate(Self,D,B); FOnAcceptDate(Self,D,B);
If B then Text:=DateToStr(FDate);
Date:=D;
end; end;
Finally except
FCalendar.Free; raise Exception.Create('Errore in calendar dialog di dateedit');
end;
FreeAndNil(CD);
end;
procedure TDateEdit.Change;//(Sender:TObject);
var Datetmp:TDate;
begin
inherited Change;//(Sender);
try
Datetmp:=StrToDate(Text);
Date:=Datetmp;
except
//inherited Change(Sender)
end; end;
end; end;
function TDateEdit.IsStoreTitle: boolean;
begin
Result:=DialogTitle<>rsPickDate;
end;
procedure TDateEdit.SetDate(Value:TDateTime);
begin
if FDate=Value then exit;
FDate:=Value;
Text:=DateToStr(FDate);
end;
{ TCalcEdit } { TCalcEdit }

View File

@ -192,29 +192,7 @@ function CreateCalculatorForm(AOwner: TComponent; ALayout : TCalculatorLayout; A
Type Type
TCalendarDialogForm = Class(TForm) { TCalendarDialog }
private
FCalendar : TCalendar;
FPanel : TPanel;
FOK : TButton;
FCancel : TButton;
function GetDate: TDateTime;
Procedure SetDate (Value : TDateTime);
function GetDisplaySettings: TDisplaySettings;
procedure SetDisplaySettings(const AValue: TDisplaySettings);
Protected
Property Calendar : TCalendar Read FCalendar;
Property ButtonPanel : TPanel Read FPanel;
Property OKButton : TButton Read FOK;
Property CancelButton : TButton Read FCancel;
Procedure InitForm; virtual;
Public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
Property Date : TDateTime Read GetDate Write SetDate;
Property DisplaySettings : TDisplaySettings Read GetDisplaySettings Write SetDisplaySettings;
end;
TCalendarDialog = class(TCommonDialog) TCalendarDialog = class(TCommonDialog)
private private
FDate: TDateTime; FDate: TDateTime;
@ -223,26 +201,29 @@ Type
FHelpContext: THelpContext; FHelpContext: THelpContext;
FMonthChanged: TNotifyEvent; FMonthChanged: TNotifyEvent;
FYearChanged: TNotifyEvent; FYearChanged: TNotifyEvent;
function GetDialogTitle: String; FDialogTitle:TCaption;
procedure SetDialogTitle(const AValue: String); FOKCaption:TCaption;
Function IsTitleStored: Boolean; FCancelCaption:TCaption;
Public FCalendar:TCalendar;
Constructor Create(AOwner: TComponent); override; function IsTitleStored: Boolean;
Destructor Destroy; override; protected
Function Execute: Boolean; override; procedure GetNewDate(Sender:TObject);//or onClick
Function CreateForm: TCalendarDialogForm; virtual; public
Published constructor Create(AOwner: TComponent); override;
Property DialogTitle: String Read GetDialogTitle Write SetDialogTitle Stored IsTitleStored; destructor Destroy; override;
Property Date: TDateTime Read FDate Write FDate; function Execute: Boolean; override;
Property DisplaySettings: TDisplaySettings Read FDisplaySettings Write FDisplaySettings; published
property HelpContext: THelpContext read FHelpContext write FHelpContext default 0; property Date: TDateTime Read FDate Write FDate;
property OnDayChanged: TNotifyEvent read FDayChanged write FDayChanged; property OnDayChanged: TNotifyEvent read FDayChanged write FDayChanged;
property DisplaySettings: TDisplaySettings Read FDisplaySettings Write FDisplaySettings;
property HelpContext: THelpContext read FHelpContext write FHelpContext default 0;
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;
property DialogTitle:TCaption Read FDialogTitle Write FDialogTitle Stored IsTitleStored;
property OKCaption:TCaption Read FOKCaption Write FOKCaption;
property CancelCaption:TCaption Read FCancelCaption Write FCancelCaption;
end; end;
function CreateCalendarForm(AOwner: TComponent; AHelpContext: THelpContext): TCalendarDialogForm;
procedure Register; procedure Register;
@ -481,21 +462,6 @@ begin
end; end;
end; end;
function CreateCalendarForm(AOwner: TComponent; AHelpContext: THelpContext
): TCalendarDialogForm;
begin
Result:=TCalendarDialogForm.Create(AOwner);
With Result do
Try
HelpContext:=AHelpContext;
Left:=(Screen.Width div 2) - (Width div 2);
Top:=(Screen.Height div 2) - (Height div 2);
except
Free;
Raise;
end;
end;
{ --------------------------------------------------------------------- { ---------------------------------------------------------------------
Calculator Dialog Calculator Dialog
---------------------------------------------------------------------} ---------------------------------------------------------------------}
@ -1184,126 +1150,16 @@ end;
TCalendarDialog TCalendarDialog
---------------------------------------------------------------------} ---------------------------------------------------------------------}
{ TCalendarDialogForm }
function TCalendarDialogForm.GetDate: TDateTime;
begin
Try
Result:=StrToDate(FCalendar.Date);
Except
Result:=0;
end;
end;
procedure TCalendarDialogForm.SetDate(Value: TDateTime);
begin
FCalendar.Date:=DateToStr(Date);
end;
function TCalendarDialogForm.GetDisplaySettings: TDisplaySettings;
begin
Result:=FCalendar.DisplaySettings;
end;
procedure TCalendarDialogForm.SetDisplaySettings(const AValue: TDisplaySettings);
begin
FCalendar.DisplaySettings:=AValue;
end;
procedure TCalendarDialogForm.InitForm;
begin
// TODO: let the size of this dialog dependent on the prefered size of
// the calendar, because the gtk calendar is smaller than the win32 calendar.
Height:=170;
Width:=200;
Position:=poScreenCenter;
BorderStyle:=bsDialog;
FCalendar:=TCalendar.Create(Self);
With FCalendar do
begin
Parent:=Self;
SetBounds(0,0,Self.Width,118);
Align:=alClient;
//Anchors:=[akLeft,akRight,akTop,akBottom];
end;
FPanel:=TPanel.Create(Self);
With FPanel do
begin
Parent:=Self;
Top:=109;
Height:=32;
Width:=200;
Align:=alBottom;
Anchors:=[akLeft,akRight,akBottom];
Caption:='';
BevelOuter:=bvLowered;
end;
FOK:=TButton.Create(Self);
With FOK do
begin
Parent:=FPanel;
Caption:='&OK';
Top:=4;
Height:=24;
Width:=50;
Left:=Self.Width-FOK.Width-4;
Anchors:=[akRight,akTop];
Default:=True;
ModalResult:=MrOK;
end;
FCancel:=TButton.Create(Self);
With FCancel do
begin
Parent:=FPanel;
Caption:='&Cancel';
Height:=24;
Top:=4;
Width:=50;
Left:=FOK.Left-FCancel.Width-4;
Anchors:=[akright,aktop];
Cancel:=True;
ModalResult:=MrCancel;
end;
end;
constructor TCalendarDialogForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
InitForm;
end;
destructor TCalendarDialogForm.Destroy;
begin
FCalendar.Free;
FOK.Free;
FCancel.Free;
FPanel.Free;
inherited Destroy;
end;
{ TCalendarDialog } { TCalendarDialog }
function TCalendarDialog.IsTitleStored: Boolean;
begin
Result:=Title<>rsPickDate;
end;
function TCalendarDialog.GetDialogTitle: String;
begin
Result:=Title;
end;
procedure TCalendarDialog.SetDialogTitle(const AValue: String);
begin
Title:=AValue;
end;
constructor TCalendarDialog.Create(AOwner: TComponent); constructor TCalendarDialog.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FDate:=Sysutils.Date; FDate:=Now;
Title:=rsPickDate; Date:=trunc(FDate);
DialogTitle:=rsPickDate;
OKCaption:=rsMbOK;
CancelCaption:=rsMbCancel;
end; end;
destructor TCalendarDialog.Destroy; destructor TCalendarDialog.Destroy;
@ -1311,37 +1167,81 @@ begin
inherited Destroy; inherited Destroy;
end; end;
function TCalendarDialog.Execute: Boolean; procedure TCalendarDialog.GetNewDate(Sender:TObject);//or onClick
Var
Dlg: TCalendarDialogForm;
D: TDateTime;
begin begin
Dlg:=CreateForm; Date:=FCalendar.DateTime;
With Dlg do
Try
D:=FDate;
Dlg.Date:=D;
Result:=ShowModal=mrOK;
If Result then
Self.Date:=Dlg.Date;
Finally
Free;
end;
end; end;
function TCalendarDialog.CreateForm: TCalendarDialogForm; function TCalendarDialog.IsTitleStored: Boolean;
begin begin
Result:=CreateCalendarForm(Self,HelpContext); Result:=DialogTitle<>rsPickDate;//controllare
Result.Caption:=DialogTitle; end;
With Result.Calendar do
function TCalendarDialog.Execute:boolean;
const dw=8;
var DF:TForm;
okButton,cancelButton:TButton;
panel:TPanel;
begin begin
displaySettings:=Self.DisplaySettings; DF:=TForm.Create(Self);
DF.Caption:=FDialogTitle;
DF.Position:=poMainFormCenter;
DF.BorderStyle:=bsDialog;
FCalendar:=TCalendar.Create(Self);
with FCalendar do begin
Parent:=DF;
DF.Width:=Width;
Align:=alTop;
DateTime:=Self.Date;
DisplaySettings:=Self.DisplaySettings;
OnDayChanged:=Self.OnDayChanged; OnDayChanged:=Self.OnDayChanged;
OnMonthChanged:=Self.OnMonthChanged; OnMonthChanged:=Self.OnMonthChanged;
OnYearChanged:=Self.OnYearChanged; OnYearChanged:=Self.OnYearChanged;
end; end;
panel:=TPanel.Create(Self);
with panel do begin
Parent:=DF;
Caption:='';
Height:=32;
DF.Height:=FCalendar.Height+Height;
Align:=alBottom;
BevelOuter:=bvLowered;
end; end;
okButton:=TButton.Create(Self);
with okButton do begin
Parent:=panel;
Caption:=OKCaption;
Constraints.MinWidth:=75;
Constraints.MaxWidth:=FCalendar.Width div 2;
Width:=DF.Canvas.TextWidth(OKCaption)+2*dw;
ModalResult:=mrOK;
OnClick:=@GetNewDate;
Align:=alRight;
end;
cancelButton:=TButton.Create(Self);
with cancelButton do begin
Parent:=panel;
Caption:=CancelCaption;
Constraints.MinWidth:=75;
Constraints.MaxWidth:=FCalendar.Width div 2;
Width:=DF.Canvas.TextWidth(CancelCaption)+2*dw;;
ModalResult:=mrCancel;
Align:=alLeft;
end;
Result:=DF.ShowModal=mrOK;
FreeAndNil(FCalendar);
FreeAndNil(panel);
FreeAndNil(okButton);
FreeAndNil(cancelButton);
FreeAndNil(DF);
end;
Initialization Initialization
{$i extdlgs.lrs} {$i extdlgs.lrs}