home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2233.zip / wxOS2-2_3_3.zip / wxWindows-2.3.3 / samples / calendar / calendar.cpp next >
C/C++ Source or Header  |  2002-09-01  |  14KB  |  431 lines

  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: calendar.cpp,v 1.16 2002/08/31 22:30:48 GD Exp $
  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("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, "&About...\tCtrl-A", "Show about dialog");
  212.     menuFile->AppendSeparator();
  213.     menuFile->Append(Calendar_File_Quit, "E&xit\tAlt-X", "Quit this program");
  214.  
  215.     wxMenu *menuCal = new wxMenu;
  216.     menuCal->Append(Calendar_Cal_Monday,
  217.                     "Monday &first weekday\tCtrl-F",
  218.                     "Toggle between Mon and Sun as the first week day",
  219.                     TRUE);
  220.     menuCal->Append(Calendar_Cal_Holidays, "Show &holidays\tCtrl-H",
  221.                     "Toggle highlighting the holidays",
  222.                     TRUE);
  223.     menuCal->Append(Calendar_Cal_Special, "Highlight &special dates\tCtrl-S",
  224.                     "Test custom highlighting",
  225.                     TRUE);
  226.     menuCal->Append(Calendar_Cal_SurroundWeeks,
  227.                     "Show s&urrounding weeks\tCtrl-W",
  228.                     "Show the neighbouring weeks in the prev/next month",
  229.                     TRUE);
  230.     menuCal->AppendSeparator();
  231.     menuCal->Append(Calendar_Cal_SeqMonth,
  232.                     "To&ggle month selector style\tCtrl-G",
  233.                     "Use another style for the calendar controls",
  234.                     TRUE);
  235.     menuCal->Append(Calendar_Cal_Month, "&Month can be changed\tCtrl-M",
  236.                     "Allow changing the month in the calendar",
  237.                     TRUE);
  238.     menuCal->Append(Calendar_Cal_Year, "&Year can be changed\tCtrl-Y",
  239.                     "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, "&File");
  245.     menuBar->Append(menuCal, "&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("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.     wxMessageBox(_T("wxCalendarCtrl sample\n⌐ 2000 Vadim Zeitlin"),
  273.                  _T("About Calendar"), wxOK | wxICON_INFORMATION, this);
  274. }
  275.  
  276. void MyFrame::OnCalMonday(wxCommandEvent& event)
  277. {
  278.     bool enable = GetMenuBar()->IsChecked(event.GetId());
  279.  
  280.     m_panel->ToggleCalStyle(enable, wxCAL_MONDAY_FIRST);
  281. }
  282.  
  283. void MyFrame::OnCalHolidays(wxCommandEvent& event)
  284. {
  285.     bool enable = GetMenuBar()->IsChecked(event.GetId());
  286.  
  287.     m_panel->GetCal()->EnableHolidayDisplay(enable);
  288. }
  289.  
  290. void MyFrame::OnCalSpecial(wxCommandEvent& event)
  291. {
  292.     m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetId()));
  293. }
  294.  
  295. void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
  296. {
  297.     bool allow = GetMenuBar()->IsChecked(event.GetId());
  298.  
  299.     m_panel->GetCal()->EnableMonthChange(allow);
  300. }
  301.  
  302. void MyFrame::OnCalAllowYear(wxCommandEvent& event)
  303. {
  304.     bool allow = GetMenuBar()->IsChecked(event.GetId());
  305.  
  306.     m_panel->GetCal()->EnableYearChange(allow);
  307. }
  308.  
  309. void MyFrame::OnCalSeqMonth(wxCommandEvent& event)
  310. {
  311.     bool allow = GetMenuBar()->IsChecked(event.GetId());
  312.  
  313.     m_panel->ToggleCalStyle(allow, wxCAL_SEQUENTIAL_MONTH_SELECTION);
  314. }
  315.  
  316. void MyFrame::OnCalShowSurroundingWeeks(wxCommandEvent& event)
  317. {
  318.     bool allow = GetMenuBar()->IsChecked(event.GetId());
  319.  
  320.     m_panel->ToggleCalStyle(allow, wxCAL_SHOW_SURROUNDING_WEEKS);
  321. }
  322.  
  323. void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event)
  324. {
  325.     event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month));
  326. }
  327.  
  328. // ----------------------------------------------------------------------------
  329. // MyPanel
  330. // ----------------------------------------------------------------------------
  331.  
  332. MyPanel::MyPanel(wxFrame *frame)
  333.        : wxPanel(frame, -1)
  334. {
  335.     SetAutoLayout(TRUE);
  336.  
  337.     wxString date;
  338.     date.Printf(wxT("Selected date: %s"),
  339.                 wxDateTime::Today().FormatISODate().c_str());
  340.     m_date = new wxStaticText(this, -1, date);
  341.     m_calendar = new wxCalendarCtrl(this, Calendar_CalCtrl,
  342.                                     wxDefaultDateTime,
  343.                                     wxDefaultPosition,
  344.                                     wxDefaultSize,
  345.                                     wxCAL_MONDAY_FIRST |
  346.                                     wxCAL_SHOW_HOLIDAYS |
  347.                                     wxRAISED_BORDER);
  348.  
  349.     wxLayoutConstraints *c = new wxLayoutConstraints;
  350.     c->left.SameAs(this, wxLeft, 10);
  351.     c->centreY.SameAs(this, wxCentreY);
  352.     c->height.AsIs();
  353.     c->width.AsIs();
  354.  
  355.     m_date->SetConstraints(c);
  356.  
  357.     c = new wxLayoutConstraints;
  358.     c->left.SameAs(m_date, wxRight, 20);
  359.     c->centreY.SameAs(this, wxCentreY);
  360.     c->height.AsIs();
  361.     c->width.AsIs();
  362.  
  363.     m_calendar->SetConstraints(c);
  364. }
  365.  
  366. void MyPanel::OnCalendar(wxCalendarEvent& event)
  367. {
  368.     wxLogMessage(wxT("Selected %s from calendar"),
  369.                  event.GetDate().FormatISODate().c_str());
  370. }
  371.  
  372. void MyPanel::OnCalendarChange(wxCalendarEvent& event)
  373. {
  374.     wxString s;
  375.     s.Printf(wxT("Selected date: %s"), event.GetDate().FormatISODate().c_str());
  376.  
  377.     m_date->SetLabel(s);
  378. }
  379.  
  380. void MyPanel::OnCalMonthChange(wxCalendarEvent& WXUNUSED(event))
  381. {
  382.     wxLogStatus(wxT("Calendar month changed"));
  383. }
  384.  
  385. void MyPanel::OnCalYearChange(wxCalendarEvent& WXUNUSED(event))
  386. {
  387.     wxLogStatus(wxT("Calendar year changed"));
  388. }
  389.  
  390. void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
  391. {
  392.     wxLogMessage(wxT("Clicked on %s"),
  393.                  wxDateTime::GetWeekDayName(event.GetWeekDay()).c_str());
  394. }
  395.  
  396. void MyPanel::ToggleCalStyle(bool on, int flag)
  397. {
  398.     long style = m_calendar->GetWindowStyle();
  399.     if ( on )
  400.         style |= flag;
  401.     else
  402.         style &= ~flag;
  403.  
  404.     m_calendar->SetWindowStyle(style);
  405.  
  406.     m_calendar->Refresh();
  407. }
  408.  
  409. void MyPanel::HighlightSpecial(bool on)
  410. {
  411.     if ( on )
  412.     {
  413.         wxCalendarDateAttr
  414.             *attrRedCircle = new wxCalendarDateAttr(wxCAL_BORDER_ROUND, *wxRED),
  415.             *attrGreenSquare = new wxCalendarDateAttr(wxCAL_BORDER_SQUARE, *wxGREEN),
  416.             *attrHeaderLike = new wxCalendarDateAttr(*wxBLUE, *wxLIGHT_GREY);
  417.  
  418.         m_calendar->SetAttr(17, attrRedCircle);
  419.         m_calendar->SetAttr(29, attrGreenSquare);
  420.         m_calendar->SetAttr(13, attrHeaderLike);
  421.     }
  422.     else // off
  423.     {
  424.         m_calendar->ResetAttr(17);
  425.         m_calendar->ResetAttr(29);
  426.         m_calendar->ResetAttr(13);
  427.     }
  428.  
  429.     m_calendar->Refresh();
  430. }
  431.