1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
33 #include "wx/stattext.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
38 #include "wx/button.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
44 #include "wx/calctrl.h"
46 #if wxUSE_DATEPICKCTRL
47 #include "wx/datectrl.h"
48 #if wxUSE_DATEPICKCTRL_GENERIC
49 #include "wx/generic/datectrl.h"
50 #endif // wxUSE_DATEPICKCTRL_GENERIC
51 #endif // wxUSE_DATEPICKCTRL
53 #include "../sample.xpm"
55 #define USE_SIZABLE_CALENDAR 0
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp
: public wxApp
65 // override base class virtuals
66 // ----------------------------
68 // this one is called on application startup and is a good place for the app
69 // initialization (doing it here and not in the ctor allows to have an error
70 // return: if OnInit() returns false, the application terminates)
71 virtual bool OnInit();
74 class MyPanel
: public wxPanel
77 MyPanel(wxFrame
*frame
);
79 void OnCalendar(wxCalendarEvent
& event
);
80 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
81 void OnCalendarChange(wxCalendarEvent
& event
);
82 void OnCalMonthChange(wxCalendarEvent
& event
);
83 void OnCalYearChange(wxCalendarEvent
& event
);
85 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
87 // turn on/off the specified style bit on the calendar control
88 void ToggleCalStyle(bool on
, int style
);
90 void HighlightSpecial(bool on
);
96 wxCalendarCtrl
*m_calendar
;
102 // Define a new frame type: this is going to be our main frame
103 class MyFrame
: public wxFrame
107 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
109 // event handlers (these functions should _not_ be virtual)
110 void OnQuit(wxCommandEvent
& event
);
111 void OnAbout(wxCommandEvent
& event
);
113 #if wxUSE_DATEPICKCTRL
114 void OnAskDate(wxCommandEvent
& event
);
116 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
117 #endif // wxUSE_DATEPICKCTRL
119 void OnCalMonday(wxCommandEvent
& event
);
120 void OnCalHolidays(wxCommandEvent
& event
);
121 void OnCalSpecial(wxCommandEvent
& event
);
123 void OnCalAllowMonth(wxCommandEvent
& event
);
124 void OnCalAllowYear(wxCommandEvent
& event
);
126 void OnCalSeqMonth(wxCommandEvent
& event
);
127 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
129 void OnSetDate(wxCommandEvent
& event
);
130 void OnToday(wxCommandEvent
& event
);
132 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
137 // any class wishing to process wxWidgets events must use this macro
138 DECLARE_EVENT_TABLE()
141 #if wxUSE_DATEPICKCTRL
143 // Define a simple modal dialog which asks the user for a date
144 class MyDialog
: public wxDialog
147 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
149 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
152 void OnDateChange(wxDateEvent
& event
);
155 wxDatePickerCtrlBase
*m_datePicker
;
159 DECLARE_EVENT_TABLE()
162 #endif // wxUSE_DATEPICKCTRL
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
168 // IDs for the controls and the menu commands
172 Calendar_File_About
= wxID_ABOUT
,
173 Calendar_File_Quit
= wxID_EXIT
,
174 Calendar_Cal_Monday
= 200,
175 Calendar_Cal_Holidays
,
176 Calendar_Cal_Special
,
179 Calendar_Cal_SeqMonth
,
180 Calendar_Cal_SurroundWeeks
,
181 Calendar_Cal_SetDate
,
183 #if wxUSE_DATEPICKCTRL
184 Calendar_DatePicker_AskDate
= 300,
185 Calendar_DatePicker_ShowCentury
,
186 Calendar_DatePicker_DropDown
,
187 Calendar_DatePicker_AllowNone
,
188 Calendar_DatePicker_StartWithNone
,
189 #if wxUSE_DATEPICKCTRL_GENERIC
190 Calendar_DatePicker_Generic
,
191 #endif // wxUSE_DATEPICKCTRL_GENERIC
192 #endif // wxUSE_DATEPICKCTRL
193 Calendar_CalCtrl
= 1000
196 // ----------------------------------------------------------------------------
197 // event tables and other macros for wxWidgets
198 // ----------------------------------------------------------------------------
200 // the event tables connect the wxWidgets events with the functions (event
201 // handlers) which process them. It can be also done at run-time, but for the
202 // simple menu events like this the static method is much simpler.
203 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
204 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
205 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
207 #if wxUSE_DATEPICKCTRL
208 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
210 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
211 MyFrame::OnUpdateUIStartWithNone
)
212 #endif // wxUSE_DATEPICKCTRL
214 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
215 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
216 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
218 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
219 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
221 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
222 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
224 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
225 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
228 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
231 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
232 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
233 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
234 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
235 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
236 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
239 #if wxUSE_DATEPICKCTRL
241 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
242 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
245 #endif // wxUSE_DATEPICKCTRL
247 // Create a new application object: this macro will allow wxWidgets to create
248 // the application object during program execution (it's better than using a
249 // static object for many reasons) and also declares the accessor function
250 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
254 // ============================================================================
256 // ============================================================================
258 // ----------------------------------------------------------------------------
259 // the application class
260 // ----------------------------------------------------------------------------
262 // `Main program' equivalent: the program execution "starts" here
265 // Create the main application window
266 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample")
268 ,wxPoint(50, 50), wxSize(450, 340)
274 // success: wxApp::OnRun() will be called which will enter the main message
275 // loop and the application will run. If we returned false here, the
276 // application would exit immediately.
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
285 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
286 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
288 // set the frame icon
289 SetIcon(wxIcon(sample_xpm
));
292 wxMenu
*menuFile
= new wxMenu
;
293 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
294 menuFile
->AppendSeparator();
295 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
297 wxMenu
*menuCal
= new wxMenu
;
298 menuCal
->Append(Calendar_Cal_Monday
,
299 _T("Monday &first weekday\tCtrl-F"),
300 _T("Toggle between Mon and Sun as the first week day"),
302 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
303 _T("Toggle highlighting the holidays"),
305 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
306 _T("Test custom highlighting"),
308 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
309 _T("Show s&urrounding weeks\tCtrl-W"),
310 _T("Show the neighbouring weeks in the prev/next month"),
312 menuCal
->AppendSeparator();
313 menuCal
->Append(Calendar_Cal_SeqMonth
,
314 _T("To&ggle month selector style\tCtrl-G"),
315 _T("Use another style for the calendar controls"),
317 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
318 _T("Allow changing the month in the calendar"),
320 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
321 _T("Allow changing the year in the calendar"),
323 menuCal
->AppendSeparator();
324 menuCal
->Append(Calendar_Cal_SetDate
, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
325 menuCal
->Append(Calendar_Cal_Today
, _T("Call &Today()"), _T("Set the current date."));
327 #if wxUSE_DATEPICKCTRL
328 wxMenu
*menuDate
= new wxMenu
;
329 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
330 _T("Al&ways show century"));
331 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
332 _T("Use &drop down control"));
333 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
334 _T("Allow &no date"));
335 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
336 _T("Start &with no date"));
337 #if wxUSE_DATEPICKCTRL_GENERIC
338 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
339 _T("Use &generic version of the control"));
340 #endif // wxUSE_DATEPICKCTRL_GENERIC
341 menuDate
->AppendSeparator();
342 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
343 #endif // wxUSE_DATEPICKCTRL
345 // now append the freshly created menu to the menu bar...
346 wxMenuBar
*menuBar
= new wxMenuBar
;
347 menuBar
->Append(menuFile
, _T("&File"));
348 menuBar
->Append(menuCal
, _T("&Calendar"));
349 #if wxUSE_DATEPICKCTRL
350 menuBar
->Append(menuDate
, _T("&Date picker"));
351 #endif // wxUSE_DATEPICKCTRL
353 menuBar
->Check(Calendar_Cal_Monday
, true);
354 menuBar
->Check(Calendar_Cal_Holidays
, true);
355 menuBar
->Check(Calendar_Cal_Month
, true);
356 menuBar
->Check(Calendar_Cal_Year
, true);
358 #if wxUSE_DATEPICKCTRL
359 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
360 #endif // wxUSE_DATEPICKCTRL
362 // ... and attach this menu bar to the frame
365 m_panel
= new MyPanel(this);
368 // create a status bar just for fun (by default with 1 pane only)
370 SetStatusText(_T("Welcome to wxWidgets!"));
371 #endif // wxUSE_STATUSBAR
374 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
376 // true is to force the frame to close
380 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
382 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
383 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
386 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
388 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
390 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
393 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
395 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
397 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
400 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
402 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
405 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
407 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
409 m_panel
->GetCal()->EnableMonthChange(allow
);
412 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
414 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
416 m_panel
->GetCal()->EnableYearChange(allow
);
419 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
421 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
423 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
426 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
428 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
430 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
433 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
435 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
438 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
443 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
448 #if wxUSE_DATEPICKCTRL
450 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
452 // it only makes sense to start with invalid date if we can have no date
453 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
456 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
458 wxDateTime dt
= m_panel
->GetCal()->GetDate();
460 int style
= wxDP_DEFAULT
;
461 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
462 style
|= wxDP_SHOWCENTURY
;
463 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
464 style
|= wxDP_DROPDOWN
;
465 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
467 style
|= wxDP_ALLOWNONE
;
469 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
470 dt
= wxDefaultDateTime
;
473 MyDialog
dlg(this, dt
, style
);
474 if ( dlg
.ShowModal() == wxID_OK
)
479 const wxDateTime today
= wxDateTime::Today();
481 if ( dt
.GetDay() == today
.GetDay() &&
482 dt
.GetMonth() == today
.GetMonth() )
484 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
487 m_panel
->GetCal()->SetDate(dt
);
489 wxLogStatus(_T("Changed the date to your input"));
493 wxLogStatus(_T("No date entered"));
498 #endif // wxUSE_DATEPICKCTRL
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 MyPanel::MyPanel(wxFrame
*frame
)
505 : wxPanel(frame
, wxID_ANY
)
508 date
.Printf(wxT("Selected date: %s"),
509 wxDateTime::Today().FormatISODate().c_str());
510 m_date
= new wxStaticText(this, wxID_ANY
, date
);
511 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
516 wxCAL_SHOW_HOLIDAYS
|
519 #if USE_SIZABLE_CALENDAR
520 wxCalendarCtrl
*sizableCalendar
= new wxCalendarCtrl(this, wxID_ANY
);
523 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
524 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
525 wxBoxSizer
*m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
527 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
528 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
530 #if USE_SIZABLE_CALENDAR
531 m_sizer
->Add(sizableCalendar
, 1, wxEXPAND
);
535 m_sizer
->SetSizeHints( this );
538 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
540 wxLogMessage(wxT("Selected %s from calendar"),
541 event
.GetDate().FormatISODate().c_str());
544 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
547 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
552 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
554 wxLogStatus(wxT("Calendar month changed"));
557 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
559 wxLogStatus(wxT("Calendar year changed"));
562 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
564 wxLogMessage(wxT("Clicked on %s"),
565 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
568 void MyPanel::ToggleCalStyle(bool on
, int flag
)
570 long style
= m_calendar
->GetWindowStyle();
576 m_calendar
->SetWindowStyle(style
);
578 m_calendar
->Refresh();
581 void MyPanel::HighlightSpecial(bool on
)
586 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
587 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
588 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
590 m_calendar
->SetAttr(17, attrRedCircle
);
591 m_calendar
->SetAttr(29, attrGreenSquare
);
592 m_calendar
->SetAttr(13, attrHeaderLike
);
596 m_calendar
->ResetAttr(17);
597 m_calendar
->ResetAttr(29);
598 m_calendar
->ResetAttr(13);
601 m_calendar
->Refresh();
604 void MyPanel::SetDate()
606 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
607 m_calendar
->SetDate(date
);
610 void MyPanel::Today()
612 m_calendar
->SetDate(wxDateTime::Today());
615 // ----------------------------------------------------------------------------
617 // ----------------------------------------------------------------------------
619 #if wxUSE_DATEPICKCTRL
621 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
622 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
624 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
625 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
626 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
627 sizerBtns
->Realize();
629 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
630 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
631 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
632 m_text
= new wxTextCtrl(this, wxID_ANY
);
633 sizerText
->Add(m_text
, wxSizerFlags().
634 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
636 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
637 sizerTop
->Add(new wxStaticText
640 _T("Enter your birthday date (not before 20th century):")
642 wxSizerFlags().Border());
644 #if wxUSE_DATEPICKCTRL_GENERIC
645 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
646 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
647 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
652 #endif // wxUSE_DATEPICKCTRL_GENERIC
653 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
654 wxDefaultPosition
, wxDefaultSize
,
656 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
658 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
660 sizerTop
->AddStretchSpacer(1);
661 sizerTop
->Add(sizerText
);
663 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
665 SetSizerAndFit(sizerTop
);
669 void MyDialog::OnDateChange(wxDateEvent
& event
)
671 const wxDateTime dt
= event
.GetDate();
673 m_text
->SetValue(dt
.FormatISODate());
675 m_text
->SetValue(wxEmptyString
);
678 #endif // wxUSE_DATEPICKCTRL