]>
Commit | Line | Data |
---|---|---|
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 | #if defined(__GNUG__) && !defined(__APPLE__) | |
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/log.h" | |
36 | #include "wx/frame.h" | |
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" | |
42 | #endif | |
43 | ||
44 | #include "wx/sizer.h" | |
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); | |
72 | void OnCalMonthChange(wxCalendarEvent& event); | |
73 | void OnCalYearChange(wxCalendarEvent& event); | |
74 | ||
75 | wxCalendarCtrl *GetCal() const { return m_calendar; } | |
76 | ||
77 | // turn on/off the specified style bit on the calendar control | |
78 | void ToggleCalStyle(bool on, int style); | |
79 | ||
80 | void HighlightSpecial(bool on); | |
81 | ||
82 | void SetDate(); | |
83 | void Today(); | |
84 | ||
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 | ||
107 | void OnCalAllowMonth(wxCommandEvent& event); | |
108 | void OnCalAllowYear(wxCommandEvent& event); | |
109 | ||
110 | void OnCalSeqMonth(wxCommandEvent& event); | |
111 | void OnCalShowSurroundingWeeks(wxCommandEvent& event); | |
112 | ||
113 | void OnSetDate(wxCommandEvent& event); | |
114 | void OnToday(wxCommandEvent& event); | |
115 | ||
116 | void OnAllowYearUpdate(wxUpdateUIEvent& event); | |
117 | ||
118 | private: | |
119 | MyPanel *m_panel; | |
120 | ||
121 | // any class wishing to process wxWindows events must use this macro | |
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, | |
138 | Calendar_Cal_Month, | |
139 | Calendar_Cal_Year, | |
140 | Calendar_Cal_SeqMonth, | |
141 | Calendar_Cal_SurroundWeeks, | |
142 | Calendar_Cal_SetDate, | |
143 | Calendar_Cal_Today, | |
144 | Calendar_CalCtrl = 1000 | |
145 | }; | |
146 | ||
147 | // ---------------------------------------------------------------------------- | |
148 | // event tables and other macros for wxWindows | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | // the event tables connect the wxWindows events with the functions (event | |
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) | |
161 | ||
162 | EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth) | |
163 | EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear) | |
164 | ||
165 | EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth) | |
166 | EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks) | |
167 | ||
168 | EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate) | |
169 | EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday) | |
170 | ||
171 | ||
172 | EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate) | |
173 | END_EVENT_TABLE() | |
174 | ||
175 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) | |
176 | EVT_CALENDAR (Calendar_CalCtrl, MyPanel::OnCalendar) | |
177 | EVT_CALENDAR_MONTH (Calendar_CalCtrl, MyPanel::OnCalMonthChange) | |
178 | EVT_CALENDAR_YEAR (Calendar_CalCtrl, MyPanel::OnCalYearChange) | |
179 | EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange) | |
180 | EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick) | |
181 | END_EVENT_TABLE() | |
182 | ||
183 | // Create a new application object: this macro will allow wxWindows to create | |
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 | |
202 | MyFrame *frame = new MyFrame(_T("Calendar wxWindows sample"), | |
203 | wxPoint(50, 50), wxSize(450, 340)); | |
204 | ||
205 | frame->Show(true); | |
206 | ||
207 | // success: wxApp::OnRun() will be called which will enter the main message | |
208 | // loop and the application will run. If we returned FALSE here, the | |
209 | // application would exit immediately. | |
210 | return true; | |
211 | } | |
212 | ||
213 | // ---------------------------------------------------------------------------- | |
214 | // main frame | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
217 | // frame constructor | |
218 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
219 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) | |
220 | { | |
221 | // create a menu bar | |
222 | wxMenu *menuFile = new wxMenu; | |
223 | ||
224 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); | |
225 | menuFile->AppendSeparator(); | |
226 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); | |
227 | ||
228 | wxMenu *menuCal = new wxMenu; | |
229 | menuCal->Append(Calendar_Cal_Monday, | |
230 | _T("Monday &first weekday\tCtrl-F"), | |
231 | _T("Toggle between Mon and Sun as the first week day"), | |
232 | true); | |
233 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), | |
234 | _T("Toggle highlighting the holidays"), | |
235 | true); | |
236 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), | |
237 | _T("Test custom highlighting"), | |
238 | true); | |
239 | menuCal->Append(Calendar_Cal_SurroundWeeks, | |
240 | _T("Show s&urrounding weeks\tCtrl-W"), | |
241 | _T("Show the neighbouring weeks in the prev/next month"), | |
242 | true); | |
243 | menuCal->AppendSeparator(); | |
244 | menuCal->Append(Calendar_Cal_SeqMonth, | |
245 | _T("To&ggle month selector style\tCtrl-G"), | |
246 | _T("Use another style for the calendar controls"), | |
247 | true); | |
248 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), | |
249 | _T("Allow changing the month in the calendar"), | |
250 | true); | |
251 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), | |
252 | _T("Allow changing the year in the calendar"), | |
253 | true); | |
254 | menuCal->AppendSeparator(); | |
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.")); | |
257 | ||
258 | // now append the freshly created menu to the menu bar... | |
259 | wxMenuBar *menuBar = new wxMenuBar; | |
260 | menuBar->Append(menuFile, _T("&File")); | |
261 | menuBar->Append(menuCal, _T("&Calendar")); | |
262 | ||
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); | |
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) | |
275 | CreateStatusBar(2); | |
276 | SetStatusText(_T("Welcome to wxWindows!")); | |
277 | #endif // wxUSE_STATUSBAR | |
278 | } | |
279 | ||
280 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
281 | { | |
282 | // TRUE is to force the frame to close | |
283 | Close(true); | |
284 | } | |
285 | ||
286 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
287 | { | |
288 |