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 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
66 // this one is called on application startup and is a good place for the app
67 // initialization (doing it here and not in the ctor allows to have an error
68 // return: if OnInit() returns false, the application terminates)
69 virtual bool OnInit();
72 class MyPanel
: public wxPanel
75 MyPanel(wxFrame
*frame
);
77 void OnCalendar(wxCalendarEvent
& event
);
78 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
79 void OnCalendarChange(wxCalendarEvent
& event
);
80 void OnCalMonthChange(wxCalendarEvent
& event
);
81 void OnCalYearChange(wxCalendarEvent
& event
);
83 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
85 // turn on/off the specified style bit on the calendar control
86 void ToggleCalStyle(bool on
, int style
);
88 void HighlightSpecial(bool on
);
94 wxCalendarCtrl
*m_calendar
;
101 // Define a new frame type: this is going to be our main frame
102 class MyFrame
: public wxFrame
106 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
108 // event handlers (these functions should _not_ be virtual)
109 void OnQuit(wxCommandEvent
& event
);
110 void OnAbout(wxCommandEvent
& event
);
112 #if wxUSE_DATEPICKCTRL
113 void OnAskDate(wxCommandEvent
& event
);
115 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
116 #endif // wxUSE_DATEPICKCTRL
118 void OnCalMonday(wxCommandEvent
& event
);
119 void OnCalHolidays(wxCommandEvent
& event
);
120 void OnCalSpecial(wxCommandEvent
& event
);
122 void OnCalAllowMonth(wxCommandEvent
& event
);
123 void OnCalAllowYear(wxCommandEvent
& event
);
125 void OnCalSeqMonth(wxCommandEvent
& event
);
126 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
128 void OnSetDate(wxCommandEvent
& event
);
129 void OnToday(wxCommandEvent
& event
);
131 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
136 // any class wishing to process wxWidgets events must use this macro
137 DECLARE_EVENT_TABLE()
140 #if wxUSE_DATEPICKCTRL
142 // Define a simple modal dialog which asks the user for a date
143 class MyDialog
: public wxDialog
146 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
148 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
151 void OnDateChange(wxDateEvent
& event
);
154 wxDatePickerCtrlBase
*m_datePicker
;
158 DECLARE_EVENT_TABLE()
161 #endif // wxUSE_DATEPICKCTRL
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 // IDs for the controls and the menu commands
171 Calendar_File_About
= wxID_ABOUT
,
172 Calendar_File_Quit
= wxID_EXIT
,
173 Calendar_Cal_Monday
= 200,
174 Calendar_Cal_Holidays
,
175 Calendar_Cal_Special
,
178 Calendar_Cal_SeqMonth
,
179 Calendar_Cal_SurroundWeeks
,
180 Calendar_Cal_SetDate
,
182 #if wxUSE_DATEPICKCTRL
183 Calendar_DatePicker_AskDate
= 300,
184 Calendar_DatePicker_ShowCentury
,
185 Calendar_DatePicker_DropDown
,
186 Calendar_DatePicker_AllowNone
,
187 Calendar_DatePicker_StartWithNone
,
188 #if wxUSE_DATEPICKCTRL_GENERIC
189 Calendar_DatePicker_Generic
,
190 #endif // wxUSE_DATEPICKCTRL_GENERIC
191 #endif // wxUSE_DATEPICKCTRL
192 Calendar_CalCtrl
= 1000
195 // ----------------------------------------------------------------------------
196 // event tables and other macros for wxWidgets
197 // ----------------------------------------------------------------------------
199 // the event tables connect the wxWidgets events with the functions (event
200 // handlers) which process them. It can be also done at run-time, but for the
201 // simple menu events like this the static method is much simpler.
202 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
203 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
204 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
206 #if wxUSE_DATEPICKCTRL
207 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
209 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
210 MyFrame::OnUpdateUIStartWithNone
)
211 #endif // wxUSE_DATEPICKCTRL
213 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
214 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
215 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
217 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
218 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
220 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
221 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
223 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
224 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
227 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
230 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
231 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
232 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
233 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
234 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
235 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
238 #if wxUSE_DATEPICKCTRL
240 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
241 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
244 #endif // wxUSE_DATEPICKCTRL
246 // Create a new application object: this macro will allow wxWidgets to create
247 // the application object during program execution (it's better than using a
248 // static object for many reasons) and also declares the accessor function
249 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
253 // ============================================================================
255 // ============================================================================
257 // ----------------------------------------------------------------------------
258 // the application class
259 // ----------------------------------------------------------------------------
261 // `Main program' equivalent: the program execution "starts" here
264 // Create the main application window
265 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample")
267 ,wxPoint(50, 50), wxSize(450, 340)
273 // success: wxApp::OnRun() will be called which will enter the main message
274 // loop and the application will run. If we returned false here, the
275 // application would exit immediately.
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
284 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
285 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
287 // set the frame icon
288 SetIcon(wxIcon(sample_xpm
));
291 wxMenu
*menuFile
= new wxMenu
;
292 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
293 menuFile
->AppendSeparator();
294 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
296 wxMenu
*menuCal
= new wxMenu
;
297 menuCal
->Append(Calendar_Cal_Monday
,
298 _T("Monday &first weekday\tCtrl-F"),
299 _T("Toggle between Mon and Sun as the first week day"),
301 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
302 _T("Toggle highlighting the holidays"),
304 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
305 _T("Test custom highlighting"),
307 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
308 _T("Show s&urrounding weeks\tCtrl-W"),
309 _T("Show the neighbouring weeks in the prev/next month"),
311 menuCal
->AppendSeparator();
312 menuCal
->Append(Calendar_Cal_SeqMonth
,
313 _T("To&ggle month selector style\tCtrl-G"),
314 _T("Use another style for the calendar controls"),
316 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
317 _T("Allow changing the month in the calendar"),
319 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
320 _T("Allow changing the year in the calendar"),
322 menuCal
->AppendSeparator();
323 menuCal
->Append(Calendar_Cal_SetDate
, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
324 menuCal
->Append(Calendar_Cal_Today
, _T("Call &Today()"), _T("Set the current date."));
326 #if wxUSE_DATEPICKCTRL
327 wxMenu
*menuDate
= new wxMenu
;
328 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
329 _T("Al&ways show century"));
330 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
331 _T("Use &drop down control"));
332 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
333 _T("Allow &no date"));
334 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
335 _T("Start &with no date"));
336 #if wxUSE_DATEPICKCTRL_GENERIC
337 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
338 _T("Use &generic version of the control"));
339 #endif // wxUSE_DATEPICKCTRL_GENERIC
340 menuDate
->AppendSeparator();
341 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
342 #endif // wxUSE_DATEPICKCTRL
344 // now append the freshly created menu to the menu bar...
345 wxMenuBar
*menuBar
= new wxMenuBar
;
346 menuBar
->Append(menuFile
, _T("&File"));
347 menuBar
->Append(menuCal
, _T("&Calendar"));
348 #if wxUSE_DATEPICKCTRL
349 menuBar
->Append(menuDate
, _T("&Date picker"));
350 #endif // wxUSE_DATEPICKCTRL
352 menuBar
->Check(Calendar_Cal_Monday
, true);
353 menuBar
->Check(Calendar_Cal_Holidays
, true);
354 menuBar
->Check(Calendar_Cal_Month
, true);
355 menuBar
->Check(Calendar_Cal_Year
, true);
357 #if wxUSE_DATEPICKCTRL
358 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
359 #endif // wxUSE_DATEPICKCTRL
361 // ... and attach this menu bar to the frame
364 m_panel
= new MyPanel(this);
367 // create a status bar just for fun (by default with 1 pane only)
369 SetStatusText(_T("Welcome to wxWidgets!"));
370 #endif // wxUSE_STATUSBAR
373 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
375 // true is to force the frame to close
379 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
381 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
382 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
385 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
387 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
389 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
392 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
394 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
396 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
399 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
401 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
404 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
406 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
408 m_panel
->GetCal()->EnableMonthChange(allow
);
411 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
413 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
415 m_panel
->GetCal()->EnableYearChange(allow
);
418 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
420 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
422 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
425 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
427 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
429 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
432 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
434 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
437 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
442 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
447 #if wxUSE_DATEPICKCTRL
449 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
451 // it only makes sense to start with invalid date if we can have no date
452 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
455 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
457 wxDateTime dt
= m_panel
->GetCal()->GetDate();
459 int style
= wxDP_DEFAULT
;
460 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
461 style
|= wxDP_SHOWCENTURY
;
462 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
463 style
|= wxDP_DROPDOWN
;
464 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
466 style
|= wxDP_ALLOWNONE
;
468 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
469 dt
= wxDefaultDateTime
;
472 MyDialog
dlg(this, dt
, style
);
473 if ( dlg
.ShowModal() == wxID_OK
)
478 const wxDateTime today
= wxDateTime::Today();
480 if ( dt
.GetDay() == today
.GetDay() &&
481 dt
.GetMonth() == today
.GetMonth() )
483 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
486 m_panel
->GetCal()->SetDate(dt
);
488 wxLogStatus(_T("Changed the date to your input"));
492 wxLogStatus(_T("No date entered"));
497 #endif // wxUSE_DATEPICKCTRL
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 MyPanel::MyPanel(wxFrame
*frame
)
504 : wxPanel(frame
, wxID_ANY
)
507 date
.Printf(wxT("Selected date: %s"),
508 wxDateTime::Today().FormatISODate().c_str());
509 m_date
= new wxStaticText(this, wxID_ANY
, date
);
510 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
515 wxCAL_SHOW_HOLIDAYS
|
518 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
519 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
520 m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
522 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
523 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
526 m_sizer
->SetSizeHints( this );
529 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
531 wxLogMessage(wxT("Selected %s from calendar"),
532 event
.GetDate().FormatISODate().c_str());
535 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
538 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
543 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
545 wxLogStatus(wxT("Calendar month changed"));
548 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
550 wxLogStatus(wxT("Calendar year changed"));
553 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
555 wxLogMessage(wxT("Clicked on %s"),
556 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
559 void MyPanel::ToggleCalStyle(bool on
, int flag
)
561 long style
= m_calendar
->GetWindowStyle();
567 if ( flag
== wxCAL_SEQUENTIAL_MONTH_SELECTION
)
569 // changing this style requires recreating the control
570 wxCalendarCtrl
*calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
571 m_calendar
->GetDate(),
575 m_sizer
->Replace(m_calendar
, calendar
);
577 m_calendar
= calendar
;
581 else // just changing the style is enough
583 m_calendar
->SetWindowStyle(style
);
585 m_calendar
->Refresh();
589 void MyPanel::HighlightSpecial(bool on
)
594 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
595 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
596 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
598 m_calendar
->SetAttr(17, attrRedCircle
);
599 m_calendar
->SetAttr(29, attrGreenSquare
);
600 m_calendar
->SetAttr(13, attrHeaderLike
);
604 m_calendar
->ResetAttr(17);
605 m_calendar
->ResetAttr(29);
606 m_calendar
->ResetAttr(13);
609 m_calendar
->Refresh();
612 void MyPanel::SetDate()
614 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
615 m_calendar
->SetDate(date
);
618 void MyPanel::Today()
620 m_calendar
->SetDate(wxDateTime::Today());
623 // ----------------------------------------------------------------------------
625 // ----------------------------------------------------------------------------
627 #if wxUSE_DATEPICKCTRL
629 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
630 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
632 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
633 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
634 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
635 sizerBtns
->Realize();
637 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
638 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
639 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
640 m_text
= new wxTextCtrl(this, wxID_ANY
);
641 sizerText
->Add(m_text
, wxSizerFlags().
642 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
644 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
645 sizerTop
->Add(new wxStaticText
648 _T("Enter your birthday date (not before 20th century):")
650 wxSizerFlags().Border());
652 #if wxUSE_DATEPICKCTRL_GENERIC
653 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
654 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
655 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
660 #endif // wxUSE_DATEPICKCTRL_GENERIC
661 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
662 wxDefaultPosition
, wxDefaultSize
,
664 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
666 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
668 sizerTop
->AddStretchSpacer(1);
669 sizerTop
->Add(sizerText
);
671 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
673 SetSizerAndFit(sizerTop
);
677 void MyDialog::OnDateChange(wxDateEvent
& event
)
679 const wxDateTime dt
= event
.GetDate();
681 m_text
->SetValue(dt
.FormatISODate());
683 m_text
->SetValue(wxEmptyString
);
686 #endif // wxUSE_DATEPICKCTRL