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
);
171 void OnCalMonthChange(wxCalendarEvent
& event
);
173 // override some base class virtuals
174 virtual wxSize
DoGetBestSize() const;
175 virtual void DoGetPosition(int *x
, int *y
) const;
176 virtual void DoGetSize(int *width
, int *height
) const;
177 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
178 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
180 // (re)calc m_widthCol and m_heightRow
181 void RecalcGeometry();
183 // set the date and send the notification
184 void SetDateAndNotify(const wxDateTime
& date
);
186 // get the week (row, in range 1..6) for the given date
187 size_t GetWeek(const wxDateTime
& date
) const;
189 // get the date from which we start drawing days
190 wxDateTime
GetStartDate() const;
192 // is this date shown?
193 bool IsDateShown(const wxDateTime
& date
) const;
195 // redraw the given date
196 void RefreshDate(const wxDateTime
& date
);
198 // change the date inside the same month/year
199 void ChangeDay(const wxDateTime
& date
);
201 // set the attributes for the holidays if needed
202 void SetHolidayAttrs();
204 // reset all holidays
205 void ResetHolidayAttrs();
207 // generate the given calendar event(s)
208 void GenerateEvent(wxEventType type
)
210 wxCalendarEvent
event(this, type
);
211 (void)GetEventHandler()->ProcessEvent(event
);
214 void GenerateEvents(wxEventType type1
, wxEventType type2
)
216 GenerateEvent(type1
);
217 GenerateEvent(type2
);
220 // do we allow changing the month/year?
221 bool AllowMonthChange() const
223 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
224 != wxCAL_NO_MONTH_CHANGE
;
226 bool AllowYearChange() const
228 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
231 // show the correct controls
232 void ShowCurrentControls();
234 // get the currently shown control for month/year
235 wxControl
*GetMonthControl() const;
236 wxControl
*GetYearControl() const;
239 wxStaticText
*m_staticMonth
;
240 wxComboBox
*m_comboMonth
;
242 wxStaticText
*m_staticYear
;
243 wxSpinCtrl
*m_spinYear
;
245 // the current selection
248 // default attributes
249 wxColour m_colHighlightFg
,
256 // the attributes for each of the month days
257 wxCalendarDateAttr
*m_attrs
[31];
259 // the width and height of one column/row in the calendar
263 // the week day names
264 wxString m_weekdays
[7];
266 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
267 DECLARE_EVENT_TABLE()
270 #endif // _WX_GENERIC_CALCTRL_H