]>
Commit | Line | Data |
---|---|---|
74a533f7 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: calendar.cpp | |
3 | // Purpose: wxCalendarCtrl sample | |
4 | // Author: Vadim Zeitlin | |
4e6bceff | 5 | // Modified by: |
74a533f7 VZ |
6 | // Created: 02.01.00 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
788233da | 20 | #if defined(__GNUG__) && !defined(__APPLE__) |
74a533f7 VZ |
21 | #pragma implementation "calendar.cpp" |
22 | #pragma interface "calendar.cpp" | |
23 | #endif | |
24 | ||
25 | // For compilers that support precompilation, includes "wx/wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
273b7ed9 | 32 | // for all others, include the necessary headers |
74a533f7 VZ |
33 | #ifndef WX_PRECOMP |
34 | #include "wx/app.h" | |
788233da | 35 | #include "wx/log.h" |
74a533f7 | 36 | #include "wx/frame.h" |
273b7ed9 VZ |
37 | #include "wx/panel.h" |
38 | #include "wx/stattext.h" | |
39 | #include "wx/menu.h" | |
40 | #include "wx/layout.h" | |
41 | #include "wx/msgdlg.h" | |
74a533f7 VZ |
42 | #endif |
43 | ||
9230b621 | 44 | #include "wx/sizer.h" |
74a533f7 VZ |
45 | #include "wx/calctrl.h" |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // private classes | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // Define a new application type, each program should derive a class from wxApp | |
52 | class MyApp : public wxApp | |
53 | { | |
54 | public: | |
55 | // override base class virtuals | |
56 | // ---------------------------- | |
57 | ||
58 | // this one is called on application startup and is a good place for the app | |
59 | // initialization (doing it here and not in the ctor allows to have an error | |
60 | // return: if OnInit() returns false, the application terminates) | |
61 | virtual bool OnInit(); | |
62 | }; | |
63 | ||
64 | class MyPanel : public wxPanel | |
65 | { | |
66 | public: | |
67 | MyPanel(wxFrame *frame); | |
68 | ||
69 | void OnCalendar(wxCalendarEvent& event); | |
70 | void OnCalendarWeekDayClick(wxCalendarEvent& event); | |
71 | void OnCalendarChange(wxCalendarEvent& event); | |
0de868d9 VZ |
72 | void OnCalMonthChange(wxCalendarEvent& event); |
73 | void OnCalYearChange(wxCalendarEvent& event); | |
74a533f7 | 74 | |
bc385ba9 VZ |
75 | wxCalendarCtrl *GetCal() const { return m_calendar; } |
76 | ||
37df1f33 VZ |
77 | // turn on/off the specified style bit on the calendar control |
78 | void ToggleCalStyle(bool on, int style); | |
79 | ||
74a533f7 VZ |
80 | void HighlightSpecial(bool on); |
81 | ||
605dfd91 JS |
82 | void SetDate(); |
83 | void Today(); | |
84 | ||
74a533f7 VZ |
85 | private: |
86 | wxCalendarCtrl *m_calendar; | |
87 | wxStaticText *m_date; | |
88 | ||
89 | DECLARE_EVENT_TABLE() | |
90 | }; | |
91 | ||
92 | // Define a new frame type: this is going to be our main frame | |
93 | class MyFrame : public wxFrame | |
94 | { | |
95 | public: | |
96 | // ctor(s) | |
97 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
98 | ||
99 | // event handlers (these functions should _not_ be virtual) | |
100 | void OnQuit(wxCommandEvent& event); | |
101 | void OnAbout(wxCommandEvent& event); | |
102 | ||
103 | void OnCalMonday(wxCommandEvent& event); | |
104 | void OnCalHolidays(wxCommandEvent& event); | |
105 | void OnCalSpecial(wxCommandEvent& event); | |
106 | ||
bc385ba9 VZ |
107 | void OnCalAllowMonth(wxCommandEvent& event); |
108 | void OnCalAllowYear(wxCommandEvent& event); | |
109 | ||
37df1f33 VZ |
110 | void OnCalSeqMonth(wxCommandEvent& event); |
111 | void OnCalShowSurroundingWeeks(wxCommandEvent& event); | |
112 | ||
605dfd91 JS |
113 | void OnSetDate(wxCommandEvent& event); |
114 | void OnToday(wxCommandEvent& event); | |
115 | ||
bc385ba9 VZ |
116 | void OnAllowYearUpdate(wxUpdateUIEvent& event); |
117 | ||
74a533f7 VZ |
118 | private: |
119 | MyPanel *m_panel; | |
120 | ||
be5a51fb | 121 | // any class wishing to process wxWidgets events must use this macro |
74a533f7 VZ |
122 | DECLARE_EVENT_TABLE() |
123 | }; | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // constants | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | // IDs for the controls and the menu commands | |
130 | enum | |
131 | { | |
132 | // menu items | |
133 | Calendar_File_About = 100, | |
134 | Calendar_File_Quit, | |
135 | Calendar_Cal_Monday = 200, | |
136 | Calendar_Cal_Holidays, | |
137 | Calendar_Cal_Special, | |
bc385ba9 VZ |
138 | Calendar_Cal_Month, |
139 | Calendar_Cal_Year, | |
37df1f33 VZ |
140 | Calendar_Cal_SeqMonth, |
141 | Calendar_Cal_SurroundWeeks, | |
605dfd91 JS |
142 | Calendar_Cal_SetDate, |
143 | Calendar_Cal_Today, | |
e9561b3b | 144 | Calendar_CalCtrl = 1000 |
74a533f7 VZ |
145 | }; |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
be5a51fb | 148 | // event tables and other macros for wxWidgets |
74a533f7 VZ |
149 | // ---------------------------------------------------------------------------- |
150 | ||
be5a51fb | 151 | // the event tables connect the wxWidgets events with the functions (event |
74a533f7 VZ |
152 | // handlers) which process them. It can be also done at run-time, but for the |
153 | // simple menu events like this the static method is much simpler. | |
154 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
155 | EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit) | |
156 | EVT_MENU(Calendar_File_About, MyFrame::OnAbout) | |
157 | ||
158 | EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday) | |
159 | EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays) | |
160 | EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial) | |
bc385ba9 VZ |
161 | |
162 | EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth) | |
163 | EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear) | |
164 | ||
37df1f33 VZ |
165 | EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth) |
166 | EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks) | |
167 | ||
605dfd91 JS |
168 | EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate) |
169 | EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday) | |
170 | ||
171 | ||
bc385ba9 | 172 | EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate) |
74a533f7 VZ |
173 | END_EVENT_TABLE() |
174 | ||
175 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
176 | EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar) | |
0de868d9 VZ |
177 | EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange) |
178 | EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange) | |
74a533f7 VZ |
179 | EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange) |
180 | EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick) | |
181 | END_EVENT_TABLE() | |
182 | ||
be5a51fb | 183 | // Create a new application object: this macro will allow wxWidgets to create |
74a533f7 VZ |
184 | // the application object during program execution (it's better than using a |
185 | // static object for many reasons) and also declares the accessor function | |
186 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
187 | // not wxApp) | |
188 | IMPLEMENT_APP(MyApp) | |
189 | ||
190 | // ============================================================================ | |
191 | // implementation | |
192 | // ============================================================================ | |
193 | ||
194 | // ---------------------------------------------------------------------------- | |
195 | // the application class | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | // `Main program' equivalent: the program execution "starts" here | |
199 | bool MyApp::OnInit() | |
200 | { | |
201 | // Create the main application window | |
be5a51fb | 202 | MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample"), |
4e6bceff | 203 | wxPoint(50, 50), wxSize(450, 340)); |
74a533f7 | 204 | |
9230b621 | 205 | frame->Show(true); |
74a533f7 VZ |
206 | |
207 | // success: wxApp::OnRun() will be called which will enter the main message | |
5014bb3a | 208 | // loop and the application will run. If we returned false here, the |
74a533f7 | 209 | // application would exit immediately. |
9230b621 | 210 | return true; |
74a533f7 VZ |
211 | } |
212 | ||
213 | // ---------------------------------------------------------------------------- | |
214 | // main frame | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
217 | // frame constructor | |
218 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
9230b621 | 219 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
74a533f7 VZ |
220 | { |
221 | // create a menu bar | |
222 | wxMenu *menuFile = new wxMenu; | |
223 | ||
9f84eccd | 224 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
74a533f7 | 225 | menuFile->AppendSeparator(); |
9f84eccd | 226 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
74a533f7 VZ |
227 | |
228 | wxMenu *menuCal = new wxMenu; | |
229 | menuCal->Append(Calendar_Cal_Monday, | |
9f84eccd MB |
230 | _T("Monday &first weekday\tCtrl-F"), |
231 | _T("Toggle between Mon and Sun as the first week day"), | |
9230b621 | 232 | true); |
9f84eccd MB |
233 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), |
234 | _T("Toggle highlighting the holidays"), | |
9230b621 | 235 | true); |
9f84eccd MB |
236 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), |
237 | _T("Test custom highlighting"), | |
9230b621 | 238 | true); |
37df1f33 | 239 | menuCal->Append(Calendar_Cal_SurroundWeeks, |
9f84eccd MB |
240 | _T("Show s&urrounding weeks\tCtrl-W"), |
241 | _T("Show the neighbouring weeks in the prev/next month"), | |
9230b621 | 242 | true); |
bc385ba9 | 243 | menuCal->AppendSeparator(); |
37df1f33 | 244 | menuCal->Append(Calendar_Cal_SeqMonth, |
9f84eccd MB |
245 | _T("To&ggle month selector style\tCtrl-G"), |
246 | _T("Use another style for the calendar controls"), | |
9230b621 | 247 | true); |
9f84eccd MB |
248 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), |
249 | _T("Allow changing the month in the calendar"), | |
9230b621 | 250 | true); |
9f84eccd MB |
251 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), |
252 | _T("Allow changing the year in the calendar"), | |
9230b621 | 253 | true); |
605dfd91 | 254 | menuCal->AppendSeparator(); |
529b7f71 JS |
255 | menuCal->Append(Calendar_Cal_SetDate, _T("SetDate()"), _T("Set date to 2005-12-24.")); |
256 | menuCal->Append(Calendar_Cal_Today, _T("Today()"), _T("Set the current date.")); | |
74a533f7 VZ |
257 | |
258 | // now append the freshly created menu to the menu bar... | |
259 | wxMenuBar *menuBar = new wxMenuBar; | |
9f84eccd MB |
260 | menuBar->Append(menuFile, _T("&File")); |
261 | menuBar->Append(menuCal, _T("&Calendar")); | |
74a533f7 | 262 | |
9230b621 VS |
263 | menuBar->Check(Calendar_Cal_Monday, true); |
264 | menuBar->Check(Calendar_Cal_Holidays, true); | |
265 | menuBar->Check(Calendar_Cal_Month, true); | |
266 | menuBar->Check(Calendar_Cal_Year, true); | |
74a533f7 VZ |
267 | |
268 | // ... and attach this menu bar to the frame | |
269 | SetMenuBar(menuBar); | |
270 | ||
271 | m_panel = new MyPanel(this); | |
272 | ||
273 | #if wxUSE_STATUSBAR | |
274 | // create a status bar just for fun (by default with 1 pane only) | |
9898fcda | 275 | CreateStatusBar(2); |
be5a51fb | 276 | SetStatusText(_T("Welcome to wxWidgets!")); |
74a533f7 VZ |
277 | #endif // wxUSE_STATUSBAR |
278 | } | |
279 | ||
280 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
281 | { | |
5014bb3a | 282 | // true is to force the frame to close |
9230b621 | 283 | Close(true); |
74a533f7 VZ |
284 | } |
285 | ||
286 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
287 | { | |
749bfe9a | 288 | wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"), |
74a533f7 VZ |
289 | _T("About Calendar"), wxOK | wxICON_INFORMATION, this); |
290 | } | |
291 | ||
292 | void MyFrame::OnCalMonday(wxCommandEvent& event) | |
293 | { | |
37df1f33 VZ |
294 | bool enable = GetMenuBar()->IsChecked(event.GetId()); |
295 | ||
296 | m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST); | |
74a533f7 VZ |
297 | } |
298 | ||
299 | void MyFrame::OnCalHolidays(wxCommandEvent& event) | |
300 | { | |
89a0a322 | 301 | bool enable = GetMenuBar()->IsChecked(event.GetId()); |
37df1f33 | 302 | |
bc385ba9 | 303 | m_panel->GetCal()->EnableHolidayDisplay(enable); |
74a533f7 VZ |
304 | } |
305 | ||
306 | void MyFrame::OnCalSpecial(wxCommandEvent& event) | |
307 | { | |
89a0a322 | 308 | m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId())); |
bc385ba9 VZ |
309 | } |
310 | ||
311 | void MyFrame::OnCalAllowMonth(wxCommandEvent& event) | |
312 | { | |
89a0a322 | 313 | bool allow = GetMenuBar()->IsChecked(event.GetId()); |
bc385ba9 VZ |
314 | |
315 | m_panel->GetCal()->EnableMonthChange(allow); | |
316 | } | |
317 | ||
318 | void MyFrame::OnCalAllowYear(wxCommandEvent& event) | |
319 | { | |
89a0a322 | 320 | bool allow = GetMenuBar()->IsChecked(event.GetId()); |
bc385ba9 VZ |
321 | |
322 | m_panel->GetCal()->EnableYearChange(allow); | |
323 | } | |
324 | ||
37df1f33 VZ |
325 | void MyFrame::OnCalSeqMonth(wxCommandEvent& event) |
326 | { | |
327 | bool allow = GetMenuBar()->IsChecked(event.GetId()); | |
328 | ||
329 | m_panel->ToggleCalStyle(allow, wxCAL_SEQUENTIAL_MONTH_SELECTION); | |
330 | } | |
331 | ||
332 | void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event) | |
333 | { | |
334 | bool allow = GetMenuBar()->IsChecked(event.GetId()); | |
335 | ||
336 | m_panel->ToggleCalStyle(allow, wxCAL_SHOW_SURROUNDING_WEEKS); | |
337 | } | |
338 | ||
bc385ba9 VZ |
339 | void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event) |
340 | { | |
341 | event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month)); | |
74a533f7 VZ |
342 | } |
343 | ||
87728739 | 344 | void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event)) |
605dfd91 JS |
345 | { |
346 | m_panel->SetDate(); | |
347 | } | |
348 | ||
87728739 | 349 | void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event)) |
605dfd91 JS |
350 | { |
351 | m_panel->Today(); | |
352 | } | |
353 | ||
74a533f7 VZ |
354 | // ---------------------------------------------------------------------------- |
355 | // MyPanel | |
356 | // ---------------------------------------------------------------------------- | |
357 | ||
358 | MyPanel::MyPanel(wxFrame *frame) | |
9230b621 | 359 | : wxPanel(frame, wxID_ANY) |
74a533f7 | 360 | { |
4e6bceff | 361 | wxString date; |
4693b20c | 362 | date.Printf(wxT("Selected date: %s"), |
74a533f7 | 363 | wxDateTime::Today().FormatISODate().c_str()); |
9230b621 | 364 | m_date = new wxStaticText(this, wxID_ANY, date); |
74a533f7 VZ |
365 | m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl, |
366 | wxDefaultDateTime, | |
367 | wxDefaultPosition, | |
368 | wxDefaultSize, | |
bc385ba9 VZ |
369 | wxCAL_MONDAY_FIRST | |
370 | wxCAL_SHOW_HOLIDAYS | | |
371 | wxRAISED_BORDER); | |
74a533f7 | 372 | |
9230b621 | 373 | wxBoxSizer *m_sizer = new wxBoxSizer( wxHORIZONTAL ); |
4e6bceff | 374 | |
9230b621 VS |
375 | m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 ); |
376 | m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT); | |
4e6bceff | 377 | |
9230b621 VS |
378 | SetSizer( m_sizer ); |
379 | m_sizer->SetSizeHints( this ); | |
74a533f7 VZ |
380 | } |
381 | ||
382 | void MyPanel::OnCalendar(wxCalendarEvent& event) | |
383 | { | |
4693b20c | 384 | wxLogMessage(wxT("Selected %s from calendar"), |
74a533f7 VZ |
385 | event.GetDate().FormatISODate().c_str()); |
386 | } | |
387 | ||
388 | void MyPanel::OnCalendarChange(wxCalendarEvent& event) | |
389 | { | |
390 | wxString s; | |
4693b20c | 391 | s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str()); |
74a533f7 VZ |
392 | |
393 | m_date->SetLabel(s); | |
394 | } | |
395 | ||
0de868d9 VZ |
396 | void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event)) |
397 | { | |
4693b20c | 398 | wxLogStatus(wxT("Calendar month changed")); |
0de868d9 VZ |
399 | } |
400 | ||
401 | void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event)) | |
402 | { | |
4693b20c | 403 | wxLogStatus(wxT("Calendar year changed")); |
0de868d9 VZ |
404 | } |
405 | ||
74a533f7 VZ |
406 | void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event) |
407 | { | |
4693b20c | 408 | wxLogMessage(wxT("Clicked on %s"), |
74a533f7 VZ |
409 | wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str()); |
410 | } | |
411 | ||
37df1f33 | 412 | void MyPanel::ToggleCalStyle(bool on, int flag) |
74a533f7 VZ |
413 | { |
414 | long style = m_calendar->GetWindowStyle(); | |
415 | if ( on ) | |
37df1f33 | 416 | style |= flag; |
74a533f7 | 417 | else |
37df1f33 | 418 | style &= ~flag; |
74a533f7 VZ |
419 | |
420 | m_calendar->SetWindowStyle(style); | |
421 | ||
422 | m_calendar->Refresh(); | |
423 | } | |
424 | ||
74a533f7 VZ |
425 | void MyPanel::HighlightSpecial(bool on) |
426 | { | |
427 | if ( on ) | |
428 | { | |
429 | wxCalendarDateAttr | |
430 | *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED), | |
431 | *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN), | |
432 | *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY); | |
433 | ||
434 | m_calendar->SetAttr(17, attrRedCircle); | |
435 | m_calendar->SetAttr(29, attrGreenSquare); | |
436 | m_calendar->SetAttr(13, attrHeaderLike); | |
437 | } | |
438 | else // off | |
439 | { | |
440 | m_calendar->ResetAttr(17); | |
441 | m_calendar->ResetAttr(29); | |
442 | m_calendar->ResetAttr(13); | |
443 | } | |
444 | ||
445 | m_calendar->Refresh(); | |
446 | } | |
605dfd91 JS |
447 | |
448 | void MyPanel::SetDate() | |
449 | { | |
450 | wxDateTime date(24, wxDateTime::Dec, 2005, 23, 59, 59); | |
451 | m_calendar->SetDate(date); | |
452 | } | |
453 | ||
454 | void MyPanel::Today() | |
455 | { | |
456 | m_calendar->SetDate(wxDateTime::Today()); | |
457 | } |