]> git.saurik.com Git - wxWidgets.git/blame - samples/calendar/calendar.cpp
Resolve ambiguity between GetClientXXX() methods in wxOSX wxComboBox.
[wxWidgets.git] / samples / calendar / calendar.cpp
CommitLineData
74a533f7
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: calendar.cpp
3// Purpose: wxCalendarCtrl sample
4// Author: Vadim Zeitlin
4e6bceff 5// Modified by:
74a533f7
VZ
6// Created: 02.01.00
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
74a533f7
VZ
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
273b7ed9 27// for all others, include the necessary headers
74a533f7
VZ
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
788233da 30 #include "wx/log.h"
74a533f7 31 #include "wx/frame.h"
273b7ed9
VZ
32 #include "wx/panel.h"
33 #include "wx/stattext.h"
34 #include "wx/menu.h"
35 #include "wx/layout.h"
36 #include "wx/msgdlg.h"
d9210ab7 37 #include "wx/icon.h"
574d4d1c
PC
38 #include "wx/button.h"
39 #include "wx/sizer.h"
40 #include "wx/textctrl.h"
41 #include "wx/settings.h"
74a533f7
VZ
42#endif
43
44#include "wx/calctrl.h"
628e155d 45#include "wx/splitter.h"
7ff2dfba
VZ
46
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
feb72429 53
48263511 54#include "../sample.xpm"
3200f37d 55
628e155d
VZ
56#ifdef wxHAS_NATIVE_CALENDARCTRL
57 #include "wx/generic/calctrlg.h"
58#endif
59
74a533f7
VZ
60// ----------------------------------------------------------------------------
61// private classes
62// ----------------------------------------------------------------------------
63
64// Define a new application type, each program should derive a class from wxApp
65class MyApp : public wxApp
66{
67public:
68 // override base class virtuals
69 // ----------------------------
70
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();
75};
76
77class MyPanel : public wxPanel
78{
79public:
628e155d 80 MyPanel(wxWindow *parent);
74a533f7
VZ
81
82 void OnCalendar(wxCalendarEvent& event);
83 void OnCalendarWeekDayClick(wxCalendarEvent& event);
232b2162 84 void OnCalendarWeekClick(wxCalendarEvent& event);
74a533f7 85 void OnCalendarChange(wxCalendarEvent& event);
0de868d9 86 void OnCalMonthChange(wxCalendarEvent& event);
74a533f7 87
628e155d 88 wxCalendarCtrlBase *GetCal() const { return m_calendar; }
bc385ba9 89
37df1f33
VZ
90 // turn on/off the specified style bit on the calendar control
91 void ToggleCalStyle(bool on, int style);
92
628e155d
VZ
93 bool IsUsingGeneric() const { return m_usingGeneric; }
94 void ToggleUseGeneric()
95 {
96 m_usingGeneric = !m_usingGeneric;
97 RecreateCalendar(m_calendar->GetWindowStyle());
98 }
99
74a533f7 100 void HighlightSpecial(bool on);
a5cf7942 101 void LimitDateRange(bool on);
74a533f7 102
a3c3a4cd
VZ
103 wxDateTime GetDate() const { return m_calendar->GetDate(); }
104 void SetDate(const wxDateTime& dt) { m_calendar->SetDate(dt); }
a7c58211 105
74a533f7 106private:
ee22a3a2
VZ
107 wxCalendarCtrlBase *DoCreateCalendar(const wxDateTime& dt, long style);
108
628e155d
VZ
109 void RecreateCalendar(long style);
110
111 wxCalendarCtrlBase *m_calendar;
74a533f7 112 wxStaticText *m_date;
2522d529 113 wxSizer *m_sizer;
74a533f7 114
628e155d
VZ
115 bool m_usingGeneric;
116
117
74a533f7
VZ
118 DECLARE_EVENT_TABLE()
119};
120
121// Define a new frame type: this is going to be our main frame
122class MyFrame : public wxFrame
123{
124public:
125 // ctor(s)
f9cc9706 126 MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
74a533f7
VZ
127
128 // event handlers (these functions should _not_ be virtual)
74a533f7 129 void OnAbout(wxCommandEvent& event);
628e155d
VZ
130 void OnClearLog(wxCommandEvent& event);
131 void OnQuit(wxCommandEvent& event);
74a533f7 132
ac78ab7b 133#if wxUSE_DATEPICKCTRL
feb72429 134 void OnAskDate(wxCommandEvent& event);
404e855d
VZ
135
136 void OnUpdateUIStartWithNone(wxUpdateUIEvent& event);
ac78ab7b 137#endif // wxUSE_DATEPICKCTRL
feb72429 138
628e155d
VZ
139#ifdef wxHAS_NATIVE_CALENDARCTRL
140 void OnCalGeneric(wxCommandEvent& WXUNUSED(event))
141 {
142 m_panel->ToggleUseGeneric();
143 }
144#endif // wxHAS_NATIVE_CALENDARCTRL
145
74a533f7
VZ
146 void OnCalMonday(wxCommandEvent& event);
147 void OnCalHolidays(wxCommandEvent& event);
148 void OnCalSpecial(wxCommandEvent& event);
149
bc385ba9 150 void OnCalAllowMonth(wxCommandEvent& event);
a5cf7942 151 void OnCalLimitDates(wxCommandEvent& event);
37df1f33
VZ
152 void OnCalSeqMonth(wxCommandEvent& event);
153 void OnCalShowSurroundingWeeks(wxCommandEvent& event);
7b0ccb8a 154 void OnCalShowWeekNumbers(wxCommandEvent& event);
37df1f33 155
605dfd91
JS
156 void OnSetDate(wxCommandEvent& event);
157 void OnToday(wxCommandEvent& event);
a3c3a4cd 158 void OnBeginDST(wxCommandEvent& event);
605dfd91 159
5f324bb6
VZ
160 void OnCalToggleResizable(wxCommandEvent& event);
161
628e155d
VZ
162 void OnUpdateUIGenericOnly(wxUpdateUIEvent& event)
163 {
164 event.Enable(m_panel->IsUsingGeneric());
165 }
bc385ba9 166
ee22a3a2
VZ
167 void OnCalRClick(wxMouseEvent& event);
168
74a533f7
VZ
169private:
170 MyPanel *m_panel;
628e155d 171 wxTextCtrl *m_logWindow;
74a533f7 172
be5a51fb 173 // any class wishing to process wxWidgets events must use this macro
74a533f7
VZ
174 DECLARE_EVENT_TABLE()
175};
176
ac78ab7b 177#if wxUSE_DATEPICKCTRL
feb72429
VZ
178
179// Define a simple modal dialog which asks the user for a date
180class MyDialog : public wxDialog
181{
182public:
d1fd3c26 183 MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle);
feb72429
VZ
184
185 wxDateTime GetDate() const { return m_datePicker->GetValue(); }
186
187private:
188 void OnDateChange(wxDateEvent& event);
189
190
7ff2dfba 191 wxDatePickerCtrlBase *m_datePicker;
feb72429
VZ
192 wxTextCtrl *m_text;
193
194
195 DECLARE_EVENT_TABLE()
196};
197
ac78ab7b 198#endif // wxUSE_DATEPICKCTRL
feb72429 199
74a533f7
VZ
200// ----------------------------------------------------------------------------
201// constants
202// ----------------------------------------------------------------------------
203
204// IDs for the controls and the menu commands
205enum
206{
207 // menu items
feb72429 208 Calendar_File_About = wxID_ABOUT,
628e155d 209 Calendar_File_ClearLog = wxID_CLEAR,
feb72429 210 Calendar_File_Quit = wxID_EXIT,
628e155d
VZ
211 Calendar_Cal_Generic = 200,
212 Calendar_Cal_Monday,
74a533f7
VZ
213 Calendar_Cal_Holidays,
214 Calendar_Cal_Special,
bc385ba9 215 Calendar_Cal_Month,
a5cf7942 216 Calendar_Cal_LimitDates,
37df1f33
VZ
217 Calendar_Cal_SeqMonth,
218 Calendar_Cal_SurroundWeeks,
7b0ccb8a 219 Calendar_Cal_WeekNumbers,
605dfd91
JS
220 Calendar_Cal_SetDate,
221 Calendar_Cal_Today,
a3c3a4cd 222 Calendar_Cal_BeginDST,
5f324bb6 223 Calendar_Cal_Resizable,
d1fd3c26
VZ
224#if wxUSE_DATEPICKCTRL
225 Calendar_DatePicker_AskDate = 300,
226 Calendar_DatePicker_ShowCentury,
227 Calendar_DatePicker_DropDown,
3200f37d 228 Calendar_DatePicker_AllowNone,
404e855d 229 Calendar_DatePicker_StartWithNone,
7ff2dfba
VZ
230#if wxUSE_DATEPICKCTRL_GENERIC
231 Calendar_DatePicker_Generic,
232#endif // wxUSE_DATEPICKCTRL_GENERIC
d1fd3c26 233#endif // wxUSE_DATEPICKCTRL
e9561b3b 234 Calendar_CalCtrl = 1000
74a533f7
VZ
235};
236
237// ----------------------------------------------------------------------------
be5a51fb 238// event tables and other macros for wxWidgets
74a533f7
VZ
239// ----------------------------------------------------------------------------
240
be5a51fb 241// the event tables connect the wxWidgets events with the functions (event
74a533f7
VZ
242// handlers) which process them. It can be also done at run-time, but for the
243// simple menu events like this the static method is much simpler.
244BEGIN_EVENT_TABLE(MyFrame, wxFrame)
74a533f7 245 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
628e155d
VZ
246 EVT_MENU(Calendar_File_ClearLog, MyFrame::OnClearLog)
247 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
74a533f7 248
ac78ab7b 249#if wxUSE_DATEPICKCTRL
d1fd3c26 250 EVT_MENU(Calendar_DatePicker_AskDate, MyFrame::OnAskDate)
404e855d
VZ
251
252 EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone,
253 MyFrame::OnUpdateUIStartWithNone)
ac78ab7b 254#endif // wxUSE_DATEPICKCTRL
feb72429 255
628e155d
VZ
256#ifdef wxHAS_NATIVE_CALENDARCTRL
257 EVT_MENU(Calendar_Cal_Generic, MyFrame::OnCalGeneric)
258#endif // wxHAS_NATIVE_CALENDARCTRL
259
74a533f7
VZ
260 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
261 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
262 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
bc385ba9
VZ
263
264 EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
bc385ba9 265
a5cf7942
VZ
266 EVT_MENU(Calendar_Cal_LimitDates, MyFrame::OnCalLimitDates)
267
37df1f33
VZ
268 EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
269 EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
7b0ccb8a 270 EVT_MENU(Calendar_Cal_WeekNumbers, MyFrame::OnCalShowWeekNumbers)
37df1f33 271
605dfd91
JS
272 EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
273 EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
a3c3a4cd 274 EVT_MENU(Calendar_Cal_BeginDST, MyFrame::OnBeginDST)
605dfd91 275
5f324bb6
VZ
276 EVT_MENU(Calendar_Cal_Resizable, MyFrame::OnCalToggleResizable)
277
605dfd91 278
628e155d 279 EVT_UPDATE_UI(Calendar_Cal_SeqMonth, MyFrame::OnUpdateUIGenericOnly)
db0b0942 280#ifdef __WXGTK20__
628e155d
VZ
281 EVT_UPDATE_UI(Calendar_Cal_Monday, MyFrame::OnUpdateUIGenericOnly)
282 EVT_UPDATE_UI(Calendar_Cal_Holidays, MyFrame::OnUpdateUIGenericOnly)
6d9b6716 283#endif
628e155d
VZ
284 EVT_UPDATE_UI(Calendar_Cal_Special, MyFrame::OnUpdateUIGenericOnly)
285 EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks, MyFrame::OnUpdateUIGenericOnly)
74a533f7
VZ
286END_EVENT_TABLE()
287
288BEGIN_EVENT_TABLE(MyPanel, wxPanel)
628e155d
VZ
289 EVT_CALENDAR(Calendar_CalCtrl, MyPanel::OnCalendar)
290 EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl, MyPanel::OnCalMonthChange)
291 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
74a533f7 292 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
232b2162 293 EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekClick)
74a533f7
VZ
294END_EVENT_TABLE()
295
ac78ab7b 296#if wxUSE_DATEPICKCTRL
feb72429
VZ
297
298BEGIN_EVENT_TABLE(MyDialog, wxDialog)
299 EVT_DATE_CHANGED(wxID_ANY, MyDialog::OnDateChange)
300END_EVENT_TABLE()
301
ac78ab7b 302#endif // wxUSE_DATEPICKCTRL
feb72429 303
be5a51fb 304// Create a new application object: this macro will allow wxWidgets to create
74a533f7
VZ
305// the application object during program execution (it's better than using a
306// static object for many reasons) and also declares the accessor function
307// wxGetApp() which will return the reference of the right type (i.e. MyApp and
308// not wxApp)
309IMPLEMENT_APP(MyApp)
310
311// ============================================================================
312// implementation
313// ============================================================================
314
315// ----------------------------------------------------------------------------
316// the application class
317// ----------------------------------------------------------------------------
318
319// `Main program' equivalent: the program execution "starts" here
320bool MyApp::OnInit()
321{
45e6e6f8
VZ
322 if ( !wxApp::OnInit() )
323 return false;
324
74a533f7 325 // Create the main application window
9a83f860 326 MyFrame *frame = new MyFrame(wxT("Calendar wxWidgets sample")
f9cc9706
WS
327#ifndef __WXWINCE__
328 ,wxPoint(50, 50), wxSize(450, 340)
329#endif
232b2162 330 );
74a533f7 331
9230b621 332 frame->Show(true);
74a533f7
VZ
333
334 // success: wxApp::OnRun() will be called which will enter the main message
5014bb3a 335 // loop and the application will run. If we returned false here, the
74a533f7 336 // application would exit immediately.
9230b621 337 return true;
74a533f7
VZ
338}
339
340// ----------------------------------------------------------------------------
341// main frame
342// ----------------------------------------------------------------------------
343
344// frame constructor
345MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
9230b621 346 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
74a533f7 347{
3200f37d 348 // set the frame icon
3cb332c1 349 SetIcon(wxICON(sample));
3200f37d 350
74a533f7
VZ
351 // create a menu bar
352 wxMenu *menuFile = new wxMenu;
9a83f860 353 menuFile->Append(Calendar_File_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
74a533f7 354 menuFile->AppendSeparator();
9a83f860 355 menuFile->Append(Calendar_File_ClearLog, wxT("&Clear log\tCtrl-L"));
628e155d 356 menuFile->AppendSeparator();
9a83f860 357 menuFile->Append(Calendar_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
74a533f7
VZ
358
359 wxMenu *menuCal = new wxMenu;
628e155d
VZ
360#ifdef wxHAS_NATIVE_CALENDARCTRL
361 menuCal->AppendCheckItem(Calendar_Cal_Generic, "Use &generic version\tCtrl-G",
362 "Toggle between native and generic control");
363 menuCal->AppendSeparator();
364#endif // wxHAS_NATIVE_CALENDARCTRL
74a533f7 365 menuCal->Append(Calendar_Cal_Monday,
9a83f860
VZ
366 wxT("Monday &first weekday\tCtrl-F"),
367 wxT("Toggle between Mon and Sun as the first week day"),
9230b621 368 true);
9a83f860
VZ
369 menuCal->Append(Calendar_Cal_Holidays, wxT("Show &holidays\tCtrl-H"),
370 wxT("Toggle highlighting the holidays"),
9230b621 371 true);
9a83f860
VZ
372 menuCal->Append(Calendar_Cal_Special, wxT("Highlight &special dates\tCtrl-S"),
373 wxT("Test custom highlighting"),
9230b621 374 true);
37df1f33 375 menuCal->Append(Calendar_Cal_SurroundWeeks,
9a83f860
VZ
376 wxT("Show s&urrounding weeks\tCtrl-W"),
377 wxT("Show the neighbouring weeks in the prev/next month"),
9230b621 378 true);
7b0ccb8a 379 menuCal->Append(Calendar_Cal_WeekNumbers,
9a83f860
VZ
380 wxT("Show &week numbers"),
381 wxT("Toggle week numbers"),
7b0ccb8a 382 true);
bc385ba9 383 menuCal->AppendSeparator();
37df1f33 384 menuCal->Append(Calendar_Cal_SeqMonth,
9a83f860
VZ
385 wxT("Toggle month selector st&yle\tCtrl-Y"),
386 wxT("Use another style for the calendar controls"),
9230b621 387 true);
9a83f860
VZ
388 menuCal->Append(Calendar_Cal_Month, wxT("&Month can be changed\tCtrl-M"),
389 wxT("Allow changing the month in the calendar"),
9230b621 390 true);
a5cf7942
VZ
391 menuCal->AppendCheckItem(Calendar_Cal_LimitDates, wxT("Toggle date ra&nge\tCtrl-N"),
392 wxT("Limit the valid dates"));
605dfd91 393 menuCal->AppendSeparator();
9a83f860
VZ
394 menuCal->Append(Calendar_Cal_SetDate, wxT("Call &SetDate(2005-12-24)"), wxT("Set date to 2005-12-24."));
395 menuCal->Append(Calendar_Cal_Today, wxT("Call &Today()"), wxT("Set to the current date."));
a3c3a4cd 396 menuCal->Append(Calendar_Cal_BeginDST, "Call SetDate(GetBeginDST())");
5f324bb6 397 menuCal->AppendSeparator();
9a83f860 398 menuCal->AppendCheckItem(Calendar_Cal_Resizable, wxT("Make &resizable\tCtrl-R"));
74a533f7 399
d1fd3c26
VZ
400#if wxUSE_DATEPICKCTRL
401 wxMenu *menuDate = new wxMenu;
402 menuDate->AppendCheckItem(Calendar_DatePicker_ShowCentury,
9a83f860 403 wxT("Al&ways show century"));
d1fd3c26 404 menuDate->AppendCheckItem(Calendar_DatePicker_DropDown,
9a83f860 405 wxT("Use &drop down control"));
3200f37d 406 menuDate->AppendCheckItem(Calendar_DatePicker_AllowNone,
9a83f860 407 wxT("Allow &no date"));
404e855d 408 menuDate->AppendCheckItem(Calendar_DatePicker_StartWithNone,
9a83f860 409 wxT("Start &with no date"));
7ff2dfba
VZ
410#if wxUSE_DATEPICKCTRL_GENERIC
411 menuDate->AppendCheckItem(Calendar_DatePicker_Generic,
9a83f860 412 wxT("Use &generic version of the control"));
7ff2dfba 413#endif // wxUSE_DATEPICKCTRL_GENERIC
d1fd3c26 414 menuDate->AppendSeparator();
9a83f860 415 menuDate->Append(Calendar_DatePicker_AskDate, wxT("&Choose date...\tCtrl-D"), wxT("Show dialog with wxDatePickerCtrl"));
d1fd3c26
VZ
416#endif // wxUSE_DATEPICKCTRL
417
74a533f7
VZ
418 // now append the freshly created menu to the menu bar...
419 wxMenuBar *menuBar = new wxMenuBar;
9a83f860
VZ
420 menuBar->Append(menuFile, wxT("&File"));
421 menuBar->Append(menuCal, wxT("&Calendar"));
d1fd3c26 422#if wxUSE_DATEPICKCTRL
9a83f860 423 menuBar->Append(menuDate, wxT("&Date picker"));
d1fd3c26 424#endif // wxUSE_DATEPICKCTRL
74a533f7 425
9230b621
VS
426 menuBar->Check(Calendar_Cal_Monday, true);
427 menuBar->Check(Calendar_Cal_Holidays, true);
428 menuBar->Check(Calendar_Cal_Month, true);
a5cf7942 429 menuBar->Check(Calendar_Cal_LimitDates, false);
74a533f7 430
d1fd3c26
VZ
431#if wxUSE_DATEPICKCTRL
432 menuBar->Check(Calendar_DatePicker_ShowCentury, true);
433#endif // wxUSE_DATEPICKCTRL
434
74a533f7
VZ
435 // ... and attach this menu bar to the frame
436 SetMenuBar(menuBar);
437
628e155d
VZ
438 wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY,
439 wxDefaultPosition, wxDefaultSize,
440 wxSP_NOBORDER);
441 m_panel = new MyPanel(splitter);
442 m_logWindow = new wxTextCtrl(splitter, wxID_ANY, wxEmptyString,
443 wxDefaultPosition, wxDefaultSize,
444 wxTE_READONLY | wxTE_MULTILINE);
445 splitter->SplitHorizontally(m_panel, m_logWindow);
446 splitter->SetMinimumPaneSize(20);
447 wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
74a533f7
VZ
448}
449
450void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
451{
5014bb3a 452 // true is to force the frame to close
9230b621 453 Close(true);
74a533f7
VZ
454}
455
456void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457{
9a83f860
VZ
458 wxMessageBox(wxT("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
459 wxT("About Calendar"), wxOK | wxICON_INFORMATION, this);
74a533f7
VZ
460}
461
628e155d 462void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
74a533f7 463{
628e155d
VZ
464 m_logWindow->Clear();
465}
37df1f33 466
628e155d
VZ
467void MyFrame::OnCalMonday(wxCommandEvent& event)
468{
469 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_MONDAY_FIRST);
74a533f7
VZ
470}
471
472void MyFrame::OnCalHolidays(wxCommandEvent& event)
473{
bf956fac 474 m_panel->GetCal()->EnableHolidayDisplay(event.IsChecked());
74a533f7
VZ
475}
476
477void MyFrame::OnCalSpecial(wxCommandEvent& event)
478{
89a0a322 479 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
bc385ba9
VZ
480}
481
a5cf7942
VZ
482void MyFrame::OnCalLimitDates(wxCommandEvent& event)
483{
484 m_panel->LimitDateRange(GetMenuBar()->IsChecked(event.GetId()));
485}
486
bc385ba9
VZ
487void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
488{
628e155d 489 m_panel->GetCal()->EnableMonthChange(event.IsChecked());
bc385ba9
VZ
490}
491
37df1f33
VZ
492void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
493{
628e155d
VZ
494 m_panel->ToggleCalStyle(event.IsChecked(),
495 wxCAL_SEQUENTIAL_MONTH_SELECTION);
37df1f33
VZ
496}
497
498void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
499{
628e155d 500 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS);
74a533f7
VZ
501}
502
7b0ccb8a
RR
503void MyFrame::OnCalShowWeekNumbers(wxCommandEvent& event)
504{
505 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS);
506}
507
87728739 508void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
605dfd91 509{
628e155d 510 m_panel->SetDate(wxDateTime(24, wxDateTime::Dec, 2005, 22, 00, 00));
605dfd91
JS
511}
512
87728739 513void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
605dfd91 514{
a3c3a4cd
VZ
515 m_panel->SetDate(wxDateTime::Today());
516}
517
518void MyFrame::OnBeginDST(wxCommandEvent &WXUNUSED(event))
519{
520 m_panel->SetDate(wxDateTime::GetBeginDST(m_panel->GetDate().GetYear()));
605dfd91
JS
521}
522
5f324bb6
VZ
523void MyFrame::OnCalToggleResizable(wxCommandEvent& event)
524{
525 wxSizer * const sizer = m_panel->GetSizer();
526 wxSizerItem * const item = sizer->GetItem(m_panel->GetCal());
527 if ( event.IsChecked() )
528 {
529 item->SetProportion(1);
530 item->SetFlag(wxEXPAND);
531 }
532 else // not resizable
533 {
534 item->SetProportion(0);
535 item->SetFlag(wxALIGN_CENTER);
536 }
537
538 sizer->Layout();
539}
540
ee22a3a2
VZ
541void MyFrame::OnCalRClick(wxMouseEvent& event)
542{
543 wxDateTime dt;
544 wxDateTime::WeekDay wd;
545
546 const wxPoint pt = event.GetPosition();
547 wxString msg = wxString::Format("Point (%d, %d) is ", pt.x, pt.y);
548
549 switch ( m_panel->GetCal()->HitTest(pt, &dt, &wd) )
550 {
551 default:
552 wxFAIL_MSG( "unexpected" );
553 // fall through
554
555 case wxCAL_HITTEST_NOWHERE:
556 msg += "nowhere";
557 break;
558
559 case wxCAL_HITTEST_HEADER:
560 msg += wxString::Format("over %s", wxDateTime::GetWeekDayName(wd));
561 break;
562
563 case wxCAL_HITTEST_DAY:
564 msg += wxString::Format("over %s", dt.FormatISODate());
565 break;
566
567 case wxCAL_HITTEST_INCMONTH:
568 msg += "over next month button";
569 break;
570
571 case wxCAL_HITTEST_DECMONTH:
572 msg += "over previous month button";
573 break;
574
575 case wxCAL_HITTEST_SURROUNDING_WEEK:
576 msg += "over a day from another month";
577 break;
578 }
579
580 wxLogMessage("%s", msg);
581}
582
ac78ab7b 583#if wxUSE_DATEPICKCTRL
feb72429 584
404e855d
VZ
585void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent& event)
586{
587 // it only makes sense to start with invalid date if we can have no date
588 event.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) );
589}
590
feb72429
VZ
591void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
592{
404e855d
VZ
593 wxDateTime dt = m_panel->GetCal()->GetDate();
594
d1fd3c26
VZ
595 int style = wxDP_DEFAULT;
596 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury) )
597 style |= wxDP_SHOWCENTURY;
598 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown) )
599 style |= wxDP_DROPDOWN;
3200f37d 600 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) )
404e855d 601 {
3200f37d 602 style |= wxDP_ALLOWNONE;
d1fd3c26 603
404e855d
VZ
604 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone) )
605 dt = wxDefaultDateTime;
606 }
607
608 MyDialog dlg(this, dt, style);
feb72429
VZ
609 if ( dlg.ShowModal() == wxID_OK )
610 {
404e855d 611 dt = dlg.GetDate();
3200f37d 612 if ( dt.IsValid() )
feb72429 613 {
3200f37d 614 const wxDateTime today = wxDateTime::Today();
feb72429 615
3200f37d
VZ
616 if ( dt.GetDay() == today.GetDay() &&
617 dt.GetMonth() == today.GetMonth() )
618 {
9a83f860 619 wxMessageBox(wxT("Happy birthday!"), wxT("Calendar Sample"));
3200f37d 620 }
feb72429 621
a3c3a4cd 622 m_panel->SetDate(dt);
3200f37d 623
9a83f860 624 wxLogStatus(wxT("Changed the date to your input"));
3200f37d
VZ
625 }
626 else
627 {
9a83f860 628 wxLogStatus(wxT("No date entered"));
3200f37d 629 }
feb72429
VZ
630 }
631}
632
ac78ab7b 633#endif // wxUSE_DATEPICKCTRL
feb72429 634
74a533f7
VZ
635// ----------------------------------------------------------------------------
636// MyPanel
637// ----------------------------------------------------------------------------
638
628e155d
VZ
639MyPanel::MyPanel(wxWindow *parent)
640 : wxPanel(parent, wxID_ANY)
74a533f7 641{
628e155d
VZ
642#ifdef wxHAS_NATIVE_CALENDARCTRL
643 m_usingGeneric = false;
644#else
645 m_usingGeneric = true;
646#endif
647
4e6bceff 648 wxString date;
4693b20c 649 date.Printf(wxT("Selected date: %s"),
74a533f7 650 wxDateTime::Today().FormatISODate().c_str());
9230b621 651 m_date = new wxStaticText(this, wxID_ANY, date);
ee22a3a2
VZ
652 m_calendar = DoCreateCalendar(wxDefaultDateTime,
653 wxCAL_MONDAY_FIRST | wxCAL_SHOW_HOLIDAYS);
74a533f7 654
f9cc9706
WS
655 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
656 bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) );
2522d529 657 m_sizer = new wxBoxSizer( horizontal ? wxHORIZONTAL : wxVERTICAL );
4e6bceff 658
9230b621
VS
659 m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
660 m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
4e6bceff 661
9230b621
VS
662 SetSizer( m_sizer );
663 m_sizer->SetSizeHints( this );
74a533f7
VZ
664}
665
666void MyPanel::OnCalendar(wxCalendarEvent& event)
667{
82c6027b
VZ
668 // clicking the same date twice unmarks it (convenient for testing)
669 static wxDateTime s_dateLast;
670 const bool mark = !s_dateLast.IsValid() || event.GetDate() != s_dateLast;
671
672 s_dateLast = event.GetDate();
673
674 m_calendar->Mark(event.GetDate().GetDay(), mark);
675 wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
676 mark ? "" : "un", s_dateLast.FormatISODate().c_str());
74a533f7
VZ
677}
678
679void MyPanel::OnCalendarChange(wxCalendarEvent& event)
680{
681 wxString s;
4693b20c 682 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
74a533f7
VZ
683
684 m_date->SetLabel(s);
628e155d 685 wxLogStatus(s);
74a533f7
VZ
686}
687
628e155d 688void MyPanel::OnCalMonthChange(wxCalendarEvent& event)
0de868d9 689{
628e155d
VZ
690 wxLogStatus(wxT("Calendar month changed to %s %d"),
691 wxDateTime::GetMonthName(event.GetDate().GetMonth()),
692 event.GetDate().GetYear());
0de868d9
VZ
693}
694
74a533f7
VZ
695void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
696{
4693b20c 697 wxLogMessage(wxT("Clicked on %s"),
74a533f7
VZ
698 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
699}
700
232b2162
VZ
701void MyPanel::OnCalendarWeekClick(wxCalendarEvent& event)
702{
703 wxLogMessage(wxT("Clicked on week %d"), event.GetDate().GetWeekOfYear());
704}
705
ee22a3a2 706wxCalendarCtrlBase *MyPanel::DoCreateCalendar(const wxDateTime& dt, long style)
628e155d
VZ
707{
708 wxCalendarCtrlBase *calendar;
709#ifdef wxHAS_NATIVE_CALENDARCTRL
710 if ( m_usingGeneric )
711 calendar = new wxGenericCalendarCtrl(this, Calendar_CalCtrl,
ee22a3a2 712 dt,
628e155d
VZ
713 wxDefaultPosition,
714 wxDefaultSize,
715 style);
716 else
717#endif // wxHAS_NATIVE_CALENDARCTRL
718 calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
ee22a3a2 719 dt,
628e155d
VZ
720 wxDefaultPosition,
721 wxDefaultSize,
722 style);
723
ee22a3a2
VZ
724 calendar->Connect(wxEVT_RIGHT_DOWN,
725 wxMouseEventHandler(MyFrame::OnCalRClick),
726 NULL,
3c778901 727 ( MyFrame * )wxGetTopLevelParent(this));
ee22a3a2
VZ
728
729 return calendar;
730}
731
732void MyPanel::RecreateCalendar(long style)
733{
734 wxCalendarCtrlBase *calendar = DoCreateCalendar(m_calendar->GetDate(), style);
735
628e155d
VZ
736 m_sizer->Replace(m_calendar, calendar);
737 delete m_calendar;
738 m_calendar = calendar;
739
740 m_sizer->Layout();
741}
742
37df1f33 743void MyPanel::ToggleCalStyle(bool on, int flag)
74a533f7
VZ
744{
745 long style = m_calendar->GetWindowStyle();
746 if ( on )
37df1f33 747 style |= flag;
74a533f7 748 else
37df1f33 749 style &= ~flag;
74a533f7 750
232b2162 751 if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION
7b0ccb8a 752 || flag == wxCAL_SHOW_WEEK_NUMBERS)
2522d529
VZ
753 {
754 // changing this style requires recreating the control
628e155d 755 RecreateCalendar(style);
2522d529
VZ
756 }
757 else // just changing the style is enough
758 {
759 m_calendar->SetWindowStyle(style);
74a533f7 760
2522d529
VZ
761 m_calendar->Refresh();
762 }
74a533f7
VZ
763}
764
74a533f7
VZ
765void MyPanel::HighlightSpecial(bool on)
766{
767 if ( on )
768 {
769 wxCalendarDateAttr
770 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
771 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
772 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
773
774 m_calendar->SetAttr(17, attrRedCircle);
775 m_calendar->SetAttr(29, attrGreenSquare);
776 m_calendar->SetAttr(13, attrHeaderLike);
777 }
778 else // off
779 {
780 m_calendar->ResetAttr(17);
781 m_calendar->ResetAttr(29);
782 m_calendar->ResetAttr(13);
783 }
784
785 m_calendar->Refresh();
786}
605dfd91 787
a5cf7942
VZ
788// Toggle a restricted date range to the six months centered on today's date.
789void MyPanel::LimitDateRange(bool on)
790{
791 if ( on )
792 {
793 // limit the choice of date to 3 months around today
794 const wxDateSpan diff = wxDateSpan::Months(3);
795 const wxDateTime today = wxDateTime::Today();
796
797 // Set the restricted date range.
798 if ( m_calendar->SetDateRange(today - diff, today + diff) )
799 {
800 wxLogStatus("Date range limited to 3 months around today.");
7b342d55
VZ
801 wxDateTime firstValidDate;
802 wxDateTime lastValidDate;
803 if ( m_calendar->GetDateRange(&firstValidDate, &lastValidDate) )
804 {
805 wxLogMessage("First valid date: %s, last valid date: %s",
806 firstValidDate.FormatISODate(),
807 lastValidDate.FormatISODate());
808 }
809 else
810 {
811 wxLogWarning("Failed to get back the valid dates range.");
812 }
a5cf7942
VZ
813 }
814 else
815 {
816 wxLogWarning("Date range not supported.");
817 }
818 }
819 else // off
820 {
821 // Remove the date restrictions.
822 if ( m_calendar->SetDateRange() )
823 {
824 wxLogStatus("Date choice is unlimited now.");
825 }
826 else
827 {
828 wxLogWarning("Date range not supported.");
829 }
830 }
831
832 m_calendar->Refresh();
833}
628e155d 834
feb72429
VZ
835// ----------------------------------------------------------------------------
836// MyDialog
837// ----------------------------------------------------------------------------
838
ac78ab7b 839#if wxUSE_DATEPICKCTRL
feb72429 840
d1fd3c26 841MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
9a83f860 842 : wxDialog(parent, wxID_ANY, wxString(wxT("Calendar: Choose a date")))
feb72429
VZ
843{
844 wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
845 sizerBtns->AddButton(new wxButton(this, wxID_OK));
846 sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
718903fe 847 sizerBtns->Realize();
feb72429
VZ
848
849 wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
9a83f860 850 sizerText->Add(new wxStaticText(this, wxID_ANY, wxT("Date in ISO format: ")),
b7e542be 851 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL));
a7c58211 852 m_text = new wxTextCtrl(this, wxID_ANY);
b7e542be
VZ
853 sizerText->Add(m_text, wxSizerFlags().
854 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL));
feb72429
VZ
855
856 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
857 sizerTop->Add(new wxStaticText
858 (
a7c58211 859 this, wxID_ANY,
9a83f860 860 wxT("Enter your birthday date (not before 20th century):")
feb72429
VZ
861 ),
862 wxSizerFlags().Border());
863
7ff2dfba
VZ
864#if wxUSE_DATEPICKCTRL_GENERIC
865 wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
866 if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
a7c58211 867 m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt,
7ff2dfba
VZ
868 wxDefaultPosition,
869 wxDefaultSize,
870 dtpStyle);
871 else
872#endif // wxUSE_DATEPICKCTRL_GENERIC
a7c58211 873 m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt,
d1fd3c26
VZ
874 wxDefaultPosition, wxDefaultSize,
875 dtpStyle);
feb72429
VZ
876 m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
877 wxDefaultDateTime);
878 sizerTop->Add(m_datePicker, wxSizerFlags().Expand().Border());
879
880 sizerTop->AddStretchSpacer(1);
881 sizerTop->Add(sizerText);
882
883 sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border());
884
885 SetSizerAndFit(sizerTop);
886 Layout();
887}
888
889void MyDialog::OnDateChange(wxDateEvent& event)
890{
b7e542be 891 const wxDateTime dt = event.GetDate();
404e855d 892 if ( dt.IsValid() )
a7c58211
WS
893 m_text->SetValue(dt.FormatISODate());
894 else
895 m_text->SetValue(wxEmptyString);
feb72429
VZ
896}
897
ac78ab7b 898#endif // wxUSE_DATEPICKCTRL