]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/calctrl.h
1. wxDateTimeHolidayAuthority class for calculating the holidays added
[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
25 #define wxCalendarNameStr _T("CalendarCtrl")
26
27 // ----------------------------------------------------------------------------
28 // wxCalendarCtrl: a control allowing the user to pick a date interactively
29 // ----------------------------------------------------------------------------
30
31 class WXDLLEXPORT wxCalendarCtrl : public wxControl
32 {
33 friend class wxMonthComboBox;
34 friend class wxYearSpinCtrl;
35
36 public:
37 // construction
38 wxCalendarCtrl() { Init(); }
39 wxCalendarCtrl(wxWindow *parent,
40 wxWindowID id,
41 const wxDateTime& date = wxDefaultDateTime,
42 const wxPoint& pos = wxDefaultPosition,
43 const wxSize& size = wxDefaultSize,
44 long style = wxCAL_SHOW_HOLIDAYS,
45 const wxString& name = wxCalendarNameStr)
46 : wxControl(parent, id, pos, size,
47 style | wxWANTS_CHARS, wxDefaultValidator, name)
48 {
49 Init();
50
51 (void)Create(parent, id, date, pos, size, style, name);
52 }
53
54 bool Create(wxWindow *parent,
55 wxWindowID id,
56 const wxDateTime& date = wxDefaultDateTime,
57 const wxPoint& pos = wxDefaultPosition,
58 const wxSize& size = wxDefaultSize,
59 long style = wxCAL_SHOW_HOLIDAYS,
60 const wxString& name = wxCalendarNameStr);
61
62 virtual ~wxCalendarCtrl();
63
64 // set/get the current date
65 // ------------------------
66
67 void SetDate(const wxDateTime& date);
68 const wxDateTime& GetDate() const { return m_date; }
69
70 // customization
71 // -------------
72
73 // header colours are used for painting the weekdays at the top
74 void SetHeaderColours(const wxColour& colFg, const wxColour& colBg)
75 {
76 m_colHeaderFg = colFg;
77 m_colHeaderBg = colBg;
78 }
79
80 const wxColour& GetHeaderColourFg() const { return m_colHeaderFg; }
81 const wxColour& GetHeaderColourBg() const { return m_colHeaderBg; }
82
83 // highlight colour is used for the currently selected date
84 void SetHighlightColours(const wxColour& colFg, const wxColour& colBg)
85 {
86 m_colHighlightFg = colFg;
87 m_colHighlightBg = colBg;
88 }
89
90 const wxColour& GetHighlightColourFg() const { return m_colHighlightFg; }
91 const wxColour& GetHighlightColourBg() const { return m_colHighlightBg; }
92
93 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
94 void SetHolidayColours(const wxColour& colFg, const wxColour& colBg)
95 {
96 m_colHolidayFg = colFg;
97 m_colHolidayBg = colBg;
98 }
99
100 const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; }
101 const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; }
102
103 // this function should be called instead of directly changing the
104 // wxCAL_SHOW_HOLIDAYS bit in the control style after the control creation
105 // (this won't work)
106 void EnableHolidayDisplay(bool display = TRUE);
107
108 // an item without custom attributes is drawn with the default colours and
109 // font and without border, setting custom attributes allows to modify this
110 //
111 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
112 // corresponding attribute is just unused if there is no such day in the
113 // current month
114
115 wxCalendarDateAttr *GetAttr(size_t day) const
116 {
117 wxCHECK_MSG( day > 0 && day < 32, NULL, _T("invalid day") );
118
119 return m_attrs[day - 1];
120 }
121
122 void SetAttr(size_t day, wxCalendarDateAttr *attr)
123 {
124 wxCHECK_RET( day > 0 && day < 32, _T("invalid day") );
125
126 delete m_attrs[day - 1];
127 m_attrs[day - 1] = attr;
128 }
129
130 void SetHoliday(size_t day);
131
132 void ResetAttr(size_t day) { SetAttr(day, (wxCalendarDateAttr *)NULL); }
133
134 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
135 // with the corresponding value (none for NOWHERE, the date for DAY and wd
136 // for HEADER)
137 wxCalendarHitTestResult HitTest(const wxPoint& pos,
138 wxDateTime *date = NULL,
139 wxDateTime::WeekDay *wd = NULL);
140
141 // implementation only from now on
142 // -------------------------------
143
144 // forward these functions to all subcontrols
145 virtual bool Enable(bool enable = TRUE);
146 virtual bool Show(bool show = TRUE);
147
148 private:
149 // common part of all ctors
150 void Init();
151
152 // event handlers
153 void OnPaint(wxPaintEvent& event);
154 void OnClick(wxMouseEvent& event);
155 void OnDClick(wxMouseEvent& event);
156 void OnChar(wxKeyEvent& event);
157 void OnMonthChange(wxCommandEvent& event);
158 void OnYearChange(wxSpinEvent& event);
159 void OnCalMonthChange(wxCalendarEvent& event);
160
161 // override some base class virtuals
162 virtual wxSize DoGetBestSize() const;
163 virtual void DoGetPosition(int *x, int *y) const;
164 virtual void DoGetSize(int *width, int *height) const;
165 virtual void DoSetSize(int x, int y, int width, int height, int sizeFlags);
166 virtual void DoMoveWindow(int x, int y, int width, int height);
167
168 // (re)calc m_widthCol and m_heightRow
169 void RecalcGeometry();
170
171 // set the date and send the notification
172 void SetDateAndNotify(const wxDateTime& date);
173
174 // get the week (row, in range 1..6) for the given date
175 size_t GetWeek(const wxDateTime& date) const;
176
177 // get the date from which we start drawing days
178 wxDateTime GetStartDate() const;
179
180 // is this date shown?
181 bool IsDateShown(const wxDateTime& date) const;
182
183 // redraw the given date
184 void RefreshDate(const wxDateTime& date);
185
186 // change the date inside the same month/year
187 void ChangeDay(const wxDateTime& date);
188
189 // set the attributes for the holidays if needed
190 void SetHolidayAttrs();
191
192 // reset all holidays
193 void ResetHolidayAttrs();
194
195 // generate the given calendar event(s)
196 void GenerateEvent(wxEventType type)
197 {
198 wxCalendarEvent event(this, type);
199 (void)GetEventHandler()->ProcessEvent(event);
200 }
201
202 void GenerateEvents(wxEventType type1, wxEventType type2)
203 {
204 GenerateEvent(type1);
205 GenerateEvent(type2);
206 }
207
208 // the subcontrols
209 wxComboBox *m_comboMonth;
210 wxSpinCtrl *m_spinYear;
211
212 // the current selection
213 wxDateTime m_date;
214
215 // default attributes
216 wxColour m_colHighlightFg,
217 m_colHighlightBg,
218 m_colHolidayFg,
219 m_colHolidayBg,
220 m_colHeaderFg,
221 m_colHeaderBg;
222
223 // the attributes for each of the month days
224 wxCalendarDateAttr *m_attrs[31];
225
226 // the width and height of one column/row in the calendar
227 wxCoord m_widthCol,
228 m_heightRow;
229
230 // the week day names
231 wxString m_weekdays[7];
232
233 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl)
234 DECLARE_EVENT_TABLE()
235 };
236
237 #endif // _WX_GENERIC_CALCTRL_H