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 license
10 ///////////////////////////////////////////////////////////////////////////////
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 WXDLLEXPORT wxCalendarCtrl
: public wxControl
34 friend class wxMonthComboBox
;
35 friend class wxYearSpinCtrl
;
39 wxCalendarCtrl() { Init(); }
40 wxCalendarCtrl(wxWindow
*parent
,
42 const wxDateTime
& date
= wxDefaultDateTime
,
43 const wxPoint
& pos
= wxDefaultPosition
,
44 const wxSize
& size
= wxDefaultSize
,
45 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
46 const wxString
& name
= wxCalendarNameStr
)
50 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
53 bool Create(wxWindow
*parent
,
55 const wxDateTime
& date
= wxDefaultDateTime
,
56 const wxPoint
& pos
= wxDefaultPosition
,
57 const wxSize
& size
= wxDefaultSize
,
58 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
59 const wxString
& name
= wxCalendarNameStr
);
61 virtual ~wxCalendarCtrl();
63 virtual bool Destroy();
65 // set/get the current date
66 // ------------------------
68 bool SetDate(const wxDateTime
& date
); // we need to be able to control if the event should be sent in SetDateAndNotify(...)
69 const wxDateTime
& GetDate() const { return m_date
; }
71 // set/get the range in which selection can occur
72 // ---------------------------------------------
74 bool SetLowerDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
75 const wxDateTime
& GetLowerDateLimit() const { return m_lowdate
; }
76 bool SetUpperDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
77 const wxDateTime
& GetUpperDateLimit() const { return m_highdate
; }
79 bool SetDateRange(const wxDateTime
& lowerdate
= wxDefaultDateTime
, const wxDateTime
& upperdate
= wxDefaultDateTime
);
84 // some calendar styles can't be changed after the control creation by
85 // just using SetWindowStyle() and Refresh() and the functions below
86 // should be used instead for them
88 // corresponds to wxCAL_NO_YEAR_CHANGE bit
89 void EnableYearChange(bool enable
= TRUE
);
91 // corresponds to wxCAL_NO_MONTH_CHANGE bit
92 void EnableMonthChange(bool enable
= TRUE
);
94 // corresponds to wxCAL_SHOW_HOLIDAYS bit
95 void EnableHolidayDisplay(bool display
= TRUE
);
100 // header colours are used for painting the weekdays at the top
101 void SetHeaderColours(const wxColour
& colFg
, const wxColour
& colBg
)
103 m_colHeaderFg
= colFg
;
104 m_colHeaderBg
= colBg
;
107 const wxColour
& GetHeaderColourFg() const { return m_colHeaderFg
; }
108 const wxColour
& GetHeaderColourBg() const { return m_colHeaderBg
; }
110 // highlight colour is used for the currently selected date
111 void SetHighlightColours(const wxColour
& colFg
, const wxColour
& colBg
)
113 m_colHighlightFg
= colFg
;
114 m_colHighlightBg
= colBg
;
117 const wxColour
& GetHighlightColourFg() const { return m_colHighlightFg
; }
118 const wxColour
& GetHighlightColourBg() const { return m_colHighlightBg
; }
120 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
121 void SetHolidayColours(const wxColour
& colFg
, const wxColour
& colBg
)
123 m_colHolidayFg
= colFg
;
124 m_colHolidayBg
= colBg
;
127 const wxColour
& GetHolidayColourFg() const { return m_colHolidayFg
; }
128 const wxColour
& GetHolidayColourBg() const { return m_colHolidayBg
; }
130 // an item without custom attributes is drawn with the default colours and
131 // font and without border, setting custom attributes allows to modify this
133 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
134 // corresponding attribute is just unused if there is no such day in the
137 wxCalendarDateAttr
*GetAttr(size_t day
) const
139 wxCHECK_MSG( day
> 0 && day
< 32, NULL
, _T("invalid day") );
141 return m_attrs
[day
- 1];
144 void SetAttr(size_t day
, wxCalendarDateAttr
*attr
)
146 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day") );
148 delete m_attrs
[day
- 1];
149 m_attrs
[day
- 1] = attr
;
152 void SetHoliday(size_t day
);
154 void ResetAttr(size_t day
) { SetAttr(day
, (wxCalendarDateAttr
*)NULL
); }
156 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
157 // with the corresponding value (none for NOWHERE, the date for DAY and wd
159 wxCalendarHitTestResult
HitTest(const wxPoint
& pos
,
160 wxDateTime
*date
= NULL
,
161 wxDateTime::WeekDay
*wd
= NULL
);
163 // implementation only from now on
164 // -------------------------------
166 // forward these functions to all subcontrols
167 virtual bool Enable(bool enable
= TRUE
);
168 virtual bool Show(bool show
= TRUE
);
171 // common part of all ctors
175 void OnPaint(wxPaintEvent
& event
);
176 void OnClick(wxMouseEvent
& event
);
177 void OnDClick(wxMouseEvent
& event
);
178 void OnChar(wxKeyEvent
& event
);
179 void OnMonthChange(wxCommandEvent
& event
);
180 void OnYearChange(wxCommandEvent
& event
);
182 // override some base class virtuals
183 virtual wxSize
DoGetBestSize() const;
184 virtual void DoGetPosition(int *x
, int *y
) const;
185 virtual void DoGetSize(int *width
, int *height
) const;
186 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
187 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
189 // (re)calc m_widthCol and m_heightRow
190 void RecalcGeometry();
192 // set the date and send the notification
193 void SetDateAndNotify(const wxDateTime
& date
);
195 // get the week (row, in range 1..6) for the given date
196 size_t GetWeek(const wxDateTime
& date
) const;
198 // get the date from which we start drawing days
199 wxDateTime
GetStartDate() const;
201 // is this date shown?
202 bool IsDateShown(const wxDateTime
& date
) const;
204 // is this date in the given range?
205 bool IsDateInRange(const wxDateTime
& date
) const;
208 bool ChangeYear(wxDateTime
* target
) const;
209 bool ChangeMonth(wxDateTime
* target
) const;
211 // redraw the given date
212 void RefreshDate(const wxDateTime
& date
);
214 // change the date inside the same month/year
215 void ChangeDay(const wxDateTime
& date
);
217 // set the attributes for the holidays if needed
218 void SetHolidayAttrs();
220 // reset all holidays
221 void ResetHolidayAttrs();
223 // generate the given calendar event(s)
224 void GenerateEvent(wxEventType type
)
226 wxCalendarEvent
event(this, type
);
227 (void)GetEventHandler()->ProcessEvent(event
);
230 void GenerateEvents(wxEventType type1
, wxEventType type2
)
232 GenerateEvent(type1
);
233 GenerateEvent(type2
);
236 // do we allow changing the month/year?
237 bool AllowMonthChange() const
239 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
240 != wxCAL_NO_MONTH_CHANGE
;
242 bool AllowYearChange() const
244 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
247 // show the correct controls
248 void ShowCurrentControls();
250 // get the currently shown control for month/year
251 wxControl
*GetMonthControl() const;
252 wxControl
*GetYearControl() const;
254 // OnPaint helper-methods
256 // Highlight the [fromdate : todate] range using pen and brush
257 void HighlightRange(wxPaintDC
* dc
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pen
, wxBrush
* brush
);
259 // Get the "coordinates" for the date relative to the month currently displayed.
260 // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6)
261 // if the date isn't visible (-1, -1) is put in (day, week) and false is returned
262 bool GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const;
265 wxStaticText
*m_staticMonth
;
266 wxComboBox
*m_comboMonth
;
268 wxStaticText
*m_staticYear
;
269 wxSpinCtrl
*m_spinYear
;
271 // the current selection
275 wxDateTime m_lowdate
;
276 wxDateTime m_highdate
;
278 // default attributes
279 wxColour m_colHighlightFg
,
286 // the attributes for each of the month days
287 wxCalendarDateAttr
*m_attrs
[31];
289 // the width and height of one column/row in the calendar
294 wxRect m_leftArrowRect
,
297 // the week day names
298 wxString m_weekdays
[7];
300 // TRUE if SetDate() is being called as the result of changing the year in
302 bool m_userChangedYear
;
304 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
305 DECLARE_EVENT_TABLE()
308 #endif // _WX_GENERIC_CALCTRL_H