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