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