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"
41 #include "wx/textctrl.h"
43 #include "wx/calctrl.h"
45 #if wxUSE_DATEPICKCTRL
46 #include "wx/datectrl.h"
47 #if wxUSE_DATEPICKCTRL_GENERIC
48 #include "wx/generic/datectrl.h"
49 #endif // wxUSE_DATEPICKCTRL_GENERIC
50 #endif // wxUSE_DATEPICKCTRL
52 #include "../sample.xpm"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // Define a new application type, each program should derive a class from wxApp
59 class MyApp
: public wxApp
62 // override base class virtuals
63 // ----------------------------
65 // this one is called on application startup and is a good place for the app
66 // initialization (doing it here and not in the ctor allows to have an error
67 // return: if OnInit() returns false, the application terminates)
68 virtual bool OnInit();
71 class MyPanel
: public wxPanel
74 MyPanel(wxFrame
*frame
);
76 void OnCalendar(wxCalendarEvent
& event
);
77 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
78 void OnCalendarChange(wxCalendarEvent
& event
);
79 void OnCalMonthChange(wxCalendarEvent
& event
);
80 void OnCalYearChange(wxCalendarEvent
& event
);
82 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
84 // turn on/off the specified style bit on the calendar control
85 void ToggleCalStyle(bool on
, int style
);
87 void HighlightSpecial(bool on
);
93 wxCalendarCtrl
*m_calendar
;
99 // Define a new frame type: this is going to be our main frame
100 class MyFrame
: public wxFrame
104 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
106 // event handlers (these functions should _not_ be virtual)
107 void OnQuit(wxCommandEvent
& event
);
108 void OnAbout(wxCommandEvent
& event
);
110 #if wxUSE_DATEPICKCTRL
111 void OnAskDate(wxCommandEvent
& event
);
112 #endif // wxUSE_DATEPICKCTRL
114 void OnCalMonday(wxCommandEvent
& event
);
115 void OnCalHolidays(wxCommandEvent
& event
);
116 void OnCalSpecial(wxCommandEvent
& event
);
118 void OnCalAllowMonth(wxCommandEvent
& event
);
119 void OnCalAllowYear(wxCommandEvent
& event
);
121 void OnCalSeqMonth(wxCommandEvent
& event
);
122 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
124 void OnSetDate(wxCommandEvent
& event
);
125 void OnToday(wxCommandEvent
& event
);
127 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
132 // any class wishing to process wxWidgets events must use this macro
133 DECLARE_EVENT_TABLE()
136 #if wxUSE_DATEPICKCTRL
138 // Define a simple modal dialog which asks the user for a date
139 class MyDialog
: public wxDialog
142 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
144 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
147 void OnDateChange(wxDateEvent
& event
);
150 wxDatePickerCtrlBase
*m_datePicker
;
154 DECLARE_EVENT_TABLE()
157 #endif // wxUSE_DATEPICKCTRL
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 // IDs for the controls and the menu commands
167 Calendar_File_About
= wxID_ABOUT
,
168 Calendar_File_Quit
= wxID_EXIT
,
169 Calendar_Cal_Monday
= 200,
170 Calendar_Cal_Holidays
,
171 Calendar_Cal_Special
,
174 Calendar_Cal_SeqMonth
,
175 Calendar_Cal_SurroundWeeks
,
176 Calendar_Cal_SetDate
,
178 #if wxUSE_DATEPICKCTRL
179 Calendar_DatePicker_AskDate
= 300,
180 Calendar_DatePicker_ShowCentury
,
181 Calendar_DatePicker_DropDown
,
182 Calendar_DatePicker_AllowNone
,
183 #if wxUSE_DATEPICKCTRL_GENERIC
184 Calendar_DatePicker_Generic
,
185 #endif // wxUSE_DATEPICKCTRL_GENERIC
186 #endif // wxUSE_DATEPICKCTRL
187 Calendar_CalCtrl
= 1000
190 // ----------------------------------------------------------------------------
191 // event tables and other macros for wxWidgets
192 // ----------------------------------------------------------------------------
194 // the event tables connect the wxWidgets events with the functions (event
195 // handlers) which process them. It can be also done at run-time, but for the
196 // simple menu events like this the static method is much simpler.
197 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
198 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
199 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
201 #if wxUSE_DATEPICKCTRL
202 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
203 #endif // wxUSE_DATEPICKCTRL
205 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
206 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
207 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
209 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
210 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
212 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
213 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
215 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
216 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
219 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
222 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
223 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
224 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
225 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
226 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
227 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
230 #if wxUSE_DATEPICKCTRL
232 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
233 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
236 #endif // wxUSE_DATEPICKCTRL
238 // Create a new application object: this macro will allow wxWidgets to create
239 // the application object during program execution (it's better than using a
240 // static object for many reasons) and also declares the accessor function
241 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
245 // ============================================================================
247 // ============================================================================
249 // ----------------------------------------------------------------------------
250 // the application class
251 // ----------------------------------------------------------------------------
253 // `Main program' equivalent: the program execution "starts" here
256 // Create the main application window
257 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
258 wxPoint(50, 50), wxSize(450, 340));
262 // success: wxApp::OnRun() will be called which will enter the main message
263 // loop and the application will run. If we returned false here, the
264 // application would exit immediately.
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
273 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
274 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
276 // set the frame icon
277 SetIcon(wxIcon(sample_xpm
));
280 wxMenu
*menuFile
= new wxMenu
;
281 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
282 menuFile
->AppendSeparator();
283 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
285 wxMenu
*menuCal
= new wxMenu
;
286 menuCal
->Append(Calendar_Cal_Monday
,
287 _T("Monday &first weekday\tCtrl-F"),
288 _T("Toggle between Mon and Sun as the first week day"),
290 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
291 _T("Toggle highlighting the holidays"),
293 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
294 _T("Test custom highlighting"),
296 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
297 _T("Show s&urrounding weeks\tCtrl-W"),
298 _T("Show the neighbouring weeks in the prev/next month"),
300 menuCal
->AppendSeparator();
301 menuCal
->Append(Calendar_Cal_SeqMonth
,
302 _T("To&ggle month selector style\tCtrl-G"),
303 _T("Use another style for the calendar controls"),
305 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
306 _T("Allow changing the month in the calendar"),
308 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
309 _T("Allow changing the year in the calendar"),
311 menuCal
->AppendSeparator();
312 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
313 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
315 #if wxUSE_DATEPICKCTRL
316 wxMenu
*menuDate
= new wxMenu
;
317 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
318 _T("Al&ways show century"));
319 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
320 _T("Use &drop down control"));
321 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
322 _T("Allow &no date"));
323 #if wxUSE_DATEPICKCTRL_GENERIC
324 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
325 _T("Use &generic version of the control"));
326 #endif // wxUSE_DATEPICKCTRL_GENERIC
327 menuDate
->AppendSeparator();
328 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
329 #endif // wxUSE_DATEPICKCTRL
331 // now append the freshly created menu to the menu bar...
332 wxMenuBar
*menuBar
= new wxMenuBar
;
333 menuBar
->Append(menuFile
, _T("&File"));
334 menuBar
->Append(menuCal
, _T("&Calendar"));
335 #if wxUSE_DATEPICKCTRL
336 menuBar
->Append(menuDate
, _T("&Date picker"));
337 #endif // wxUSE_DATEPICKCTRL
339 menuBar
->Check(Calendar_Cal_Monday
, true);
340 menuBar
->Check(Calendar_Cal_Holidays
, true);
341 menuBar
->Check(Calendar_Cal_Month
, true);
342 menuBar
->Check(Calendar_Cal_Year
, true);
344 #if wxUSE_DATEPICKCTRL
345 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
346 #endif // wxUSE_DATEPICKCTRL
348 // ... and attach this menu bar to the frame
351 m_panel
= new MyPanel(this);
354 // create a status bar just for fun (by default with 1 pane only)
356 SetStatusText(_T("Welcome to wxWidgets!"));
357 #endif // wxUSE_STATUSBAR
360 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
362 // true is to force the frame to close
366 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
368 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
369 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
372 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
374 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
376 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
379 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
381 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
383 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
386 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
388 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
391 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
393 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
395 m_panel
->GetCal()->EnableMonthChange(allow
);
398 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
400 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
402 m_panel
->GetCal()->EnableYearChange(allow
);
405 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
407 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
409 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
412 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
414 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
416 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
419 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
421 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
424 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
429 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
434 #if wxUSE_DATEPICKCTRL
436 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
438 int style
= wxDP_DEFAULT
;
439 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
440 style
|= wxDP_SHOWCENTURY
;
441 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
442 style
|= wxDP_DROPDOWN
;
443 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
444 style
|= wxDP_ALLOWNONE
;
446 MyDialog
dlg(this, m_panel
->GetCal()->GetDate(), style
);
447 if ( dlg
.ShowModal() == wxID_OK
)
449 const wxDateTime dt
= dlg
.GetDate();
452 const wxDateTime today
= wxDateTime::Today();
454 if ( dt
.GetDay() == today
.GetDay() &&
455 dt
.GetMonth() == today
.GetMonth() )
457 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
460 m_panel
->GetCal()->SetDate(dt
);
462 wxLogStatus(_T("Changed the date to your input"));
466 wxLogStatus(_T("No date entered"));
471 #endif // wxUSE_DATEPICKCTRL
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 MyPanel::MyPanel(wxFrame
*frame
)
478 : wxPanel(frame
, wxID_ANY
)
481 date
.Printf(wxT("Selected date: %s"),
482 wxDateTime::Today().FormatISODate().c_str());
483 m_date
= new wxStaticText(this, wxID_ANY
, date
);
484 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
489 wxCAL_SHOW_HOLIDAYS
|
492 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
494 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
495 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
498 m_sizer
->SetSizeHints( this );
501 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
503 wxLogMessage(wxT("Selected %s from calendar"),
504 event
.GetDate().FormatISODate().c_str());
507 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
510 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
515 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
517 wxLogStatus(wxT("Calendar month changed"));
520 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
522 wxLogStatus(wxT("Calendar year changed"));
525 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
527 wxLogMessage(wxT("Clicked on %s"),
528 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
531 void MyPanel::ToggleCalStyle(bool on
, int flag
)
533 long style
= m_calendar
->GetWindowStyle();
539 m_calendar
->SetWindowStyle(style
);
541 m_calendar
->Refresh();
544 void MyPanel::HighlightSpecial(bool on
)
549 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
550 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
551 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
553 m_calendar
->SetAttr(17, attrRedCircle
);
554 m_calendar
->SetAttr(29, attrGreenSquare
);
555 m_calendar
->SetAttr(13, attrHeaderLike
);
559 m_calendar
->ResetAttr(17);
560 m_calendar
->ResetAttr(29);
561 m_calendar
->ResetAttr(13);
564 m_calendar
->Refresh();
567 void MyPanel::SetDate()
569 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
570 m_calendar
->SetDate(date
);
573 void MyPanel::Today()
575 m_calendar
->SetDate(wxDateTime::Today());
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 #if wxUSE_DATEPICKCTRL
584 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
585 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
587 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
588 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
589 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
590 sizerBtns
->Realize();
592 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
593 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
594 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
595 m_text
= new wxTextCtrl(this, wxID_ANY
);
596 sizerText
->Add(m_text
, wxSizerFlags().
597 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
599 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
600 sizerTop
->Add(new wxStaticText
603 _T("Enter your birthday date (not before 20th century):")
605 wxSizerFlags().Border());
607 #if wxUSE_DATEPICKCTRL_GENERIC
608 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
609 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
610 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
615 #endif // wxUSE_DATEPICKCTRL_GENERIC
616 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
617 wxDefaultPosition
, wxDefaultSize
,
619 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
621 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
623 sizerTop
->AddStretchSpacer(1);
624 sizerTop
->Add(sizerText
);
626 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
628 SetSizerAndFit(sizerTop
);
632 void MyDialog::OnDateChange(wxDateEvent
& event
)
634 const wxDateTime dt
= event
.GetDate();
636 m_text
->SetValue(dt
.FormatISODate());
638 m_text
->SetValue(wxEmptyString
);
641 #endif // wxUSE_DATEPICKCTRL