added possibility to use generic date control if configured
[wxWidgets.git] / samples / calendar / calendar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: calendar.cpp
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
5 // Modified by:
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
20 #if defined(__GNUG__) && !defined(__APPLE__)
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
32 // for all others, include the necessary headers
33 #ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/log.h"
36 #include "wx/frame.h"
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"
42 #endif
43
44 #include "wx/sizer.h"
45 #include "wx/textctrl.h"
46
47 #include "wx/calctrl.h"
48
49 #if wxUSE_DATEPICKCTRL
50 #include "wx/datectrl.h"
51 #if wxUSE_DATEPICKCTRL_GENERIC
52 #include "wx/generic/datectrl.h"
53 #endif // wxUSE_DATEPICKCTRL_GENERIC
54 #endif // wxUSE_DATEPICKCTRL
55
56 // ----------------------------------------------------------------------------
57 // private classes
58 // ----------------------------------------------------------------------------
59
60 // Define a new application type, each program should derive a class from wxApp
61 class MyApp : public wxApp
62 {
63 public:
64 // override base class virtuals
65 // ----------------------------
66
67 // this one is called on application startup and is a good place for the app
68 // initialization (doing it here and not in the ctor allows to have an error
69 // return: if OnInit() returns false, the application terminates)
70 virtual bool OnInit();
71 };
72
73 class MyPanel : public wxPanel
74 {
75 public:
76 MyPanel(wxFrame *frame);
77
78 void OnCalendar(wxCalendarEvent& event);
79 void OnCalendarWeekDayClick(wxCalendarEvent& event);
80 void OnCalendarChange(wxCalendarEvent& event);
81 void OnCalMonthChange(wxCalendarEvent& event);
82 void OnCalYearChange(wxCalendarEvent& event);
83
84 wxCalendarCtrl *GetCal() const { return m_calendar; }
85
86 // turn on/off the specified style bit on the calendar control
87 void ToggleCalStyle(bool on, int style);
88
89 void HighlightSpecial(bool on);
90
91 void SetDate();
92 void Today();
93
94 private:
95 wxCalendarCtrl *m_calendar;
96 wxStaticText *m_date;
97
98 DECLARE_EVENT_TABLE()
99 };
100
101 // Define a new frame type: this is going to be our main frame
102 class MyFrame : public wxFrame
103 {
104 public:
105 // ctor(s)
106 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
107
108 // event handlers (these functions should _not_ be virtual)
109 void OnQuit(wxCommandEvent& event);
110 void OnAbout(wxCommandEvent& event);
111
112 #if wxUSE_DATEPICKCTRL
113 void OnAskDate(wxCommandEvent& event);
114 #endif // wxUSE_DATEPICKCTRL
115
116 void OnCalMonday(wxCommandEvent& event);
117 void OnCalHolidays(wxCommandEvent& event);
118 void OnCalSpecial(wxCommandEvent& event);
119
120 void OnCalAllowMonth(wxCommandEvent& event);
121 void OnCalAllowYear(wxCommandEvent& event);
122
123 void OnCalSeqMonth(wxCommandEvent& event);
124 void OnCalShowSurroundingWeeks(wxCommandEvent& event);
125
126 void OnSetDate(wxCommandEvent& event);
127 void OnToday(wxCommandEvent& event);
128
129 void OnAllowYearUpdate(wxUpdateUIEvent& event);
130
131 private:
132 MyPanel *m_panel;
133
134 // any class wishing to process wxWidgets events must use this macro
135 DECLARE_EVENT_TABLE()
136 };
137
138 #if wxUSE_DATEPICKCTRL
139
140 // Define a simple modal dialog which asks the user for a date
141 class MyDialog : public wxDialog
142 {
143 public:
144 MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle);
145
146 wxDateTime GetDate() const { return m_datePicker->GetValue(); }
147
148 private:
149 void OnDateChange(wxDateEvent& event);
150
151
152 wxDatePickerCtrlBase *m_datePicker;
153 wxTextCtrl *m_text;
154
155
156 DECLARE_EVENT_TABLE()
157 };
158
159 #endif // wxUSE_DATEPICKCTRL
160
161 // ----------------------------------------------------------------------------
162 // constants
163 // ----------------------------------------------------------------------------
164
165 // IDs for the controls and the menu commands
166 enum
167 {
168 // menu items
169 Calendar_File_About = wxID_ABOUT,
170 Calendar_File_Quit = wxID_EXIT,
171 Calendar_Cal_Monday = 200,
172 Calendar_Cal_Holidays,
173 Calendar_Cal_Special,
174 Calendar_Cal_Month,
175 Calendar_Cal_Year,
176 Calendar_Cal_SeqMonth,
177 Calendar_Cal_SurroundWeeks,
178 Calendar_Cal_SetDate,
179 Calendar_Cal_Today,
180 #if wxUSE_DATEPICKCTRL
181 Calendar_DatePicker_AskDate = 300,
182 Calendar_DatePicker_ShowCentury,
183 Calendar_DatePicker_DropDown,
184 #if wxUSE_DATEPICKCTRL_GENERIC
185 Calendar_DatePicker_Generic,
186 #endif // wxUSE_DATEPICKCTRL_GENERIC
187 #endif // wxUSE_DATEPICKCTRL
188 Calendar_CalCtrl = 1000
189 };
190
191 // ----------------------------------------------------------------------------
192 // event tables and other macros for wxWidgets
193 // ----------------------------------------------------------------------------
194
195 // the event tables connect the wxWidgets events with the functions (event
196 // handlers) which process them. It can be also done at run-time, but for the
197 // simple menu events like this the static method is much simpler.
198 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
199 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
200 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
201
202 #if wxUSE_DATEPICKCTRL
203 EVT_MENU(Calendar_DatePicker_AskDate, MyFrame::OnAskDate)
204 #endif // wxUSE_DATEPICKCTRL
205
206 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
207 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
208 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
209
210 EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
211 EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear)
212
213 EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
214 EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
215
216 EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
217 EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
218
219
220 EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate)
221 END_EVENT_TABLE()
222
223 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
224 EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar)
225 EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange)
226 EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange)
227 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
228 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
229 END_EVENT_TABLE()
230
231 #if wxUSE_DATEPICKCTRL
232
233 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
234 EVT_DATE_CHANGED(wxID_ANY, MyDialog::OnDateChange)
235 END_EVENT_TABLE()
236
237 #endif // wxUSE_DATEPICKCTRL
238
239 // Create a new application object: this macro will allow wxWidgets to create
240 // the application object during program execution (it's better than using a
241 // static object for many reasons) and also declares the accessor function
242 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
243 // not wxApp)
244 IMPLEMENT_APP(MyApp)
245
246 // ============================================================================
247 // implementation
248 // ============================================================================
249
250 // ----------------------------------------------------------------------------
251 // the application class
252 // ----------------------------------------------------------------------------
253
254 // `Main program' equivalent: the program execution "starts" here
255 bool MyApp::OnInit()
256 {
257 // Create the main application window
258 MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample"),
259 wxPoint(50, 50), wxSize(450, 340));
260
261 frame->Show(true);
262
263 // success: wxApp::OnRun() will be called which will enter the main message
264 // loop and the application will run. If we returned false here, the
265 // application would exit immediately.
266 return true;
267 }
268
269 // ----------------------------------------------------------------------------
270 // main frame
271 // ----------------------------------------------------------------------------
272
273 // frame constructor
274 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
275 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
276 {
277 // create a menu bar
278 wxMenu *menuFile = new wxMenu;
279 menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
280 menuFile->AppendSeparator();
281 menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
282
283 wxMenu *menuCal = new wxMenu;
284 menuCal->Append(Calendar_Cal_Monday,
285 _T("Monday &first weekday\tCtrl-F"),
286 _T("Toggle between Mon and Sun as the first week day"),
287 true);
288 menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"),
289 _T("Toggle highlighting the holidays"),
290 true);
291 menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"),
292 _T("Test custom highlighting"),
293 true);
294 menuCal->Append(Calendar_Cal_SurroundWeeks,
295 _T("Show s&urrounding weeks\tCtrl-W"),
296 _T("Show the neighbouring weeks in the prev/next month"),
297 true);
298 menuCal->AppendSeparator();
299 menuCal->Append(Calendar_Cal_SeqMonth,
300 _T("To&ggle month selector style\tCtrl-G"),
301 _T("Use another style for the calendar controls"),
302 true);
303 menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"),
304 _T("Allow changing the month in the calendar"),
305 true);
306 menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"),
307 _T("Allow changing the year in the calendar"),
308 true);
309 menuCal->AppendSeparator();
310 menuCal->Append(Calendar_Cal_SetDate, _T("SetDate()"), _T("Set date to 2005-12-24."));
311 menuCal->Append(Calendar_Cal_Today, _T("Today()"), _T("Set the current date."));
312
313 #if wxUSE_DATEPICKCTRL
314 wxMenu *menuDate = new wxMenu;
315 menuDate->AppendCheckItem(Calendar_DatePicker_ShowCentury,
316 _T("Al&ways show century"));
317 menuDate->AppendCheckItem(Calendar_DatePicker_DropDown,
318 _T("Use &drop down control"));
319 #if wxUSE_DATEPICKCTRL_GENERIC
320 menuDate->AppendCheckItem(Calendar_DatePicker_Generic,
321 _T("Use &generic version of the control"));
322 #endif // wxUSE_DATEPICKCTRL_GENERIC
323 menuDate->AppendSeparator();
324 menuDate->Append(Calendar_DatePicker_AskDate, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
325 #endif // wxUSE_DATEPICKCTRL
326
327 // now append the freshly created menu to the menu bar...
328 wxMenuBar *menuBar = new wxMenuBar;
329 menuBar->Append(menuFile, _T("&File"));
330 menuBar->Append(menuCal, _T("&Calendar"));
331 #if wxUSE_DATEPICKCTRL
332 menuBar->Append(menuDate, _T("&Date picker"));
333 #endif // wxUSE_DATEPICKCTRL
334
335 menuBar->Check(Calendar_Cal_Monday, true);
336 menuBar->Check(Calendar_Cal_Holidays, true);
337 menuBar->Check(Calendar_Cal_Month, true);
338 menuBar->Check(Calendar_Cal_Year, true);
339
340 #if wxUSE_DATEPICKCTRL
341 menuBar->Check(Calendar_DatePicker_ShowCentury, true);
342 #endif // wxUSE_DATEPICKCTRL
343
344 // ... and attach this menu bar to the frame
345 SetMenuBar(menuBar);
346
347 m_panel = new MyPanel(this);
348
349 #if wxUSE_STATUSBAR
350 // create a status bar just for fun (by default with 1 pane only)
351 CreateStatusBar(2);
352 SetStatusText(_T("Welcome to wxWidgets!"));
353 #endif // wxUSE_STATUSBAR
354 }
355
356 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
357 {
358 // true is to force the frame to close
359 Close(true);
360 }
361
362 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
363 {
364 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
365 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
366 }
367
368 void MyFrame::OnCalMonday(wxCommandEvent& event)
369 {
370 bool enable = GetMenuBar()->IsChecked(event.GetId());
371
372 m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST);
373 }
374
375 void MyFrame::OnCalHolidays(wxCommandEvent& event)
376 {
377 bool enable = GetMenuBar()->IsChecked(event.GetId());
378
379 m_panel->GetCal()->EnableHolidayDisplay(enable);
380 }
381
382 void MyFrame::OnCalSpecial(wxCommandEvent& event)
383 {
384 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
385 }
386
387 void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
388 {
389 bool allow = GetMenuBar()->IsChecked(event.GetId());
390
391 m_panel->GetCal()->EnableMonthChange(allow);
392 }
393
394 void MyFrame::OnCalAllowYear(wxCommandEvent& event)
395 {
396 bool allow = GetMenuBar()->IsChecked(event.GetId());
397
398 m_panel->GetCal()->EnableYearChange(allow);
399 }
400
401 void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
402 {
403 bool allow = GetMenuBar()->IsChecked(event.GetId());
404
405 m_panel->ToggleCalStyle(allow, wxCAL_SEQUENTIAL_MONTH_SELECTION);
406 }
407
408 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
409 {
410 bool allow = GetMenuBar()->IsChecked(event.GetId());
411
412 m_panel->ToggleCalStyle(allow, wxCAL_SHOW_SURROUNDING_WEEKS);
413 }
414
415 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event)
416 {
417 event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month));
418 }
419
420 void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
421 {
422 m_panel->SetDate();
423 }
424
425 void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
426 {
427 m_panel->Today();
428 }
429
430 #if wxUSE_DATEPICKCTRL
431
432 void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
433 {
434 int style = wxDP_DEFAULT;
435 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury) )
436 style |= wxDP_SHOWCENTURY;
437 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown) )
438 style |= wxDP_DROPDOWN;
439
440 MyDialog dlg(this, m_panel->GetCal()->GetDate(), style);
441 if ( dlg.ShowModal() == wxID_OK )
442 {
443 const wxDateTime dt = dlg.GetDate(),
444 today = wxDateTime::Today();
445
446 if ( dt.GetDay() == today.GetDay() &&
447 dt.GetMonth() == today.GetMonth() )
448 {
449 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
450 }
451
452 m_panel->GetCal()->SetDate(dt);
453
454 wxLogStatus(_T("Changed the date to your birthday"));
455 }
456 }
457
458 #endif // wxUSE_DATEPICKCTRL
459
460 // ----------------------------------------------------------------------------
461 // MyPanel
462 // ----------------------------------------------------------------------------
463
464 MyPanel::MyPanel(wxFrame *frame)
465 : wxPanel(frame, wxID_ANY)
466 {
467 wxString date;
468 date.Printf(wxT("Selected date: %s"),
469 wxDateTime::Today().FormatISODate().c_str());
470 m_date = new wxStaticText(this, wxID_ANY, date);
471 m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
472 wxDefaultDateTime,
473 wxDefaultPosition,
474 wxDefaultSize,
475 wxCAL_MONDAY_FIRST |
476 wxCAL_SHOW_HOLIDAYS |
477 wxRAISED_BORDER);
478
479 wxBoxSizer *m_sizer = new wxBoxSizer( wxHORIZONTAL );
480
481 m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
482 m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
483
484 SetSizer( m_sizer );
485 m_sizer->SetSizeHints( this );
486 }
487
488 void MyPanel::OnCalendar(wxCalendarEvent& event)
489 {
490 wxLogMessage(wxT("Selected %s from calendar"),
491 event.GetDate().FormatISODate().c_str());
492 }
493
494 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
495 {
496 wxString s;
497 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
498
499 m_date->SetLabel(s);
500 }
501
502 void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event))
503 {
504 wxLogStatus(wxT("Calendar month changed"));
505 }
506
507 void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event))
508 {
509 wxLogStatus(wxT("Calendar year changed"));
510 }
511
512 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
513 {
514 wxLogMessage(wxT("Clicked on %s"),
515 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
516 }
517
518 void MyPanel::ToggleCalStyle(bool on, int flag)
519 {
520 long style = m_calendar->GetWindowStyle();
521 if ( on )
522 style |= flag;
523 else
524 style &= ~flag;
525
526 m_calendar->SetWindowStyle(style);
527
528 m_calendar->Refresh();
529 }
530
531 void MyPanel::HighlightSpecial(bool on)
532 {
533 if ( on )
534 {
535 wxCalendarDateAttr
536 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
537 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
538 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
539
540 m_calendar->SetAttr(17, attrRedCircle);
541 m_calendar->SetAttr(29, attrGreenSquare);
542 m_calendar->SetAttr(13, attrHeaderLike);
543 }
544 else // off
545 {
546 m_calendar->ResetAttr(17);
547 m_calendar->ResetAttr(29);
548 m_calendar->ResetAttr(13);
549 }
550
551 m_calendar->Refresh();
552 }
553
554 void MyPanel::SetDate()
555 {
556 wxDateTime date(24, wxDateTime::Dec, 2005, 23, 59, 59);
557 m_calendar->SetDate(date);
558 }
559
560 void MyPanel::Today()
561 {
562 m_calendar->SetDate(wxDateTime::Today());
563 }
564
565 // ----------------------------------------------------------------------------
566 // MyDialog
567 // ----------------------------------------------------------------------------
568
569 #if wxUSE_DATEPICKCTRL
570
571 MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
572 : wxDialog(parent, -1, wxString(_T("Calendar: Choose a date")))
573 {
574 wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
575 sizerBtns->AddButton(new wxButton(this, wxID_OK));
576 sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
577 sizerBtns->Finalise();
578
579 wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
580 sizerText->Add(new wxStaticText(this, -1, _T("Date in ISO format: ")),
581 wxSizerFlags().Border());
582 m_text = new wxTextCtrl(this, -1);
583 sizerText->Add(m_text, wxSizerFlags().Expand().Border());
584
585 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
586 sizerTop->Add(new wxStaticText
587 (
588 this, -1,
589 _T("Enter your birthday date (not before 20th century):")
590 ),
591 wxSizerFlags().Border());
592
593 #if wxUSE_DATEPICKCTRL_GENERIC
594 wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
595 if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
596 m_datePicker = new wxDatePickerCtrlGeneric(this, -1, dt,
597 wxDefaultPosition,
598 wxDefaultSize,
599 dtpStyle);
600 else
601 #endif // wxUSE_DATEPICKCTRL_GENERIC
602 m_datePicker = new wxDatePickerCtrl(this, -1, dt,
603 wxDefaultPosition, wxDefaultSize,
604 dtpStyle);
605 m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
606 wxDefaultDateTime);
607 sizerTop->Add(m_datePicker, wxSizerFlags().Expand().Border());
608
609 sizerTop->AddStretchSpacer(1);
610 sizerTop->Add(sizerText);
611
612 sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border());
613
614 SetSizerAndFit(sizerTop);
615 Layout();
616 }
617
618 void MyDialog::OnDateChange(wxDateEvent& event)
619 {
620 m_text->SetValue(event.GetDate().FormatISODate());
621 }
622
623 #endif // wxUSE_DATEPICKCTRL
624