]>
git.saurik.com Git - wxWidgets.git/blob - samples/calendar/calendar.cpp
64fa307a45030400ee2778ec14c748d0496d4cdb
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCalendarCtrl sample
4 // Author: Vadim Zeitlin
5 // Modified by: Mark Johnson : Added wxLayoutConstraints support for Ctrl
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "calendar.cpp"
22 #pragma interface "calendar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers
37 #include "wx/stattext.h"
39 #include "wx/layout.h"
40 #include "wx/msgdlg.h"
43 #include "wx/calctrl.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp
: public wxApp
53 // override base class virtuals
54 // ----------------------------
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();
62 class MyPanel
: public wxPanel
65 MyPanel(wxFrame
*frame
);
67 void OnCalendar(wxCalendarEvent
& event
);
68 void OnCalendarWeekDayClick(wxCalendarEvent
& event
);
69 void OnCalendarChange(wxCalendarEvent
& event
);
70 void OnCalMonthChange(wxCalendarEvent
& event
);
71 void OnCalYearChange(wxCalendarEvent
& event
);
73 wxCalendarCtrl
*GetCal() const { return m_calendar
; }
75 void StartWithMonday(bool on
);
76 void HighlightSpecial(bool on
);
79 wxCalendarCtrl
*m_calendar
;
85 // Define a new frame type: this is going to be our main frame
86 class MyFrame
: public wxFrame
90 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
92 // event handlers (these functions should _not_ be virtual)
93 void OnQuit(wxCommandEvent
& event
);
94 void OnAbout(wxCommandEvent
& event
);
96 void OnCalMonday(wxCommandEvent
& event
);
97 void OnCalHolidays(wxCommandEvent
& event
);
98 void OnCalSpecial(wxCommandEvent
& event
);
100 void OnCalAllowMonth(wxCommandEvent
& event
);
101 void OnCalAllowYear(wxCommandEvent
& event
);
103 void OnAllowYearUpdate(wxUpdateUIEvent
& event
);
108 // any class wishing to process wxWindows events must use this macro
109 DECLARE_EVENT_TABLE()
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // IDs for the controls and the menu commands
120 Calendar_File_About
= 100,
122 Calendar_Cal_Monday
= 200,
123 Calendar_Cal_Holidays
,
124 Calendar_Cal_Special
,
127 Calendar_CalCtrl
= 1000,
130 // ----------------------------------------------------------------------------
131 // event tables and other macros for wxWindows
132 // ----------------------------------------------------------------------------
134 // the event tables connect the wxWindows events with the functions (event
135 // handlers) which process them. It can be also done at run-time, but for the
136 // simple menu events like this the static method is much simpler.
137 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
138 EVT_MENU(Calendar_File_Quit
, MyFrame::OnQuit
)
139 EVT_MENU(Calendar_File_About
, MyFrame::OnAbout
)
141 EVT_MENU(Calendar_Cal_Monday
, MyFrame::OnCalMonday
)
142 EVT_MENU(Calendar_Cal_Holidays
, MyFrame::OnCalHolidays
)
143 EVT_MENU(Calendar_Cal_Special
, MyFrame::OnCalSpecial
)
145 EVT_MENU(Calendar_Cal_Month
, MyFrame::OnCalAllowMonth
)
146 EVT_MENU(Calendar_Cal_Year
, MyFrame::OnCalAllowYear
)
148 EVT_UPDATE_UI(Calendar_Cal_Year
, MyFrame::OnAllowYearUpdate
)
151 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
152 EVT_CALENDAR (Calendar_CalCtrl
, MyPanel::OnCalendar
)
153 EVT_CALENDAR_MONTH (Calendar_CalCtrl
, MyPanel::OnCalMonthChange
)
154 EVT_CALENDAR_YEAR (Calendar_CalCtrl
, MyPanel::OnCalYearChange
)
155 EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl
, MyPanel::OnCalendarChange
)
156 EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl
, MyPanel::OnCalendarWeekDayClick
)
159 // Create a new application object: this macro will allow wxWindows to create
160 // the application object during program execution (it's better than using a
161 // static object for many reasons) and also declares the accessor function
162 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
171 // the application class
172 // ----------------------------------------------------------------------------
174 // `Main program' equivalent: the program execution "starts" here
177 // Create the main application window
178 MyFrame
*frame
= new MyFrame("Calendar wxWindows sample",
179 wxPoint(50, 50), wxSize(450, 375));
181 // Show it and tell the application that it's our main window
182 // @@@ what does it do exactly, in fact? is it necessary here?
186 // success: wxApp::OnRun() will be called which will enter the main message
187 // loop and the application will run. If we returned FALSE here, the
188 // application would exit immediately.
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
197 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
198 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
201 wxMenu
*menuFile
= new wxMenu
;
203 menuFile
->Append(Calendar_File_About
, "&About...\tCtrl-A", "Show about dialog");
204 menuFile
->AppendSeparator();
205 menuFile
->Append(Calendar_File_Quit
, "E&xit\tAlt-X", "Quit this program");
207 wxMenu
*menuCal
= new wxMenu
;
208 menuCal
->Append(Calendar_Cal_Monday
,
209 "Monday &first weekday\tCtrl-F",
210 "Toggle between Mon and Sun as the first week day",
212 menuCal
->Append(Calendar_Cal_Holidays
, "Show &holidays\tCtrl-H",
213 "Toggle highlighting the holidays",
215 menuCal
->Append(Calendar_Cal_Special
, "Highlight &special dates\tCtrl-S",
216 "Test custom highlighting",
218 menuCal
->AppendSeparator();
219 menuCal
->Append(Calendar_Cal_Month
, "&Month can be changed\tCtrl-M",
220 "Allow changing the month in the calendar",
222 menuCal
->Append(Calendar_Cal_Year
, "&Year can be changed\tCtrl-Y",
223 "Allow changing the year in the calendar",
226 // now append the freshly created menu to the menu bar...
227 wxMenuBar
*menuBar
= new wxMenuBar
;
228 menuBar
->Append(menuFile
, "&File");
229 menuBar
->Append(menuCal
, "&Calendar");
231 menuBar
->Check(Calendar_Cal_Monday
, TRUE
);
232 menuBar
->Check(Calendar_Cal_Holidays
, TRUE
);
233 menuBar
->Check(Calendar_Cal_Month
, TRUE
);
234 menuBar
->Check(Calendar_Cal_Year
, TRUE
);
236 // ... and attach this menu bar to the frame
239 m_panel
= new MyPanel(this);
242 // create a status bar just for fun (by default with 1 pane only)
244 SetStatusText("Welcome to wxWindows!");
245 SetStatusText("Try changing the size of the Window !",1);
246 #endif // wxUSE_STATUSBAR
249 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
251 // TRUE is to force the frame to close
255 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
257 wxMessageBox(_T("wxCalendarCtrl sample\n© 2000 Vadim Zeitlin"),
258 _T("About Calendar"), wxOK
| wxICON_INFORMATION
, this);
261 void MyFrame::OnCalMonday(wxCommandEvent
& event
)
263 m_panel
->StartWithMonday(GetMenuBar()->IsChecked(event
.GetInt()));
266 void MyFrame::OnCalHolidays(wxCommandEvent
& event
)
268 bool enable
= GetMenuBar()->IsChecked(event
.GetInt());
269 m_panel
->GetCal()->EnableHolidayDisplay(enable
);
272 void MyFrame::OnCalSpecial(wxCommandEvent
& event
)
274 m_panel
->HighlightSpecial(GetMenuBar()->IsChecked(event
.GetInt()));
277 void MyFrame::OnCalAllowMonth(wxCommandEvent
& event
)
279 bool allow
= GetMenuBar()->IsChecked(event
.GetInt());
281 m_panel
->GetCal()->EnableMonthChange(allow
);
284 void MyFrame::OnCalAllowYear(wxCommandEvent
& event
)
286 bool allow
= GetMenuBar()->IsChecked(event
.GetInt());
288 m_panel
->GetCal()->EnableYearChange(allow
);
291 void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent
& event
)
293 event
.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month
));
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 MyPanel::MyPanel(wxFrame
*frame
)
306 date
.Printf("Selected date: %s ",
307 wxDateTime::Today().FormatISODate().c_str());
308 m_date
= new wxStaticText(this, -1, date
);
310 m_calendar
= new wxCalendarCtrl(this, Calendar_CalCtrl
,
315 wxCAL_SHOW_HOLIDAYS
|
318 //m_calendar->SetAutoLayout(TRUE);
320 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
322 c
= new wxLayoutConstraints
;
323 c
->left
.PercentOf(this, wxWidth
, 10);
324 c
->right
.PercentOf(this, wxWidth
, 60);
325 c
->top
.PercentOf(this, wxHeight
, 10);
326 c
->bottom
.PercentOf(this, wxHeight
, 60);
327 m_calendar
->SetConstraints(c
);
329 c
= new wxLayoutConstraints
;
330 c
->left
.SameAs(m_calendar
, wxLeft
, 10);
331 c
->top
.SameAs(m_calendar
, wxBottom
);
334 m_date
->SetConstraints(c
);
339 void MyPanel::OnCalendar(wxCalendarEvent
& event
)
341 wxLogMessage("Selected %s from calendar",
342 event
.GetDate().FormatISODate().c_str());
345 void MyPanel::OnCalendarChange(wxCalendarEvent
& event
)
348 s
.Printf("Selected date: %s", event
.GetDate().FormatISODate().c_str());
353 void MyPanel::OnCalMonthChange(wxCalendarEvent
& WXUNUSED(event
))
355 wxLogStatus("Calendar month changed");
358 void MyPanel::OnCalYearChange(wxCalendarEvent
& WXUNUSED(event
))
360 wxLogStatus("Calendar year changed");
363 void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent
& event
)
365 wxLogMessage("Clicked on %s",
366 wxDateTime::GetWeekDayName(event
.GetWeekDay()).c_str());
369 void MyPanel::StartWithMonday(bool on
)
371 long style
= m_calendar
->GetWindowStyle();
373 style
|= wxCAL_MONDAY_FIRST
;
375 style
&= ~wxCAL_MONDAY_FIRST
;
377 m_calendar
->SetWindowStyle(style
);
379 m_calendar
->Refresh();
382 void MyPanel::HighlightSpecial(bool on
)
387 *attrRedCircle
= new wxCalendarDateAttr(wxCAL_BORDER_ROUND
, *wxRED
),
388 *attrGreenSquare
= new wxCalendarDateAttr(wxCAL_BORDER_SQUARE
, *wxGREEN
),
389 *attrHeaderLike
= new wxCalendarDateAttr(*wxBLUE
, *wxLIGHT_GREY
);
391 m_calendar
->SetAttr(17, attrRedCircle
);
392 m_calendar
->SetAttr(29, attrGreenSquare
);
393 m_calendar
->SetAttr(13, attrHeaderLike
);
397 m_calendar
->ResetAttr(17);
398 m_calendar
->ResetAttr(29);
399 m_calendar
->ResetAttr(13);
402 m_calendar
->Refresh();