]>
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/calctrl.h" | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // private classes | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | // Define a new application type, each program should derive a class from wxApp | |
51 | class MyApp : public wxApp | |
52 | { | |
53 | public: | |
54 | // override base class virtuals | |
55 | // ---------------------------- | |
56 | ||
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(); | |
61 | }; | |
62 | ||
63 | class MyPanel : public wxPanel | |
64 | { | |
65 | public: | |
66 | MyPanel(wxFrame *frame); | |
67 | ||
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); | |
73 | ||
74 | wxCalendarCtrl *GetCal() const { return m_calendar; } | |
75 | ||
76 | // turn on/off the specified style bit on the calendar control | |
77 | void ToggleCalStyle(bool on, int style); | |
78 | ||
79 | void HighlightSpecial(bool on); | |
80 | ||
81 | private: | |
82 | wxCalendarCtrl *m_calendar; | |
83 | wxStaticText *m_date; | |
84 | ||
85 | DECLARE_EVENT_TABLE() | |
86 | }; | |
87 | ||
88 | // Define a new frame type: this is going to be our main frame | |
89 | class MyFrame : public wxFrame | |
90 | { | |
91 | public: | |
92 | // ctor(s) | |
93 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
94 | ||
95 | // event handlers (these functions should _not_ be virtual) | |
96 | void OnQuit(wxCommandEvent& event); | |
97 | void OnAbout(wxCommandEvent& event); | |
98 | ||
99 | void OnCalMonday(wxCommandEvent& event); | |
100 | void OnCalHolidays(wxCommandEvent& event); | |
101 | void OnCalSpecial(wxCommandEvent& event); | |
102 | ||
103 | void OnCalAllowMonth(wxCommandEvent& event); | |
104 | void OnCalAllowYear(wxCommandEvent& event); | |
105 | ||
106 | void OnCalSeqMonth(wxCommandEvent& event); | |
107 | void OnCalShowSurroundingWeeks(wxCommandEvent& event); | |
108 | ||
109 | void OnAllowYearUpdate(wxUpdateUIEvent& event); | |
110 | ||
111 | private: | |
112 | MyPanel *m_panel; | |
113 | ||
114 | // any class wishing to process wxWindows events must use this macro | |
115 | DECLARE_EVENT_TABLE() | |
116 | }; | |
117 | ||
118 | // ---------------------------------------------------------------------------- | |
119 | // constants | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
122 | // IDs for the controls and the menu commands | |
123 | enum | |
124 | { | |
125 | // menu items | |
126 | Calendar_File_About = 100, | |
127 | Calendar_File_Quit, | |
128 | Calendar_Cal_Monday = 200, | |
129 | Calendar_Cal_Holidays, | |
130 | Calendar_Cal_Special, | |
131 | Calendar_Cal_Month, | |
132 | Calendar_Cal_Year, | |
133 | Calendar_Cal_SeqMonth, | |
134 | Calendar_Cal_SurroundWeeks, | |
135 | Calendar_CalCtrl = 1000 | |
136 | }; | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // event tables and other macros for wxWindows | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
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) | |
148 | ||
149 | EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday) | |
150 | EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays) | |
151 | EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial) | |
152 | ||
153 | EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth) | |
154 | EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear) | |
155 | ||
156 | EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth) | |
157 | EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks) | |
158 | ||
159 | EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate) | |
160 | END_EVENT_TABLE() | |
161 | ||
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) | |
168 | END_EVENT_TABLE() | |
169 | ||
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 | |
174 | // not wxApp) | |
175 | IMPLEMENT_APP(MyApp) | |
176 | ||
177 | // ============================================================================ | |
178 | // implementation | |
179 | // ============================================================================ | |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // the application class | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | // `Main program' equivalent: the program execution "starts" here | |
186 | bool MyApp::OnInit() | |
187 | { | |
188 | // Create the main application window | |
189 | MyFrame *frame = new MyFrame(_T("Calendar wxWindows sample"), | |
190 | wxPoint(50, 50), wxSize(450, 340)); | |
191 | ||
192 | frame->Show(TRUE); | |
193 | ||
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. | |
197 | return TRUE; | |
198 | } | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // main frame | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | // frame constructor | |
205 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
206 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
207 | { | |
208 | // create a menu bar | |
209 | wxMenu *menuFile = new wxMenu; | |
210 | ||
211 | menuFile->Append(Calendar_File_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); | |
212 | menuFile->AppendSeparator(); | |
213 | menuFile->Append(Calendar_File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); | |
214 | ||
215 | wxMenu *menuCal = new wxMenu; | |
216 | menuCal->Append(Calendar_Cal_Monday, | |
217 | _T("Monday &first weekday\tCtrl-F"), | |
218 | _T("Toggle between Mon and Sun as the first week day"), | |
219 | TRUE); | |
220 | menuCal->Append(Calendar_Cal_Holidays, _T("Show &holidays\tCtrl-H"), | |
221 | _T("Toggle highlighting the holidays"), | |
222 | TRUE); | |
223 | menuCal->Append(Calendar_Cal_Special, _T("Highlight &special dates\tCtrl-S"), | |
224 | _T("Test custom highlighting"), | |
225 | TRUE); | |
226 | menuCal->Append(Calendar_Cal_SurroundWeeks, | |
227 | _T("Show s&urrounding weeks\tCtrl-W"), | |
228 | _T("Show the neighbouring weeks in the prev/next month"), | |
229 | TRUE); | |
230 | menuCal->AppendSeparator(); | |
231 | menuCal->Append(Calendar_Cal_SeqMonth, | |
232 | _T("To&ggle month selector style\tCtrl-G"), | |
233 | _T("Use another style for the calendar controls"), | |
234 | TRUE); | |
235 | menuCal->Append(Calendar_Cal_Month, _T("&Month can be changed\tCtrl-M"), | |
236 | _T("Allow changing the month in the calendar"), | |
237 | TRUE); | |
238 | menuCal->Append(Calendar_Cal_Year, _T("&Year can be changed\tCtrl-Y"), | |
239 | _T("Allow changing the year in the calendar"), | |
240 | TRUE); | |
241 | ||
242 | // now append the freshly created menu to the menu bar... | |
243 | wxMenuBar *menuBar = new wxMenuBar; | |
244 | menuBar->Append(menuFile, _T("&File")); | |
245 | menuBar->Append(menuCal, _T("&Calendar")); | |
246 | ||
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); | |
251 | ||
252 | // ... and attach this menu bar to the frame | |
253 | SetMenuBar(menuBar); | |
254 | ||
255 | m_panel = new MyPanel(this); | |
256 | ||
257 | #if wxUSE_STATUSBAR | |
258 | // create a status bar just for fun (by default with 1 pane only) | |
259 | CreateStatusBar(2); | |
260 | SetStatusText(_T("Welcome to wxWindows!")); | |
261 | #endif // wxUSE_STATUSBAR | |
262 | } | |
263 | ||
264 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
265 | { | |
266 | // TRUE is to force the frame to close | |
267 | Close(TRUE); | |
268 | } | |
269 | ||
270 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
271 | { | |
272 |