calendar.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: calendar.cpp
  3. // Purpose: wxCalendarCtrl sample
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 02.01.00
  7. // Copyright: (c) Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers
  22. #ifndef WX_PRECOMP
  23. #include "wx/app.h"
  24. #include "wx/log.h"
  25. #include "wx/frame.h"
  26. #include "wx/panel.h"
  27. #include "wx/stattext.h"
  28. #include "wx/menu.h"
  29. #include "wx/layout.h"
  30. #include "wx/msgdlg.h"
  31. #include "wx/icon.h"
  32. #include "wx/button.h"
  33. #include "wx/sizer.h"
  34. #include "wx/textctrl.h"
  35. #include "wx/settings.h"
  36. #endif
  37. #include "wx/calctrl.h"
  38. #include "wx/splitter.h"
  39. #if wxUSE_DATEPICKCTRL
  40. #include "wx/datectrl.h"
  41. #if wxUSE_DATEPICKCTRL_GENERIC
  42. #include "wx/generic/datectrl.h"
  43. #endif // wxUSE_DATEPICKCTRL_GENERIC
  44. #endif // wxUSE_DATEPICKCTRL
  45. #if wxUSE_TIMEPICKCTRL
  46. #include "wx/timectrl.h"
  47. #if wxUSE_TIMEPICKCTRL_GENERIC
  48. #include "wx/generic/timectrl.h"
  49. #endif // wxUSE_TIMEPICKCTRL_GENERIC
  50. #endif // wxUSE_TIMEPICKCTRL
  51. #include "../sample.xpm"
  52. #ifdef wxHAS_NATIVE_CALENDARCTRL
  53. #include "wx/generic/calctrlg.h"
  54. #endif
  55. // ----------------------------------------------------------------------------
  56. // private classes
  57. // ----------------------------------------------------------------------------
  58. // Define a new application type, each program should derive a class from wxApp
  59. class MyApp : public wxApp
  60. {
  61. public:
  62. // override base class virtuals
  63. // ----------------------------
  64. // this one is called on application startup and is a good place for the app
  65. // initialization (doing it here and not in the ctor allows to have an error
  66. // return: if OnInit() returns false, the application terminates)
  67. virtual bool OnInit();
  68. };
  69. class MyPanel : public wxPanel
  70. {
  71. public:
  72. MyPanel(wxWindow *parent);
  73. void OnCalendar(wxCalendarEvent& event);
  74. void OnCalendarWeekDayClick(wxCalendarEvent& event);
  75. void OnCalendarWeekClick(wxCalendarEvent& event);
  76. void OnCalendarChange(wxCalendarEvent& event);
  77. void OnCalMonthChange(wxCalendarEvent& event);
  78. wxCalendarCtrlBase *GetCal() const { return m_calendar; }
  79. // turn on/off the specified style bit on the calendar control
  80. void ToggleCalStyle(bool on, int style);
  81. bool IsUsingGeneric() const { return m_usingGeneric; }
  82. void ToggleUseGeneric()
  83. {
  84. m_usingGeneric = !m_usingGeneric;
  85. RecreateCalendar(m_calendar->GetWindowStyle());
  86. }
  87. void HighlightSpecial(bool on);
  88. void LimitDateRange(bool on);
  89. wxDateTime GetDate() const { return m_calendar->GetDate(); }
  90. void SetDate(const wxDateTime& dt) { m_calendar->SetDate(dt); }
  91. private:
  92. wxCalendarCtrlBase *DoCreateCalendar(const wxDateTime& dt, long style);
  93. void RecreateCalendar(long style);
  94. wxCalendarCtrlBase *m_calendar;
  95. wxStaticText *m_date;
  96. wxSizer *m_sizer;
  97. bool m_usingGeneric;
  98. wxDECLARE_EVENT_TABLE();
  99. };
  100. // Define a new frame type: this is going to be our main frame
  101. class MyFrame : public wxFrame
  102. {
  103. public:
  104. // ctor(s)
  105. MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
  106. // event handlers (these functions should _not_ be virtual)
  107. void OnAbout(wxCommandEvent& event);
  108. void OnClearLog(wxCommandEvent& event);
  109. void OnQuit(wxCommandEvent& event);
  110. #if wxUSE_DATEPICKCTRL
  111. void OnAskDate(wxCommandEvent& event);
  112. void OnUpdateUIStartWithNone(wxUpdateUIEvent& event);
  113. #endif // wxUSE_DATEPICKCTRL
  114. #if wxUSE_TIMEPICKCTRL
  115. void OnAskTime(wxCommandEvent& event);
  116. #endif // wxUSE_TIMEPICKCTRL
  117. #ifdef wxHAS_NATIVE_CALENDARCTRL
  118. void OnCalGeneric(wxCommandEvent& WXUNUSED(event))
  119. {
  120. m_panel->ToggleUseGeneric();
  121. }
  122. #endif // wxHAS_NATIVE_CALENDARCTRL
  123. void OnCalMonday(wxCommandEvent& event);
  124. void OnCalHolidays(wxCommandEvent& event);
  125. void OnCalSpecial(wxCommandEvent& event);
  126. void OnCalAllowMonth(wxCommandEvent& event);
  127. void OnCalLimitDates(wxCommandEvent& event);
  128. void OnCalSeqMonth(wxCommandEvent& event);
  129. void OnCalShowSurroundingWeeks(wxCommandEvent& event);
  130. void OnCalShowWeekNumbers(wxCommandEvent& event);
  131. void OnSetDate(wxCommandEvent& event);
  132. void OnToday(wxCommandEvent& event);
  133. void OnBeginDST(wxCommandEvent& event);
  134. void OnCalToggleResizable(wxCommandEvent& event);
  135. void OnUpdateUIGenericOnly(wxUpdateUIEvent& event)
  136. {
  137. event.Enable(m_panel->IsUsingGeneric());
  138. }
  139. void OnCalRClick(wxMouseEvent& event);
  140. private:
  141. MyPanel *m_panel;
  142. wxTextCtrl *m_logWindow;
  143. // any class wishing to process wxWidgets events must use this macro
  144. wxDECLARE_EVENT_TABLE();
  145. };
  146. #if wxUSE_DATEPICKCTRL
  147. // Define a simple modal dialog which asks the user for a date
  148. class MyDateDialog : public wxDialog
  149. {
  150. public:
  151. MyDateDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle);
  152. wxDateTime GetDate() const { return m_datePicker->GetValue(); }
  153. private:
  154. void OnDateChange(wxDateEvent& event);
  155. wxDatePickerCtrlBase *m_datePicker;
  156. wxStaticText *m_dateText;
  157. wxDECLARE_EVENT_TABLE();
  158. };
  159. #endif // wxUSE_DATEPICKCTRL
  160. #if wxUSE_TIMEPICKCTRL
  161. // Another simple dialog, this one asking for time.
  162. class MyTimeDialog : public wxDialog
  163. {
  164. public:
  165. MyTimeDialog(wxWindow* parent);
  166. wxDateTime GetTime() const { return m_timePicker->GetValue(); }
  167. private:
  168. void OnTimeChange(wxDateEvent& event);
  169. wxTimePickerCtrlBase* m_timePicker;
  170. wxStaticText* m_timeText;
  171. wxDECLARE_EVENT_TABLE();
  172. };
  173. #endif // wxUSE_TIMEPICKCTRL
  174. // ----------------------------------------------------------------------------
  175. // constants
  176. // ----------------------------------------------------------------------------
  177. // IDs for the controls and the menu commands
  178. enum
  179. {
  180. // menu items
  181. Calendar_File_About = wxID_ABOUT,
  182. Calendar_File_ClearLog = wxID_CLEAR,
  183. Calendar_File_Quit = wxID_EXIT,
  184. Calendar_Cal_Generic = 200,
  185. Calendar_Cal_Monday,
  186. Calendar_Cal_Holidays,
  187. Calendar_Cal_Special,
  188. Calendar_Cal_Month,
  189. Calendar_Cal_LimitDates,
  190. Calendar_Cal_SeqMonth,
  191. Calendar_Cal_SurroundWeeks,
  192. Calendar_Cal_WeekNumbers,
  193. Calendar_Cal_SetDate,
  194. Calendar_Cal_Today,
  195. Calendar_Cal_BeginDST,
  196. Calendar_Cal_Resizable,
  197. #if wxUSE_DATEPICKCTRL
  198. Calendar_DatePicker_AskDate = 300,
  199. Calendar_DatePicker_ShowCentury,
  200. Calendar_DatePicker_DropDown,
  201. Calendar_DatePicker_AllowNone,
  202. Calendar_DatePicker_StartWithNone,
  203. #if wxUSE_DATEPICKCTRL_GENERIC
  204. Calendar_DatePicker_Generic,
  205. #endif // wxUSE_DATEPICKCTRL_GENERIC
  206. #endif // wxUSE_DATEPICKCTRL
  207. #if wxUSE_TIMEPICKCTRL
  208. Calendar_TimePicker_AskTime = 400,
  209. #if wxUSE_TIMEPICKCTRL_GENERIC
  210. Calendar_TimePicker_Generic,
  211. #endif // wxUSE_TIMEPICKCTRL_GENERIC
  212. #endif // wxUSE_TIMEPICKCTRL
  213. Calendar_CalCtrl = 1000
  214. };
  215. // ----------------------------------------------------------------------------
  216. // event tables and other macros for wxWidgets
  217. // ----------------------------------------------------------------------------
  218. // the event tables connect the wxWidgets events with the functions (event
  219. // handlers) which process them. It can be also done at run-time, but for the
  220. // simple menu events like this the static method is much simpler.
  221. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  222. EVT_MENU(Calendar_File_About, MyFrame::OnAbout)
  223. EVT_MENU(Calendar_File_ClearLog, MyFrame::OnClearLog)
  224. EVT_MENU(Calendar_File_Quit, MyFrame::OnQuit)
  225. #if wxUSE_DATEPICKCTRL
  226. EVT_MENU(Calendar_DatePicker_AskDate, MyFrame::OnAskDate)
  227. EVT_UPDATE_UI(Calendar_DatePicker_StartWithNone,
  228. MyFrame::OnUpdateUIStartWithNone)
  229. #endif // wxUSE_DATEPICKCTRL
  230. #if wxUSE_TIMEPICKCTRL
  231. EVT_MENU(Calendar_TimePicker_AskTime, MyFrame::OnAskTime)
  232. #endif // wxUSE_TIMEPICKCTRL
  233. #ifdef wxHAS_NATIVE_CALENDARCTRL
  234. EVT_MENU(Calendar_Cal_Generic, MyFrame::OnCalGeneric)
  235. #endif // wxHAS_NATIVE_CALENDARCTRL
  236. EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
  237. EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
  238. EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
  239. EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
  240. EVT_MENU(Calendar_Cal_LimitDates, MyFrame::OnCalLimitDates)
  241. EVT_MENU(Calendar_Cal_SeqMonth, MyFrame::OnCalSeqMonth)
  242. EVT_MENU(Calendar_Cal_SurroundWeeks, MyFrame::OnCalShowSurroundingWeeks)
  243. EVT_MENU(Calendar_Cal_WeekNumbers, MyFrame::OnCalShowWeekNumbers)
  244. EVT_MENU(Calendar_Cal_SetDate, MyFrame::OnSetDate)
  245. EVT_MENU(Calendar_Cal_Today, MyFrame::OnToday)
  246. EVT_MENU(Calendar_Cal_BeginDST, MyFrame::OnBeginDST)
  247. EVT_MENU(Calendar_Cal_Resizable, MyFrame::OnCalToggleResizable)
  248. EVT_UPDATE_UI(Calendar_Cal_SeqMonth, MyFrame::OnUpdateUIGenericOnly)
  249. #ifdef __WXGTK20__
  250. EVT_UPDATE_UI(Calendar_Cal_Monday, MyFrame::OnUpdateUIGenericOnly)
  251. EVT_UPDATE_UI(Calendar_Cal_Holidays, MyFrame::OnUpdateUIGenericOnly)
  252. #endif
  253. EVT_UPDATE_UI(Calendar_Cal_Special, MyFrame::OnUpdateUIGenericOnly)
  254. EVT_UPDATE_UI(Calendar_Cal_SurroundWeeks, MyFrame::OnUpdateUIGenericOnly)
  255. wxEND_EVENT_TABLE()
  256. wxBEGIN_EVENT_TABLE(MyPanel, wxPanel)
  257. EVT_CALENDAR(Calendar_CalCtrl, MyPanel::OnCalendar)
  258. EVT_CALENDAR_PAGE_CHANGED(Calendar_CalCtrl, MyPanel::OnCalMonthChange)
  259. EVT_CALENDAR_SEL_CHANGED(Calendar_CalCtrl, MyPanel::OnCalendarChange)
  260. EVT_CALENDAR_WEEKDAY_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekDayClick)
  261. EVT_CALENDAR_WEEK_CLICKED(Calendar_CalCtrl, MyPanel::OnCalendarWeekClick)
  262. wxEND_EVENT_TABLE()
  263. // Create a new application object: this macro will allow wxWidgets to create
  264. // the application object during program execution (it's better than using a
  265. // static object for many reasons) and also declares the accessor function
  266. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  267. // not wxApp)
  268. IMPLEMENT_APP(MyApp)
  269. // ============================================================================
  270. // implementation
  271. // ============================================================================
  272. // ----------------------------------------------------------------------------
  273. // the application class
  274. // ----------------------------------------------------------------------------
  275. // `Main program' equivalent: the program execution "starts" here
  276. bool MyApp::OnInit()
  277. {
  278. if ( !wxApp::OnInit() )
  279. return false;
  280. // Create the main application window
  281. MyFrame *frame = new MyFrame(wxT("Calendar wxWidgets sample")
  282. #ifndef __WXWINCE__
  283. ,wxPoint(50, 50), wxSize(450, 340)
  284. #endif
  285. );
  286. frame->Show(true);
  287. // success: wxApp::OnRun() will be called which will enter the main message
  288. // loop and the application will run. If we returned false here, the
  289. // application would exit immediately.
  290. return true;
  291. }
  292. // ----------------------------------------------------------------------------
  293. // main frame
  294. // ----------------------------------------------------------------------------
  295. // frame constructor
  296. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  297. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
  298. {
  299. // set the frame icon
  300. SetIcon(wxICON(sample));
  301. // create a menu bar
  302. wxMenuBar *menuBar = new wxMenuBar;
  303. wxMenu *menuFile = new wxMenu;
  304. menuFile->Append(Calendar_File_About, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
  305. menuFile->AppendSeparator();
  306. menuFile->Append(Calendar_File_ClearLog, wxT("&Clear log\tCtrl-L"));
  307. menuFile->AppendSeparator();
  308. menuFile->Append(Calendar_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
  309. menuBar->Append(menuFile, wxT("&File"));
  310. wxMenu *menuCal = new wxMenu;
  311. #ifdef wxHAS_NATIVE_CALENDARCTRL
  312. menuCal->AppendCheckItem(Calendar_Cal_Generic, "Use &generic version\tCtrl-G",
  313. "Toggle between native and generic control");
  314. menuCal->AppendSeparator();
  315. #endif // wxHAS_NATIVE_CALENDARCTRL
  316. menuCal->Append(Calendar_Cal_Monday,
  317. wxT("Monday &first weekday\tCtrl-F"),
  318. wxT("Toggle between Mon and Sun as the first week day"),
  319. true);
  320. menuCal->Append(Calendar_Cal_Holidays, wxT("Show &holidays\tCtrl-H"),
  321. wxT("Toggle highlighting the holidays"),
  322. true);
  323. menuCal->Append(Calendar_Cal_Special, wxT("Highlight &special dates\tCtrl-S"),
  324. wxT("Test custom highlighting"),
  325. true);
  326. menuCal->Append(Calendar_Cal_SurroundWeeks,
  327. wxT("Show s&urrounding weeks\tCtrl-W"),
  328. wxT("Show the neighbouring weeks in the prev/next month"),
  329. true);
  330. menuCal->Append(Calendar_Cal_WeekNumbers,
  331. wxT("Show &week numbers"),
  332. wxT("Toggle week numbers"),
  333. true);
  334. menuCal->AppendSeparator();
  335. menuCal->Append(Calendar_Cal_SeqMonth,
  336. wxT("Toggle month selector st&yle\tCtrl-Y"),
  337. wxT("Use another style for the calendar controls"),
  338. true);
  339. menuCal->Append(Calendar_Cal_Month, wxT("&Month can be changed\tCtrl-M"),
  340. wxT("Allow changing the month in the calendar"),
  341. true);
  342. menuCal->AppendCheckItem(Calendar_Cal_LimitDates, wxT("Toggle date ra&nge\tCtrl-N"),
  343. wxT("Limit the valid dates"));
  344. menuCal->AppendSeparator();
  345. menuCal->Append(Calendar_Cal_SetDate, wxT("Call &SetDate(2005-12-24)"), wxT("Set date to 2005-12-24."));
  346. menuCal->Append(Calendar_Cal_Today, wxT("Call &Today()"), wxT("Set to the current date."));
  347. menuCal->Append(Calendar_Cal_BeginDST, "Call SetDate(GetBeginDST())");
  348. menuCal->AppendSeparator();
  349. menuCal->AppendCheckItem(Calendar_Cal_Resizable, wxT("Make &resizable\tCtrl-R"));
  350. menuBar->Append(menuCal, wxT("&Calendar"));
  351. #if wxUSE_DATEPICKCTRL
  352. wxMenu *menuDate = new wxMenu;
  353. menuDate->AppendCheckItem(Calendar_DatePicker_ShowCentury,
  354. wxT("Al&ways show century"));
  355. menuDate->AppendCheckItem(Calendar_DatePicker_DropDown,
  356. wxT("Use &drop down control"));
  357. menuDate->AppendCheckItem(Calendar_DatePicker_AllowNone,
  358. wxT("Allow &no date"));
  359. menuDate->AppendCheckItem(Calendar_DatePicker_StartWithNone,
  360. wxT("Start &with no date"));
  361. #if wxUSE_DATEPICKCTRL_GENERIC
  362. menuDate->AppendCheckItem(Calendar_DatePicker_Generic,
  363. wxT("Use &generic version of the control"));
  364. #endif // wxUSE_DATEPICKCTRL_GENERIC
  365. menuDate->AppendSeparator();
  366. menuDate->Append(Calendar_DatePicker_AskDate, wxT("&Choose date...\tCtrl-D"), wxT("Show dialog with wxDatePickerCtrl"));
  367. menuBar->Append(menuDate, wxT("&Date picker"));
  368. #endif // wxUSE_DATEPICKCTRL
  369. #if wxUSE_TIMEPICKCTRL
  370. wxMenu *menuTime = new wxMenu;
  371. #if wxUSE_TIMEPICKCTRL_GENERIC
  372. menuTime->AppendCheckItem(Calendar_TimePicker_Generic,
  373. wxT("Use &generic version of the control"));
  374. menuTime->AppendSeparator();
  375. #endif // wxUSE_TIMEPICKCTRL_GENERIC
  376. menuTime->Append(Calendar_TimePicker_AskTime, wxT("&Choose time...\tCtrl-T"), wxT("Show dialog with wxTimePickerCtrl"));
  377. menuBar->Append(menuTime, wxT("&Time picker"));
  378. #endif // wxUSE_TIMEPICKCTRL
  379. menuBar->Check(Calendar_Cal_Monday, true);
  380. menuBar->Check(Calendar_Cal_Holidays, true);
  381. menuBar->Check(Calendar_Cal_Month, true);
  382. menuBar->Check(Calendar_Cal_LimitDates, false);
  383. #if wxUSE_DATEPICKCTRL
  384. menuBar->Check(Calendar_DatePicker_ShowCentury, true);
  385. #endif // wxUSE_DATEPICKCTRL
  386. // ... and attach this menu bar to the frame
  387. SetMenuBar(menuBar);
  388. wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY,
  389. wxDefaultPosition, wxDefaultSize,
  390. wxSP_NOBORDER);
  391. m_panel = new MyPanel(splitter);
  392. m_logWindow = new wxTextCtrl(splitter, wxID_ANY, wxEmptyString,
  393. wxDefaultPosition, wxDefaultSize,
  394. wxTE_READONLY | wxTE_MULTILINE);
  395. splitter->SplitHorizontally(m_panel, m_logWindow);
  396. splitter->SetMinimumPaneSize(20);
  397. wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
  398. }
  399. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  400. {
  401. // true is to force the frame to close
  402. Close(true);
  403. }
  404. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  405. {
  406. wxMessageBox(wxT("wxCalendarCtrl sample\n(c) 2000--2008 Vadim Zeitlin"),
  407. wxT("About Calendar"), wxOK | wxICON_INFORMATION, this);
  408. }
  409. void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
  410. {
  411. m_logWindow->Clear();
  412. }
  413. void MyFrame::OnCalMonday(wxCommandEvent& event)
  414. {
  415. m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_MONDAY_FIRST);
  416. }
  417. void MyFrame::OnCalHolidays(wxCommandEvent& event)
  418. {
  419. m_panel->GetCal()->EnableHolidayDisplay(event.IsChecked());
  420. }
  421. void MyFrame::OnCalSpecial(wxCommandEvent& event)
  422. {
  423. m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
  424. }
  425. void MyFrame::OnCalLimitDates(wxCommandEvent& event)
  426. {
  427. m_panel->LimitDateRange(GetMenuBar()->IsChecked(event.GetId()));
  428. }
  429. void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
  430. {
  431. m_panel->GetCal()->EnableMonthChange(event.IsChecked());
  432. }
  433. void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
  434. {
  435. m_panel->ToggleCalStyle(event.IsChecked(),
  436. wxCAL_SEQUENTIAL_MONTH_SELECTION);
  437. }
  438. void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
  439. {
  440. m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_SURROUNDING_WEEKS);
  441. }
  442. void MyFrame::OnCalShowWeekNumbers(wxCommandEvent& event)
  443. {
  444. m_panel->ToggleCalStyle(event.IsChecked(), wxCAL_SHOW_WEEK_NUMBERS);
  445. }
  446. void MyFrame::OnSetDate(wxCommandEvent &WXUNUSED(event))
  447. {
  448. m_panel->SetDate(wxDateTime(24, wxDateTime::Dec, 2005, 22, 00, 00));
  449. }
  450. void MyFrame::OnToday(wxCommandEvent &WXUNUSED(event))
  451. {
  452. m_panel->SetDate(wxDateTime::Today());
  453. }
  454. void MyFrame::OnBeginDST(wxCommandEvent &WXUNUSED(event))
  455. {
  456. m_panel->SetDate(wxDateTime::GetBeginDST(m_panel->GetDate().GetYear()));
  457. }
  458. void MyFrame::OnCalToggleResizable(wxCommandEvent& event)
  459. {
  460. wxSizer * const sizer = m_panel->GetSizer();
  461. wxSizerItem * const item = sizer->GetItem(m_panel->GetCal());
  462. if ( event.IsChecked() )
  463. {
  464. item->SetProportion(1);
  465. item->SetFlag(wxEXPAND);
  466. }
  467. else // not resizable
  468. {
  469. item->SetProportion(0);
  470. item->SetFlag(wxALIGN_CENTER);
  471. }
  472. sizer->Layout();
  473. }
  474. void MyFrame::OnCalRClick(wxMouseEvent& event)
  475. {
  476. wxDateTime dt;
  477. wxDateTime::WeekDay wd;
  478. const wxPoint pt = event.GetPosition();
  479. wxString msg = wxString::Format("Point (%d, %d) is ", pt.x, pt.y);
  480. switch ( m_panel->GetCal()->HitTest(pt, &dt, &wd) )
  481. {
  482. default:
  483. wxFAIL_MSG( "unexpected" );
  484. // fall through
  485. case wxCAL_HITTEST_NOWHERE:
  486. msg += "nowhere";
  487. break;
  488. case wxCAL_HITTEST_HEADER:
  489. msg += wxString::Format("over %s", wxDateTime::GetWeekDayName(wd));
  490. break;
  491. case wxCAL_HITTEST_DAY:
  492. msg += wxString::Format("over %s", dt.FormatISODate());
  493. break;
  494. case wxCAL_HITTEST_INCMONTH:
  495. msg += "over next month button";
  496. break;
  497. case wxCAL_HITTEST_DECMONTH:
  498. msg += "over previous month button";
  499. break;
  500. case wxCAL_HITTEST_SURROUNDING_WEEK:
  501. msg += "over a day from another month";
  502. break;
  503. }
  504. wxLogMessage("%s", msg);
  505. }
  506. #if wxUSE_DATEPICKCTRL
  507. void MyFrame::OnUpdateUIStartWithNone(wxUpdateUIEvent& event)
  508. {
  509. // it only makes sense to start with invalid date if we can have no date
  510. event.Enable( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) );
  511. }
  512. void MyFrame::OnAskDate(wxCommandEvent& WXUNUSED(event))
  513. {
  514. wxDateTime dt = m_panel->GetCal()->GetDate();
  515. int style = wxDP_DEFAULT;
  516. if ( GetMenuBar()->IsChecked(Calendar_DatePicker_ShowCentury) )
  517. style |= wxDP_SHOWCENTURY;
  518. if ( GetMenuBar()->IsChecked(Calendar_DatePicker_DropDown) )
  519. style |= wxDP_DROPDOWN;
  520. if ( GetMenuBar()->IsChecked(Calendar_DatePicker_AllowNone) )
  521. {
  522. style |= wxDP_ALLOWNONE;
  523. if ( GetMenuBar()->IsChecked(Calendar_DatePicker_StartWithNone) )
  524. dt = wxDefaultDateTime;
  525. }
  526. MyDateDialog dlg(this, dt, style);
  527. if ( dlg.ShowModal() == wxID_OK )
  528. {
  529. dt = dlg.GetDate();
  530. if ( dt.IsValid() )
  531. {
  532. const wxDateTime today = wxDateTime::Today();
  533. if ( dt.GetDay() == today.GetDay() &&
  534. dt.GetMonth() == today.GetMonth() )
  535. {
  536. wxMessageBox(wxT("Happy birthday!"), wxT("Calendar Sample"));
  537. }
  538. m_panel->SetDate(dt);
  539. wxLogStatus(wxT("Changed the date to your input"));
  540. }
  541. else
  542. {
  543. wxLogStatus(wxT("No date entered"));
  544. }
  545. }
  546. }
  547. #endif // wxUSE_DATEPICKCTRL
  548. #if wxUSE_TIMEPICKCTRL
  549. void MyFrame::OnAskTime(wxCommandEvent& WXUNUSED(event))
  550. {
  551. MyTimeDialog dlg(this);
  552. if ( dlg.ShowModal() == wxID_OK )
  553. {
  554. wxLogMessage("You entered %s", dlg.GetTime().FormatISOTime());
  555. }
  556. }
  557. #endif // wxUSE_TIMEPICKCTRL
  558. // ----------------------------------------------------------------------------
  559. // MyPanel
  560. // ----------------------------------------------------------------------------
  561. MyPanel::MyPanel(wxWindow *parent)
  562. : wxPanel(parent, wxID_ANY)
  563. {
  564. #ifdef wxHAS_NATIVE_CALENDARCTRL
  565. m_usingGeneric = false;
  566. #else
  567. m_usingGeneric = true;
  568. #endif
  569. wxString date;
  570. date.Printf(wxT("Selected date: %s"),
  571. wxDateTime::Today().FormatISODate().c_str());
  572. m_date = new wxStaticText(this, wxID_ANY, date);
  573. m_calendar = DoCreateCalendar(wxDefaultDateTime,
  574. wxCAL_MONDAY_FIRST | wxCAL_SHOW_HOLIDAYS);
  575. // adjust to vertical/horizontal display, check mostly dedicated to WinCE
  576. bool horizontal = ( wxSystemSettings::GetMetric(wxSYS_SCREEN_X) > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) );
  577. m_sizer = new wxBoxSizer( horizontal ? wxHORIZONTAL : wxVERTICAL );
  578. m_sizer->Add(m_date, 0, wxALIGN_CENTER | wxALL, 10 );
  579. m_sizer->Add(m_calendar, 0, wxALIGN_CENTER | wxALIGN_LEFT);
  580. SetSizer( m_sizer );
  581. m_sizer->SetSizeHints( this );
  582. }
  583. void MyPanel::OnCalendar(wxCalendarEvent& event)
  584. {
  585. // clicking the same date twice unmarks it (convenient for testing)
  586. static wxDateTime s_dateLast;
  587. const bool mark = !s_dateLast.IsValid() || event.GetDate() != s_dateLast;
  588. s_dateLast = event.GetDate();
  589. m_calendar->Mark(event.GetDate().GetDay(), mark);
  590. wxLogMessage(wxT("Selected (and %smarked) %s from calendar."),
  591. mark ? "" : "un", s_dateLast.FormatISODate().c_str());
  592. }
  593. void MyPanel::OnCalendarChange(wxCalendarEvent& event)
  594. {
  595. wxString s;
  596. s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
  597. m_date->SetLabel(s);
  598. wxLogStatus(s);
  599. }
  600. void MyPanel::OnCalMonthChange(wxCalendarEvent& event)
  601. {
  602. wxLogStatus(wxT("Calendar month changed to %s %d"),
  603. wxDateTime::GetMonthName(event.GetDate().GetMonth()),
  604. event.GetDate().GetYear());
  605. }
  606. void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
  607. {
  608. wxLogMessage(wxT("Clicked on %s"),
  609. wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
  610. }
  611. void MyPanel::OnCalendarWeekClick(wxCalendarEvent& event)
  612. {
  613. wxLogMessage(wxT("Clicked on week %d"), event.GetDate().GetWeekOfYear());
  614. }
  615. wxCalendarCtrlBase *MyPanel::DoCreateCalendar(const wxDateTime& dt, long style)
  616. {
  617. wxCalendarCtrlBase *calendar;
  618. #ifdef wxHAS_NATIVE_CALENDARCTRL
  619. if ( m_usingGeneric )
  620. calendar = new wxGenericCalendarCtrl(this, Calendar_CalCtrl,
  621. dt,
  622. wxDefaultPosition,
  623. wxDefaultSize,
  624. style);
  625. else
  626. #endif // wxHAS_NATIVE_CALENDARCTRL
  627. calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
  628. dt,
  629. wxDefaultPosition,
  630. wxDefaultSize,
  631. style);
  632. calendar->Connect(wxEVT_RIGHT_DOWN,
  633. wxMouseEventHandler(MyFrame::OnCalRClick),
  634. NULL,
  635. ( MyFrame * )wxGetTopLevelParent(this));
  636. return calendar;
  637. }
  638. void MyPanel::RecreateCalendar(long style)
  639. {
  640. wxCalendarCtrlBase *calendar = DoCreateCalendar(m_calendar->GetDate(), style);
  641. m_sizer->Replace(m_calendar, calendar);
  642. delete m_calendar;
  643. m_calendar = calendar;
  644. m_sizer->Layout();
  645. }
  646. void MyPanel::ToggleCalStyle(bool on, int flag)
  647. {
  648. long style = m_calendar->GetWindowStyle();
  649. if ( on )
  650. style |= flag;
  651. else
  652. style &= ~flag;
  653. if ( flag == wxCAL_SEQUENTIAL_MONTH_SELECTION
  654. || flag == wxCAL_SHOW_WEEK_NUMBERS)
  655. {
  656. // changing this style requires recreating the control
  657. RecreateCalendar(style);
  658. }
  659. else // just changing the style is enough
  660. {
  661. m_calendar->SetWindowStyle(style);
  662. m_calendar->Refresh();
  663. }
  664. }
  665. void MyPanel::HighlightSpecial(bool on)
  666. {
  667. if ( on )
  668. {
  669. wxCalendarDateAttr
  670. *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
  671. *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
  672. *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
  673. m_calendar->SetAttr(17, attrRedCircle);
  674. m_calendar->SetAttr(29, attrGreenSquare);
  675. m_calendar->SetAttr(13, attrHeaderLike);
  676. }
  677. else // off
  678. {
  679. m_calendar->ResetAttr(17);
  680. m_calendar->ResetAttr(29);
  681. m_calendar->ResetAttr(13);
  682. }
  683. m_calendar->Refresh();
  684. }
  685. // Toggle a restricted date range to the six months centered on today's date.
  686. void MyPanel::LimitDateRange(bool on)
  687. {
  688. if ( on )
  689. {
  690. // limit the choice of date to 3 months around today
  691. const wxDateSpan diff = wxDateSpan::Months(3);
  692. const wxDateTime today = wxDateTime::Today();
  693. // Set the restricted date range.
  694. if ( m_calendar->SetDateRange(today - diff, today + diff) )
  695. {
  696. wxLogStatus("Date range limited to 3 months around today.");
  697. wxDateTime firstValidDate;
  698. wxDateTime lastValidDate;
  699. if ( m_calendar->GetDateRange(&firstValidDate, &lastValidDate) )
  700. {
  701. wxLogMessage("First valid date: %s, last valid date: %s",
  702. firstValidDate.FormatISODate(),
  703. lastValidDate.FormatISODate());
  704. }
  705. else
  706. {
  707. wxLogWarning("Failed to get back the valid dates range.");
  708. }
  709. }
  710. else
  711. {
  712. wxLogWarning("Date range not supported.");
  713. }
  714. }
  715. else // off
  716. {
  717. // Remove the date restrictions.
  718. if ( m_calendar->SetDateRange() )
  719. {
  720. wxLogStatus("Date choice is unlimited now.");
  721. }
  722. else
  723. {
  724. wxLogWarning("Date range not supported.");
  725. }
  726. }
  727. m_calendar->Refresh();
  728. }
  729. // ----------------------------------------------------------------------------
  730. // MyDateDialog
  731. // ----------------------------------------------------------------------------
  732. #if wxUSE_DATEPICKCTRL
  733. wxBEGIN_EVENT_TABLE(MyDateDialog, wxDialog)
  734. EVT_DATE_CHANGED(wxID_ANY, MyDateDialog::OnDateChange)
  735. wxEND_EVENT_TABLE()
  736. MyDateDialog::MyDateDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle)
  737. : wxDialog(parent, wxID_ANY, wxString(wxT("Calendar: Choose a date")))
  738. {
  739. #if wxUSE_DATEPICKCTRL_GENERIC
  740. wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
  741. if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) )
  742. m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt,
  743. wxDefaultPosition,
  744. wxDefaultSize,
  745. dtpStyle);
  746. else
  747. #endif // wxUSE_DATEPICKCTRL_GENERIC
  748. m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt,
  749. wxDefaultPosition, wxDefaultSize,
  750. dtpStyle);
  751. m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900),
  752. wxDefaultDateTime);
  753. m_dateText = new wxStaticText(this, wxID_ANY,
  754. dt.IsValid() ? dt.FormatISODate()
  755. : wxString());
  756. const wxSizerFlags flags = wxSizerFlags().Centre().Border();
  757. wxFlexGridSizer* const sizerMain = new wxFlexGridSizer(2);
  758. sizerMain->Add(new wxStaticText(this, wxID_ANY, "Enter &date:"), flags);
  759. sizerMain->Add(m_datePicker, flags);
  760. sizerMain->Add(new wxStaticText(this, wxID_ANY, "Date in ISO format:"),
  761. flags);
  762. sizerMain->Add(m_dateText, flags);
  763. wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
  764. sizerTop->Add(sizerMain, flags);
  765. sizerTop->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), flags);
  766. SetSizerAndFit(sizerTop);
  767. }
  768. void MyDateDialog::OnDateChange(wxDateEvent& event)
  769. {
  770. const wxDateTime dt = event.GetDate();
  771. if ( dt.IsValid() )
  772. m_dateText->SetLabel(dt.FormatISODate());
  773. else
  774. m_dateText->SetLabel(wxEmptyString);
  775. }
  776. #endif // wxUSE_DATEPICKCTRL
  777. // ----------------------------------------------------------------------------
  778. // MyTimeDialog
  779. // ----------------------------------------------------------------------------
  780. #if wxUSE_TIMEPICKCTRL
  781. wxBEGIN_EVENT_TABLE(MyTimeDialog, wxDialog)
  782. EVT_TIME_CHANGED(wxID_ANY, MyTimeDialog::OnTimeChange)
  783. wxEND_EVENT_TABLE()
  784. MyTimeDialog::MyTimeDialog(wxWindow *parent)
  785. : wxDialog(parent, wxID_ANY, wxString(wxT("Calendar: Choose time")))
  786. {
  787. #if wxUSE_TIMEPICKCTRL_GENERIC
  788. wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent);
  789. if ( frame && frame->GetMenuBar()->IsChecked(Calendar_TimePicker_Generic) )
  790. m_timePicker = new wxTimePickerCtrlGeneric(this, wxID_ANY);
  791. else
  792. #endif // wxUSE_TIMEPICKCTRL_GENERIC
  793. m_timePicker = new wxTimePickerCtrl(this, wxID_ANY);
  794. m_timeText = new wxStaticText(this, wxID_ANY,
  795. m_timePicker->GetValue().FormatISOTime());
  796. const wxSizerFlags flags = wxSizerFlags().Centre().Border();
  797. wxFlexGridSizer* const sizerMain = new wxFlexGridSizer(2);
  798. sizerMain->Add(new wxStaticText(this, wxID_ANY, "Enter &time:"), flags);
  799. sizerMain->Add(m_timePicker, flags);
  800. sizerMain->Add(new wxStaticText(this, wxID_ANY, "Time in ISO format:"),
  801. flags);
  802. sizerMain->Add(m_timeText, flags);
  803. wxSizer* sizerTop = new wxBoxSizer(wxVERTICAL);
  804. sizerTop->Add(sizerMain, flags);
  805. sizerTop->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), flags);
  806. SetSizerAndFit(sizerTop);
  807. }
  808. void MyTimeDialog::OnTimeChange(wxDateEvent& event)
  809. {
  810. m_timeText->SetLabel(event.GetDate().FormatISOTime());
  811. }
  812. #endif // wxUSE_TIMEPICKCTRL