]> git.saurik.com Git - wxWidgets.git/blob - samples/calendar/calendar.cpp
moved wxCalendarCtrl code from control sample to a new one - calendar
[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 #ifdef __GNUG__
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
34 #ifndef WX_PRECOMP
35 #include "wx/app.h"
36 #include "wx/frame.h"
37 #endif
38
39 #include "wx/calctrl.h"
40
41 // ----------------------------------------------------------------------------
42 // private classes
43 // ----------------------------------------------------------------------------
44
45 // Define a new application type, each program should derive a class from wxApp
46 class MyApp : public wxApp
47 {
48 public:
49 // override base class virtuals
50 // ----------------------------
51
52 // this one is called on application startup and is a good place for the app
53 // initialization (doing it here and not in the ctor allows to have an error
54 // return: if OnInit() returns false, the application terminates)
55 virtual bool OnInit();
56 };
57
58 class MyPanel : public wxPanel
59 {
60 public:
61 MyPanel(wxFrame *frame);
62
63 void OnCalendar(wxCalendarEvent& event);
64 void OnCalendarWeekDayClick(wxCalendarEvent& event);
65 void OnCalendarChange(wxCalendarEvent& event);
66
67 void StartWithMonday(bool on);
68 void ShowHolidays(bool on);
69 void HighlightSpecial(bool on);
70
71 private:
72 wxCalendarCtrl *m_calendar;
73 wxStaticText *m_date;
74
75 DECLARE_EVENT_TABLE()
76 };
77
78 // Define a new frame type: this is going to be our main frame
79 class MyFrame : public wxFrame
80 {
81 public:
82 // ctor(s)
83 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
84
85 // event handlers (these functions should _not_ be virtual)
86 void OnQuit(wxCommandEvent& event);
87 void OnAbout(wxCommandEvent& event);
88
89 void OnCalMonday(wxCommandEvent& event);
90 void OnCalHolidays(wxCommandEvent& event);
91 void OnCalSpecial(wxCommandEvent& event);
92
93 private:
94 MyPanel *m_panel;
95
96 // any class wishing to process wxWindows events must use this macro
97 DECLARE_EVENT_TABLE()
98 };
99
100 // ----------------------------------------------------------------------------
101 // constants
102 // ----------------------------------------------------------------------------
103
104 // IDs for the controls and the menu commands
105 enum
106 {
107 // menu items
108 Calendar_File_About = 100,
109 Calendar_File_Quit,
110 Calendar_Cal_Monday = 200,
111 Calendar_Cal_Holidays,
112 Calendar_Cal_Special,
113 Calendar_CalCtrl = 1000,
114 };
115
116 // ----------------------------------------------------------------------------
117 // event tables and other macros for wxWindows
118 // ----------------------------------------------------------------------------
119
120 // the event tables connect the wxWindows events with the functions (event
121 // handlers) which process them. It can be also done at run-time, but for the
122 // simple menu events like this the static method is much simpler.
123 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
124 EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
125 EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
126
127 EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
128 EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
129 EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
130 END_EVENT_TABLE()
131
132 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
133 EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar)
134 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
135 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
136 END_EVENT_TABLE()
137
138 // Create a new application object: this macro will allow wxWindows to create
139 // the application object during program execution (it's better than using a
140 // static object for many reasons) and also declares the accessor function
141 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
142 // not wxApp)
143 IMPLEMENT_APP(MyApp)
144
145 // ============================================================================
146 // implementation
147 // ============================================================================
148
149 // ----------------------------------------------------------------------------
150 // the application class
151 // ----------------------------------------------------------------------------
152
153 // `Main program' equivalent: the program execution "starts" here
154 bool MyApp::OnInit()
155 {
156 // Create the main application window
157 MyFrame *frame = new MyFrame("Minimal wxWindows App",
158 wxPoint(50, 50), wxSize(450, 340));
159
160 // Show it and tell the application that it's our main window
161 // @@@ what does it do exactly, in fact? is it necessary here?
162 frame->Show(TRUE);
163 SetTopWindow(frame);
164
165 // success: wxApp::OnRun() will be called which will enter the main message
166 // loop and the application will run. If we returned FALSE here, the
167 // application would exit immediately.
168 return TRUE;
169 }
170
171 // ----------------------------------------------------------------------------
172 // main frame
173 // ----------------------------------------------------------------------------
174
175 // frame constructor
176 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
177 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
178 {
179 // create a menu bar
180 wxMenu *menuFile = new wxMenu;
181
182 menuFile->Append(Calendar_File_About, "&About...\tCtrl-A", "Show about dialog");
183 menuFile->AppendSeparator();
184 menuFile->Append(Calendar_File_Quit, "E&xit\tAlt-X", "Quit this program");
185
186 wxMenu *menuCal = new wxMenu;
187 menuCal->Append(Calendar_Cal_Monday,
188 "&Monday first weekday\tCtrl-M",
189 "Toggle between Mon and Sun as the first week day",
190 TRUE);
191 menuCal->Append(Calendar_Cal_Holidays, "Show &holidays\tCtrl-H",
192 "Toggle highlighting the holidays",
193 TRUE);
194 menuCal->Append(Calendar_Cal_Special, "Highlight &special dates\tCtrl-S",
195 "Test custom highlighting",
196 TRUE);
197
198 // now append the freshly created menu to the menu bar...
199 wxMenuBar *menuBar = new wxMenuBar;
200 menuBar->Append(menuFile, "&File");
201 menuBar->Append(menuCal, "&Calendar");
202
203 menuBar->Check(Calendar_Cal_Monday, TRUE);
204 menuBar->Check(Calendar_Cal_Holidays, TRUE);
205
206 // ... and attach this menu bar to the frame
207 SetMenuBar(menuBar);
208
209 m_panel = new MyPanel(this);
210
211 #if wxUSE_STATUSBAR
212 // create a status bar just for fun (by default with 1 pane only)
213 CreateStatusBar(2);
214 SetStatusText("Welcome to wxWindows!");
215 #endif // wxUSE_STATUSBAR
216 }
217
218 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
219 {
220 // TRUE is to force the frame to close
221 Close(TRUE);
222 }
223
224 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
225 {
226 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
227 _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
228 }
229
230 void MyFrame::OnCalMonday(wxCommandEvent& event)
231 {
232 m_panel->StartWithMonday(GetMenuBar()->IsChecked(event.GetInt()) != 0);
233 }
234
235 void MyFrame::OnCalHolidays(wxCommandEvent& event)
236 {
237 m_panel->ShowHolidays(GetMenuBar()->IsChecked(event.GetInt()) != 0);
238 }
239
240 void MyFrame::OnCalSpecial(wxCommandEvent& event)
241 {
242 m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetInt()) != 0);
243 }
244
245 // ----------------------------------------------------------------------------
246 // MyPanel
247 // ----------------------------------------------------------------------------
248
249 MyPanel::MyPanel(wxFrame *frame)
250 : wxPanel(frame, -1)
251 {
252 SetAutoLayout(TRUE);
253
254 wxString date;
255 date.Printf("Selected date: %s",
256 wxDateTime::Today().FormatISODate().c_str());
257 m_date = new wxStaticText(this, -1, date);
258 m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
259 wxDefaultDateTime,
260 wxDefaultPosition,
261 wxDefaultSize,
262 wxCAL_MONDAY_FIRST | wxCAL_SHOW_HOLIDAYS);
263
264 wxLayoutConstraints *c = new wxLayoutConstraints;
265 c->left.SameAs(this, wxLeft, 10);
266 c->centreY.SameAs(m_calendar, wxCentreY);
267 c->height.AsIs();
268 c->width.AsIs();
269
270 m_date->SetConstraints(c);
271
272 c = new wxLayoutConstraints;
273 c->left.SameAs(m_date, wxRight, 10);
274 c->top.SameAs(this, wxTop, 10);
275 c->height.AsIs();
276 c->width.AsIs();
277
278 m_calendar->SetConstraints(c);
279 }
280
281 void MyPanel::OnCalendar(wxCalendarEvent& event)
282 {
283 wxLogMessage("Selected %s from calendar",
284 event.GetDate().FormatISODate().c_str());
285 }
286
287 void MyPanel::OnCalendarChange(wxCalendarEvent& event)
288 {
289 wxString s;
290 s.Printf("Selected date: %s", event.GetDate().FormatISODate().c_str());
291
292 m_date->SetLabel(s);
293 }
294
295 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
296 {
297 wxLogMessage("Clicked on %s",
298 wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
299 }
300
301 void MyPanel::StartWithMonday(bool on)
302 {
303 long style = m_calendar->GetWindowStyle();
304 if ( on )
305 style |= wxCAL_MONDAY_FIRST;
306 else
307 style &= ~wxCAL_MONDAY_FIRST;
308
309 m_calendar->SetWindowStyle(style);
310
311 m_calendar->Refresh();
312 }
313
314 void MyPanel::ShowHolidays(bool on)
315 {
316 m_calendar->EnableHolidayDisplay(on);
317 }
318
319 void MyPanel::HighlightSpecial(bool on)
320 {
321 if ( on )
322 {
323 wxCalendarDateAttr
324 *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
325 *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
326 *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
327
328 m_calendar->SetAttr(17, attrRedCircle);
329 m_calendar->SetAttr(29, attrGreenSquare);
330 m_calendar->SetAttr(13, attrHeaderLike);
331 }
332 else // off
333 {
334 m_calendar->ResetAttr(17);
335 m_calendar->ResetAttr(29);
336 m_calendar->ResetAttr(13);
337 }
338
339 m_calendar->Refresh();
340 }
341