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
);
82 wxCalendarCtrl
*m_calendar
;
88 // Define a new frame type: this is going to be our main frame
89 class MyFrame
: public wxFrame
93 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
95 // event handlers (these functions should _not_ be virtual)
96 void OnQuit(wxCommandEvent
& event
);
97 void OnAbout(wxCommandEvent
& event
);
99 void OnCalMonday(wxCommandEvent
& event
);
100 void OnCalHolidays(wxCommandEvent
& event
);
101 void OnCalSpecial(wxCommandEvent
& event
);
103 void OnCalAllowMonth(wxCommandEvent
& event
);
104 void OnCalAllowYear(wxCommandEvent
& event
);
106 void OnCalSeqMonth(wxCommandEvent
& event
);
107 void OnCalShowSurroundingWeeks(wxCommandEvent
& event
);
109 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
114 // any class wishing to process wxWindows events must use this macro
115 DECLARE_EVENT_TABLE()
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // IDs for the controls and the menu commands
126 Calendar_File_About
= 100,
128 Calendar_Cal_Monday
= 200,
129 Calendar_Cal_Holidays
,
130 Calendar_Cal_Special
,
133 Calendar_Cal_SeqMonth
,
134 Calendar_Cal_SurroundWeeks
,
135 Calendar_CalCtrl
= 1000,
138 // ----------------------------------------------------------------------------
139 // event tables and other macros for wxWindows
140 // ----------------------------------------------------------------------------
142 // the event tables connect the wxWindows events with the functions (event
143 // handlers) which process them. It can be also done at run-time, but for the
144 // simple menu events like this the static method is much simpler.
145 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
146 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
147 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
149 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
150 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
151 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
153 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
154 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
156 EVT_MENU(Calendar_Cal_SeqMonth
, MyFrame::OnCalSeqMonth
)
157 EVT_MENU(Calendar_Cal_SurroundWeeks
, MyFrame::OnCalShowSurroundingWeeks
)
159 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
162 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
163 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
164 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
165 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
166 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
167 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
170 // Create a new application object: this macro will allow wxWindows to create
171 // the application object during program execution (it's better than using a
172 // static object for many reasons) and also declares the accessor function
173 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
177 // ============================================================================
179 // ============================================================================
181 // ----------------------------------------------------------------------------
182 // the application class
183 // ----------------------------------------------------------------------------
185 // `Main program' equivalent: the program execution "starts" here
188 // Create the main application window
189 MyFrame
*frame
= new MyFrame("Calendar wxWindows sample",
190 wxPoint(50, 50), wxSize(450, 340));
194 // success: wxApp::OnRun() will be called which will enter the main message
195 // loop and the application will run. If we returned FALSE here, the
196 // application would exit immediately.
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
205 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
206 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
209 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(Calendar_File_About
, "&About...\tCtrl-A", "Show about dialog");
212 menuFile
->AppendSeparator();
213 menuFile
->Append(Calendar_File_Quit
, "E&xit\tAlt-X", "Quit this program");
215 wxMenu
*menuCal
= new wxMenu
;
216 menuCal
->Append(Calendar_Cal_Monday
,
217 "Monday &first weekday\tCtrl-F",
218 "Toggle between Mon and Sun as the first week day",
220 menuCal
->Append(Calendar_Cal_Holidays
, "Show &holidays\tCtrl-H",
221 "Toggle highlighting the holidays",
223 menuCal
->Append(Calendar_Cal_Special
, "Highlight &special dates\tCtrl-S",
224 "Test custom highlighting",
226 menuCal
->Append(Calendar_Cal_SurroundWeeks
,
227 "Show s&urrounding weeks\tCtrl-W",
228 "Show the neighbouring weeks in the prev/next month",
230 menuCal
->AppendSeparator();
231 menuCal
->Append(Calendar_Cal_SeqMonth
,
232 "To&ggle month selector style\tCtrl-G",
233 "Use another style for the calendar controls",
235 menuCal
->Append(Calendar_Cal_Month
, "&Month can be changed\tCtrl-M",
236 "Allow changing the month in the calendar",
238 menuCal
->Append(Calendar_Cal_Year
, "&Year can be changed\tCtrl-Y",
239 "Allow changing the year in the calendar",
242 // now append the freshly created menu to the menu bar...
243 wxMenuBar
*menuBar
= new wxMenuBar
;
244 menuBar
->Append(menuFile
, "&File");
245 menuBar
->Append(menuCal
, "&Calendar");
247 menuBar
->Check(Calendar_Cal_Monday
, TRUE
);
248 menuBar
->Check(Calendar_Cal_Holidays
, TRUE
);
249 menuBar
->Check(Calendar_Cal_Month
, TRUE
);
250 menuBar
->Check(Calendar_Cal_Year
, TRUE
);
252 // ... and attach this menu bar to the frame
255 m_panel
= new MyPanel(this);
258 // create a status bar just for fun (by default with 1 pane only)
260 SetStatusText("Welcome to wxWindows!");
261 #endif // wxUSE_STATUSBAR
264 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
266 // TRUE is to force the frame to close
270 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
272 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
273 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
276 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
278 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
280 m_panel
->ToggleCalStyle(enable
, wxCAL_MONDAY_FIRST
);
283 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
285 bool enable
= GetMenuBar()->IsChecked(event
.GetId());
287 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
290 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
292 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetId()));
295 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
297 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
299 m_panel
->GetCal()->EnableMonthChange(allow
);
302 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
304 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
306 m_panel
->GetCal()->EnableYearChange(allow
);
309 void MyFrame::OnCalSeqMonth(wxCommandEvent
& event
)
311 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
313 m_panel
->ToggleCalStyle(allow
, wxCAL_SEQUENTIAL_MONTH_SELECTION
);
316 void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent
& event
)
318 bool allow
= GetMenuBar()->IsChecked(event
.GetId());
320 m_panel
->ToggleCalStyle(allow
, wxCAL_SHOW_SURROUNDING_WEEKS
);
323 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
325 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 MyPanel::MyPanel(wxFrame
*frame
)
338 date
.Printf(wxT("Selected date: %s"),
339 wxDateTime::Today().FormatISODate().c_str());
340 m_date
= new wxStaticText(this, -1, date
);
341 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
346 wxCAL_SHOW_HOLIDAYS
|
349 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
350 c
->left
.SameAs(this, wxLeft
, 10);
351 c
->centreY
.SameAs(this, wxCentreY
);
355 m_date
->SetConstraints(c
);
357 c
= new wxLayoutConstraints
;
358 c
->left
.SameAs(m_date
, wxRight
, 20);
359 c
->centreY
.SameAs(this, wxCentreY
);
363 m_calendar
->SetConstraints(c
);
366 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
368 wxLogMessage(wxT("Selected %s from calendar"),
369 event
.GetDate().FormatISODate().c_str());
372 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
375 s
.Printf(wxT("Selected date: %s"), event
.GetDate().FormatISODate().c_str());
380 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
382 wxLogStatus(wxT("Calendar month changed"));
385 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
387 wxLogStatus(wxT("Calendar year changed"));
390 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
392 wxLogMessage(wxT("Clicked on %s"),
393 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
396 void MyPanel::ToggleCalStyle(bool on
, int flag
)
398 long style
= m_calendar
->GetWindowStyle();
404 m_calendar
->SetWindowStyle(style
);
406 m_calendar
->Refresh();
409 void MyPanel::HighlightSpecial(bool on
)
414 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
415 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
416 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
418 m_calendar
->SetAttr(17, attrRedCircle
);
419 m_calendar
->SetAttr(29, attrGreenSquare
);
420 m_calendar
->SetAttr(13, attrHeaderLike
);
424 m_calendar
->ResetAttr(17);
425 m_calendar
->ResetAttr(29);
426 m_calendar
->ResetAttr(13);
429 m_calendar
->Refresh();