]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/calctrl.h
1. added range checks in wxGridStringTable
[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 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
20
21 #include "wx/spinctrl.h" // for wxSpinEvent
22
23 class WXDLLEXPORT wxComboBox;
24 class WXDLLEXPORT wxStaticText;
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 {
34 friend class wxMonthComboBox;
35 friend class wxYearSpinCtrl;
36
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,
45 long style = wxCAL_SHOW_HOLIDAYS,
46 const wxString& name = wxCalendarNameStr)
47 : wxControl(parent, id, pos, size,
48 style | wxWANTS_CHARS, wxDefaultValidator, name)
49 {
50 Init();
51
52 (void)Create(parent, id, date, pos, size, style, name);
53 }
54
55 bool Create(wxWindow *parent,
56 wxWindowID id,
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);
62
63 virtual ~wxCalendarCtrl();
64
65 // set/get the current date
66 // ------------------------
67
68 void SetDate(const wxDateTime& date);
69 const wxDateTime& GetDate() const { return m_date; }
70
71 // calendar mode
72 // -------------
73
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
77
78 // corresponds to wxCAL_NO_YEAR_CHANGE bit
79 void EnableYearChange(bool enable = TRUE);
80
81 // corresponds to wxCAL_NO_MONTH_CHANGE bit
82 void EnableMonthChange(bool enable = TRUE);
83
84 // corresponds to wxCAL_SHOW_HOLIDAYS bit
85 void EnableHolidayDisplay(bool display = TRUE);
86
87 // customization
88 // -------------
89
90 // header colours are used for painting the weekdays at the top
91 void SetHeaderColours(const wxColour& colFg, const wxColour& colBg)
92 {
93 m_colHeaderFg = colFg;
94 m_colHeaderBg = colBg;
95 }
96
97 const wxColour& GetHeaderColourFg() const { return m_colHeaderFg; }
98 const wxColour& GetHeaderColourBg() const { return m_colHeaderBg; }
99
100 // highlight colour is used for the currently selected date
101 void SetHighlightColours(const wxColour& colFg, const wxColour& colBg)
102 {
103 m_colHighlightFg = colFg;
104 m_colHighlightBg = colBg;
105 }
106
107 const wxColour& GetHighlightColourFg() const { return m_colHighlightFg; }
108 const wxColour& GetHighlightColourBg() const { return m_colHighlightBg; }
109
110 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
111 void SetHolidayColours(const wxColour& colFg, const wxColour& colBg)
112 {
113 m_colHolidayFg = colFg;
114 m_colHolidayBg = colBg;
115 }
116
117 const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; }
118 const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; }
119
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
122 //
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
125 // current month
126
127 wxCalendarDateAttr *GetAttr(size_t day) const
128 {
129 wxCHECK_MSG( day > 0 && day < 32, NULL, _T("invalid day") );
130
131 return m_attrs[day - 1];
132 }
133
134 void SetAttr(size_t day, wxCalendarDateAttr *attr)
135 {
136 wxCHECK_RET( day > 0 && day < 32, _T("invalid day") );
137
138 delete m_attrs[day - 1];
139 m_attrs[day - 1] = attr;
140 }
141
142 void SetHoliday(size_t day);
143
144 void ResetAttr(size_t day) { SetAttr(day, (wxCalendarDateAttr *)NULL); }
145
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
148 // for HEADER)
149 wxCalendarHitTestResult HitTest(const wxPoint& pos,
150 wxDateTime *date = NULL,
151 wxDateTime::WeekDay *wd = NULL);
152
153 // implementation only from now on
154 // -------------------------------
155
156 // forward these functions to all subcontrols
157 virtual bool Enable(bool enable = TRUE);
158 virtual bool Show(bool show = TRUE);
159
160 private:
161 // common part of all ctors
162 void Init();
163
164 // event handlers
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
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);
178
179 // (re)calc m_widthCol and m_heightRow
180 void RecalcGeometry();
181
182 // set the date and send the notification
183 void SetDateAndNotify(const wxDateTime& date);
184
185 // get the week (row, in range 1..6) for the given date
186 size_t GetWeek(const wxDateTime& date) const;
187
188 // get the date from which we start drawing days
189 wxDateTime GetStartDate() const;
190
191 // is this date shown?
192 bool IsDateShown(const wxDateTime& date) const;
193
194 // redraw the given date
195 void RefreshDate(const wxDateTime& date);
196
197 // change the date inside the same month/year
198 void ChangeDay(const wxDateTime& date);
199
200 // set the attributes for the holidays if needed
201 void SetHolidayAttrs();
202
203 // reset all holidays
204 void ResetHolidayAttrs();
205
206 // generate the given calendar event(s)
207 void GenerateEvent(wxEventType type)
208 {
209 wxCalendarEvent event(this, type);
210 (void)GetEventHandler()->ProcessEvent(event);
211 }
212
213 void GenerateEvents(wxEventType type1, wxEventType type2)
214 {
215 GenerateEvent(type1);
216 GenerateEvent(type2);
217 }
218
219 // do we allow changing the month/year?
220 bool AllowMonthChange() const
221 {
222 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE)
223 != wxCAL_NO_MONTH_CHANGE;
224 }
225 bool AllowYearChange() const
226 {
227 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE);
228 }
229
230 // show the correct controls
231 void ShowCurrentControls();
232
233 // get the currently shown control for month/year
234 wxControl *GetMonthControl() const;
235 wxControl *GetYearControl() const;
236
237 // the subcontrols
238 wxStaticText *m_staticMonth;
239 wxComboBox *m_comboMonth;
240
241 wxStaticText *m_staticYear;
242 wxSpinCtrl *m_spinYear;
243
244 // the current selection
245 wxDateTime m_date;
246
247 // default attributes
248 wxColour m_colHighlightFg,
249 m_colHighlightBg,
250 m_colHolidayFg,
251 m_colHolidayBg,
252 m_colHeaderFg,
253 m_colHeaderBg;
254
255 // the attributes for each of the month days
256 wxCalendarDateAttr *m_attrs[31];
257
258 // the width and height of one column/row in the calendar
259 wxCoord m_widthCol,
260 m_heightRow;
261
262 // the week day names
263 wxString m_weekdays[7];
264
265 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl)
266 DECLARE_EVENT_TABLE()
267 };
268
269 #endif // _WX_GENERIC_CALCTRL_H