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
21 #include "wx/spinctrl.h" // for wxSpinEvent
23 class WXDLLEXPORT wxComboBox
;
24 class WXDLLEXPORT wxStaticText
;
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
,
46 const wxString
& name
= wxCalendarNameStr
)
47 : wxControl(parent
, id
, pos
, size
,
48 style
| wxWANTS_CHARS
, wxDefaultValidator
, name
)
52 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
55 bool Create(wxWindow
*parent
,
57 const wxDateTime
& date
= wxDefaultDateTime
,
58 const wxPoint
& pos
= wxDefaultPosition
,
59 const wxSize
& size
= wxDefaultSize
,
60 long style
= wxCAL_SHOW_HOLIDAYS
,
61 const wxString
& name
= wxCalendarNameStr
);
63 virtual ~wxCalendarCtrl();
65 // set/get the current date
66 // ------------------------
68 void SetDate(const wxDateTime
& date
);
69 const wxDateTime
& GetDate() const { return m_date
; }
74 // some calendar styles can't be changed after the control creation by
75 // just using SetWindowStyle() and Refresh() and the functions below
76 // should be used instead for them
78 // corresponds to wxCAL_NO_YEAR_CHANGE bit
79 void EnableYearChange(bool enable
= TRUE
);
81 // corresponds to wxCAL_NO_MONTH_CHANGE bit
82 void EnableMonthChange(bool enable
= TRUE
);
84 // corresponds to wxCAL_SHOW_HOLIDAYS bit
85 void EnableHolidayDisplay(bool display
= TRUE
);
90 // header colours are used for painting the weekdays at the top
91 void SetHeaderColours(const wxColour
& colFg
, const wxColour
& colBg
)
93 m_colHeaderFg
= colFg
;
94 m_colHeaderBg
= colBg
;
97 const wxColour
& GetHeaderColourFg() const { return m_colHeaderFg
; }
98 const wxColour
& GetHeaderColourBg() const { return m_colHeaderBg
; }
100 // highlight colour is used for the currently selected date
101 void SetHighlightColours(const wxColour
& colFg
, const wxColour
& colBg
)
103 m_colHighlightFg
= colFg
;
104 m_colHighlightBg
= colBg
;
107 const wxColour
& GetHighlightColourFg() const { return m_colHighlightFg
; }
108 const wxColour
& GetHighlightColourBg() const { return m_colHighlightBg
; }
110 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
111 void SetHolidayColours(const wxColour
& colFg
, const wxColour
& colBg
)
113 m_colHolidayFg
= colFg
;
114 m_colHolidayBg
= colBg
;
117 const wxColour
& GetHolidayColourFg() const { return m_colHolidayFg
; }
118 const wxColour
& GetHolidayColourBg() const { return m_colHolidayBg
; }
120 // an item without custom attributes is drawn with the default colours and
121 // font and without border, setting custom attributes allows to modify this
123 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
124 // corresponding attribute is just unused if there is no such day in the
127 wxCalendarDateAttr
*GetAttr(size_t day
) const
129 wxCHECK_MSG( day
> 0 && day
< 32, NULL
, _T("invalid day") );
131 return m_attrs
[day
- 1];
134 void SetAttr(size_t day
, wxCalendarDateAttr
*attr
)
136 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day") );
138 delete m_attrs
[day
- 1];
139 m_attrs
[day
- 1] = attr
;
142 void SetHoliday(size_t day
);
144 void ResetAttr(size_t day
) { SetAttr(day
, (wxCalendarDateAttr
*)NULL
); }
146 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
147 // with the corresponding value (none for NOWHERE, the date for DAY and wd
149 wxCalendarHitTestResult
HitTest(const wxPoint
& pos
,
150 wxDateTime
*date
= NULL
,
151 wxDateTime::WeekDay
*wd
= NULL
);
153 // implementation only from now on
154 // -------------------------------
156 // forward these functions to all subcontrols
157 virtual bool Enable(bool enable
= TRUE
);
158 virtual bool Show(bool show
= TRUE
);
161 // common part of all ctors
165 void OnPaint(wxPaintEvent
& event
);
166 void OnClick(wxMouseEvent
& event
);
167 void OnDClick(wxMouseEvent
& event
);
168 void OnChar(wxKeyEvent
& event
);
169 void OnMonthChange(wxCommandEvent
& event
);
170 void OnYearChange(wxSpinEvent
& event
);
172 // override some base class virtuals
173 virtual wxSize
DoGetBestSize() const;
174 virtual void DoGetPosition(int *x
, int *y
) const;
175 virtual void DoGetSize(int *width
, int *height
) const;
176 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
177 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
179 // (re)calc m_widthCol and m_heightRow
180 void RecalcGeometry();
182 // set the date and send the notification
183 void SetDateAndNotify(const wxDateTime
& date
);
185 // get the week (row, in range 1..6) for the given date
186 size_t GetWeek(const wxDateTime
& date
) const;
188 // get the date from which we start drawing days
189 wxDateTime
GetStartDate() const;
191 // is this date shown?
192 bool IsDateShown(const wxDateTime
& date
) const;
194 // redraw the given date
195 void RefreshDate(const wxDateTime
& date
);
197 // change the date inside the same month/year
198 void ChangeDay(const wxDateTime
& date
);
200 // set the attributes for the holidays if needed
201 void SetHolidayAttrs();
203 // reset all holidays
204 void ResetHolidayAttrs();
206 // generate the given calendar event(s)
207 void GenerateEvent(wxEventType type
)
209 wxCalendarEvent
event(this, type
);
210 (void)GetEventHandler()->ProcessEvent(event
);
213 void GenerateEvents(wxEventType type1
, wxEventType type2
)
215 GenerateEvent(type1
);
216 GenerateEvent(type2
);
219 // do we allow changing the month/year?
220 bool AllowMonthChange() const
222 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
223 != wxCAL_NO_MONTH_CHANGE
;
225 bool AllowYearChange() const
227 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
230 // show the correct controls
231 void ShowCurrentControls();
233 // get the currently shown control for month/year
234 wxControl
*GetMonthControl() const;
235 wxControl
*GetYearControl() const;
238 wxStaticText
*m_staticMonth
;
239 wxComboBox
*m_comboMonth
;
241 wxStaticText
*m_staticYear
;
242 wxSpinCtrl
*m_spinYear
;
244 // the current selection
247 // default attributes
248 wxColour m_colHighlightFg
,
255 // the attributes for each of the month days
256 wxCalendarDateAttr
*m_attrs
[31];
258 // the width and height of one column/row in the calendar
262 // the week day names
263 wxString m_weekdays
[7];
265 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
266 DECLARE_EVENT_TABLE()
269 #endif // _WX_GENERIC_CALCTRL_H