calctrlg.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/calctrlg.h
  3. // Purpose: generic implementation of date-picker control
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 29.12.99
  7. // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_GENERIC_CALCTRLG_H
  11. #define _WX_GENERIC_CALCTRLG_H
  12. #include "wx/control.h" // the base class
  13. #include "wx/dcclient.h" // for wxPaintDC
  14. class WXDLLIMPEXP_FWD_CORE wxComboBox;
  15. class WXDLLIMPEXP_FWD_CORE wxStaticText;
  16. class WXDLLIMPEXP_FWD_CORE wxSpinCtrl;
  17. class WXDLLIMPEXP_FWD_CORE wxSpinEvent;
  18. // ----------------------------------------------------------------------------
  19. // wxGenericCalendarCtrl
  20. // ----------------------------------------------------------------------------
  21. class WXDLLIMPEXP_ADV wxGenericCalendarCtrl : public wxCalendarCtrlBase
  22. {
  23. public:
  24. // construction
  25. wxGenericCalendarCtrl() { Init(); }
  26. wxGenericCalendarCtrl(wxWindow *parent,
  27. wxWindowID id,
  28. const wxDateTime& date = wxDefaultDateTime,
  29. const wxPoint& pos = wxDefaultPosition,
  30. const wxSize& size = wxDefaultSize,
  31. long style = wxCAL_SHOW_HOLIDAYS,
  32. const wxString& name = wxCalendarNameStr);
  33. bool Create(wxWindow *parent,
  34. wxWindowID id,
  35. const wxDateTime& date = wxDefaultDateTime,
  36. const wxPoint& pos = wxDefaultPosition,
  37. const wxSize& size = wxDefaultSize,
  38. long style = wxCAL_SHOW_HOLIDAYS,
  39. const wxString& name = wxCalendarNameStr);
  40. virtual ~wxGenericCalendarCtrl();
  41. virtual bool Destroy();
  42. // set/get the current date
  43. // ------------------------
  44. virtual bool SetDate(const wxDateTime& date);
  45. virtual wxDateTime GetDate() const { return m_date; }
  46. // set/get the range in which selection can occur
  47. // ---------------------------------------------
  48. virtual bool SetDateRange(const wxDateTime& lowerdate = wxDefaultDateTime,
  49. const wxDateTime& upperdate = wxDefaultDateTime);
  50. virtual bool GetDateRange(wxDateTime *lowerdate, wxDateTime *upperdate) const;
  51. // these functions are for generic version only, don't use them but use the
  52. // Set/GetDateRange() above instead
  53. bool SetLowerDateLimit(const wxDateTime& date = wxDefaultDateTime);
  54. const wxDateTime& GetLowerDateLimit() const { return m_lowdate; }
  55. bool SetUpperDateLimit(const wxDateTime& date = wxDefaultDateTime);
  56. const wxDateTime& GetUpperDateLimit() const { return m_highdate; }
  57. // calendar mode
  58. // -------------
  59. // some calendar styles can't be changed after the control creation by
  60. // just using SetWindowStyle() and Refresh() and the functions below
  61. // should be used instead for them
  62. // corresponds to wxCAL_NO_MONTH_CHANGE bit
  63. virtual bool EnableMonthChange(bool enable = true);
  64. // corresponds to wxCAL_NO_YEAR_CHANGE bit, deprecated, generic only
  65. void EnableYearChange(bool enable = true);
  66. // customization
  67. // -------------
  68. virtual void Mark(size_t day, bool mark);
  69. // all other functions in this section are for generic version only
  70. // header colours are used for painting the weekdays at the top
  71. virtual void SetHeaderColours(const wxColour& colFg, const wxColour& colBg)
  72. {
  73. m_colHeaderFg = colFg;
  74. m_colHeaderBg = colBg;
  75. }
  76. virtual const wxColour& GetHeaderColourFg() const { return m_colHeaderFg; }
  77. virtual const wxColour& GetHeaderColourBg() const { return m_colHeaderBg; }
  78. // highlight colour is used for the currently selected date
  79. virtual void SetHighlightColours(const wxColour& colFg, const wxColour& colBg)
  80. {
  81. m_colHighlightFg = colFg;
  82. m_colHighlightBg = colBg;
  83. }
  84. virtual const wxColour& GetHighlightColourFg() const { return m_colHighlightFg; }
  85. virtual const wxColour& GetHighlightColourBg() const { return m_colHighlightBg; }
  86. // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
  87. virtual void SetHolidayColours(const wxColour& colFg, const wxColour& colBg)
  88. {
  89. m_colHolidayFg = colFg;
  90. m_colHolidayBg = colBg;
  91. }
  92. virtual const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; }
  93. virtual const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; }
  94. virtual wxCalendarDateAttr *GetAttr(size_t day) const
  95. {
  96. wxCHECK_MSG( day > 0 && day < 32, NULL, wxT("invalid day") );
  97. return m_attrs[day - 1];
  98. }
  99. virtual void SetAttr(size_t day, wxCalendarDateAttr *attr)
  100. {
  101. wxCHECK_RET( day > 0 && day < 32, wxT("invalid day") );
  102. delete m_attrs[day - 1];
  103. m_attrs[day - 1] = attr;
  104. }
  105. virtual void ResetAttr(size_t day) { SetAttr(day, NULL); }
  106. virtual void SetHoliday(size_t day);
  107. virtual wxCalendarHitTestResult HitTest(const wxPoint& pos,
  108. wxDateTime *date = NULL,
  109. wxDateTime::WeekDay *wd = NULL);
  110. // implementation only from now on
  111. // -------------------------------
  112. // forward these functions to all subcontrols
  113. virtual bool Enable(bool enable = true);
  114. virtual bool Show(bool show = true);
  115. virtual void SetWindowStyleFlag(long style);
  116. virtual wxVisualAttributes GetDefaultAttributes() const
  117. { return GetClassDefaultAttributes(GetWindowVariant()); }
  118. static wxVisualAttributes
  119. GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
  120. void OnSysColourChanged(wxSysColourChangedEvent& event);
  121. protected:
  122. // override some base class virtuals
  123. virtual wxSize DoGetBestSize() const;
  124. virtual void DoMoveWindow(int x, int y, int width, int height);
  125. virtual void DoGetSize(int *width, int *height) const;
  126. private:
  127. // common part of all ctors
  128. void Init();
  129. // startup colours and reinitialization after colour changes in system
  130. void InitColours();
  131. // event handlers
  132. void OnPaint(wxPaintEvent& event);
  133. void OnClick(wxMouseEvent& event);
  134. void OnDClick(wxMouseEvent& event);
  135. void OnChar(wxKeyEvent& event);
  136. void OnMonthChange(wxCommandEvent& event);
  137. void HandleYearChange(wxCommandEvent& event);
  138. void OnYearChange(wxSpinEvent& event);
  139. void OnYearTextChange(wxCommandEvent& event);
  140. // (re)calc m_widthCol and m_heightRow
  141. void RecalcGeometry();
  142. // set the date and send the notification
  143. void SetDateAndNotify(const wxDateTime& date);
  144. // get the week (row, in range 1..6) for the given date
  145. size_t GetWeek(const wxDateTime& date) const;
  146. // get the date from which we start drawing days
  147. wxDateTime GetStartDate() const;
  148. // get the first/last days of the week corresponding to the current style
  149. wxDateTime::WeekDay GetWeekStart() const
  150. {
  151. return HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Mon
  152. : wxDateTime::Sun;
  153. }
  154. wxDateTime::WeekDay GetWeekEnd() const
  155. {
  156. return HasFlag(wxCAL_MONDAY_FIRST) ? wxDateTime::Sun
  157. : wxDateTime::Sat;
  158. }
  159. // is this date shown?
  160. bool IsDateShown(const wxDateTime& date) const;
  161. // is this date in the currently allowed range?
  162. bool IsDateInRange(const wxDateTime& date) const;
  163. // adjust the date to the currently allowed range, return true if it was
  164. // changed
  165. bool AdjustDateToRange(wxDateTime *date) const;
  166. // redraw the given date
  167. void RefreshDate(const wxDateTime& date);
  168. // change the date inside the same month/year
  169. void ChangeDay(const wxDateTime& date);
  170. // deprecated
  171. bool AllowYearChange() const
  172. {
  173. return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE);
  174. }
  175. // show the correct controls
  176. void ShowCurrentControls();
  177. // create the month combo and year spin controls
  178. void CreateMonthComboBox();
  179. void CreateYearSpinCtrl();
  180. public:
  181. // get the currently shown control for month/year
  182. wxControl *GetMonthControl() const;
  183. wxControl *GetYearControl() const;
  184. private:
  185. virtual void ResetHolidayAttrs();
  186. virtual void RefreshHolidays() { Refresh(); }
  187. // OnPaint helper-methods
  188. // Highlight the [fromdate : todate] range using pen and brush
  189. void HighlightRange(wxPaintDC* dc, const wxDateTime& fromdate, const wxDateTime& todate, const wxPen* pen, const wxBrush* brush);
  190. // Get the "coordinates" for the date relative to the month currently displayed.
  191. // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6)
  192. // if the date isn't visible (-1, -1) is put in (day, week) and false is returned
  193. bool GetDateCoord(const wxDateTime& date, int *day, int *week) const;
  194. // Set the flag for SetDate(): otherwise it would overwrite the year
  195. // typed in by the user
  196. void SetUserChangedYear() { m_userChangedYear = true; }
  197. // the subcontrols
  198. wxStaticText *m_staticMonth;
  199. wxComboBox *m_comboMonth;
  200. wxStaticText *m_staticYear;
  201. wxSpinCtrl *m_spinYear;
  202. // the current selection
  203. wxDateTime m_date;
  204. // the date-range
  205. wxDateTime m_lowdate;
  206. wxDateTime m_highdate;
  207. // default attributes
  208. wxColour m_colHighlightFg,
  209. m_colHighlightBg,
  210. m_colHolidayFg,
  211. m_colHolidayBg,
  212. m_colHeaderFg,
  213. m_colHeaderBg,
  214. m_colBackground,
  215. m_colSurrounding;
  216. // the attributes for each of the month days
  217. wxCalendarDateAttr *m_attrs[31];
  218. // the width and height of one column/row in the calendar
  219. wxCoord m_widthCol,
  220. m_heightRow,
  221. m_rowOffset,
  222. m_calendarWeekWidth;
  223. wxRect m_leftArrowRect,
  224. m_rightArrowRect;
  225. // the week day names
  226. wxString m_weekdays[7];
  227. // true if SetDate() is being called as the result of changing the year in
  228. // the year control
  229. bool m_userChangedYear;
  230. DECLARE_DYNAMIC_CLASS(wxGenericCalendarCtrl)
  231. DECLARE_EVENT_TABLE()
  232. wxDECLARE_NO_COPY_CLASS(wxGenericCalendarCtrl);
  233. };
  234. #endif // _WX_GENERIC_CALCTRLG_H