1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.h
3 // Purpose: generic implementation of date-picker control
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_GENERIC_CALCTRL_H
13 #define _WX_GENERIC_CALCTRL_H
15 #include "wx/control.h" // the base class
16 #include "wx/dcclient.h" // for wxPaintDC
18 class WXDLLEXPORT wxComboBox
;
19 class WXDLLEXPORT wxStaticText
;
20 class WXDLLEXPORT wxSpinCtrl
;
22 #define wxCalendarNameStr _T("CalendarCtrl")
24 // ----------------------------------------------------------------------------
25 // wxCalendarCtrl: a control allowing the user to pick a date interactively
26 // ----------------------------------------------------------------------------
28 class WXDLLIMPEXP_ADV wxCalendarCtrl
: public wxControl
32 wxCalendarCtrl() { Init(); }
33 wxCalendarCtrl(wxWindow
*parent
,
35 const wxDateTime
& date
= wxDefaultDateTime
,
36 const wxPoint
& pos
= wxDefaultPosition
,
37 const wxSize
& size
= wxDefaultSize
,
38 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
39 const wxString
& name
= wxCalendarNameStr
);
41 bool Create(wxWindow
*parent
,
43 const wxDateTime
& date
= wxDefaultDateTime
,
44 const wxPoint
& pos
= wxDefaultPosition
,
45 const wxSize
& size
= wxDefaultSize
,
46 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
47 const wxString
& name
= wxCalendarNameStr
);
49 virtual ~wxCalendarCtrl();
51 virtual bool Destroy();
53 // set/get the current date
54 // ------------------------
56 bool SetDate(const wxDateTime
& date
); // we need to be able to control if the event should be sent in SetDateAndNotify(...)
57 const wxDateTime
& GetDate() const { return m_date
; }
59 // set/get the range in which selection can occur
60 // ---------------------------------------------
62 bool SetLowerDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
63 const wxDateTime
& GetLowerDateLimit() const { return m_lowdate
; }
64 bool SetUpperDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
65 const wxDateTime
& GetUpperDateLimit() const { return m_highdate
; }
67 bool SetDateRange(const wxDateTime
& lowerdate
= wxDefaultDateTime
, const wxDateTime
& upperdate
= wxDefaultDateTime
);
72 // some calendar styles can't be changed after the control creation by
73 // just using SetWindowStyle() and Refresh() and the functions below
74 // should be used instead for them
76 // corresponds to wxCAL_NO_YEAR_CHANGE bit
77 void EnableYearChange(bool enable
= true);
79 // corresponds to wxCAL_NO_MONTH_CHANGE bit
80 void EnableMonthChange(bool enable
= true);
82 // corresponds to wxCAL_SHOW_HOLIDAYS bit
83 void EnableHolidayDisplay(bool display
= true);
88 // header colours are used for painting the weekdays at the top
89 void SetHeaderColours(const wxColour
& colFg
, const wxColour
& colBg
)
91 m_colHeaderFg
= colFg
;
92 m_colHeaderBg
= colBg
;
95 const wxColour
& GetHeaderColourFg() const { return m_colHeaderFg
; }
96 const wxColour
& GetHeaderColourBg() const { return m_colHeaderBg
; }
98 // highlight colour is used for the currently selected date
99 void SetHighlightColours(const wxColour
& colFg
, const wxColour
& colBg
)
101 m_colHighlightFg
= colFg
;
102 m_colHighlightBg
= colBg
;
105 const wxColour
& GetHighlightColourFg() const { return m_colHighlightFg
; }
106 const wxColour
& GetHighlightColourBg() const { return m_colHighlightBg
; }
108 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
109 void SetHolidayColours(const wxColour
& colFg
, const wxColour
& colBg
)
111 m_colHolidayFg
= colFg
;
112 m_colHolidayBg
= colBg
;
115 const wxColour
& GetHolidayColourFg() const { return m_colHolidayFg
; }
116 const wxColour
& GetHolidayColourBg() const { return m_colHolidayBg
; }
118 // an item without custom attributes is drawn with the default colours and
119 // font and without border, setting custom attributes allows to modify this
121 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
122 // corresponding attribute is just unused if there is no such day in the
125 wxCalendarDateAttr
*GetAttr(size_t day
) const
127 wxCHECK_MSG( day
> 0 && day
< 32, NULL
, _T("invalid day") );
129 return m_attrs
[day
- 1];
132 void SetAttr(size_t day
, wxCalendarDateAttr
*attr
)
134 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day") );
136 delete m_attrs
[day
- 1];
137 m_attrs
[day
- 1] = attr
;
140 void SetHoliday(size_t day
);
142 void ResetAttr(size_t day
) { SetAttr(day
, (wxCalendarDateAttr
*)NULL
); }
144 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
145 // with the corresponding value (none for NOWHERE, the date for DAY and wd
147 wxCalendarHitTestResult
HitTest(const wxPoint
& pos
,
148 wxDateTime
*date
= NULL
,
149 wxDateTime::WeekDay
*wd
= NULL
);
151 // implementation only from now on
152 // -------------------------------
154 // forward these functions to all subcontrols
155 virtual bool Enable(bool enable
= true);
156 virtual bool Show(bool show
= true);
158 virtual void SetWindowStyleFlag(long style
);
160 virtual wxVisualAttributes
GetDefaultAttributes() const
161 { return GetClassDefaultAttributes(GetWindowVariant()); }
163 static wxVisualAttributes
164 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
166 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
169 // override some base class virtuals
170 virtual wxSize
DoGetBestSize() const;
171 virtual void DoGetPosition(int *x
, int *y
) const;
172 virtual void DoGetSize(int *width
, int *height
) const;
173 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
174 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
177 // common part of all ctors
180 // startup colours and reinitialization after colour changes in system
184 void OnPaint(wxPaintEvent
& event
);
185 void OnClick(wxMouseEvent
& event
);
186 void OnDClick(wxMouseEvent
& event
);
187 void OnChar(wxKeyEvent
& event
);
188 void OnMonthChange(wxCommandEvent
& event
);
189 void OnYearChange(wxCommandEvent
& event
);
190 void OnYearTextChange(wxCommandEvent
& event
);
192 // (re)calc m_widthCol and m_heightRow
193 void RecalcGeometry();
195 // set the date and send the notification
196 void SetDateAndNotify(const wxDateTime
& date
);
198 // get the week (row, in range 1..6) for the given date
199 size_t GetWeek(const wxDateTime
& date
) const;
201 // get the date from which we start drawing days
202 wxDateTime
GetStartDate() const;
204 // is this date shown?
205 bool IsDateShown(const wxDateTime
& date
) const;
207 // is this date in the given range?
208 bool IsDateInRange(const wxDateTime
& date
) const;
211 bool ChangeYear(wxDateTime
* target
) const;
212 bool ChangeMonth(wxDateTime
* target
) const;
214 // redraw the given date
215 void RefreshDate(const wxDateTime
& date
);
217 // change the date inside the same month/year
218 void ChangeDay(const wxDateTime
& date
);
220 // set the attributes for the holidays if needed
221 void SetHolidayAttrs();
223 // reset all holidays
224 void ResetHolidayAttrs();
226 // generate the given calendar event(s)
227 void GenerateEvent(wxEventType type
)
229 wxCalendarEvent
event(this, type
);
230 (void)GetEventHandler()->ProcessEvent(event
);
233 void GenerateEvents(wxEventType type1
, wxEventType type2
)
235 GenerateEvent(type1
);
236 GenerateEvent(type2
);
239 // do we allow changing the month/year?
240 bool AllowMonthChange() const
242 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
243 != wxCAL_NO_MONTH_CHANGE
;
245 bool AllowYearChange() const
247 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
250 // show the correct controls
251 void ShowCurrentControls();
253 // create the month combo and year spin controls
254 void CreateMonthComboBox();
255 void CreateYearSpinCtrl();
258 // get the currently shown control for month/year
259 wxControl
*GetMonthControl() const;
260 wxControl
*GetYearControl() const;
263 // OnPaint helper-methods
265 // Highlight the [fromdate : todate] range using pen and brush
266 void HighlightRange(wxPaintDC
* dc
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, const wxPen
* pen
, const wxBrush
* brush
);
268 // Get the "coordinates" for the date relative to the month currently displayed.
269 // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6)
270 // if the date isn't visible (-1, -1) is put in (day, week) and false is returned
271 bool GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const;
273 // Set the flag for SetDate(): otherwise it would overwrite the year
274 // typed in by the user
275 void SetUserChangedYear() { m_userChangedYear
= true; }
278 wxStaticText
*m_staticMonth
;
279 wxComboBox
*m_comboMonth
;
281 wxStaticText
*m_staticYear
;
282 wxSpinCtrl
*m_spinYear
;
284 // the current selection
288 wxDateTime m_lowdate
;
289 wxDateTime m_highdate
;
291 // default attributes
292 wxColour m_colHighlightFg
,
301 // the attributes for each of the month days
302 wxCalendarDateAttr
*m_attrs
[31];
304 // the width and height of one column/row in the calendar
309 wxRect m_leftArrowRect
,
312 // the week day names
313 wxString m_weekdays
[7];
315 // true if SetDate() is being called as the result of changing the year in
317 bool m_userChangedYear
;
319 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
320 DECLARE_EVENT_TABLE()
321 DECLARE_NO_COPY_CLASS(wxCalendarCtrl
)
324 #endif // _WX_GENERIC_CALCTRL_H