1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
38 #include "wx/stattext.h"
40 #include "wx/layout.h"
41 #include "wx/msgdlg.h"
45 #include "wx/calctrl.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // Define a new application type, each program should derive a class from wxApp
52 class MyApp
: public wxApp
55 // override base class virtuals
56 // ----------------------------
58 // this one is called on application startup and is a good place for the app
59 // initialization (doing it here and not in the ctor allows to have an error
60 // return: if OnInit() returns false, the application terminates)
61 virtual bool OnInit();
64 class MyPanel
: public wxPanel
67 MyPanel(wxFrame
*frame
);
69 void OnCalendar(wxCalendarEvent
& event
);
70 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
71 void OnCalendarChange(wxCalendarEvent
& event
);
72 void OnCalMonthChange(wxCalendarEvent
& event
);
73 void OnCalYearChange(wxCalendarEvent
& event
);
75 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
77 // turn on/off the specified style bit on the calendar control
78 void ToggleCalStyle(bool on
, int style
);
80 void HighlightSpecial(bool on
);
86 wxCalendarCtrl
*m_calendar
;
92 // Define a new frame type: this is going to be our main frame
93 class MyFrame
: public wxFrame
97 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
99 // event handlers (these functions should _not_ be virtual)
100 void OnQuit(wxCommandEvent
& event
);
101 void OnAbout(wxCommandEvent
& event
);
103 void OnCalMonday(wxCommandEvent
& event
);
104 void OnCalHolidays(wxCommandEvent
& event
);
105 void OnCalSpecial(wxCommandEvent
& event
);
107 void OnCalAllowMonth(wxCommandEvent
& event
);
108 void OnCalAllowYear(wxCommandEvent
& event
);
110 void OnCalSeqMonth(wxCommandEvent
& event
);
111 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
113 void OnSetDate(wxCommandEvent
& event
);
114 void OnToday(wxCommandEvent
& event
);
116 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
121 // any class wishing to process wxWidgets events must use this macro
122 DECLARE_EVENT_TABLE()
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // IDs for the controls and the menu commands
133 Calendar_File_About
= 100,
135 Calendar_Cal_Monday
= 200,
136 Calendar_Cal_Holidays
,
137 Calendar_Cal_Special
,
140 Calendar_Cal_SeqMonth
,
141 Calendar_Cal_SurroundWeeks
,
142 Calendar_Cal_SetDate
,
144 Calendar_CalCtrl
= 1000
147 // ----------------------------------------------------------------------------
148 // event tables and other macros for wxWidgets
149 // ----------------------------------------------------------------------------
151 // the event tables connect the wxWidgets events with the functions (event
152 // handlers) which process them. It can be also done at run-time, but for the
153 // simple menu events like this the static method is much simpler.
154 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
155 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
156 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
158 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
159 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
160 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
162 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
163 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
165 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
166 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
168 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
169 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
172 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
175 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
176 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
177 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
178 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
179 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
180 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
183 // Create a new application object: this macro will allow wxWidgets to create
184 // the application object during program execution (it's better than using a
185 // static object for many reasons) and also declares the accessor function
186 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
190 // ============================================================================
192 // ============================================================================
194 // ----------------------------------------------------------------------------
195 // the application class
196 // ----------------------------------------------------------------------------
198 // `Main program' equivalent: the program execution "starts" here
201 // Create the main application window
202 MyFrame
*frame
= new MyFrame(_T("Calendar wxWidgets sample"),
203 wxPoint(50, 50), wxSize(450, 340));
207 // success: wxApp::OnRun() will be called which will enter the main message
208 // loop and the application will run. If we returned FALSE here, the
209 // application would exit immediately.
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
218 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
219 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
222 wxMenu
*menuFile
= new wxMenu
;
224 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
225 menuFile
->AppendSeparator();
226 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
228 wxMenu
*menuCal
= new wxMenu
;
229 menuCal
->Append(Calendar_Cal_Monday
,
230 _T("Monday &first weekday\tCtrl-F"),
231 _T("Toggle between Mon and Sun as the first week day"),
233 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
234 _T("Toggle highlighting the holidays"),
236 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
237 _T("Test custom highlighting"),
239 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
240 _T("Show s&urrounding weeks\tCtrl-W"),
241 _T("Show the neighbouring weeks in the prev/next month"),
243 menuCal
->AppendSeparator();
244 menuCal
->Append(Calendar_Cal_SeqMonth
,
245 _T("To&ggle month selector style\tCtrl-G"),
246 _T("Use another style for the calendar controls"),
248 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
249 _T("Allow changing the month in the calendar"),
251 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
252 _T("Allow changing the year in the calendar"),
254 menuCal
->AppendSeparator();
255 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
256 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
258 // now append the freshly created menu to the menu bar...
259 wxMenuBar
*menuBar
= new wxMenuBar
;
260 menuBar
->Append(menuFile
, _T("&File"));
261 menuBar
->Append(menuCal
, _T("&Calendar"));
263 menuBar
->Check(Calendar_Cal_Monday
, true);
264 menuBar
->Check(Calendar_Cal_Holidays
, true);
265 menuBar
->Check(Calendar_Cal_Month
, true);
266 menuBar
->Check(Calendar_Cal_Year
, true);
268 // ... and attach this menu bar to the frame
271 m_panel
= new MyPanel(this);
274 // create a status bar just for fun (by default with 1 pane only)
276 SetStatusText(_T("Welcome to wxWidgets!"));
277 #endif // wxUSE_STATUSBAR
280 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
282 // TRUE is to force the frame to close
286 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
288 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
289 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
292 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
294 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
296 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
299 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
301 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
303 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
306 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
308 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
311 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
313 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
315 m_panel
->GetCal()->EnableMonthChange(allow
);
318 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
320 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
322 m_panel
->GetCal()->EnableYearChange(allow
);
325 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
327 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
329 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
332 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
334 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
336 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
339 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
341 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
344 void MyFrame::OnSetDate(wxCommandEvent
&WXUNUSED(event
))
349 void MyFrame::OnToday(wxCommandEvent
&WXUNUSED(event
))
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 MyPanel::MyPanel(wxFrame
*frame
)
359 : wxPanel(frame
, wxID_ANY
)
362 date
.Printf(wxT("Selected date: %s"),
363 wxDateTime::Today().FormatISODate().c_str());
364 m_date
= new wxStaticText(this, wxID_ANY
, date
);
365 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
370 wxCAL_SHOW_HOLIDAYS
|
373 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxHORIZONTAL
);
375 m_sizer
->Add(m_date
, 0, wxALIGN_CENTER
| wxALL
, 10 );
376 m_sizer
->Add(m_calendar
, 0, wxALIGN_CENTER
| wxALIGN_LEFT
);
379 m_sizer
->SetSizeHints( this );
382 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
384 wxLogMessage(wxT("Selected %s from calendar"),
385 event
.GetDate().FormatISODate().c_str());
388 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
391 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
396 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
398 wxLogStatus(wxT("Calendar month changed"));
401 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
403 wxLogStatus(wxT("Calendar year changed"));
406 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
408 wxLogMessage(wxT("Clicked on %s"),
409 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
412 void MyPanel::ToggleCalStyle(bool on
, int flag
)
414 long style
= m_calendar
->GetWindowStyle();
420 m_calendar
->SetWindowStyle(style
);
422 m_calendar
->Refresh();
425 void MyPanel::HighlightSpecial(bool on
)
430 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
431 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
432 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
434 m_calendar
->SetAttr(17, attrRedCircle
);
435 m_calendar
->SetAttr(29, attrGreenSquare
);
436 m_calendar
->SetAttr(13, attrHeaderLike
);
440 m_calendar
->ResetAttr(17);
441 m_calendar
->ResetAttr(29);
442 m_calendar
->ResetAttr(13);
445 m_calendar
->Refresh();
448 void MyPanel::SetDate()
450 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
451 m_calendar
->SetDate(date
);
454 void MyPanel::Today()
456 m_calendar
->SetDate(wxDateTime::Today());