corrected #if tests to use correct wxUSE_DATEPICKCTRL spelling
[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);
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 #if wxUSE_DATEPICKCTRL
166 Calendar_File_AskDate = 100,
167 #endif // wxUSE_DATEPICKCTRL
168 Calendar_Cal_Monday = 200,
169 Calendar_Cal_Holidays,
170 Calendar_Cal_Special,
171 Calendar_Cal_Month,
172 Calendar_Cal_Year,
173 Calendar_Cal_SeqMonth,
174 Calendar_Cal_SurroundWeeks,
175 Calendar_Cal_SetDate,
176 Calendar_Cal_Today,
177 Calendar_CalCtrl = 1000
178 };
179
180 // ----------------------------------------------------------------------------
181 // event tables and other macros for wxWidgets
182 // ----------------------------------------------------------------------------
183
184 // the event tables connect the wxWidgets events with the functions (event
185 // handlers) which process them. It can be also done at run-time, but for the
186 // simple menu events like this the static method is much simpler.
187 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
188 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
189 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
190
191 #if wxUSE_DATEPICKCTRL
192 EVT_MENU(Calendar_File_AskDate, MyFrame::OnAskDate)
193 #endif // wxUSE_DATEPICKCTRL
194
195 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
196 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
197 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
198
199 EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
200 EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear)
201
202 EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
203 EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
204
205 EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
206 EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
207
208
209 EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate)
210 END_EVENT_TABLE()
211
212 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
213 EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar)
214 EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange)
215 EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange)
216 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
217 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
218 END_EVENT_TABLE()
219
220 #if wxUSE_DATEPICKCTRL
221
222 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
223 EVT_DATE_CHANGED(wxID_ANY, MyDialog::OnDateChange)
224 END_EVENT_TABLE()
225
226 #endif // wxUSE_DATEPICKCTRL
227
228 // Create a new application object: this macro will allow wxWidgets to create
229 // the application object during program execution (it's better than using a
230 // static object for many reasons) and also declares the accessor function
231 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
232 // not wxApp)
233 IMPLEMENT_APP(MyApp)
234
235 // ============================================================================
236 // implementation
237 // ============================================================================
238
239 // ----------------------------------------------------------------------------
240 // the application class
241 // ----------------------------------------------------------------------------
242
243 // `Main program' equivalent: the program execution "starts" here
244 bool MyApp::OnInit()
245 {
246 // Create the main application window
247 MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample"),
248 wxPoint(50, 50), wxSize(450, 340));
249
250 frame->Show(true);
251
252 // success: wxApp::OnRun() will be called which will enter the main message
253 // loop and the application will run. If we returned false here, the
254 // application would exit immediately.
255 return true;
256 }
257
258 // ----------------------------------------------------------------------------
259 // main frame
260 // ----------------------------------------------------------------------------
261
262 // frame constructor
263 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
264 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
265 {
266 // create a menu bar
267 wxMenu *menuFile = new wxMenu;
268
269 #if wxUSE_DATEPICKCTRL
270 menuFile->Append(Calendar_File_AskDate, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
271 menuFile->AppendSeparator();
272 #endif // wxUSE_DATEPICKCTRL
273
274 menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
275 menuFile->AppendSeparator();
276 menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
277
278 wxMenu *menuCal = new wxMenu;
279 menuCal->Append(Calendar_Cal_Monday,
280 _T("Monday &first weekday\tCtrl-F"),
281 _T("Toggle between Mon and Sun as the first week day"),
282 true);
283 menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"),
284 _T("Toggle highlighting the holidays"),
285 true);
286 menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"),
287 _T("Test custom highlighting"),
288 true);
289 menuCal->Append(Calendar_Cal_SurroundWeeks,
290 _T("Show s&urrounding weeks\tCtrl-W"),
291 _T("Show the neighbouring weeks in the prev/next month"),
292 true);
293 menuCal->AppendSeparator();
294 menuCal->Append(Calendar_Cal_SeqMonth,
295 _T("To&ggle month selector style\tCtrl-G"),
296 _T("Use another style for the calendar controls"),
297 true);
298 menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"),
299 _T("Allow changing the month in the calendar"),
300 true);
301 menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"),
302 _T("Allow changing the year in the calendar"),
303 true);
304 menuCal->AppendSeparator();
305 menuCal->Append(Calendar_Cal_SetDate, _T("SetDate()"), _T("Set date to 2005-12-24."));
306 menuCal->Append(Calendar_Cal_Today, _T("Today()"), _T("Set the current date."));
307
308 // now append the freshly created menu to the menu bar...
309 wxMenuBar *menuBar = new wxMenuBar;
310 menuBar->Append(menuFile, _T("&File"));
311 menuBar->Append(menuCal, _T("&Calendar"));
312
313 menuBar->Check(Calendar_Cal_Monday, true);
314 menuBar->Check(Calendar_Cal_Holidays, true);
315 menuBar->Check(Calendar_Cal_Month, true);
316 menuBar->Check(Calendar_Cal_Year, true);
317
318 // ... and attach this menu bar to the frame
319 SetMenuBar(menuBar);
320
321 m_panel = new MyPanel(this);
322
323 #if wxUSE_STATUSBAR
324 // create a status bar just for fun (by default with 1 pane only)
325 CreateStatusBar(2);
326 SetStatusText(_T("Welcome to wxWidgets!"));
327 #endif // wxUSE_STATUSBAR
328 }
329
330 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
331 {
332 // true is to force the frame to close
333 Close(true);
334 }
335
336 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
337 {
338 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
339 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
340 }
341
342 void MyFrame::OnCalMonday(wxCommandEvent& event)
343 {
344 bool enable = GetMenuBar()->IsChecked(event.GetId());
345
346 m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST);
347 }
348
349 void MyFrame::OnCalHolidays(wxCommandEvent& event)
350 {
351 bool enable = GetMenuBar()->IsChecked(event.GetId());
352
353 m_panel->GetCal()->EnableHolidayDisplay(enable);
354 }
355
356 void MyFrame::OnCalSpecial(wxCommandEvent& event)
357 {
358 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
359 }
360
361 void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
362 {
363 bool allow = GetMenuBar()->IsChecked(event.GetId());
364
365 m_panel->GetCal()->EnableMonthChange(allow);
366 }
367
368 void MyFrame::OnCalAllowYear(wxCommandEvent& event)
369 {
370 bool allow = GetMenuBar()->IsChecked(event.GetId());
371
372 m_panel->GetCal()->EnableYearChange(allow);
373 }
374
375 void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
376 {
377 bool allow = GetMenuBar()->IsChecked(event.GetId());
378
379 m_panel->ToggleCalStyle(allow, wxCAL_SEQUENTIAL_MONTH_SELECTION);
380 }
381
382 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
383 {
384 bool allow = GetMenuBar()->IsChecked(event.GetId());
385
386 m_panel->ToggleCalStyle(allow, wxCAL_SHOW_SURROUNDING_WEEKS);
387 }
388
389 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event)
390 {
391 event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month));
392 }
393
394 void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
395 {
396 m_panel->SetDate();
397 }
398
399 void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
400 {
401 m_panel->Today();
402 }
403
404 #if wxUSE_DATEPICKCTRL
405
406 void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
407 {
408 MyDialog dlg(this, m_panel->GetCal()->GetDate());
409 if ( dlg.ShowModal() == wxID_OK )
410 {
411 const wxDateTime dt = dlg.GetDate(),
412 today = wxDateTime::Today();
413
414 if ( dt.GetDay() == today.GetDay() &&
415 dt.GetMonth() == today.GetMonth() )
416 {
417 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
418 }
419
420 m_panel->GetCal()->SetDate(dt);
421
422 wxLogStatus(_T("Changed the date to your birthday"));
423 }
424 }
425
426 #endif // wxUSE_DATEPICKCTRL
427
428 // ----------------------------------------------------------------------------
429 // MyPanel
430 // ----------------------------------------------------------------------------
431
432 MyPanel::MyPanel(wxFrame *frame)
433 : wxPanel(frame, wxID_ANY)
434 {
435 wxString date;
436 date.Printf(wxT("Selected date: %s"),
437 wxDateTime::Today().FormatISODate().c_str());
438 m_date = new wxStaticText(this, wxID_ANY, date);
439 m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
440 wxDefaultDateTime,
441 wxDefaultPosition,
442 wxDefaultSize,
443 wxCAL_MONDAY_FIRST |
444 wxCAL_SHOW_HOLIDAYS |
445 wxRAISED_BORDER);
446
447 wxBoxSizer *m_sizer = new wxBoxSizer( wxHORIZONTAL );
448
449 m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
450 m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
451
452 SetSizer( m_sizer );
453 m_sizer->SetSizeHints( this );
454 }
455
456 void MyPanel::OnCalendar(wxCalendarEvent& event)
457 {
458 wxLogMessage(wxT("Selected %s from calendar"),
459 event.GetDate().FormatISODate().c_str());
460 }
461
462 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
463 {
464 wxString s;
465 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
466
467 m_date->SetLabel(s);
468 }
469
470 void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event))
471 {
472 wxLogStatus(wxT("Calendar month changed"));
473 }
474
475 void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event))
476 {
477 wxLogStatus(wxT("Calendar year changed"));
478 }
479
480 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
481 {
482 wxLogMessage(wxT("Clicked on %s"),
483 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
484 }
485
486 void MyPanel::ToggleCalStyle(bool on, int flag)
487 {
488 long style = m_calendar->GetWindowStyle();
489 if ( on )
490 style |= flag;
491 else
492 style &= ~flag;
493
494 m_calendar->SetWindowStyle(style);
495
496 m_calendar->Refresh();
497 }
498
499 void MyPanel::HighlightSpecial(bool on)
500 {
501 if ( on )
502 {
503 wxCalendarDateAttr
504 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
505 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
506 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
507
508 m_calendar->SetAttr(17, attrRedCircle);
509 m_calendar->SetAttr(29, attrGreenSquare);
510 m_calendar->SetAttr(13, attrHeaderLike);
511 }
512 else // off
513 {
514 m_calendar->ResetAttr(17);
515 m_calendar->ResetAttr(29);
516 m_calendar->ResetAttr(13);
517 }
518
519 m_calendar->Refresh();
520 }
521
522 void MyPanel::SetDate()
523 {
524 wxDateTime date(24, wxDateTime::Dec, 2005, 23, 59, 59);
525 m_calendar->SetDate(date);
526 }
527
528 void MyPanel::Today()
529 {
530 m_calendar->SetDate(wxDateTime::Today());
531 }
532
533 // ----------------------------------------------------------------------------
534 // MyDialog
535 // ----------------------------------------------------------------------------
536
537 #if wxUSE_DATEPICKCTRL
538
539 MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt)
540 : wxDialog(parent, -1, wxString(_T("Calendar: Choose a date")))
541 {
542 wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
543 sizerBtns->AddButton(new wxButton(this, wxID_OK));
544 sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
545 sizerBtns->Finalise();
546
547 wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
548 sizerText->Add(new wxStaticText(this, -1, _T("Date in ISO format: ")),
549 wxSizerFlags().Border());
550 m_text = new wxTextCtrl(this, -1);
551 sizerText->Add(m_text, wxSizerFlags().Expand().Border());
552
553 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
554 sizerTop->Add(new wxStaticText
555 (
556 this, -1,
557 _T("Enter your birthday date (not before 20th century):")
558 ),
559 wxSizerFlags().Border());
560
561 m_datePicker = new wxDatePickerCtrl(this, -1, dt);
562 m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
563 wxDefaultDateTime);
564 sizerTop->Add(m_datePicker, wxSizerFlags().Expand().Border());
565
566 sizerTop->AddStretchSpacer(1);
567 sizerTop->Add(sizerText);
568
569 sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border());
570
571 SetSizerAndFit(sizerTop);
572 Layout();
573 }
574
575 void MyDialog::OnDateChange(wxDateEvent& event)
576 {
577 m_text->SetValue(event.GetDate().FormatISODate());
578 }
579
580 #endif // wxUSE_DATEPICKCTRL
581