1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
38 #include "wx/stattext.h"
40 #include "wx/layout.h"
41 #include "wx/msgdlg.h"
45 #include "wx/textctrl.h"
47 #include "wx/calctrl.h"
49 #if wxUSE_DATEPICKCTRL
50 #include "wx/datectrl.h"
51 #if wxUSE_DATEPICKCTRL_GENERIC
52 #include "wx/generic/datectrl.h"
53 #endif // wxUSE_DATEPICKCTRL_GENERIC
54 #endif // wxUSE_DATEPICKCTRL
56 // the application icon (under Windows and OS/2 it is in resources and even
57 // though we could still include the XPM here it would be unused)
58 #if !defined(__WXMSW__) && !defined(__WXPM__)
59 #include "../sample.xpm"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // Define a new application type, each program should derive a class from wxApp
67 class MyApp
: public wxApp
70 // override base class virtuals
71 // ----------------------------
73 // this one is called on application startup and is a good place for the app
74 // initialization (doing it here and not in the ctor allows to have an error
75 // return: if OnInit() returns false, the application terminates)
76 virtual bool OnInit();
79 class MyPanel
: public wxPanel
82 MyPanel(wxFrame
*frame
);
84 void OnCalendar(wxCalendarEvent
& event
);
85 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
86 void OnCalendarChange(wxCalendarEvent
& event
);
87 void OnCalMonthChange(wxCalendarEvent
& event
);
88 void OnCalYearChange(wxCalendarEvent
& event
);
90 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
92 // turn on/off the specified style bit on the calendar control
93 void ToggleCalStyle(bool on
, int style
);
95 void HighlightSpecial(bool on
);
101 wxCalendarCtrl
*m_calendar
;
102 wxStaticText
*m_date
;
104 DECLARE_EVENT_TABLE()
107 // Define a new frame type: this is going to be our main frame
108 class MyFrame
: public wxFrame
112 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
114 // event handlers (these functions should _not_ be virtual)
115 void OnQuit(wxCommandEvent
& event
);
116 void OnAbout(wxCommandEvent
& event
);
118 #if wxUSE_DATEPICKCTRL
119 void OnAskDate(wxCommandEvent
& event
);
120 #endif // wxUSE_DATEPICKCTRL
122 void OnCalMonday(wxCommandEvent
& event
);
123 void OnCalHolidays(wxCommandEvent
& event
);
124 void OnCalSpecial(wxCommandEvent
& event
);
126 void OnCalAllowMonth(wxCommandEvent
& event
);
127 void OnCalAllowYear(wxCommandEvent
& event
);
129 void OnCalSeqMonth(wxCommandEvent
& event
);
130 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
132 void OnSetDate(wxCommandEvent
& event
);
133 void OnToday(wxCommandEvent
& event
);
135 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
140 // any class wishing to process wxWidgets events must use this macro
141 DECLARE_EVENT_TABLE()
144 #if wxUSE_DATEPICKCTRL
146 // Define a simple modal dialog which asks the user for a date
147 class MyDialog
: public wxDialog
150 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
152 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
155 void OnDateChange(wxDateEvent
& event
);
158 wxDatePickerCtrlBase
*m_datePicker
;
162 DECLARE_EVENT_TABLE()
165 #endif // wxUSE_DATEPICKCTRL
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 // IDs for the controls and the menu commands
175 Calendar_File_About
= wxID_ABOUT
,
176 Calendar_File_Quit
= wxID_EXIT
,
177 Calendar_Cal_Monday
= 200,
178 Calendar_Cal_Holidays
,
179 Calendar_Cal_Special
,
182 Calendar_Cal_SeqMonth
,
183 Calendar_Cal_SurroundWeeks
,
184 Calendar_Cal_SetDate
,
186 #if wxUSE_DATEPICKCTRL
187 Calendar_DatePicker_AskDate
= 300,
188 Calendar_DatePicker_ShowCentury
,
189 Calendar_DatePicker_DropDown
,
190 Calendar_DatePicker_AllowNone
,
191 #if wxUSE_DATEPICKCTRL_GENERIC
192 Calendar_DatePicker_Generic
,
193 #endif // wxUSE_DATEPICKCTRL_GENERIC
194 #endif // wxUSE_DATEPICKCTRL
195 Calendar_CalCtrl
= 1000
198 // ----------------------------------------------------------------------------
199 // event tables and other macros for wxWidgets
200 // ----------------------------------------------------------------------------
202 // the event tables connect the wxWidgets events with the functions (event
203 // handlers) which process them. It can be also done at run-time, but for the
204 // simple menu events like this the static method is much simpler.
205 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
206 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
207 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
209 #if wxUSE_DATEPICKCTRL
210 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
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"),
266 wxPoint(50, 50), wxSize(450, 340));
270 // success: wxApp::OnRun() will be called which will enter the main message
271 // loop and the application will run. If we returned false here, the
272 // application would exit immediately.
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
281 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
282 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
284 // set the frame icon
285 SetIcon(wxICON(sample
));
288 wxMenu
*menuFile
= new wxMenu
;
289 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
290 menuFile
->AppendSeparator();
291 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
293 wxMenu
*menuCal
= new wxMenu
;
294 menuCal
->Append(Calendar_Cal_Monday
,
295 _T("Monday &first weekday\tCtrl-F"),
296 _T("Toggle between Mon and Sun as the first week day"),
298 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
299 _T("Toggle highlighting the holidays"),
301 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
302 _T("Test custom highlighting"),
304 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
305 _T("Show s&urrounding weeks\tCtrl-W"),
306 _T("Show the neighbouring weeks in the prev/next month"),
308 menuCal
->AppendSeparator();
309 menuCal
->Append(Calendar_Cal_SeqMonth
,
310 _T("To&ggle month selector style\tCtrl-G"),
311 _T("Use another style for the calendar controls"),
313 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
314 _T("Allow changing the month in the calendar"),
316 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
317 _T("Allow changing the year in the calendar"),
319 menuCal
->AppendSeparator();
320 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
321 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
323 #if wxUSE_DATEPICKCTRL
324 wxMenu
*menuDate
= new wxMenu
;
325 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
326 _T("Al&ways show century"));
327 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
328 _T("Use &drop down control"));
329 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
330 _T("Allow &no date"));
331 #if wxUSE_DATEPICKCTRL_GENERIC
332 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
333 _T("Use &generic version of the control"));
334 #endif // wxUSE_DATEPICKCTRL_GENERIC
335 menuDate
->AppendSeparator();
336 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
337 #endif // wxUSE_DATEPICKCTRL
339 // now append the freshly created menu to the menu bar...
340 wxMenuBar
*menuBar
= new wxMenuBar
;
341 menuBar
->Append(menuFile
, _T("&File"));
342 menuBar
->Append(menuCal
, _T("&Calendar"));
343 #if wxUSE_DATEPICKCTRL
344 menuBar
->Append(menuDate
, _T("&Date picker"));
345 #endif // wxUSE_DATEPICKCTRL
347 menuBar
->Check(Calendar_Cal_Monday
, true);
348 menuBar
->Check(Calendar_Cal_Holidays
, true);
349 menuBar
->Check(Calendar_Cal_Month
, true);
350 menuBar
->Check(Calendar_Cal_Year
, true);
352 #if wxUSE_DATEPICKCTRL
353 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
354 #endif // wxUSE_DATEPICKCTRL
356 // ... and attach this menu bar to the frame
359 m_panel
= new MyPanel(this);
362 // create a status bar just for fun (by default with 1 pane only)
364 SetStatusText(_T("Welcome to wxWidgets!"));
365 #endif // wxUSE_STATUSBAR
368 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
370 // true is to force the frame to close
374 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
376 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
377 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
380 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
382 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
384 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
387 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
389 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
391 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
394 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
396 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
399 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
401 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
403 m_panel
->GetCal()->EnableMonthChange(allow
);
406 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
408 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
410 m_panel
->GetCal()->EnableYearChange(allow
);
413 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
415 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
417 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
420 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
422 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
424 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
427 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
429 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
432 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
437 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
442 #if wxUSE_DATEPICKCTRL
444 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
446 int style
= wxDP_DEFAULT
;
447 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
448 style
|= wxDP_SHOWCENTURY
;
449 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
450 style
|= wxDP_DROPDOWN
;
451 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
452 style
|= wxDP_ALLOWNONE
;
454 MyDialog
dlg(this, m_panel
->GetCal()->GetDate(), style
);
455 if ( dlg
.ShowModal() == wxID_OK
)
457 const wxDateTime dt
= dlg
.GetDate();
460 const wxDateTime today
= wxDateTime::Today();
462 if ( dt
.GetDay() == today
.GetDay() &&
463 dt
.GetMonth() == today
.GetMonth() )
465 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
468 m_panel
->GetCal()->SetDate(dt
);
470 wxLogStatus(_T("Changed the date to your input"));
474 wxLogStatus(_T("No date entered"));
479 #endif // wxUSE_DATEPICKCTRL
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 MyPanel::MyPanel(wxFrame
*frame
)
486 : wxPanel(frame
, wxID_ANY
)
489 date
.Printf(wxT("Selected date: %s"),
490 wxDateTime::Today().FormatISODate().c_str());
491 m_date
= new wxStaticText(this, wxID_ANY
, date
);
492 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
497 wxCAL_SHOW_HOLIDAYS
|
500 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
502 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
503 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
506 m_sizer
->SetSizeHints( this );
509 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
511 wxLogMessage(wxT("Selected %s from calendar"),
512 event
.GetDate().FormatISODate().c_str());
515 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
518 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
523 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
525 wxLogStatus(wxT("Calendar month changed"));
528 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
530 wxLogStatus(wxT("Calendar year changed"));
533 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
535 wxLogMessage(wxT("Clicked on %s"),
536 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
539 void MyPanel::ToggleCalStyle(bool on
, int flag
)
541 long style
= m_calendar
->GetWindowStyle();
547 m_calendar
->SetWindowStyle(style
);
549 m_calendar
->Refresh();
552 void MyPanel::HighlightSpecial(bool on
)
557 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
558 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
559 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
561 m_calendar
->SetAttr(17, attrRedCircle
);
562 m_calendar
->SetAttr(29, attrGreenSquare
);
563 m_calendar
->SetAttr(13, attrHeaderLike
);
567 m_calendar
->ResetAttr(17);
568 m_calendar
->ResetAttr(29);
569 m_calendar
->ResetAttr(13);
572 m_calendar
->Refresh();
575 void MyPanel::SetDate()
577 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
578 m_calendar
->SetDate(date
);
581 void MyPanel::Today()
583 m_calendar
->SetDate(wxDateTime::Today());
586 // ----------------------------------------------------------------------------
588 // ----------------------------------------------------------------------------
590 #if wxUSE_DATEPICKCTRL
592 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
593 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
595 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
596 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
597 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
598 sizerBtns
->Finalise();
600 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
601 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
602 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
603 m_text
= new wxTextCtrl(this, wxID_ANY
);
604 sizerText
->Add(m_text
, wxSizerFlags().
605 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
607 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
608 sizerTop
->Add(new wxStaticText
611 _T("Enter your birthday date (not before 20th century):")
613 wxSizerFlags().Border());
615 #if wxUSE_DATEPICKCTRL_GENERIC
616 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
617 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
618 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
623 #endif // wxUSE_DATEPICKCTRL_GENERIC
624 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
625 wxDefaultPosition
, wxDefaultSize
,
627 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
629 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
631 sizerTop
->AddStretchSpacer(1);
632 sizerTop
->Add(sizerText
);
634 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
636 SetSizerAndFit(sizerTop
);
640 void MyDialog::OnDateChange(wxDateEvent
& event
)
642 const wxDateTime dt
= event
.GetDate();
644 m_text
->SetValue(dt
.FormatISODate());
646 m_text
->SetValue(wxEmptyString
);
649 #endif // wxUSE_DATEPICKCTRL