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