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"
48 #include "wx/datectrl.h"
50 #define wxUSE_DATEPICKERCTRL 1
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // Define a new application type, each program should derive a class from wxApp
57 class MyApp
: public wxApp
60 // override base class virtuals
61 // ----------------------------
63 // this one is called on application startup and is a good place for the app
64 // initialization (doing it here and not in the ctor allows to have an error
65 // return: if OnInit() returns false, the application terminates)
66 virtual bool OnInit();
69 class MyPanel
: public wxPanel
72 MyPanel(wxFrame
*frame
);
74 void OnCalendar(wxCalendarEvent
& event
);
75 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
76 void OnCalendarChange(wxCalendarEvent
& event
);
77 void OnCalMonthChange(wxCalendarEvent
& event
);
78 void OnCalYearChange(wxCalendarEvent
& event
);
80 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
82 // turn on/off the specified style bit on the calendar control
83 void ToggleCalStyle(bool on
, int style
);
85 void HighlightSpecial(bool on
);
91 wxCalendarCtrl
*m_calendar
;
97 // Define a new frame type: this is going to be our main frame
98 class MyFrame
: public wxFrame
102 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
104 // event handlers (these functions should _not_ be virtual)
105 void OnQuit(wxCommandEvent
& event
);
106 void OnAbout(wxCommandEvent
& event
);
108 #if wxUSE_DATEPICKERCTRL
109 void OnAskDate(wxCommandEvent
& event
);
110 #endif // wxUSE_DATEPICKERCTRL
112 void OnCalMonday(wxCommandEvent
& event
);
113 void OnCalHolidays(wxCommandEvent
& event
);
114 void OnCalSpecial(wxCommandEvent
& event
);
116 void OnCalAllowMonth(wxCommandEvent
& event
);
117 void OnCalAllowYear(wxCommandEvent
& event
);
119 void OnCalSeqMonth(wxCommandEvent
& event
);
120 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
122 void OnSetDate(wxCommandEvent
& event
);
123 void OnToday(wxCommandEvent
& event
);
125 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
130 // any class wishing to process wxWidgets events must use this macro
131 DECLARE_EVENT_TABLE()
134 #if wxUSE_DATEPICKERCTRL
136 // Define a simple modal dialog which asks the user for a date
137 class MyDialog
: public wxDialog
140 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
);
142 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
145 void OnDateChange(wxDateEvent
& event
);
148 wxDatePickerCtrl
*m_datePicker
;
152 DECLARE_EVENT_TABLE()
155 #endif // wxUSE_DATEPICKERCTRL
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // IDs for the controls and the menu commands
165 Calendar_File_About
= wxID_ABOUT
,
166 Calendar_File_Quit
= wxID_EXIT
,
167 #if wxUSE_DATEPICKERCTRL
168 Calendar_File_AskDate
= 100,
169 #endif // wxUSE_DATEPICKERCTRL
170 Calendar_Cal_Monday
= 200,
171 Calendar_Cal_Holidays
,
172 Calendar_Cal_Special
,
175 Calendar_Cal_SeqMonth
,
176 Calendar_Cal_SurroundWeeks
,
177 Calendar_Cal_SetDate
,
179 Calendar_CalCtrl
= 1000
182 // ----------------------------------------------------------------------------
183 // event tables and other macros for wxWidgets
184 // ----------------------------------------------------------------------------
186 // the event tables connect the wxWidgets events with the functions (event
187 // handlers) which process them. It can be also done at run-time, but for the
188 // simple menu events like this the static method is much simpler.
189 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
190 EVT_MENU(Calendar_File_Quit
, MyFrame
::OnQuit
)
191 EVT_MENU(Calendar_File_About
, MyFrame
::OnAbout
)
193 #if wxUSE_DATEPICKERCTRL
194 EVT_MENU(Calendar_File_AskDate
, MyFrame
::OnAskDate
)
195 #endif // wxUSE_DATEPICKERCTRL
197 EVT_MENU(Calendar_Cal_Monday
, MyFrame
::OnCalMonday
)
198 EVT_MENU(Calendar_Cal_Holidays
, MyFrame
::OnCalHolidays
)
199 EVT_MENU(Calendar_Cal_Special
, MyFrame
::OnCalSpecial
)
201 EVT_MENU(Calendar_Cal_Month
, MyFrame
::OnCalAllowMonth
)
202 EVT_MENU(Calendar_Cal_Year
, MyFrame
::OnCalAllowYear
)
204 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame
::OnCalSeqMonth
)
205 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame
::OnCalShowSurroundingWeeks
)
207 EVT_MENU(Calendar_Cal_SetDate
, MyFrame
::OnSetDate
)
208 EVT_MENU(Calendar_Cal_Today
, MyFrame
::OnToday
)
211 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame
::OnAllowYearUpdate
)
214 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
215 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel
::OnCalendar
)
216 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel
::OnCalMonthChange
)
217 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel
::OnCalYearChange
)
218 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel
::OnCalendarChange
)
219 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel
::OnCalendarWeekDayClick
)
222 #if wxUSE_DATEPICKERCTRL
224 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
225 EVT_DATE_CHANGED(wxID_ANY
, MyDialog
::OnDateChange
)
228 #endif // wxUSE_DATEPICKERCTRL
230 // Create a new application object: this macro will allow wxWidgets to create
231 // the application object during program execution (it's better than using a
232 // static object for many reasons) and also declares the accessor function
233 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
237 // ============================================================================
239 // ============================================================================
241 // ----------------------------------------------------------------------------
242 // the application class
243 // ----------------------------------------------------------------------------
245 // `Main program' equivalent: the program execution "starts" here
248 // Create the main application window
249 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
250 wxPoint(50, 50), wxSize(450, 340));
254 // success: wxApp::OnRun() will be called which will enter the main message
255 // loop and the application will run. If we returned false here, the
256 // application would exit immediately.
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
265 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
266 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
269 wxMenu
*menuFile
= new wxMenu
;
271 #if wxUSE_DATEPICKERCTRL
272 menuFile
->Append(Calendar_File_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
273 menuFile
->AppendSeparator();
274 #endif // wxUSE_DATEPICKERCTRL
276 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
277 menuFile
->AppendSeparator();
278 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
280 wxMenu
*menuCal
= new wxMenu
;
281 menuCal
->Append(Calendar_Cal_Monday
,
282 _T("Monday &first weekday\tCtrl-F"),
283 _T("Toggle between Mon and Sun as the first week day"),
285 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
286 _T("Toggle highlighting the holidays"),
288 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
289 _T("Test custom highlighting"),
291 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
292 _T("Show s&urrounding weeks\tCtrl-W"),
293 _T("Show the neighbouring weeks in the prev/next month"),
295 menuCal
->AppendSeparator();
296 menuCal
->Append(Calendar_Cal_SeqMonth
,
297 _T("To&ggle month selector style\tCtrl-G"),
298 _T("Use another style for the calendar controls"),
300 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
301 _T("Allow changing the month in the calendar"),
303 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
304 _T("Allow changing the year in the calendar"),
306 menuCal
->AppendSeparator();
307 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
308 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
310 // now append the freshly created menu to the menu bar...
311 wxMenuBar
*menuBar
= new wxMenuBar
;
312 menuBar
->Append(menuFile
, _T("&File"));
313 menuBar
->Append(menuCal
, _T("&Calendar"));
315 menuBar
->Check(Calendar_Cal_Monday
, true);
316 menuBar
->Check(Calendar_Cal_Holidays
, true);
317 menuBar
->Check(Calendar_Cal_Month
, true);
318 menuBar
->Check(Calendar_Cal_Year
, true);
320 // ... and attach this menu bar to the frame
323 m_panel
= new MyPanel(this);
326 // create a status bar just for fun (by default with 1 pane only)
328 SetStatusText(_T("Welcome to wxWidgets!"));
329 #endif // wxUSE_STATUSBAR
332 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
334 // true is to force the frame to close
338 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
340 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
341 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
344 void MyFrame
::OnCalMonday(wxCommandEvent
& event
)
346 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
348 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
351 void MyFrame
::OnCalHolidays(wxCommandEvent
& event
)
353 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
355 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
358 void MyFrame
::OnCalSpecial(wxCommandEvent
& event
)
360 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
363 void MyFrame
::OnCalAllowMonth(wxCommandEvent
& event
)
365 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
367 m_panel
->GetCal()->EnableMonthChange(allow
);
370 void MyFrame
::OnCalAllowYear(wxCommandEvent
& event
)
372 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
374 m_panel
->GetCal()->EnableYearChange(allow
);
377 void MyFrame
::OnCalSeqMonth(wxCommandEvent
& event
)
379 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
381 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
384 void MyFrame
::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
386 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
388 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
391 void MyFrame
::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
393 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
396 void MyFrame
::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
401 void MyFrame
::OnToday(wxCommandEvent
&WXUNUSED(event
))
406 #if wxUSE_DATEPICKERCTRL
408 void MyFrame
::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
410 MyDialog
dlg(this, m_panel
->GetCal()->GetDate());
411 if ( dlg
.ShowModal() == wxID_OK
)
413 const wxDateTime dt
= dlg
.GetDate(),
414 today
= wxDateTime
::Today();
416 if ( dt
.GetDay() == today
.GetDay() &&
417 dt
.GetMonth() == today
.GetMonth() )
419 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
422 m_panel
->GetCal()->SetDate(dt
);
424 wxLogStatus(_T("Changed the date to your birthday"));
428 #endif // wxUSE_DATEPICKERCTRL
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 MyPanel
::MyPanel(wxFrame
*frame
)
435 : wxPanel(frame
, wxID_ANY
)
438 date
.Printf(wxT("Selected date: %s"),
439 wxDateTime
::Today().FormatISODate().c_str());
440 m_date
= new wxStaticText(this, wxID_ANY
, date
);
441 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
446 wxCAL_SHOW_HOLIDAYS
|
449 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
451 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
452 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
455 m_sizer
->SetSizeHints( this );
458 void MyPanel
::OnCalendar(wxCalendarEvent
& event
)
460 wxLogMessage(wxT("Selected %s from calendar"),
461 event
.GetDate().FormatISODate().c_str());
464 void MyPanel
::OnCalendarChange(wxCalendarEvent
& event
)
467 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
472 void MyPanel
::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
474 wxLogStatus(wxT("Calendar month changed"));
477 void MyPanel
::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
479 wxLogStatus(wxT("Calendar year changed"));
482 void MyPanel
::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
484 wxLogMessage(wxT("Clicked on %s"),
485 wxDateTime
::GetWeekDayName(event
.GetWeekDay()).c_str());
488 void MyPanel
::ToggleCalStyle(bool on
, int flag
)
490 long style
= m_calendar
->GetWindowStyle();
496 m_calendar
->SetWindowStyle(style
);
498 m_calendar
->Refresh();
501 void MyPanel
::HighlightSpecial(bool on
)
506 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
507 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
508 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
510 m_calendar
->SetAttr(17, attrRedCircle
);
511 m_calendar
->SetAttr(29, attrGreenSquare
);
512 m_calendar
->SetAttr(13, attrHeaderLike
);
516 m_calendar
->ResetAttr(17);
517 m_calendar
->ResetAttr(29);
518 m_calendar
->ResetAttr(13);
521 m_calendar
->Refresh();
524 void MyPanel
::SetDate()
526 wxDateTime
date(24, wxDateTime
::Dec
, 2005, 23, 59, 59);
527 m_calendar
->SetDate(date
);
530 void MyPanel
::Today()
532 m_calendar
->SetDate(wxDateTime
::Today());
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 #if wxUSE_DATEPICKERCTRL
541 MyDialog
::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
)
542 : wxDialog(parent
, -1, wxString(_T("Calendar: Choose a date")))
544 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
545 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
546 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
547 sizerBtns
->Finalise();
549 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
550 sizerText
->Add(new wxStaticText(this, -1, _T("Date in ISO format: ")),
551 wxSizerFlags().Border());
552 m_text
= new wxTextCtrl(this, -1);
553 sizerText
->Add(m_text
, wxSizerFlags().Expand().Border());
555 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
556 sizerTop
->Add(new wxStaticText
559 _T("Enter your birthday date (not before 20th century):")
561 wxSizerFlags().Border());
563 m_datePicker
= new wxDatePickerCtrl(this, -1, dt
);
564 m_datePicker
->SetRange(wxDateTime(1, wxDateTime
::Jan
, 1900),
566 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
568 sizerTop
->AddStretchSpacer(1);
569 sizerTop
->Add(sizerText
);
571 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
573 SetSizerAndFit(sizerTop
);
577 void MyDialog
::OnDateChange(wxDateEvent
& event
)
579 m_text
->SetValue(event
.GetDate().FormatISODate());
582 #endif // wxUSE_DATEPICKERCTRL