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