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"
44 #include "wx/calctrl.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp
: public wxApp
54 // override base class virtuals
55 // ----------------------------
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
63 class MyPanel
: public wxPanel
66 MyPanel(wxFrame
*frame
);
68 void OnCalendar(wxCalendarEvent
& event
);
69 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
70 void OnCalendarChange(wxCalendarEvent
& event
);
71 void OnCalMonthChange(wxCalendarEvent
& event
);
72 void OnCalYearChange(wxCalendarEvent
& event
);
74 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
76 // turn on/off the specified style bit on the calendar control
77 void ToggleCalStyle(bool on
, int style
);
79 void HighlightSpecial(bool on
);
85 wxCalendarCtrl
*m_calendar
;
91 // Define a new frame type: this is going to be our main frame
92 class MyFrame
: public wxFrame
96 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
98 // event handlers (these functions should _not_ be virtual)
99 void OnQuit(wxCommandEvent
& event
);
100 void OnAbout(wxCommandEvent
& event
);
102 void OnCalMonday(wxCommandEvent
& event
);
103 void OnCalHolidays(wxCommandEvent
& event
);
104 void OnCalSpecial(wxCommandEvent
& event
);
106 void OnCalAllowMonth(wxCommandEvent
& event
);
107 void OnCalAllowYear(wxCommandEvent
& event
);
109 void OnCalSeqMonth(wxCommandEvent
& event
);
110 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
112 void OnSetDate(wxCommandEvent
& event
);
113 void OnToday(wxCommandEvent
& event
);
115 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
120 // any class wishing to process wxWindows events must use this macro
121 DECLARE_EVENT_TABLE()
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // IDs for the controls and the menu commands
132 Calendar_File_About
= 100,
134 Calendar_Cal_Monday
= 200,
135 Calendar_Cal_Holidays
,
136 Calendar_Cal_Special
,
139 Calendar_Cal_SeqMonth
,
140 Calendar_Cal_SurroundWeeks
,
141 Calendar_Cal_SetDate
,
143 Calendar_CalCtrl
= 1000
146 // ----------------------------------------------------------------------------
147 // event tables and other macros for wxWindows
148 // ----------------------------------------------------------------------------
150 // the event tables connect the wxWindows events with the functions (event
151 // handlers) which process them. It can be also done at run-time, but for the
152 // simple menu events like this the static method is much simpler.
153 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
154 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
155 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
157 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
158 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
159 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
161 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
162 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
164 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
165 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
167 EVT_MENU(Calendar_Cal_SetDate
, MyFrame::OnSetDate
)
168 EVT_MENU(Calendar_Cal_Today
, MyFrame::OnToday
)
171 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
174 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
175 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
176 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
177 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
178 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
179 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
182 // Create a new application object: this macro will allow wxWindows to create
183 // the application object during program execution (it's better than using a
184 // static object for many reasons) and also declares the accessor function
185 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
189 // ============================================================================
191 // ============================================================================
193 // ----------------------------------------------------------------------------
194 // the application class
195 // ----------------------------------------------------------------------------
197 // `Main program' equivalent: the program execution "starts" here
200 // Create the main application window
201 MyFrame
*frame
= new MyFrame(_T("Calendar wxWindows sample"),
202 wxPoint(50, 50), wxSize(450, 340));
206 // success: wxApp::OnRun() will be called which will enter the main message
207 // loop and the application will run. If we returned FALSE here, the
208 // application would exit immediately.
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
217 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
218 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
221 wxMenu
*menuFile
= new wxMenu
;
223 menuFile
->Append(Calendar_File_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
224 menuFile
->AppendSeparator();
225 menuFile
->Append(Calendar_File_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
227 wxMenu
*menuCal
= new wxMenu
;
228 menuCal
->Append(Calendar_Cal_Monday
,
229 _T("Monday &first weekday\tCtrl-F"),
230 _T("Toggle between Mon and Sun as the first week day"),
232 menuCal
->Append(Calendar_Cal_Holidays
, _T("Show &holidays\tCtrl-H"),
233 _T("Toggle highlighting the holidays"),
235 menuCal
->Append(Calendar_Cal_Special
, _T("Highlight &special dates\tCtrl-S"),
236 _T("Test custom highlighting"),
238 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
239 _T("Show s&urrounding weeks\tCtrl-W"),
240 _T("Show the neighbouring weeks in the prev/next month"),
242 menuCal
->AppendSeparator();
243 menuCal
->Append(Calendar_Cal_SeqMonth
,
244 _T("To&ggle month selector style\tCtrl-G"),
245 _T("Use another style for the calendar controls"),
247 menuCal
->Append(Calendar_Cal_Month
, _T("&Month can be changed\tCtrl-M"),
248 _T("Allow changing the month in the calendar"),
250 menuCal
->Append(Calendar_Cal_Year
, _T("&Year can be changed\tCtrl-Y"),
251 _T("Allow changing the year in the calendar"),
253 menuCal
->AppendSeparator();
254 menuCal
->Append(Calendar_Cal_SetDate
, _T("SetDate()"), _T("Set date to 2005-12-24."));
255 menuCal
->Append(Calendar_Cal_Today
, _T("Today()"), _T("Set the current date."));
257 // now append the freshly created menu to the menu bar...
258 wxMenuBar
*menuBar
= new wxMenuBar
;
259 menuBar
->Append(menuFile
, _T("&File"));
260 menuBar
->Append(menuCal
, _T("&Calendar"));
262 menuBar
->Check(Calendar_Cal_Monday
, TRUE
);
263 menuBar
->Check(Calendar_Cal_Holidays
, TRUE
);
264 menuBar
->Check(Calendar_Cal_Month
, TRUE
);
265 menuBar
->Check(Calendar_Cal_Year
, TRUE
);
267 // ... and attach this menu bar to the frame
270 m_panel
= new MyPanel(this);
273 // create a status bar just for fun (by default with 1 pane only)
275 SetStatusText(_T("Welcome to wxWindows!"));
276 #endif // wxUSE_STATUSBAR
279 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
281 // TRUE is to force the frame to close
285 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
287 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
288 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
291 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
293 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
295 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
298 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
300 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
302 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
305 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
307 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
310 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
312 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
314 m_panel
->GetCal()->EnableMonthChange(allow
);
317 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
319 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
321 m_panel
->GetCal()->EnableYearChange(allow
);
324 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
326 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
328 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
331 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
333 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
335 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
338 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
340 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
343 void MyFrame::OnSetDate(wxCommandEvent
&event
)
348 void MyFrame::OnToday(wxCommandEvent
&event
)
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 MyPanel::MyPanel(wxFrame
*frame
)
363 date
.Printf(wxT("Selected date: %s"),
364 wxDateTime::Today().FormatISODate().c_str());
365 m_date
= new wxStaticText(this, -1, date
);
366 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
371 wxCAL_SHOW_HOLIDAYS
|
374 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
375 c
->left
.SameAs(this, wxLeft
, 10);
376 c
->centreY
.SameAs(this, wxCentreY
);
380 m_date
->SetConstraints(c
);
382 c
= new wxLayoutConstraints
;
383 c
->left
.SameAs(m_date
, wxRight
, 20);
384 c
->centreY
.SameAs(this, wxCentreY
);
388 m_calendar
->SetConstraints(c
);
391 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
393 wxLogMessage(wxT("Selected %s from calendar"),
394 event
.GetDate().FormatISODate().c_str());
397 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
400 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
405 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
407 wxLogStatus(wxT("Calendar month changed"));
410 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
412 wxLogStatus(wxT("Calendar year changed"));
415 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
417 wxLogMessage(wxT("Clicked on %s"),
418 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
421 void MyPanel::ToggleCalStyle(bool on
, int flag
)
423 long style
= m_calendar
->GetWindowStyle();
429 m_calendar
->SetWindowStyle(style
);
431 m_calendar
->Refresh();
434 void MyPanel::HighlightSpecial(bool on
)
439 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
440 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
441 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
443 m_calendar
->SetAttr(17, attrRedCircle
);
444 m_calendar
->SetAttr(29, attrGreenSquare
);
445 m_calendar
->SetAttr(13, attrHeaderLike
);
449 m_calendar
->ResetAttr(17);
450 m_calendar
->ResetAttr(29);
451 m_calendar
->ResetAttr(13);
454 m_calendar
->Refresh();
457 void MyPanel::SetDate()
459 wxDateTime
date(24, wxDateTime::Dec
, 2005, 23, 59, 59);
460 m_calendar
->SetDate(date
);
463 void MyPanel::Today()
465 m_calendar
->SetDate(wxDateTime::Today());