move all appearace-related methods down to wxCalendarCtrlBase from wxGenericCalendarCtrl
[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 m_panel->GetCal()->EnableHolidayDisplay(event.IsChecked());
453 }
454
455 void MyFrame::OnCalSpecial(wxCommandEvent& event)
456 {
457 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
458 }
459
460 void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
461 {
462 m_panel->GetCal()->EnableMonthChange(event.IsChecked());
463 }
464
465 void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
466 {
467 m_panel->ToggleCalStyle(event.IsChecked(),
468 wxCAL_SEQUENTIAL_MONTH_SELECTION);
469 }
470
471 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
472 {
473 m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS);
474 }
475
476 void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
477 {
478 m_panel->SetDate(wxDateTime(24, wxDateTime::Dec, 2005, 22, 00, 00));
479 }
480
481 void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
482 {
483 m_panel->SetDate(wxDateTime::Today());
484 }
485
486 void MyFrame::OnBeginDST(wxCommandEvent &WXUNUSED(event))
487 {
488 m_panel->SetDate(wxDateTime::GetBeginDST(m_panel->GetDate().GetYear()));
489 }
490
491 void MyFrame::OnCalToggleResizable(wxCommandEvent& event)
492 {
493 wxSizer * const sizer = m_panel->GetSizer();
494 wxSizerItem * const item = sizer->GetItem(m_panel->GetCal());
495 if ( event.IsChecked() )
496 {
497 item->SetProportion(1);
498 item->SetFlag(wxEXPAND);
499 }
500 else // not resizable
501 {
502 item->SetProportion(0);
503 item->SetFlag(wxALIGN_CENTER);
504 }
505
506 sizer->Layout();
507 }
508
509 #if wxUSE_DATEPICKCTRL
510
511 void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent& event)
512 {
513 // it only makes sense to start with invalid date if we can have no date
514 event.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) );
515 }
516
517 void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
518 {
519 wxDateTime dt = m_panel->GetCal()->GetDate();
520
521 int style = wxDP_DEFAULT;
522 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury) )
523 style |= wxDP_SHOWCENTURY;
524 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown) )
525 style |= wxDP_DROPDOWN;
526 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) )
527 {
528 style |= wxDP_ALLOWNONE;
529
530 if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone) )
531 dt = wxDefaultDateTime;
532 }
533
534 MyDialog dlg(this, dt, style);
535 if ( dlg.ShowModal() == wxID_OK )
536 {
537 dt = dlg.GetDate();
538 if ( dt.IsValid() )
539 {
540 const wxDateTime today = wxDateTime::Today();
541
542 if ( dt.GetDay() == today.GetDay() &&
543 dt.GetMonth() == today.GetMonth() )
544 {
545 wxMessageBox(_T("Happy birthday!"), _T("Calendar Sample"));
546 }
547
548 m_panel->SetDate(dt);
549
550 wxLogStatus(_T("Changed the date to your input"));
551 }
552 else
553 {
554 wxLogStatus(_T("No date entered"));
555 }
556 }
557 }
558
559 #endif // wxUSE_DATEPICKCTRL
560
561 // ----------------------------------------------------------------------------
562 // MyPanel
563 // ----------------------------------------------------------------------------
564
565 MyPanel::MyPanel(wxWindow *parent)
566 : wxPanel(parent, wxID_ANY)
567 {
568 #ifdef wxHAS_NATIVE_CALENDARCTRL
569 m_usingGeneric = false;
570 #else
571 m_usingGeneric = true;
572 #endif
573
574 wxString date;
575 date.Printf(wxT("Selected date: %s"),
576 wxDateTime::Today().FormatISODate().c_str());
577 m_date = new wxStaticText(this, wxID_ANY, date);
578 m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
579 wxDefaultDateTime,
580 wxDefaultPosition,
581 wxDefaultSize,
582 wxCAL_MONDAY_FIRST |
583 wxCAL_SHOW_HOLIDAYS |
584 wxRAISED_BORDER);
585
586 // adjust to vertical/horizontal display, check mostly dedicated to WinCE
587 bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) );
588 m_sizer = new wxBoxSizer( horizontal ? wxHORIZONTAL : wxVERTICAL );
589
590 m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
591 m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
592
593 SetSizer( m_sizer );
594 m_sizer->SetSizeHints( this );
595 }
596
597 void MyPanel::OnCalendar(wxCalendarEvent& event)
598 {
599 m_calendar->Mark(event.GetDate().GetDay(), true);
600 wxLogMessage(wxT("Selected (and marked) %s from calendar."),
601 event.GetDate().FormatISODate().c_str());
602 }
603
604 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
605 {
606 wxString s;
607 s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
608
609 m_date->SetLabel(s);
610 wxLogStatus(s);
611 }
612
613 void MyPanel::OnCalMonthChange(wxCalendarEvent& event)
614 {
615 wxLogStatus(wxT("Calendar month changed to %s %d"),
616 wxDateTime::GetMonthName(event.GetDate().GetMonth()),
617 event.GetDate().GetYear());
618 }
619
620 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
621 {
622 wxLogMessage(wxT("Clicked on %s"),
623 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
624 }
625
626 void MyPanel::RecreateCalendar(long style)
627 {
628 wxCalendarCtrlBase *calendar;
629 #ifdef wxHAS_NATIVE_CALENDARCTRL
630 if ( m_usingGeneric )
631 calendar = new wxGenericCalendarCtrl(this, Calendar_CalCtrl,
632 m_calendar->GetDate(),
633 wxDefaultPosition,
634 wxDefaultSize,
635 style);
636 else
637 #endif // wxHAS_NATIVE_CALENDARCTRL
638 calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
639 m_calendar->GetDate(),
640 wxDefaultPosition,
641 wxDefaultSize,
642 style);
643
644 m_sizer->Replace(m_calendar, calendar);
645 delete m_calendar;
646 m_calendar = calendar;
647
648 m_sizer->Layout();
649 }
650
651 void MyPanel::ToggleCalStyle(bool on, int flag)
652 {
653 long style = m_calendar->GetWindowStyle();
654 if ( on )
655 style |= flag;
656 else
657 style &= ~flag;
658
659 if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION )
660 {
661 // changing this style requires recreating the control
662 RecreateCalendar(style);
663 }
664 else // just changing the style is enough
665 {
666 m_calendar->SetWindowStyle(style);
667
668 m_calendar->Refresh();
669 }
670 }
671
672 void MyPanel::HighlightSpecial(bool on)
673 {
674 if ( on )
675 {
676 wxCalendarDateAttr
677 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
678 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
679 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
680
681 m_calendar->SetAttr(17, attrRedCircle);
682 m_calendar->SetAttr(29, attrGreenSquare);
683 m_calendar->SetAttr(13, attrHeaderLike);
684 }
685 else // off
686 {
687 m_calendar->ResetAttr(17);
688 m_calendar->ResetAttr(29);
689 m_calendar->ResetAttr(13);
690 }
691
692 m_calendar->Refresh();
693 }
694
695
696 // ----------------------------------------------------------------------------
697 // MyDialog
698 // ----------------------------------------------------------------------------
699
700 #if wxUSE_DATEPICKCTRL
701
702 MyDialog::MyDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
703 : wxDialog(parent, wxID_ANY, wxString(_T("Calendar: Choose a date")))
704 {
705 wxStdDialogButtonSizer *sizerBtns = new wxStdDialogButtonSizer;
706 sizerBtns->AddButton(new wxButton(this, wxID_OK));
707 sizerBtns->AddButton(new wxButton(this, wxID_CANCEL));
708 sizerBtns->Realize();
709
710 wxSizer *sizerText = new wxBoxSizer(wxHORIZONTAL);
711 sizerText->Add(new wxStaticText(this, wxID_ANY, _T("Date in ISO format: ")),
712 wxSizerFlags().Border().Align(wxALIGN_CENTRE_VERTICAL));
713 m_text = new wxTextCtrl(this, wxID_ANY);
714 sizerText->Add(m_text, wxSizerFlags().
715 Expand().Border().Align(wxALIGN_CENTRE_VERTICAL));
716
717 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
718 sizerTop->Add(new wxStaticText
719 (
720 this, wxID_ANY,
721 _T("Enter your birthday date (not before 20th century):")
722 ),
723 wxSizerFlags().Border());
724
725 #if wxUSE_DATEPICKCTRL_GENERIC
726 wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
727 if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
728 m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt,
729 wxDefaultPosition,
730 wxDefaultSize,
731 dtpStyle);
732 else
733 #endif // wxUSE_DATEPICKCTRL_GENERIC
734 m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt,
735 wxDefaultPosition, wxDefaultSize,
736 dtpStyle);
737 m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
738 wxDefaultDateTime);
739 sizerTop->Add(m_datePicker, wxSizerFlags().Expand().Border());
740
741 sizerTop->AddStretchSpacer(1);
742 sizerTop->Add(sizerText);
743
744 sizerTop->Add(sizerBtns, wxSizerFlags().Centre().Border());
745
746 SetSizerAndFit(sizerTop);
747 Layout();
748 }
749
750 void MyDialog::OnDateChange(wxDateEvent& event)
751 {
752 const wxDateTime dt = event.GetDate();
753 if ( dt.IsValid() )
754 m_text->SetValue(dt.FormatISODate());
755 else
756 m_text->SetValue(wxEmptyString);
757 }
758
759 #endif // wxUSE_DATEPICKCTRL