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"
45 #include "wx/splitter.h"
47 #if wxUSE_DATEPICKCTRL
48 #include "wx/datectrl.h"
49 #if wxUSE_DATEPICKCTRL_GENERIC
50 #include "wx/generic/datectrl.h"
51 #endif // wxUSE_DATEPICKCTRL_GENERIC
52 #endif // wxUSE_DATEPICKCTRL
54 #include "../sample.xpm"
56 #ifdef wxHAS_NATIVE_CALENDARCTRL
57 #include "wx/generic/calctrlg.h"
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp
: public wxApp
68 // override base class virtuals
69 // ----------------------------
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
77 class MyPanel
: public wxPanel
80 MyPanel(wxWindow
*parent
);
82 void OnCalendar(wxCalendarEvent
& event
);
83 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
84 void OnCalendarWeekClick(wxCalendarEvent
& event
);
85 void OnCalendarChange(wxCalendarEvent
& event
);
86 void OnCalMonthChange(wxCalendarEvent
& event
);
88 wxCalendarCtrlBase
*GetCal() const { return m_calendar
; }
90 // turn on/off the specified style bit on the calendar control
91 void ToggleCalStyle(bool on
, int style
);
93 bool IsUsingGeneric() const { return m_usingGeneric
; }
94 void ToggleUseGeneric()
96 m_usingGeneric
= !m_usingGeneric
;
97 RecreateCalendar(m_calendar
->GetWindowStyle());
100 void HighlightSpecial(bool on
);
102 wxDateTime
GetDate() const { return m_calendar
->GetDate(); }
103 void SetDate(const wxDateTime
& dt
) { m_calendar
->SetDate(dt
); }
106 wxCalendarCtrlBase
*DoCreateCalendar(const wxDateTime
& dt
, long style
);
108 void RecreateCalendar(long style
);
110 wxCalendarCtrlBase
*m_calendar
;
111 wxStaticText
*m_date
;
117 DECLARE_EVENT_TABLE()
120 // Define a new frame type: this is going to be our main frame
121 class MyFrame
: public wxFrame
125 MyFrame(const wxString
& title
, const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
);
127 // event handlers (these functions should _not_ be virtual)
128 void OnAbout(wxCommandEvent
& event
);
129 void OnClearLog(wxCommandEvent
& event
);
130 void OnQuit(wxCommandEvent
& event
);
132 #if wxUSE_DATEPICKCTRL
133 void OnAskDate(wxCommandEvent
& event
);
135 void OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
);
136 #endif // wxUSE_DATEPICKCTRL
138 #ifdef wxHAS_NATIVE_CALENDARCTRL
139 void OnCalGeneric(wxCommandEvent
& WXUNUSED(event
))
141 m_panel
->ToggleUseGeneric();
143 #endif // wxHAS_NATIVE_CALENDARCTRL
145 void OnCalMonday(wxCommandEvent
& event
);
146 void OnCalHolidays(wxCommandEvent
& event
);
147 void OnCalSpecial(wxCommandEvent
& event
);
149 void OnCalAllowMonth(wxCommandEvent
& event
);
151 void OnCalSeqMonth(wxCommandEvent
& event
);
152 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
153 void OnCalShowWeekNumbers(wxCommandEvent
& event
);
155 void OnSetDate(wxCommandEvent
& event
);
156 void OnToday(wxCommandEvent
& event
);
157 void OnBeginDST(wxCommandEvent
& event
);
159 void OnCalToggleResizable(wxCommandEvent
& event
);
161 void OnUpdateUIGenericOnly(wxUpdateUIEvent
& event
)
163 event
.Enable(m_panel
->IsUsingGeneric());
166 void OnCalRClick(wxMouseEvent
& event
);
170 wxTextCtrl
*m_logWindow
;
172 // any class wishing to process wxWidgets events must use this macro
173 DECLARE_EVENT_TABLE()
176 #if wxUSE_DATEPICKCTRL
178 // Define a simple modal dialog which asks the user for a date
179 class MyDialog
: public wxDialog
182 MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
);
184 wxDateTime
GetDate() const { return m_datePicker
->GetValue(); }
187 void OnDateChange(wxDateEvent
& event
);
190 wxDatePickerCtrlBase
*m_datePicker
;
194 DECLARE_EVENT_TABLE()
197 #endif // wxUSE_DATEPICKCTRL
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 // IDs for the controls and the menu commands
207 Calendar_File_About
= wxID_ABOUT
,
208 Calendar_File_ClearLog
= wxID_CLEAR
,
209 Calendar_File_Quit
= wxID_EXIT
,
210 Calendar_Cal_Generic
= 200,
212 Calendar_Cal_Holidays
,
213 Calendar_Cal_Special
,
215 Calendar_Cal_SeqMonth
,
216 Calendar_Cal_SurroundWeeks
,
217 Calendar_Cal_WeekNumbers
,
218 Calendar_Cal_SetDate
,
220 Calendar_Cal_BeginDST
,
221 Calendar_Cal_Resizable
,
222 #if wxUSE_DATEPICKCTRL
223 Calendar_DatePicker_AskDate
= 300,
224 Calendar_DatePicker_ShowCentury
,
225 Calendar_DatePicker_DropDown
,
226 Calendar_DatePicker_AllowNone
,
227 Calendar_DatePicker_StartWithNone
,
228 #if wxUSE_DATEPICKCTRL_GENERIC
229 Calendar_DatePicker_Generic
,
230 #endif // wxUSE_DATEPICKCTRL_GENERIC
231 #endif // wxUSE_DATEPICKCTRL
232 Calendar_CalCtrl
= 1000
235 // ----------------------------------------------------------------------------
236 // event tables and other macros for wxWidgets
237 // ----------------------------------------------------------------------------
239 // the event tables connect the wxWidgets events with the functions (event
240 // handlers) which process them. It can be also done at run-time, but for the
241 // simple menu events like this the static method is much simpler.
242 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
243 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
244 EVT_MENU(Calendar_File_ClearLog
, MyFrame::OnClearLog
)
245 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
247 #if wxUSE_DATEPICKCTRL
248 EVT_MENU(Calendar_DatePicker_AskDate
, MyFrame::OnAskDate
)
250 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone
,
251 MyFrame::OnUpdateUIStartWithNone
)
252 #endif // wxUSE_DATEPICKCTRL
254 #ifdef wxHAS_NATIVE_CALENDARCTRL
255 EVT_MENU(Calendar_Cal_Generic
, MyFrame::OnCalGeneric
)
256 #endif // wxHAS_NATIVE_CALENDARCTRL
258 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
259 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
260 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
262 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
264 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
265 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
266 EVT_MENU(Calendar_Cal_WeekNumbers
, MyFrame::OnCalShowWeekNumbers
)
268 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
269 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
270 EVT_MENU(Calendar_Cal_BeginDST
, MyFrame::OnBeginDST
)
272 EVT_MENU(Calendar_Cal_Resizable
, MyFrame::OnCalToggleResizable
)
275 EVT_UPDATE_UI(Calendar_Cal_SeqMonth
, MyFrame::OnUpdateUIGenericOnly
)
277 EVT_UPDATE_UI(Calendar_Cal_Monday
, MyFrame::OnUpdateUIGenericOnly
)
278 EVT_UPDATE_UI(Calendar_Cal_Holidays
, MyFrame::OnUpdateUIGenericOnly
)
280 EVT_UPDATE_UI(Calendar_Cal_Special
, MyFrame::OnUpdateUIGenericOnly
)
281 EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks
, MyFrame::OnUpdateUIGenericOnly
)
284 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
285 EVT_CALENDAR(Calendar_CalCtrl
, MyPanel::OnCalendar
)
286 EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
287 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
288 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
289 EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekClick
)
292 #if wxUSE_DATEPICKCTRL
294 BEGIN_EVENT_TABLE(MyDialog
, wxDialog
)
295 EVT_DATE_CHANGED(wxID_ANY
, MyDialog::OnDateChange
)
298 #endif // wxUSE_DATEPICKCTRL
300 // Create a new application object: this macro will allow wxWidgets to create
301 // the application object during program execution (it's better than using a
302 // static object for many reasons) and also declares the accessor function
303 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
307 // ============================================================================
309 // ============================================================================
311 // ----------------------------------------------------------------------------
312 // the application class
313 // ----------------------------------------------------------------------------
315 // `Main program' equivalent: the program execution "starts" here
318 if ( !wxApp::OnInit() )
321 // Create the main application window
322 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample")
324 ,wxPoint(50, 50), wxSize(450, 340)
330 // success: wxApp::OnRun() will be called which will enter the main message
331 // loop and the application will run. If we returned false here, the
332 // application would exit immediately.
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
341 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
342 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
344 // set the frame icon
345 SetIcon(wxIcon(sample_xpm
));
348 wxMenu
*menuFile
= new wxMenu
;
349 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
350 menuFile
->AppendSeparator();
351 menuFile
->Append(Calendar_File_ClearLog
, _T("&Clear log\tCtrl-L"));
352 menuFile
->AppendSeparator();
353 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
355 wxMenu
*menuCal
= new wxMenu
;
356 #ifdef wxHAS_NATIVE_CALENDARCTRL
357 menuCal
->AppendCheckItem(Calendar_Cal_Generic
, "Use &generic version\tCtrl-G",
358 "Toggle between native and generic control");
359 menuCal
->AppendSeparator();
360 #endif // wxHAS_NATIVE_CALENDARCTRL
361 menuCal
->Append(Calendar_Cal_Monday
,
362 _T("Monday &first weekday\tCtrl-F"),
363 _T("Toggle between Mon and Sun as the first week day"),
365 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
366 _T("Toggle highlighting the holidays"),
368 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
369 _T("Test custom highlighting"),
371 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
372 _T("Show s&urrounding weeks\tCtrl-W"),
373 _T("Show the neighbouring weeks in the prev/next month"),
375 menuCal
->Append(Calendar_Cal_WeekNumbers
,
376 _T("Show &week numbers"),
377 _T("Toggle week numbers"),
379 menuCal
->AppendSeparator();
380 menuCal
->Append(Calendar_Cal_SeqMonth
,
381 _T("Toggle month selector st&yle\tCtrl-Y"),
382 _T("Use another style for the calendar controls"),
384 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
385 _T("Allow changing the month in the calendar"),
387 menuCal
->AppendSeparator();
388 menuCal
->Append(Calendar_Cal_SetDate
, _T("Call &SetDate(2005-12-24)"), _T("Set date to 2005-12-24."));
389 menuCal
->Append(Calendar_Cal_Today
, _T("Call &Today()"), _T("Set to the current date."));
390 menuCal
->Append(Calendar_Cal_BeginDST
, "Call SetDate(GetBeginDST())");
391 menuCal
->AppendSeparator();
392 menuCal
->AppendCheckItem(Calendar_Cal_Resizable
, _T("Make &resizable\tCtrl-R"));
394 #if wxUSE_DATEPICKCTRL
395 wxMenu
*menuDate
= new wxMenu
;
396 menuDate
->AppendCheckItem(Calendar_DatePicker_ShowCentury
,
397 _T("Al&ways show century"));
398 menuDate
->AppendCheckItem(Calendar_DatePicker_DropDown
,
399 _T("Use &drop down control"));
400 menuDate
->AppendCheckItem(Calendar_DatePicker_AllowNone
,
401 _T("Allow &no date"));
402 menuDate
->AppendCheckItem(Calendar_DatePicker_StartWithNone
,
403 _T("Start &with no date"));
404 #if wxUSE_DATEPICKCTRL_GENERIC
405 menuDate
->AppendCheckItem(Calendar_DatePicker_Generic
,
406 _T("Use &generic version of the control"));
407 #endif // wxUSE_DATEPICKCTRL_GENERIC
408 menuDate
->AppendSeparator();
409 menuDate
->Append(Calendar_DatePicker_AskDate
, _T("&Choose date...\tCtrl-D"), _T("Show dialog with wxDatePickerCtrl"));
410 #endif // wxUSE_DATEPICKCTRL
412 // now append the freshly created menu to the menu bar...
413 wxMenuBar
*menuBar
= new wxMenuBar
;
414 menuBar
->Append(menuFile
, _T("&File"));
415 menuBar
->Append(menuCal
, _T("&Calendar"));
416 #if wxUSE_DATEPICKCTRL
417 menuBar
->Append(menuDate
, _T("&Date picker"));
418 #endif // wxUSE_DATEPICKCTRL
420 menuBar
->Check(Calendar_Cal_Monday
, true);
421 menuBar
->Check(Calendar_Cal_Holidays
, true);
422 menuBar
->Check(Calendar_Cal_Month
, true);
424 #if wxUSE_DATEPICKCTRL
425 menuBar
->Check(Calendar_DatePicker_ShowCentury
, true);
426 #endif // wxUSE_DATEPICKCTRL
428 // ... and attach this menu bar to the frame
431 wxSplitterWindow
*splitter
= new wxSplitterWindow(this, wxID_ANY
,
432 wxDefaultPosition
, wxDefaultSize
,
434 m_panel
= new MyPanel(splitter
);
435 m_logWindow
= new wxTextCtrl(splitter
, wxID_ANY
, wxEmptyString
,
436 wxDefaultPosition
, wxDefaultSize
,
437 wxTE_READONLY
| wxTE_MULTILINE
);
438 splitter
->SplitHorizontally(m_panel
, m_logWindow
);
439 splitter
->SetMinimumPaneSize(20);
440 wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
443 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
445 // true is to force the frame to close
449 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
451 wxMessageBox(_T("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
452 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
455 void MyFrame::OnClearLog(wxCommandEvent
& WXUNUSED(event
))
457 m_logWindow
->Clear();
460 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
462 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_MONDAY_FIRST
);
465 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
467 m_panel
->GetCal()->EnableHolidayDisplay(event
.IsChecked());
470 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
472 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
475 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
477 m_panel
->GetCal()->EnableMonthChange(event
.IsChecked());
480 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
482 m_panel
->ToggleCalStyle(event
.IsChecked(),
483 wxCAL_SEQUENTIAL_MONTH_SELECTION
);
486 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
488 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS
);
491 void MyFrame::OnCalShowWeekNumbers(wxCommandEvent
& event
)
493 m_panel
->ToggleCalStyle(event
.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS
);
496 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
498 m_panel
->SetDate(wxDateTime(24, wxDateTime::Dec
, 2005, 22, 00, 00));
501 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
503 m_panel
->SetDate(wxDateTime::Today());
506 void MyFrame::OnBeginDST(wxCommandEvent
&WXUNUSED(event
))
508 m_panel
->SetDate(wxDateTime::GetBeginDST(m_panel
->GetDate().GetYear()));
511 void MyFrame::OnCalToggleResizable(wxCommandEvent
& event
)
513 wxSizer
* const sizer
= m_panel
->GetSizer();
514 wxSizerItem
* const item
= sizer
->GetItem(m_panel
->GetCal());
515 if ( event
.IsChecked() )
517 item
->SetProportion(1);
518 item
->SetFlag(wxEXPAND
);
520 else // not resizable
522 item
->SetProportion(0);
523 item
->SetFlag(wxALIGN_CENTER
);
529 void MyFrame::OnCalRClick(wxMouseEvent
& event
)
532 wxDateTime::WeekDay wd
;
534 const wxPoint pt
= event
.GetPosition();
535 wxString msg
= wxString::Format("Point (%d, %d) is ", pt
.x
, pt
.y
);
537 switch ( m_panel
->GetCal()->HitTest(pt
, &dt
, &wd
) )
540 wxFAIL_MSG( "unexpected" );
543 case wxCAL_HITTEST_NOWHERE
:
547 case wxCAL_HITTEST_HEADER
:
548 msg
+= wxString::Format("over %s", wxDateTime::GetWeekDayName(wd
));
551 case wxCAL_HITTEST_DAY
:
552 msg
+= wxString::Format("over %s", dt
.FormatISODate());
555 case wxCAL_HITTEST_INCMONTH
:
556 msg
+= "over next month button";
559 case wxCAL_HITTEST_DECMONTH
:
560 msg
+= "over previous month button";
563 case wxCAL_HITTEST_SURROUNDING_WEEK
:
564 msg
+= "over a day from another month";
568 wxLogMessage("%s", msg
);
571 #if wxUSE_DATEPICKCTRL
573 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent
& event
)
575 // it only makes sense to start with invalid date if we can have no date
576 event
.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) );
579 void MyFrame::OnAskDate(wxCommandEvent
& WXUNUSED(event
))
581 wxDateTime dt
= m_panel
->GetCal()->GetDate();
583 int style
= wxDP_DEFAULT
;
584 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury
) )
585 style
|= wxDP_SHOWCENTURY
;
586 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown
) )
587 style
|= wxDP_DROPDOWN
;
588 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone
) )
590 style
|= wxDP_ALLOWNONE
;
592 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone
) )
593 dt
= wxDefaultDateTime
;
596 MyDialog
dlg(this, dt
, style
);
597 if ( dlg
.ShowModal() == wxID_OK
)
602 const wxDateTime today
= wxDateTime::Today();
604 if ( dt
.GetDay() == today
.GetDay() &&
605 dt
.GetMonth() == today
.GetMonth() )
607 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
610 m_panel
->SetDate(dt
);
612 wxLogStatus(_T("Changed the date to your input"));
616 wxLogStatus(_T("No date entered"));
621 #endif // wxUSE_DATEPICKCTRL
623 // ----------------------------------------------------------------------------
625 // ----------------------------------------------------------------------------
627 MyPanel::MyPanel(wxWindow
*parent
)
628 : wxPanel(parent
, wxID_ANY
)
630 #ifdef wxHAS_NATIVE_CALENDARCTRL
631 m_usingGeneric
= false;
633 m_usingGeneric
= true;
637 date
.Printf(wxT("Selected date: %s"),
638 wxDateTime::Today().FormatISODate().c_str());
639 m_date
= new wxStaticText(this, wxID_ANY
, date
);
640 m_calendar
= DoCreateCalendar(wxDefaultDateTime
,
641 wxCAL_MONDAY_FIRST
| wxCAL_SHOW_HOLIDAYS
);
643 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
644 bool horizontal
= ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) );
645 m_sizer
= new wxBoxSizer( horizontal
? wxHORIZONTAL
: wxVERTICAL
);
647 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
648 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
651 m_sizer
->SetSizeHints( this );
654 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
656 // clicking the same date twice unmarks it (convenient for testing)
657 static wxDateTime s_dateLast
;
658 const bool mark
= !s_dateLast
.IsValid() || event
.GetDate() != s_dateLast
;
660 s_dateLast
= event
.GetDate();
662 m_calendar
->Mark(event
.GetDate().GetDay(), mark
);
663 wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
664 mark
? "" : "un", s_dateLast
.FormatISODate().c_str());
667 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
670 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
676 void MyPanel::OnCalMonthChange(wxCalendarEvent
& event
)
678 wxLogStatus(wxT("Calendar month changed to %s %d"),
679 wxDateTime::GetMonthName(event
.GetDate().GetMonth()),
680 event
.GetDate().GetYear());
683 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
685 wxLogMessage(wxT("Clicked on %s"),
686 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
689 void MyPanel::OnCalendarWeekClick(wxCalendarEvent
& event
)
691 wxLogMessage(wxT("Clicked on week %d"), event
.GetDate().GetWeekOfYear());
694 wxCalendarCtrlBase
*MyPanel::DoCreateCalendar(const wxDateTime
& dt
, long style
)
696 wxCalendarCtrlBase
*calendar
;
697 #ifdef wxHAS_NATIVE_CALENDARCTRL
698 if ( m_usingGeneric
)
699 calendar
= new wxGenericCalendarCtrl(this, Calendar_CalCtrl
,
705 #endif // wxHAS_NATIVE_CALENDARCTRL
706 calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
712 calendar
->Connect(wxEVT_RIGHT_DOWN
,
713 wxMouseEventHandler(MyFrame::OnCalRClick
),
715 ( MyFrame
* )wxGetTopLevelParent(this));
720 void MyPanel::RecreateCalendar(long style
)
722 wxCalendarCtrlBase
*calendar
= DoCreateCalendar(m_calendar
->GetDate(), style
);
724 m_sizer
->Replace(m_calendar
, calendar
);
726 m_calendar
= calendar
;
731 void MyPanel::ToggleCalStyle(bool on
, int flag
)
733 long style
= m_calendar
->GetWindowStyle();
739 if ( flag
== wxCAL_SEQUENTIAL_MONTH_SELECTION
740 || flag
== wxCAL_SHOW_WEEK_NUMBERS
)
742 // changing this style requires recreating the control
743 RecreateCalendar(style
);
745 else // just changing the style is enough
747 m_calendar
->SetWindowStyle(style
);
749 m_calendar
->Refresh();
753 void MyPanel::HighlightSpecial(bool on
)
758 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
759 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
760 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
762 m_calendar
->SetAttr(17, attrRedCircle
);
763 m_calendar
->SetAttr(29, attrGreenSquare
);
764 m_calendar
->SetAttr(13, attrHeaderLike
);
768 m_calendar
->ResetAttr(17);
769 m_calendar
->ResetAttr(29);
770 m_calendar
->ResetAttr(13);
773 m_calendar
->Refresh();
777 // ----------------------------------------------------------------------------
779 // ----------------------------------------------------------------------------
781 #if wxUSE_DATEPICKCTRL
783 MyDialog::MyDialog(wxWindow
*parent
, const wxDateTime
& dt
, int dtpStyle
)
784 : wxDialog(parent
, wxID_ANY
, wxString(_T("Calendar: Choose a date")))
786 wxStdDialogButtonSizer
*sizerBtns
= new wxStdDialogButtonSizer
;
787 sizerBtns
->AddButton(new wxButton(this, wxID_OK
));
788 sizerBtns
->AddButton(new wxButton(this, wxID_CANCEL
));
789 sizerBtns
->Realize();
791 wxSizer
*sizerText
= new wxBoxSizer(wxHORIZONTAL
);
792 sizerText
->Add(new wxStaticText(this, wxID_ANY
, _T("Date in ISO format: ")),
793 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL
));
794 m_text
= new wxTextCtrl(this, wxID_ANY
);
795 sizerText
->Add(m_text
, wxSizerFlags().
796 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL
));
798 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
799 sizerTop
->Add(new wxStaticText
802 _T("Enter your birthday date (not before 20th century):")
804 wxSizerFlags().Border());
806 #if wxUSE_DATEPICKCTRL_GENERIC
807 wxFrame
*frame
= (wxFrame
*)wxGetTopLevelParent(parent
);
808 if ( frame
&& frame
->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic
) )
809 m_datePicker
= new wxDatePickerCtrlGeneric(this, wxID_ANY
, dt
,
814 #endif // wxUSE_DATEPICKCTRL_GENERIC
815 m_datePicker
= new wxDatePickerCtrl(this, wxID_ANY
, dt
,
816 wxDefaultPosition
, wxDefaultSize
,
818 m_datePicker
->SetRange(wxDateTime(1, wxDateTime::Jan
, 1900),
820 sizerTop
->Add(m_datePicker
, wxSizerFlags().Expand().Border());
822 sizerTop
->AddStretchSpacer(1);
823 sizerTop
->Add(sizerText
);
825 sizerTop
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
827 SetSizerAndFit(sizerTop
);
831 void MyDialog::OnDateChange(wxDateEvent
& event
)
833 const wxDateTime dt
= event
.GetDate();
835 m_text
->SetValue(dt
.FormatISODate());
837 m_text
->SetValue(wxEmptyString
);
840 #endif // wxUSE_DATEPICKCTRL