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 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // Define a new application type, each program should derive a class from wxApp
61 class MyApp
: public wxApp
64 // override base class virtuals
65 // ----------------------------
67 // this one is called on application startup and is a good place for the app
68 // initialization (doing it here and not in the ctor allows to have an error
69 // return: if OnInit() returns false, the application terminates)
70 virtual bool OnInit();
73 class MyPanel
: public wxPanel
76 MyPanel(wxFrame
*frame
);
78 void OnCalendar(wxCalendarEvent
& event
);
79 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
80 void OnCalendarChange(wxCalendarEvent
& event
);
81 void OnCalMonthChange(wxCalendarEvent
& event
);
82 void OnCalYearChange(wxCalendarEvent
& event
);
84 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
86 // turn on/off the specified style bit on the calendar control
87 void ToggleCalStyle(bool on
, int style
);
89 void HighlightSpecial(bool on
);
95 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
, const wxSize
& size
);
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
);
114 #endif // wxUSE_DATEPICKCTRL
116 void OnCalMonday(wxCommandEvent
& event
);
117 void OnCalHolidays(wxCommandEvent
& event
);
118 void OnCalSpecial(wxCommandEvent
& event
);
120 void OnCalAllowMonth(wxCommandEvent
& event
);
121 void OnCalAllowYear(wxCommandEvent
& event
);
123 void OnCalSeqMonth(wxCommandEvent
& event
);
124 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
126 void OnSetDate(wxCommandEvent
& event
);
127 void OnToday(wxCommandEvent
& event
);
129 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
134 // any class wishing to process wxWidgets events must use this macro
135 DECLARE_EVENT_TABLE()
138 #if wxUSE_DATEPICKCTRL
140 // Define a simple modal dialog which asks the user for a date
141 class MyDialog
: public wxDialog
144 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
146 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
149 void OnDateChange(wxDateEvent
& event
);
152 wxDatePickerCtrlBase
*m_datePicker
;
156 DECLARE_EVENT_TABLE()
159 #endif // wxUSE_DATEPICKCTRL
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 // IDs for the controls and the menu commands
169 Calendar_File_About
= wxID_ABOUT
,
170 Calendar_File_Quit
= wxID_EXIT
,
171 Calendar_Cal_Monday
= 200,
172 Calendar_Cal_Holidays
,
173 Calendar_Cal_Special
,
176 Calendar_Cal_SeqMonth
,
177 Calendar_Cal_SurroundWeeks
,
178 Calendar_Cal_SetDate
,
180 #if wxUSE_DATEPICKCTRL
181 Calendar_DatePicker_AskDate
= 300,
182 Calendar_DatePicker_ShowCentury
,
183 Calendar_DatePicker_DropDown
,
184 #if wxUSE_DATEPICKCTRL_GENERIC
185 Calendar_DatePicker_Generic
,
186 #endif // wxUSE_DATEPICKCTRL_GENERIC
187 #endif // wxUSE_DATEPICKCTRL
188 Calendar_CalCtrl
= 1000
191 // ----------------------------------------------------------------------------
192 // event tables and other macros for wxWidgets
193 // ----------------------------------------------------------------------------
195 // the event tables connect the wxWidgets events with the functions (event
196 // handlers) which process them. It can be also done at run-time, but for the
197 // simple menu events like this the static method is much simpler.
198 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
199 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
200 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
202 #if wxUSE_DATEPICKCTRL
203 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
204 #endif // wxUSE_DATEPICKCTRL
206 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
207 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
208 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
210 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
211 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
213 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
214 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
216 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
217 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
220 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
223 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
224 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
225 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
226 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
227 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
228 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
231 #if wxUSE_DATEPICKCTRL
233 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
234 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
237 #endif // wxUSE_DATEPICKCTRL
239 // Create a new application object: this macro will allow wxWidgets to create
240 // the application object during program execution (it's better than using a
241 // static object for many reasons) and also declares the accessor function
242 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
246 // ============================================================================
248 // ============================================================================
250 // ----------------------------------------------------------------------------
251 // the application class
252 // ----------------------------------------------------------------------------
254 // `Main program' equivalent: the program execution "starts" here
257 // Create the main application window
258 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
259 wxPoint(50, 50), wxSize(450, 340));
263 // success: wxApp::OnRun() will be called which will enter the main message
264 // loop and the application will run. If we returned false here, the
265 // application would exit immediately.
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
274 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
275 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
278 wxMenu
*menuFile
= new wxMenu
;
279 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
280 menuFile
->AppendSeparator();
281 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
283 wxMenu
*menuCal
= new wxMenu
;
284 menuCal
->Append(Calendar_Cal_Monday
,
285 _T("Monday &first weekday\tCtrl-F"),
286 _T("Toggle between Mon and Sun as the first week day"),
288 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
289 _T("Toggle highlighting the holidays"),
291 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
292 _T("Test custom highlighting"),
294 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
295 _T("Show s&urrounding weeks\tCtrl-W"),
296 _T("Show the neighbouring weeks in the prev/next month"),
298 menuCal
->AppendSeparator();
299 menuCal
->Append(Calendar_Cal_SeqMonth
,
300 _T("To&ggle month selector style\tCtrl-G"),
301 _T("Use another style for the calendar controls"),
303 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
304 _T("Allow changing the month in the calendar"),
306 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
307 _T("Allow changing the year in the calendar"),
309 menuCal
->AppendSeparator();
310 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
311 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
313 #if wxUSE_DATEPICKCTRL
314 wxMenu
*menuDate
= new wxMenu
;
315 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
316 _T("Al&ways show century"));
317 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
318 _T("Use &drop down control"));
319 #if wxUSE_DATEPICKCTRL_GENERIC
320 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
321 _T("Use &generic version of the control"));
322 #endif // wxUSE_DATEPICKCTRL_GENERIC
323 menuDate
->AppendSeparator();
324 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
325 #endif // wxUSE_DATEPICKCTRL
327 // now append the freshly created menu to the menu bar...
328 wxMenuBar
*menuBar
= new wxMenuBar
;
329 menuBar
->Append(menuFile
, _T("&File"));
330 menuBar
->Append(menuCal
, _T("&Calendar"));
331 #if wxUSE_DATEPICKCTRL
332 menuBar
->Append(menuDate
, _T("&Date picker"));
333 #endif // wxUSE_DATEPICKCTRL
335 menuBar
->Check(Calendar_Cal_Monday
, true);
336 menuBar
->Check(Calendar_Cal_Holidays
, true);
337 menuBar
->Check(Calendar_Cal_Month
, true);
338 menuBar
->Check(Calendar_Cal_Year
, true);
340 #if wxUSE_DATEPICKCTRL
341 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
342 #endif // wxUSE_DATEPICKCTRL
344 // ... and attach this menu bar to the frame
347 m_panel
= new MyPanel(this);
350 // create a status bar just for fun (by default with 1 pane only)
352 SetStatusText(_T("Welcome to wxWidgets!"));
353 #endif // wxUSE_STATUSBAR
356 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
358 // true is to force the frame to close
362 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
364 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000 Vadim Zeitlin"),
365 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
368 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
370 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
372 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
375 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
377 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
379 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
382 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
384 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
387 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
389 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
391 m_panel
->GetCal()->EnableMonthChange(allow
);
394 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
396 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
398 m_panel
->GetCal()->EnableYearChange(allow
);
401 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
403 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
405 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
408 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
410 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
412 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
415 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
417 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
420 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
425 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
430 #if wxUSE_DATEPICKCTRL
432 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
434 int style
= wxDP_DEFAULT
;
435 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
436 style
|= wxDP_SHOWCENTURY
;
437 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
438 style
|= wxDP_DROPDOWN
;
440 MyDialog
dlg(this, m_panel
->GetCal()->GetDate(), style
);
441 if ( dlg
.ShowModal() == wxID_OK
)
443 const wxDateTime dt
= dlg
.GetDate(),
444 today
= wxDateTime::Today();
446 if ( dt
.GetDay() == today
.GetDay() &&
447 dt
.GetMonth() == today
.GetMonth() )
449 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
452 m_panel
->GetCal()->SetDate(dt
);
454 wxLogStatus(_T("Changed the date to your birthday"));
458 #endif // wxUSE_DATEPICKCTRL
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 MyPanel::MyPanel(wxFrame
*frame
)
465 : wxPanel(frame
, wxID_ANY
)
468 date
.Printf(wxT("Selected date: %s"),
469 wxDateTime::Today().FormatISODate().c_str());
470 m_date
= new wxStaticText(this, wxID_ANY
, date
);
471 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
476 wxCAL_SHOW_HOLIDAYS
|
479 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
481 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
482 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
485 m_sizer
->SetSizeHints( this );
488 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
490 wxLogMessage(wxT("Selected %s from calendar"),
491 event
.GetDate().FormatISODate().c_str());
494 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
497 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
502 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
504 wxLogStatus(wxT("Calendar month changed"));
507 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
509 wxLogStatus(wxT("Calendar year changed"));
512 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
514 wxLogMessage(wxT("Clicked on %s"),
515 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
518 void MyPanel::ToggleCalStyle(bool on
, int flag
)
520 long style
= m_calendar
->GetWindowStyle();
526 m_calendar
->SetWindowStyle(style
);
528 m_calendar
->Refresh();
531 void MyPanel::HighlightSpecial(bool on
)
536 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
537 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
538 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
540 m_calendar
->SetAttr(17, attrRedCircle
);
541 m_calendar
->SetAttr(29, attrGreenSquare
);
542 m_calendar
->SetAttr(13, attrHeaderLike
);
546 m_calendar
->ResetAttr(17);
547 m_calendar
->ResetAttr(29);
548 m_calendar
->ResetAttr(13);
551 m_calendar
->Refresh();
554 void MyPanel::SetDate()
556 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
557 m_calendar
->SetDate(date
);
560 void MyPanel::Today()
562 m_calendar
->SetDate(wxDateTime::Today());
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 #if wxUSE_DATEPICKCTRL
571 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
572 : wxDialog(parent
, -1, wxString(_T("Calendar: Choose a date")))
574 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
575 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
576 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
577 sizerBtns
->Finalise();
579 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
580 sizerText
->Add(new wxStaticText(this, -1, _T("Date in ISO format: ")),
581 wxSizerFlags().Border());
582 m_text
= new wxTextCtrl(this, -1);
583 sizerText
->Add(m_text
, wxSizerFlags().Expand().Border());
585 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
586 sizerTop
->Add(new wxStaticText
589 _T("Enter your birthday date (not before 20th century):")
591 wxSizerFlags().Border());
593 #if wxUSE_DATEPICKCTRL_GENERIC
594 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
595 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
596 m_datePicker
= new wxDatePickerCtrlGeneric(this, -1, dt
,
601 #endif // wxUSE_DATEPICKCTRL_GENERIC
602 m_datePicker
= new wxDatePickerCtrl(this, -1, dt
,
603 wxDefaultPosition
, wxDefaultSize
,
605 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
607 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
609 sizerTop
->AddStretchSpacer(1);
610 sizerTop
->Add(sizerText
);
612 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
614 SetSizerAndFit(sizerTop
);
618 void MyDialog::OnDateChange(wxDateEvent
& event
)
620 m_text
->SetValue(event
.GetDate().FormatISODate());
623 #endif // wxUSE_DATEPICKCTRL