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