From 6b53432dd4b1414f3ed62fb7ce90fbfe4f9e9bf1 Mon Sep 17 00:00:00 2001 From: zoran Date: Mon, 4 Jan 2021 11:57:45 +0000 Subject: [PATCH] DateTimePicker: add InMonthView function to TCalendarControlWrapper - adds layer of abstraction to using of calendar "view" git-svn-id: trunk@64325 - --- .../datetimectrls/calcontrolwrapper.pas | 23 +++++++++++++++---- components/datetimectrls/datetimepicker.pas | 8 +++---- components/datetimectrls/lclcalwrapper.pas | 11 ++++++--- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/components/datetimectrls/calcontrolwrapper.pas b/components/datetimectrls/calcontrolwrapper.pas index c6dfe20f15..5c7195c129 100644 --- a/components/datetimectrls/calcontrolwrapper.pas +++ b/components/datetimectrls/calcontrolwrapper.pas @@ -21,7 +21,7 @@ calendar is clicked on date, but not when the user clicks in title area changing months or years, then we let the user keep browsing the calendar). When creating new wrapper, there are four abstract methods which need to be -overriden. Please see the coments in code below. +overriden. Please see the comments in code below. ----------------------------------------------------------- LICENCE @@ -51,11 +51,11 @@ type { TCalendarControlWrapper } - TCalendarControlWrapper = class + TCalendarControlWrapper = class abstract (TObject) private FCalendarControl: TControl; public - { There are four methods that derived classes should override: } + { There are four abstract methods that derived classes should override: } { Should be overriden to just return the class of the calendar control. } class function GetCalendarControlClass: TControlClass; virtual abstract; @@ -69,9 +69,19 @@ type { This function should return True if coordinates (X, Y) are on the date in the calendar control (DateTimePicker calls this function when the calendar is clicked, to determine whether the drop-down calendar should return the - date or not). } + date or not). } function AreCoordinatesOnDate(X, Y: Integer): Boolean; virtual abstract; + public + { Not mandatory to override: } + + { Override only if the calendar class have "views", like Windows calendar + control (LCL's TCalendar in Win WS). + Should return False only when the calendar is in some view other than month. + DateTimePicker asks this when closing calendar. } + function InMonthView: Boolean; virtual; + + public function GetCalendarControl: TControl; constructor Create; virtual; destructor Destroy; override; @@ -83,6 +93,11 @@ implementation { TCalendarControlWrapper } +function TCalendarControlWrapper.InMonthView: Boolean; +begin + Result := True; +end; + function TCalendarControlWrapper.GetCalendarControl: TControl; begin Result := FCalendarControl; diff --git a/components/datetimectrls/datetimepicker.pas b/components/datetimectrls/datetimepicker.pas index de081fd52e..f06221d186 100644 --- a/components/datetimectrls/datetimepicker.pas +++ b/components/datetimectrls/datetimepicker.pas @@ -46,7 +46,7 @@ uses clocale, // needed to initialize default locale settings on Linux. {$endif} Classes, SysUtils, Controls, LCLType, Graphics, Math, StdCtrls, Buttons, - ExtCtrls, Forms, ComCtrls, Types, LMessages, Calendar, LazUTF8, LCLIntf, + ExtCtrls, Forms, ComCtrls, Types, LMessages, LazUTF8, LCLIntf, LCLProc, Themes, CalControlWrapper; const @@ -769,9 +769,7 @@ begin case Key of VK_ESCAPE, VK_RETURN, VK_SPACE, VK_TAB: - if (not(Cal.GetCalendarControl is TCustomCalendar)) - or (TCustomCalendar(Cal.GetCalendarControl).GetCalendarView = cvMonth) - then begin + if Cal.InMonthView then begin ApplyTheDate := Key in [VK_RETURN, VK_SPACE]; Key := 0; CloseCalendarForm(ApplyTheDate); @@ -819,7 +817,7 @@ procedure TDTCalendarForm.WMActivate(var Message: TLMActivate); var PP: HWND; begin - inherited; + inherited WMActivate(Message); PP := LCLIntf.GetParent(Handle); if (PP <> 0) then diff --git a/components/datetimectrls/lclcalwrapper.pas b/components/datetimectrls/lclcalwrapper.pas index b902ae16fa..bedab9a6db 100644 --- a/components/datetimectrls/lclcalwrapper.pas +++ b/components/datetimectrls/lclcalwrapper.pas @@ -47,6 +47,7 @@ type procedure SetDate(Date: TDate); override; function GetDate: TDate; override; function AreCoordinatesOnDate(X, Y: Integer): Boolean; override; + function InMonthView: Boolean; override; constructor Create; override; destructor Destroy; override; @@ -60,7 +61,7 @@ procedure TLCLCalendarWrapper.LCLCalendarWrapperWndProc( var TheMessage: TLMessage); begin if TheMessage.msg = LM_LBUTTONDOWN then - CanClose := TCalendar(GetCalendarControl).GetCalendarView = cvMonth; + CanClose := InMonthView; if Assigned(PrevCalendarWndProc) then PrevCalendarWndProc(TheMessage); @@ -84,13 +85,17 @@ end; function TLCLCalendarWrapper.AreCoordinatesOnDate(X, Y: Integer): Boolean; begin Result := - CanClose and - (TCalendar(GetCalendarControl).GetCalendarView = cvMonth) and + CanClose and InMonthView and (TCalendar(GetCalendarControl).HitTest(Point(X, Y)) in [cpDate, cpNoWhere]); CanClose := True; end; +function TLCLCalendarWrapper.InMonthView: Boolean; +begin + Result := TCalendar(GetCalendarControl).GetCalendarView = cvMonth; +end; + constructor TLCLCalendarWrapper.Create; begin inherited Create;