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