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