]>
Commit | Line | Data |
---|---|---|
2ef31e80 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: generic/calctrl.h | |
3 | // Purpose: generic implementation of date-picker control | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29.12.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
2ef31e80 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12028905 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
2ef31e80 VZ |
13 | #pragma interface "calctrl.h" |
14 | #endif | |
15 | ||
16 | #ifndef _WX_GENERIC_CALCTRL_H | |
17 | #define _WX_GENERIC_CALCTRL_H | |
18 | ||
19 | #include "wx/control.h" // the base class | |
49a47c50 | 20 | #include "wx/dcclient.h" // for wxPaintDC |
4f6aed9c VZ |
21 | |
22 | class WXDLLEXPORT wxComboBox; | |
bc385ba9 | 23 | class WXDLLEXPORT wxStaticText; |
f0d5e7a2 | 24 | class WXDLLEXPORT wxSpinCtrl; |
2ef31e80 VZ |
25 | |
26 | #define wxCalendarNameStr _T("CalendarCtrl") | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // wxCalendarCtrl: a control allowing the user to pick a date interactively | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
12f190b0 | 32 | class WXDLLIMPEXP_ADV wxCalendarCtrl : public wxControl |
2ef31e80 | 33 | { |
0185cd09 VZ |
34 | friend class wxMonthComboBox; |
35 | friend class wxYearSpinCtrl; | |
36 | ||
2ef31e80 VZ |
37 | public: |
38 | // construction | |
39 | wxCalendarCtrl() { Init(); } | |
40 | wxCalendarCtrl(wxWindow *parent, | |
41 | wxWindowID id, | |
42 | const wxDateTime& date = wxDefaultDateTime, | |
43 | const wxPoint& pos = wxDefaultPosition, | |
44 | const wxSize& size = wxDefaultSize, | |
4965c3d7 | 45 | long style = wxCAL_SHOW_HOLIDAYS | wxWANTS_CHARS, |
2b5f62a0 | 46 | const wxString& name = wxCalendarNameStr); |
2ef31e80 VZ |
47 | |
48 | bool Create(wxWindow *parent, | |
49 | wxWindowID id, | |
50 | const wxDateTime& date = wxDefaultDateTime, | |
51 | const wxPoint& pos = wxDefaultPosition, | |
52 | const wxSize& size = wxDefaultSize, | |
4965c3d7 | 53 | long style = wxCAL_SHOW_HOLIDAYS | wxWANTS_CHARS, |
2ef31e80 VZ |
54 | const wxString& name = wxCalendarNameStr); |
55 | ||
882a8f40 VZ |
56 | virtual ~wxCalendarCtrl(); |
57 | ||
b70462f4 MB |
58 | virtual bool Destroy(); |
59 | ||
2ef31e80 | 60 | // set/get the current date |
4f6aed9c VZ |
61 | // ------------------------ |
62 | ||
37df1f33 | 63 | bool SetDate(const wxDateTime& date); // we need to be able to control if the event should be sent in SetDateAndNotify(...) |
2ef31e80 VZ |
64 | const wxDateTime& GetDate() const { return m_date; } |
65 | ||
37df1f33 VZ |
66 | // set/get the range in which selection can occur |
67 | // --------------------------------------------- | |
68 | ||
69 | bool SetLowerDateLimit(const wxDateTime& date = wxDefaultDateTime); | |
70 | const wxDateTime& GetLowerDateLimit() const { return m_lowdate; } | |
71 | bool SetUpperDateLimit(const wxDateTime& date = wxDefaultDateTime); | |
72 | const wxDateTime& GetUpperDateLimit() const { return m_highdate; } | |
73 | ||
74 | bool SetDateRange(const wxDateTime& lowerdate = wxDefaultDateTime, const wxDateTime& upperdate = wxDefaultDateTime); | |
f0d5e7a2 | 75 | |
bc385ba9 VZ |
76 | // calendar mode |
77 | // ------------- | |
78 | ||
79 | // some calendar styles can't be changed after the control creation by | |
80 | // just using SetWindowStyle() and Refresh() and the functions below | |
81 | // should be used instead for them | |
82 | ||
83 | // corresponds to wxCAL_NO_YEAR_CHANGE bit | |
ca65c044 | 84 | void EnableYearChange(bool enable = true); |
bc385ba9 VZ |
85 | |
86 | // corresponds to wxCAL_NO_MONTH_CHANGE bit | |
ca65c044 | 87 | void EnableMonthChange(bool enable = true); |
bc385ba9 VZ |
88 | |
89 | // corresponds to wxCAL_SHOW_HOLIDAYS bit | |
ca65c044 | 90 | void EnableHolidayDisplay(bool display = true); |
bc385ba9 | 91 | |
4f6aed9c VZ |
92 | // customization |
93 | // ------------- | |
94 | ||
95 | // header colours are used for painting the weekdays at the top | |
96 | void SetHeaderColours(const wxColour& colFg, const wxColour& colBg) | |
97 | { | |
98 | m_colHeaderFg = colFg; | |
99 | m_colHeaderBg = colBg; | |
100 | } | |
101 | ||
102 | const wxColour& GetHeaderColourFg() const { return m_colHeaderFg; } | |
103 | const wxColour& GetHeaderColourBg() const { return m_colHeaderBg; } | |
104 | ||
105 | // highlight colour is used for the currently selected date | |
106 | void SetHighlightColours(const wxColour& colFg, const wxColour& colBg) | |
107 | { | |
108 | m_colHighlightFg = colFg; | |
109 | m_colHighlightBg = colBg; | |
110 | } | |
111 | ||
112 | const wxColour& GetHighlightColourFg() const { return m_colHighlightFg; } | |
113 | const wxColour& GetHighlightColourBg() const { return m_colHighlightBg; } | |
114 | ||
115 | // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS) | |
116 | void SetHolidayColours(const wxColour& colFg, const wxColour& colBg) | |
117 | { | |
118 | m_colHolidayFg = colFg; | |
119 | m_colHolidayBg = colBg; | |
120 | } | |
121 | ||
122 | const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; } | |
123 | const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; } | |
124 | ||
4f6aed9c VZ |
125 | // an item without custom attributes is drawn with the default colours and |
126 | // font and without border, setting custom attributes allows to modify this | |
127 | // | |
128 | // the day parameter should be in 1..31 range, for days 29, 30, 31 the | |
129 | // corresponding attribute is just unused if there is no such day in the | |
130 | // current month | |
131 | ||
132 | wxCalendarDateAttr *GetAttr(size_t day) const | |
133 | { | |
134 | wxCHECK_MSG( day > 0 && day < 32, NULL, _T("invalid day") ); | |
135 | ||
136 | return m_attrs[day - 1]; | |
137 | } | |
138 | ||
139 | void SetAttr(size_t day, wxCalendarDateAttr *attr) | |
140 | { | |
141 | wxCHECK_RET( day > 0 && day < 32, _T("invalid day") ); | |
142 | ||
143 | delete m_attrs[day - 1]; | |
144 | m_attrs[day - 1] = attr; | |
145 | } | |
146 | ||
147 | void SetHoliday(size_t day); | |
148 | ||
149 | void ResetAttr(size_t day) { SetAttr(day, (wxCalendarDateAttr *)NULL); } | |
150 | ||
0185cd09 VZ |
151 | // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd |
152 | // with the corresponding value (none for NOWHERE, the date for DAY and wd | |
153 | // for HEADER) | |
154 | wxCalendarHitTestResult HitTest(const wxPoint& pos, | |
155 | wxDateTime *date = NULL, | |
156 | wxDateTime::WeekDay *wd = NULL); | |
2ef31e80 | 157 | |
882a8f40 VZ |
158 | // implementation only from now on |
159 | // ------------------------------- | |
160 | ||
161 | // forward these functions to all subcontrols | |
ca65c044 WS |
162 | virtual bool Enable(bool enable = true); |
163 | virtual bool Show(bool show = true); | |
882a8f40 | 164 | |
ab473127 RD |
165 | virtual wxVisualAttributes GetDefaultAttributes() const |
166 | { return GetClassDefaultAttributes(GetWindowVariant()); } | |
167 | ||
168 | static wxVisualAttributes | |
169 | GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL); | |
170 | ||
0185cd09 VZ |
171 | private: |
172 | // common part of all ctors | |
173 | void Init(); | |
174 | ||
882a8f40 VZ |
175 | // event handlers |
176 | void OnPaint(wxPaintEvent& event); | |
177 | void OnClick(wxMouseEvent& event); | |
0185cd09 | 178 | void OnDClick(wxMouseEvent& event); |
882a8f40 VZ |
179 | void OnChar(wxKeyEvent& event); |
180 | void OnMonthChange(wxCommandEvent& event); | |
f0d5e7a2 | 181 | void OnYearChange(wxCommandEvent& event); |
882a8f40 | 182 | |
2ef31e80 VZ |
183 | // override some base class virtuals |
184 | virtual wxSize DoGetBestSize() const; | |
882a8f40 VZ |
185 | virtual void DoGetPosition(int *x, int *y) const; |
186 | virtual void DoGetSize(int *width, int *height) const; | |
2ef31e80 VZ |
187 | virtual void DoSetSize(int x, int y, int width, int height, int sizeFlags); |
188 | virtual void DoMoveWindow(int x, int y, int width, int height); | |
189 | ||
9d9b7755 VZ |
190 | // (re)calc m_widthCol and m_heightRow |
191 | void RecalcGeometry(); | |
192 | ||
9d9b7755 VZ |
193 | // set the date and send the notification |
194 | void SetDateAndNotify(const wxDateTime& date); | |
195 | ||
196 | // get the week (row, in range 1..6) for the given date | |
197 | size_t GetWeek(const wxDateTime& date) const; | |
198 | ||
2ef31e80 VZ |
199 | // get the date from which we start drawing days |
200 | wxDateTime GetStartDate() const; | |
201 | ||
202 | // is this date shown? | |
203 | bool IsDateShown(const wxDateTime& date) const; | |
204 | ||
37df1f33 VZ |
205 | // is this date in the given range? |
206 | bool IsDateInRange(const wxDateTime& date) const; | |
f0d5e7a2 | 207 | |
37df1f33 VZ |
208 | // range helpers |
209 | bool ChangeYear(wxDateTime* target) const; | |
210 | bool ChangeMonth(wxDateTime* target) const; | |
211 | ||
9d9b7755 VZ |
212 | // redraw the given date |
213 | void RefreshDate(const wxDateTime& date); | |
214 | ||
215 | // change the date inside the same month/year | |
216 | void ChangeDay(const wxDateTime& date); | |
217 | ||
4f6aed9c VZ |
218 | // set the attributes for the holidays if needed |
219 | void SetHolidayAttrs(); | |
220 | ||
221 | // reset all holidays | |
222 | void ResetHolidayAttrs(); | |
223 | ||
224 | // generate the given calendar event(s) | |
225 | void GenerateEvent(wxEventType type) | |
226 | { | |
227 | wxCalendarEvent event(this, type); | |
228 | (void)GetEventHandler()->ProcessEvent(event); | |
229 | } | |
230 | ||
231 | void GenerateEvents(wxEventType type1, wxEventType type2) | |
232 | { | |
233 | GenerateEvent(type1); | |
234 | GenerateEvent(type2); | |
235 | } | |
9d9b7755 | 236 | |
bc385ba9 VZ |
237 | // do we allow changing the month/year? |
238 | bool AllowMonthChange() const | |
239 | { | |
240 | return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE) | |
241 | != wxCAL_NO_MONTH_CHANGE; | |
242 | } | |
243 | bool AllowYearChange() const | |
244 | { | |
245 | return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE); | |
246 | } | |
247 | ||
248 | // show the correct controls | |
249 | void ShowCurrentControls(); | |
250 | ||
b6fa290b | 251 | public: |
bc385ba9 VZ |
252 | // get the currently shown control for month/year |
253 | wxControl *GetMonthControl() const; | |
254 | wxControl *GetYearControl() const; | |
255 | ||
b6fa290b | 256 | private: |
37df1f33 | 257 | // OnPaint helper-methods |
f0d5e7a2 | 258 | |
37df1f33 VZ |
259 | // Highlight the [fromdate : todate] range using pen and brush |
260 | void HighlightRange(wxPaintDC* dc, const wxDateTime& fromdate, const wxDateTime& todate, wxPen* pen, wxBrush* brush); | |
f0d5e7a2 | 261 | |
37df1f33 VZ |
262 | // Get the "coordinates" for the date relative to the month currently displayed. |
263 | // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6) | |
264 | // if the date isn't visible (-1, -1) is put in (day, week) and false is returned | |
265 | bool GetDateCoord(const wxDateTime& date, int *day, int *week) const; | |
266 | ||
605dfd91 JS |
267 | // Set the flag for SetDate(): otherwise it would overwrite the year |
268 | // typed in by the user | |
ca65c044 | 269 | void SetUserChangedYear() { m_userChangedYear = true; } |
605dfd91 | 270 | |
2ef31e80 | 271 | // the subcontrols |
bc385ba9 | 272 | wxStaticText *m_staticMonth; |
2ef31e80 | 273 | wxComboBox *m_comboMonth; |
bc385ba9 VZ |
274 | |
275 | wxStaticText *m_staticYear; | |
2ef31e80 VZ |
276 | wxSpinCtrl *m_spinYear; |
277 | ||
4f6aed9c | 278 | // the current selection |
2ef31e80 VZ |
279 | wxDateTime m_date; |
280 | ||
37df1f33 VZ |
281 | // the date-range |
282 | wxDateTime m_lowdate; | |
283 | wxDateTime m_highdate; | |
284 | ||
4f6aed9c VZ |
285 | // default attributes |
286 | wxColour m_colHighlightFg, | |
287 | m_colHighlightBg, | |
288 | m_colHolidayFg, | |
289 | m_colHolidayBg, | |
290 | m_colHeaderFg, | |
291 | m_colHeaderBg; | |
292 | ||
293 | // the attributes for each of the month days | |
294 | wxCalendarDateAttr *m_attrs[31]; | |
295 | ||
2ef31e80 VZ |
296 | // the width and height of one column/row in the calendar |
297 | wxCoord m_widthCol, | |
37df1f33 VZ |
298 | m_heightRow, |
299 | m_rowOffset; | |
300 | ||
301 | wxRect m_leftArrowRect, | |
f0d5e7a2 | 302 | m_rightArrowRect; |
2ef31e80 | 303 | |
9d9b7755 VZ |
304 | // the week day names |
305 | wxString m_weekdays[7]; | |
306 | ||
ca65c044 | 307 | // true if SetDate() is being called as the result of changing the year in |
f0d5e7a2 VZ |
308 | // the year control |
309 | bool m_userChangedYear; | |
310 | ||
2ef31e80 VZ |
311 | DECLARE_DYNAMIC_CLASS(wxCalendarCtrl) |
312 | DECLARE_EVENT_TABLE() | |
22f3361e | 313 | DECLARE_NO_COPY_CLASS(wxCalendarCtrl) |
2ef31e80 VZ |
314 | }; |
315 | ||
316 | #endif // _WX_GENERIC_CALCTRL_H |