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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma interface "calctrl.h"
16 #ifndef _WX_GENERIC_CALCTRL_H
17 #define _WX_GENERIC_CALCTRL_H
19 #include "wx/control.h" // the base class
20 #include "wx/dcclient.h" // for wxPaintDC
22 class WXDLLEXPORT wxComboBox
;
23 class WXDLLEXPORT wxStaticText
;
24 class WXDLLEXPORT wxSpinCtrl
;
26 #define wxCalendarNameStr _T("CalendarCtrl")
28 // ----------------------------------------------------------------------------
29 // wxCalendarCtrl: a control allowing the user to pick a date interactively
30 // ----------------------------------------------------------------------------
32 class WXDLLIMPEXP_ADV wxCalendarCtrl
: public wxControl
36 wxCalendarCtrl() { Init(); }
37 wxCalendarCtrl(wxWindow
*parent
,
39 const wxDateTime
& date
= wxDefaultDateTime
,
40 const wxPoint
& pos
= wxDefaultPosition
,
41 const wxSize
& size
= wxDefaultSize
,
42 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
43 const wxString
& name
= wxCalendarNameStr
);
45 bool Create(wxWindow
*parent
,
47 const wxDateTime
& date
= wxDefaultDateTime
,
48 const wxPoint
& pos
= wxDefaultPosition
,
49 const wxSize
& size
= wxDefaultSize
,
50 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
51 const wxString
& name
= wxCalendarNameStr
);
53 virtual ~wxCalendarCtrl();
55 virtual bool Destroy();
57 // set/get the current date
58 // ------------------------
60 bool SetDate(const wxDateTime
& date
); // we need to be able to control if the event should be sent in SetDateAndNotify(...)
61 const wxDateTime
& GetDate() const { return m_date
; }
63 // set/get the range in which selection can occur
64 // ---------------------------------------------
66 bool SetLowerDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
67 const wxDateTime
& GetLowerDateLimit() const { return m_lowdate
; }
68 bool SetUpperDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
69 const wxDateTime
& GetUpperDateLimit() const { return m_highdate
; }
71 bool SetDateRange(const wxDateTime
& lowerdate
= wxDefaultDateTime
, const wxDateTime
& upperdate
= wxDefaultDateTime
);
76 // some calendar styles can't be changed after the control creation by
77 // just using SetWindowStyle() and Refresh() and the functions below
78 // should be used instead for them
80 // corresponds to wxCAL_NO_YEAR_CHANGE bit
81 void EnableYearChange(bool enable
= true);
83 // corresponds to wxCAL_NO_MONTH_CHANGE bit
84 void EnableMonthChange(bool enable
= true);
86 // corresponds to wxCAL_SHOW_HOLIDAYS bit
87 void EnableHolidayDisplay(bool display
= true);
92 // header colours are used for painting the weekdays at the top
93 void SetHeaderColours(const wxColour
& colFg
, const wxColour
& colBg
)
95 m_colHeaderFg
= colFg
;
96 m_colHeaderBg
= colBg
;
99 const wxColour
& GetHeaderColourFg() const { return m_colHeaderFg
; }
100 const wxColour
& GetHeaderColourBg() const { return m_colHeaderBg
; }
102 // highlight colour is used for the currently selected date
103 void SetHighlightColours(const wxColour
& colFg
, const wxColour
& colBg
)
105 m_colHighlightFg
= colFg
;
106 m_colHighlightBg
= colBg
;
109 const wxColour
& GetHighlightColourFg() const { return m_colHighlightFg
; }
110 const wxColour
& GetHighlightColourBg() const { return m_colHighlightBg
; }
112 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
113 void SetHolidayColours(const wxColour
& colFg
, const wxColour
& colBg
)
115 m_colHolidayFg
= colFg
;
116 m_colHolidayBg
= colBg
;
119 const wxColour
& GetHolidayColourFg() const { return m_colHolidayFg
; }
120 const wxColour
& GetHolidayColourBg() const { return m_colHolidayBg
; }
122 // an item without custom attributes is drawn with the default colours and
123 // font and without border, setting custom attributes allows to modify this
125 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
126 // corresponding attribute is just unused if there is no such day in the
129 wxCalendarDateAttr
*GetAttr(size_t day
) const
131 wxCHECK_MSG( day
> 0 && day
< 32, NULL
, _T("invalid day") );
133 return m_attrs
[day
- 1];
136 void SetAttr(size_t day
, wxCalendarDateAttr
*attr
)
138 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day") );
140 delete m_attrs
[day
- 1];
141 m_attrs
[day
- 1] = attr
;
144 void SetHoliday(size_t day
);
146 void ResetAttr(size_t day
) { SetAttr(day
, (wxCalendarDateAttr
*)NULL
); }
148 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
149 // with the corresponding value (none for NOWHERE, the date for DAY and wd
151 wxCalendarHitTestResult
HitTest(const wxPoint
& pos
,
152 wxDateTime
*date
= NULL
,
153 wxDateTime::WeekDay
*wd
= NULL
);
155 // implementation only from now on
156 // -------------------------------
158 // forward these functions to all subcontrols
159 virtual bool Enable(bool enable
= true);
160 virtual bool Show(bool show
= true);
162 virtual wxVisualAttributes
GetDefaultAttributes() const
163 { return GetClassDefaultAttributes(GetWindowVariant()); }
165 static wxVisualAttributes
166 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
169 // common part of all ctors
173 void OnPaint(wxPaintEvent
& event
);
174 void OnClick(wxMouseEvent
& event
);
175 void OnDClick(wxMouseEvent
& event
);
176 void OnChar(wxKeyEvent
& event
);
177 void OnMonthChange(wxCommandEvent
& event
);
178 void OnYearChange(wxCommandEvent
& event
);
179 void OnYearTextChange(wxCommandEvent
& event
);
181 // override some base class virtuals
182 virtual wxSize
DoGetBestSize() const;
183 virtual void DoGetPosition(int *x
, int *y
) const;
184 virtual void DoGetSize(int *width
, int *height
) const;
185 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
186 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
188 // (re)calc m_widthCol and m_heightRow
189 void RecalcGeometry();
191 // set the date and send the notification
192 void SetDateAndNotify(const wxDateTime
& date
);
194 // get the week (row, in range 1..6) for the given date
195 size_t GetWeek(const wxDateTime
& date
) const;
197 // get the date from which we start drawing days
198 wxDateTime
GetStartDate() const;
200 // is this date shown?
201 bool IsDateShown(const wxDateTime
& date
) const;
203 // is this date in the given range?
204 bool IsDateInRange(const wxDateTime
& date
) const;
207 bool ChangeYear(wxDateTime
* target
) const;
208 bool ChangeMonth(wxDateTime
* target
) const;
210 // redraw the given date
211 void RefreshDate(const wxDateTime
& date
);
213 // change the date inside the same month/year
214 void ChangeDay(const wxDateTime
& date
);
216 // set the attributes for the holidays if needed
217 void SetHolidayAttrs();
219 // reset all holidays
220 void ResetHolidayAttrs();
222 // generate the given calendar event(s)
223 void GenerateEvent(wxEventType type
)
225 wxCalendarEvent
event(this, type
);
226 (void)GetEventHandler()->ProcessEvent(event
);
229 void GenerateEvents(wxEventType type1
, wxEventType type2
)
231 GenerateEvent(type1
);
232 GenerateEvent(type2
);
235 // do we allow changing the month/year?
236 bool AllowMonthChange() const
238 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
239 != wxCAL_NO_MONTH_CHANGE
;
241 bool AllowYearChange() const
243 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
246 // show the correct controls
247 void ShowCurrentControls();
249 // create the month combo and year spin controls
250 void CreateMonthComboBox();
251 void CreateYearSpinCtrl();
254 // get the currently shown control for month/year
255 wxControl
*GetMonthControl() const;
256 wxControl
*GetYearControl() const;
259 // OnPaint helper-methods
261 // Highlight the [fromdate : todate] range using pen and brush
262 void HighlightRange(wxPaintDC
* dc
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pen
, wxBrush
* brush
);
264 // Get the "coordinates" for the date relative to the month currently displayed.
265 // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6)
266 // if the date isn't visible (-1, -1) is put in (day, week) and false is returned
267 bool GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const;
269 // Set the flag for SetDate(): otherwise it would overwrite the year
270 // typed in by the user
271 void SetUserChangedYear() { m_userChangedYear
= true; }
274 wxStaticText
*m_staticMonth
;
275 wxComboBox
*m_comboMonth
;
277 wxStaticText
*m_staticYear
;
278 wxSpinCtrl
*m_spinYear
;
280 // the current selection
284 wxDateTime m_lowdate
;
285 wxDateTime m_highdate
;
287 // default attributes
288 wxColour m_colHighlightFg
,
295 // the attributes for each of the month days
296 wxCalendarDateAttr
*m_attrs
[31];
298 // the width and height of one column/row in the calendar
303 wxRect m_leftArrowRect
,
306 // the week day names
307 wxString m_weekdays
[7];
309 // true if SetDate() is being called as the result of changing the year in
311 bool m_userChangedYear
;
313 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
314 DECLARE_EVENT_TABLE()
315 DECLARE_NO_COPY_CLASS(wxCalendarCtrl
)
318 #endif // _WX_GENERIC_CALCTRL_H