]>
Commit | Line | Data |
---|---|---|
74a533f7 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: calendar.cpp | |
3 | // Purpose: wxCalendarCtrl sample | |
4 | // Author: Vadim Zeitlin | |
4e6bceff | 5 | // Modified by: |
74a533f7 VZ |
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 | ||
788233da | 20 | #if defined(__GNUG__) && !defined(__APPLE__) |
74a533f7 VZ |
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 | ||
273b7ed9 | 32 | // for all others, include the necessary headers |
74a533f7 VZ |
33 | #ifndef WX_PRECOMP |
34 | #include "wx/app.h" | |
788233da | 35 | #include "wx/log.h" |
74a533f7 | 36 | #include "wx/frame.h" |
273b7ed9 VZ |
37 | #include "wx/panel.h" |
38 | #include "wx/stattext.h" | |
39 | #include "wx/menu.h" | |
40 | #include "wx/layout.h" | |
41 | #include "wx/msgdlg.h" | |
74a533f7 VZ |
42 | #endif |
43 | ||
9230b621 | 44 | #include "wx/sizer.h" |
74a533f7 VZ |
45 | #include "wx/calctrl.h" |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // private classes | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // Define a new application type, each program should derive a class from wxApp | |
52 | class MyApp : public wxApp | |
53 | { | |
54 | public: | |
55 | // override base class virtuals | |
56 | // ---------------------------- | |
57 | ||
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(); | |
62 | }; | |
63 | ||
64 | class MyPanel : public wxPanel | |
65 | { | |
66 | public: | |
67 | MyPanel(wxFrame *frame); | |
68 | ||
69 | void OnCalendar(wxCalendarEvent& event); | |
70 | void OnCalendarWeekDayClick(wxCalendarEvent& event); | |
71 | void OnCalendarChange(wxCalendarEvent& event); | |
0de868d9 VZ |
72 | void OnCalMonthChange(wxCalendarEvent& event); |
73 | void OnCalYearChange(wxCalendarEvent& event); | |
74a533f7 | 74 | |
bc385ba9 VZ |
75 | wxCalendarCtrl *GetCal() const { return m_calendar; } |
76 | ||
37df1f33 VZ |
77 | // turn on/off the specified style bit on the calendar control |
78 | void ToggleCalStyle(bool on, int style); | |
79 | ||
74a533f7 VZ |
80 | void HighlightSpecial(bool on); |
81 | ||
605dfd91 JS |
82 | void SetDate(); |
83 | void Today(); | |
84 | ||
74a533f7 VZ |
85 | private: |
86 | wxCalendarCtrl *m_calendar; | |
87 | wxStaticText *m_date; | |
88 | ||
89 | DECLARE_EVENT_TABLE() | |
90 | }; | |
91 | ||
92 | // Define a new frame type: this is going to be our main frame | |
93 | class MyFrame : public wxFrame | |
94 | { | |
95 | public: | |
96 | // ctor(s) | |
97 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
98 | ||
99 | // event handlers (these functions should _not_ be virtual) | |
100 | void OnQuit(wxCommandEvent& event); | |
101 | void OnAbout(wxCommandEvent& event); | |
102 | ||
103 | void OnCalMonday(wxCommandEvent& event); | |
104 | void OnCalHolidays(wxCommandEvent& event); | |
105 | void OnCalSpecial(wxCommandEvent& event); | |
106 | ||
bc385ba9 VZ |
107 | void OnCalAllowMonth(wxCommandEvent& event); |
108 | void OnCalAllowYear(wxCommandEvent& event); | |
109 | ||
37df1f33 VZ |
110 | void OnCalSeqMonth(wxCommandEvent& event); |
111 | void OnCalShowSurroundingWeeks(wxCommandEvent& event); | |
112 | ||
605dfd91 JS |
113 | void OnSetDate(wxCommandEvent& event); |
114 | void OnToday(wxCommandEvent& event); | |
115 | ||
bc385ba9 VZ |
116 | void OnAllowYearUpdate(wxUpdateUIEvent& event); |
117 | ||
74a533f7 VZ |
118 | private: |
119 | MyPanel *m_panel; | |
120 | ||
be5a51fb | 121 | // any class wishing to process wxWidgets events must use this macro |
74a533f7 VZ |
122 | DECLARE_EVENT_TABLE() |
123 | }; | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // constants | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
129 | // IDs for the controls and the menu commands | |
130 | enum | |
131 | { | |
132 | // menu items | |
133 | Calendar_File_About = 100, | |
134 | Calendar_File_Quit, | |
135 | Calendar_Cal_Monday = 200, | |
136 | Calendar_Cal_Holidays, | |
137 | Calendar_Cal_Special, | |
bc385ba9 VZ |
138 | Calendar_Cal_Month, |
139 | Calendar_Cal_Year, | |
37df1f33 VZ |
140 | Calendar_Cal_SeqMonth, |
141 | Calendar_Cal_SurroundWeeks, | |
605dfd91 JS |
142 | Calendar_Cal_SetDate, |
143 | Calendar_Cal_Today, | |
e9561b3b | 144 | Calendar_CalCtrl = 1000 |
74a533f7 VZ |
145 | }; |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
be5a51fb | 148 | // event tables and other macros for wxWidgets |
74a533f7 VZ |
149 | // ---------------------------------------------------------------------------- |
150 | ||
be5a51fb | 151 | // the event tables connect the wxWidgets events with the functions (event |
74a533f7 VZ |
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) | |
157 | ||
158 | EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday) | |
159 | EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays) | |
160 | EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial) | |
bc385ba9 VZ |
161 | |
162 | EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth) | |
163 | EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear) | |
164 | ||
37df1f33 VZ |
165 | EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth) |
166 | EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks) | |
167 | ||
605dfd91 JS |
168 | EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate) |
169 | EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday) | |
170 | ||
171 | ||
bc385ba9 | 172 | EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate) |
74a533f7 VZ |
173 | END_EVENT_TABLE() |
174 | ||
175 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
176 | EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar) | |
0de868d9 VZ |
177 | EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange) |
178 | EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange) | |
74a533f7 VZ |
179 | EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange) |
180 | EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick) | |
181 | END_EVENT_TABLE() | |
182 | ||
be5a51fb | 183 | // Create a new application object: this macro will allow wxWidgets to create |
74a533f7 VZ |
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 | |
187 | // not wxApp) | |
188 | IMPLEMENT_APP(MyApp) | |
189 | ||
190 | // ============================================================================ | |
191 | // implementation | |
192 | // ============================================================================ | |
193 | ||
194 | // ---------------------------------------------------------------------------- | |
195 | // the application class | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | // `Main program' equivalent: the program execution "starts" here | |
199 | bool MyApp::OnInit() | |
200 | { | |
201 | // Create the main application window | |
be5a51fb | 202 | MyFrame *frame = new MyFrame(_T("Calendar wxWidgets sample"), |
4e6bceff | 203 | wxPoint(50, 50), wxSize(450, 340)); |
74a533f7 | 204 | |
9230b621 | 205 | frame->Show(true); |
74a533f7 VZ |
206 | |
207 | // success: wxApp::OnRun() will be called which will enter the main message | |
5014bb3a | 208 | // loop and the application will run. If we returned false here, the |
74a533f7 | 209 | // application would exit immediately. |
9230b621 | 210 | return true; |
74a533f7 VZ |
211 | } |
212 | ||
213 | // ---------------------------------------------------------------------------- | |
214 | // main frame | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
217 | // frame constructor | |
218 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
9230b621 | 219 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
74a533f7 VZ |
220 | { |
221 | // create a menu bar | |
222 | wxMenu *menuFile = new wxMenu; | |
223 | ||
9f84eccd | 224 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
74a533f7 | 225 | menuFile->AppendSeparator(); |
9f84eccd | 226 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
74a533f7 VZ |
227 | |
228 | wxMenu *menuCal = new wxMenu; | |
229 | menuCal->Append(Calendar_Cal_Monday, | |
9f84eccd MB |
230 | _T("Monday &first weekday\tCtrl-F"), |
231 | _T("Toggle between Mon and Sun as the first week day"), | |
9230b621 | 232 | true); |
9f84eccd MB |
233 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), |
234 | _T("Toggle highlighting the holidays"), | |
9230b621 | 235 | true); |
9f84eccd MB |
236 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), |
237 | _T("Test custom highlighting"), | |
9230b621 | 238 | true); |
37df1f33 | 239 | menuCal->Append(Calendar_Cal_SurroundWeeks, |
9f84eccd MB |
240 | _T("Show s&urrounding weeks\tCtrl-W"), |
241 | _T("Show the neighbouring weeks in the prev/next month"), | |
9230b621 | 242 | true); |
bc385ba9 | 243 | menuCal->AppendSeparator(); |
37df1f33 | 244 | menuCal->Append(Calendar_Cal_SeqMonth, |
9f84eccd MB |
245 | _T("To&ggle month selector style\tCtrl-G"), |
246 | _T("Use another style for the calendar controls"), | |
9230b621 | 247 | true); |
9f84eccd MB |
248 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), |
249 | _T("Allow changing the month in the calendar"), | |
9230b621 | 250 | true); |
9f84eccd MB |
251 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), |
252 | _T("Allow changing the year in the calendar"), | |
9230b621 | 253 | true); |
605dfd91 | 254 | menuCal->AppendSeparator(); |
529b7f71 JS |
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.")); | |
74a533f7 VZ |
257 | |
258 | // now append the freshly created menu to the menu bar... | |
259 | wxMenuBar *menuBar = new wxMenuBar; | |
9f84eccd MB |
260 | menuBar->Append(menuFile, _T("&File")); |
261 | menuBar->Append(menuCal, _T("&Calendar")); | |
74a533f7 | 262 | |
9230b621 VS |
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); | |
74a533f7 VZ |
267 | |
268 | // ... and attach this menu bar to the frame | |
269 | SetMenuBar(menuBar); | |
270 | ||
271 | m_panel = new MyPanel(this); | |
272 | ||
273 | #if wxUSE_STATUSBAR | |
274 | // create a status bar just for fun (by default with 1 pane only) | |
9898fcda | 275 | CreateStatusBar(2); |
be5a51fb | 276 | SetStatusText(_T("Welcome to wxWidgets!")); |
74a533f7 VZ |
277 | #endif // wxUSE_STATUSBAR |
278 | } | |
279 | ||
280 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
281 | { | |
5014bb3a | 282 | // true is to force the frame to close |
9230b621 | 283 | Close(true); |
74a533f7 VZ |
284 | } |
285 | ||
286 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
287 | { | |
288 |