]>
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> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
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 | ||
32 | class WXDLLEXPORT wxCalendarCtrl : public wxControl | |
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, |
2ef31e80 | 46 | const wxString& name = wxCalendarNameStr) |
2ef31e80 VZ |
47 | { |
48 | Init(); | |
49 | ||
50 | (void)Create(parent, id, date, pos, size, style, name); | |
51 | } | |
52 | ||
53 | bool Create(wxWindow *parent, | |
54 | wxWindowID id, | |
55 | const wxDateTime& date = wxDefaultDateTime, | |
56 | const wxPoint& pos = wxDefaultPosition, | |
57 | const wxSize& size = wxDefaultSize, | |
4965c3d7 | 58 | long style = wxCAL_SHOW_HOLIDAYS | wxWANTS_CHARS, |
2ef31e80 VZ |
59 | const wxString& name = wxCalendarNameStr); |
60 | ||
882a8f40 VZ |
61 | virtual ~wxCalendarCtrl(); |
62 | ||
b70462f4 MB |
63 | virtual bool Destroy(); |
64 | ||
2ef31e80 | 65 | // set/get the current date |
4f6aed9c VZ |
66 | // ------------------------ |
67 | ||
37df1f33 | 68 | bool SetDate(const wxDateTime& date); // we need to be able to control if the event should be sent in SetDateAndNotify(...) |
2ef31e80 VZ |
69 | const wxDateTime& GetDate() const { return m_date; } |
70 | ||
37df1f33 VZ |
71 | // set/get the range in which selection can occur |
72 | // --------------------------------------------- | |
73 | ||
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; } | |
78 | ||
79 | bool SetDateRange(const wxDateTime& lowerdate = wxDefaultDateTime, const wxDateTime& upperdate = wxDefaultDateTime); | |
f0d5e7a2 | 80 | |
bc385ba9 VZ |
81 | // calendar mode |
82 | // ------------- | |
83 | ||
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 | |
87 | ||
88 | // corresponds to wxCAL_NO_YEAR_CHANGE bit | |
89 | void EnableYearChange(bool enable = TRUE); | |
90 | ||
91 | // corresponds to wxCAL_NO_MONTH_CHANGE bit | |
92 | void EnableMonthChange(bool enable = TRUE); | |
93 | ||
94 | // corresponds to wxCAL_SHOW_HOLIDAYS bit | |
95 | void EnableHolidayDisplay(bool display = TRUE); | |
96 | ||
4f6aed9c VZ |
97 | // customization |
98 | // ------------- | |
99 | ||
100 | // header colours are used for painting the weekdays at the top | |
101 | void SetHeaderColours(const wxColour& colFg, const wxColour& colBg) | |
102 | { | |
103 | m_colHeaderFg = colFg; | |
104 | m_colHeaderBg = colBg; | |
105 | } | |
106 | ||
107 | const wxColour& GetHeaderColourFg() const { return m_colHeaderFg; } | |
108 | const wxColour& GetHeaderColourBg() const { return m_colHeaderBg; } | |
109 | ||
110 | // highlight colour is used for the currently selected date | |
111 | void SetHighlightColours(const wxColour& colFg, const wxColour& colBg) | |
112 | { | |
113 | m_colHighlightFg = colFg; | |
114 | m_colHighlightBg = colBg; | |
115 | } | |
116 | ||
117 | const wxColour& GetHighlightColourFg() const { return m_colHighlightFg; } | |
118 | const wxColour& GetHighlightColourBg() const { return m_colHighlightBg; } | |
119 | ||
120 | // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS) | |
121 | void SetHolidayColours(const wxColour& colFg, const wxColour& colBg) | |
122 | { | |
123 | m_colHolidayFg = colFg; | |
124 | m_colHolidayBg = colBg; | |
125 | } | |
126 | ||
127 | const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; } | |
128 | const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; } | |
129 | ||
4f6aed9c VZ |
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 | |
132 | // | |
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 | |
135 | // current month | |
136 | ||
137 | wxCalendarDateAttr *GetAttr(size_t day) const | |
138 | { | |
139 | wxCHECK_MSG( day > 0 && day < 32, NULL, _T("invalid day") ); | |
140 | ||
141 | return m_attrs[day - 1]; | |
142 | } | |
143 | ||
144 | void SetAttr(size_t day, wxCalendarDateAttr *attr) | |
145 | { | |
146 | wxCHECK_RET( day > 0 && day < 32, _T("invalid day") ); | |
147 | ||
148 | delete m_attrs[day - 1]; | |
149 | m_attrs[day - 1] = attr; | |
150 | } | |
151 | ||
152 | void SetHoliday(size_t day); | |
153 | ||
154 | void ResetAttr(size_t day) { SetAttr(day, (wxCalendarDateAttr *)NULL); } | |
155 | ||
0185cd09 VZ |
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 | |
158 | // for HEADER) | |
159 | wxCalendarHitTestResult HitTest(const wxPoint& pos, | |
160 | wxDateTime *date = NULL, | |
161 | wxDateTime::WeekDay *wd = NULL); | |
2ef31e80 | 162 | |
882a8f40 VZ |
163 | // implementation only from now on |
164 | // ------------------------------- | |
165 | ||
166 | // forward these functions to all subcontrols | |
167 | virtual bool Enable(bool enable = TRUE); | |
168 | virtual bool Show(bool show = TRUE); | |
169 | ||
0185cd09 VZ |
170 | private: |
171 | // common part of all ctors | |
172 | void Init(); | |
173 | ||
882a8f40 VZ |
174 | // event handlers |
175 | void OnPaint(wxPaintEvent& event); | |
176 | void OnClick(wxMouseEvent& event); | |
0185cd09 | 177 | void OnDClick(wxMouseEvent& event); |
882a8f40 VZ |
178 | void OnChar(wxKeyEvent& event); |
179 | void OnMonthChange(wxCommandEvent& event); | |
f0d5e7a2 | 180 | void OnYearChange(wxCommandEvent& event); |
882a8f40 | 181 | |
2ef31e80 VZ |
182 | // override some base class virtuals |
183 | virtual wxSize DoGetBestSize() const; | |
882a8f40 VZ |
184 | virtual void DoGetPosition(int *x, int *y) const; |
185 | virtual void DoGetSize(int *width, int *height) const; | |
2ef31e80 VZ |
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); | |
188 | ||
9d9b7755 VZ |
189 | // (re)calc m_widthCol and m_heightRow |
190 | void RecalcGeometry(); | |
191 | ||
9d9b7755 VZ |
192 | // set the date and send the notification |
193 | void SetDateAndNotify(const wxDateTime& date); | |
194 | ||
195 | // get the week (row, in range 1..6) for the given date | |
196 | size_t GetWeek(const wxDateTime& date) const; | |
197 | ||
2ef31e80 VZ |
198 | // get the date from which we start drawing days |
199 | wxDateTime GetStartDate() const; | |
200 | ||
201 | // is this date shown? | |
202 | bool IsDateShown(const wxDateTime& date) const; | |
203 | ||
37df1f33 VZ |
204 | // is this date in the given range? |
205 | bool IsDateInRange(const wxDateTime& date) const; | |
f0d5e7a2 | 206 | |
37df1f33 VZ |
207 | // range helpers |
208 | bool ChangeYear(wxDateTime* target) const; | |
209 | bool ChangeMonth(wxDateTime* target) const; | |
210 | ||
9d9b7755 VZ |
211 | // redraw the given date |
212 | void RefreshDate(const wxDateTime& date); | |
213 | ||
214 | // change the date inside the same month/year | |
215 | void ChangeDay(const wxDateTime& date); | |
216 | ||
4f6aed9c VZ |
217 | // set the attributes for the holidays if needed |
218 | void SetHolidayAttrs(); | |
219 | ||
220 | // reset all holidays | |
221 | void ResetHolidayAttrs(); | |
222 | ||
223 | // generate the given calendar event(s) | |
224 | void GenerateEvent(wxEventType type) | |
225 | { | |
226 | wxCalendarEvent event(this, type); | |
227 | (void)GetEventHandler()->ProcessEvent(event); | |
228 | } | |
229 | ||
230 | void GenerateEvents(wxEventType type1, wxEventType type2) | |
231 | { | |
232 | GenerateEvent(type1); | |
233 | GenerateEvent(type2); | |
234 | } | |
9d9b7755 | 235 | |
bc385ba9 VZ |
236 | // do we allow changing the month/year? |
237 | bool AllowMonthChange() const | |
238 | { | |
239 | return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE) | |
240 | != wxCAL_NO_MONTH_CHANGE; | |
241 | } | |
242 | bool AllowYearChange() const | |
243 | { | |
244 | return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE); | |
245 | } | |
246 | ||
247 | // show the correct controls | |
248 | void ShowCurrentControls(); | |
249 | ||
250 | // get the currently shown control for month/year | |
251 | wxControl *GetMonthControl() const; | |
252 | wxControl *GetYearControl() const; | |
253 | ||
37df1f33 | 254 | // OnPaint helper-methods |
f0d5e7a2 | 255 | |
37df1f33 VZ |
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); | |
f0d5e7a2 | 258 | |
37df1f33 VZ |
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; | |
263 | ||
2ef31e80 | 264 | // the subcontrols |
bc385ba9 | 265 | wxStaticText *m_staticMonth; |
2ef31e80 | 266 | wxComboBox *m_comboMonth; |
bc385ba9 VZ |
267 | |
268 | wxStaticText *m_staticYear; | |
2ef31e80 VZ |
269 | wxSpinCtrl *m_spinYear; |
270 | ||
4f6aed9c | 271 | // the current selection |
2ef31e80 VZ |
272 | wxDateTime m_date; |
273 | ||
37df1f33 VZ |
274 | // the date-range |
275 | wxDateTime m_lowdate; | |
276 | wxDateTime m_highdate; | |
277 | ||
4f6aed9c VZ |
278 | // default attributes |
279 | wxColour m_colHighlightFg, | |
280 | m_colHighlightBg, | |
281 | m_colHolidayFg, | |
282 | m_colHolidayBg, | |
283 | m_colHeaderFg, | |
284 | m_colHeaderBg; | |
285 | ||
286 | // the attributes for each of the month days | |
287 | wxCalendarDateAttr *m_attrs[31]; | |
288 | ||
2ef31e80 VZ |
289 | // the width and height of one column/row in the calendar |
290 | wxCoord m_widthCol, | |
37df1f33 VZ |
291 | m_heightRow, |
292 | m_rowOffset; | |
293 | ||
294 | wxRect m_leftArrowRect, | |
f0d5e7a2 | 295 | m_rightArrowRect; |
2ef31e80 | 296 | |
9d9b7755 VZ |
297 | // the week day names |
298 | wxString m_weekdays[7]; | |
299 | ||
f0d5e7a2 VZ |
300 | // TRUE if SetDate() is being called as the result of changing the year in |
301 | // the year control | |
302 | bool m_userChangedYear; | |
303 | ||
2ef31e80 VZ |
304 | DECLARE_DYNAMIC_CLASS(wxCalendarCtrl) |
305 | DECLARE_EVENT_TABLE() | |
306 | }; | |
307 | ||
308 | #endif // _WX_GENERIC_CALCTRL_H |