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