calctrl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/calctrl.h
  3. // Purpose: 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_CALCTRL_H_
  11. #define _WX_CALCTRL_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_CALENDARCTRL
  14. #include "wx/dateevt.h"
  15. #include "wx/colour.h"
  16. #include "wx/font.h"
  17. #include "wx/control.h"
  18. // ----------------------------------------------------------------------------
  19. // wxCalendarCtrl flags
  20. // ----------------------------------------------------------------------------
  21. enum
  22. {
  23. // show Sunday as the first day of the week (default)
  24. wxCAL_SUNDAY_FIRST = 0x0000,
  25. // show Monday as the first day of the week
  26. wxCAL_MONDAY_FIRST = 0x0001,
  27. // highlight holidays
  28. wxCAL_SHOW_HOLIDAYS = 0x0002,
  29. // disable the year change control, show only the month change one
  30. // deprecated
  31. wxCAL_NO_YEAR_CHANGE = 0x0004,
  32. // don't allow changing neither month nor year (implies
  33. // wxCAL_NO_YEAR_CHANGE)
  34. wxCAL_NO_MONTH_CHANGE = 0x000c,
  35. // use MS-style month-selection instead of combo-spin combination
  36. wxCAL_SEQUENTIAL_MONTH_SELECTION = 0x0010,
  37. // show the neighbouring weeks in the previous and next month
  38. wxCAL_SHOW_SURROUNDING_WEEKS = 0x0020,
  39. // show week numbers on the left side of the calendar.
  40. wxCAL_SHOW_WEEK_NUMBERS = 0x0040
  41. };
  42. // ----------------------------------------------------------------------------
  43. // constants
  44. // ----------------------------------------------------------------------------
  45. // return values for the HitTest() method
  46. enum wxCalendarHitTestResult
  47. {
  48. wxCAL_HITTEST_NOWHERE, // outside of anything
  49. wxCAL_HITTEST_HEADER, // on the header (weekdays)
  50. wxCAL_HITTEST_DAY, // on a day in the calendar
  51. wxCAL_HITTEST_INCMONTH,
  52. wxCAL_HITTEST_DECMONTH,
  53. wxCAL_HITTEST_SURROUNDING_WEEK,
  54. wxCAL_HITTEST_WEEK
  55. };
  56. // border types for a date
  57. enum wxCalendarDateBorder
  58. {
  59. wxCAL_BORDER_NONE, // no border (default)
  60. wxCAL_BORDER_SQUARE, // a rectangular border
  61. wxCAL_BORDER_ROUND // a round border
  62. };
  63. // ----------------------------------------------------------------------------
  64. // wxCalendarDateAttr: custom attributes for a calendar date
  65. // ----------------------------------------------------------------------------
  66. class WXDLLIMPEXP_ADV wxCalendarDateAttr
  67. {
  68. public:
  69. // ctors
  70. wxCalendarDateAttr(const wxColour& colText = wxNullColour,
  71. const wxColour& colBack = wxNullColour,
  72. const wxColour& colBorder = wxNullColour,
  73. const wxFont& font = wxNullFont,
  74. wxCalendarDateBorder border = wxCAL_BORDER_NONE)
  75. : m_colText(colText), m_colBack(colBack),
  76. m_colBorder(colBorder), m_font(font)
  77. {
  78. Init(border);
  79. }
  80. wxCalendarDateAttr(wxCalendarDateBorder border,
  81. const wxColour& colBorder = wxNullColour)
  82. : m_colBorder(colBorder)
  83. {
  84. Init(border);
  85. }
  86. // setters
  87. void SetTextColour(const wxColour& colText) { m_colText = colText; }
  88. void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
  89. void SetBorderColour(const wxColour& col) { m_colBorder = col; }
  90. void SetFont(const wxFont& font) { m_font = font; }
  91. void SetBorder(wxCalendarDateBorder border) { m_border = border; }
  92. void SetHoliday(bool holiday) { m_holiday = holiday; }
  93. // accessors
  94. bool HasTextColour() const { return m_colText.IsOk(); }
  95. bool HasBackgroundColour() const { return m_colBack.IsOk(); }
  96. bool HasBorderColour() const { return m_colBorder.IsOk(); }
  97. bool HasFont() const { return m_font.IsOk(); }
  98. bool HasBorder() const { return m_border != wxCAL_BORDER_NONE; }
  99. bool IsHoliday() const { return m_holiday; }
  100. const wxColour& GetTextColour() const { return m_colText; }
  101. const wxColour& GetBackgroundColour() const { return m_colBack; }
  102. const wxColour& GetBorderColour() const { return m_colBorder; }
  103. const wxFont& GetFont() const { return m_font; }
  104. wxCalendarDateBorder GetBorder() const { return m_border; }
  105. // get or change the "mark" attribute, i.e. the one used for the items
  106. // marked with wxCalendarCtrl::Mark()
  107. static const wxCalendarDateAttr& GetMark() { return m_mark; }
  108. static void SetMark(wxCalendarDateAttr const& m) { m_mark = m; }
  109. protected:
  110. void Init(wxCalendarDateBorder border = wxCAL_BORDER_NONE)
  111. {
  112. m_border = border;
  113. m_holiday = false;
  114. }
  115. private:
  116. static wxCalendarDateAttr m_mark;
  117. wxColour m_colText,
  118. m_colBack,
  119. m_colBorder;
  120. wxFont m_font;
  121. wxCalendarDateBorder m_border;
  122. bool m_holiday;
  123. };
  124. // ----------------------------------------------------------------------------
  125. // wxCalendarCtrl events
  126. // ----------------------------------------------------------------------------
  127. class WXDLLIMPEXP_FWD_ADV wxCalendarCtrl;
  128. class WXDLLIMPEXP_ADV wxCalendarEvent : public wxDateEvent
  129. {
  130. public:
  131. wxCalendarEvent() : m_wday(wxDateTime::Inv_WeekDay) { }
  132. wxCalendarEvent(wxWindow *win, const wxDateTime& dt, wxEventType type)
  133. : wxDateEvent(win, dt, type),
  134. m_wday(wxDateTime::Inv_WeekDay) { }
  135. wxCalendarEvent(const wxCalendarEvent& event)
  136. : wxDateEvent(event), m_wday(event.m_wday) { }
  137. void SetWeekDay(const wxDateTime::WeekDay wd) { m_wday = wd; }
  138. wxDateTime::WeekDay GetWeekDay() const { return m_wday; }
  139. virtual wxEvent *Clone() const { return new wxCalendarEvent(*this); }
  140. private:
  141. wxDateTime::WeekDay m_wday;
  142. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxCalendarEvent)
  143. };
  144. // ----------------------------------------------------------------------------
  145. // wxCalendarCtrlBase
  146. // ----------------------------------------------------------------------------
  147. class WXDLLIMPEXP_ADV wxCalendarCtrlBase : public wxControl
  148. {
  149. public:
  150. // do we allow changing the month/year?
  151. bool AllowMonthChange() const { return !HasFlag(wxCAL_NO_MONTH_CHANGE); }
  152. // get/set the current date
  153. virtual wxDateTime GetDate() const = 0;
  154. virtual bool SetDate(const wxDateTime& date) = 0;
  155. // restricting the dates shown by the control to the specified range: only
  156. // implemented in the generic and MSW versions for now
  157. // if either date is set, the corresponding limit will be enforced and true
  158. // returned; if none are set, the existing restrictions are removed and
  159. // false is returned
  160. virtual bool
  161. SetDateRange(const wxDateTime& WXUNUSED(lowerdate) = wxDefaultDateTime,
  162. const wxDateTime& WXUNUSED(upperdate) = wxDefaultDateTime)
  163. {
  164. return false;
  165. }
  166. // retrieves the limits currently in use (wxDefaultDateTime if none) in the
  167. // provided pointers (which may be NULL) and returns true if there are any
  168. // limits or false if none
  169. virtual bool
  170. GetDateRange(wxDateTime *lowerdate, wxDateTime *upperdate) const
  171. {
  172. if ( lowerdate )
  173. *lowerdate = wxDefaultDateTime;
  174. if ( upperdate )
  175. *upperdate = wxDefaultDateTime;
  176. return false;
  177. }
  178. // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
  179. // with the corresponding value (none for NOWHERE, the date for DAY and wd
  180. // for HEADER)
  181. //
  182. // notice that this is not implemented in all versions
  183. virtual wxCalendarHitTestResult
  184. HitTest(const wxPoint& WXUNUSED(pos),
  185. wxDateTime* WXUNUSED(date) = NULL,
  186. wxDateTime::WeekDay* WXUNUSED(wd) = NULL)
  187. {
  188. return wxCAL_HITTEST_NOWHERE;
  189. }
  190. // allow or disable changing the current month (and year), return true if
  191. // the value of this option really changed or false if it was already set
  192. // to the required value
  193. //
  194. // NB: we provide implementation for this pure virtual function, derived
  195. // classes should call it
  196. virtual bool EnableMonthChange(bool enable = true) = 0;
  197. // an item without custom attributes is drawn with the default colours and
  198. // font and without border, setting custom attributes allows to modify this
  199. //
  200. // the day parameter should be in 1..31 range, for days 29, 30, 31 the
  201. // corresponding attribute is just unused if there is no such day in the
  202. // current month
  203. //
  204. // notice that currently arbitrary attributes are supported only in the
  205. // generic version, the native controls only support Mark() which assigns
  206. // some special appearance (which can be customized using SetMark() for the
  207. // generic version) to the given day
  208. virtual void Mark(size_t day, bool mark) = 0;
  209. virtual wxCalendarDateAttr *GetAttr(size_t WXUNUSED(day)) const
  210. { return NULL; }
  211. virtual void SetAttr(size_t WXUNUSED(day), wxCalendarDateAttr *attr)
  212. { delete attr; }
  213. virtual void ResetAttr(size_t WXUNUSED(day)) { }
  214. // holidays support
  215. //
  216. // currently only the generic version implements all functions in this
  217. // section; wxMSW implements simple support for holidays (they can be
  218. // just enabled or disabled) and wxGTK doesn't support them at all
  219. // equivalent to changing wxCAL_SHOW_HOLIDAYS flag but should be called
  220. // instead of just changing it
  221. virtual void EnableHolidayDisplay(bool display = true);
  222. // set/get the colours to use for holidays (if they're enabled)
  223. virtual void SetHolidayColours(const wxColour& WXUNUSED(colFg),
  224. const wxColour& WXUNUSED(colBg)) { }
  225. virtual const wxColour& GetHolidayColourFg() const { return wxNullColour; }
  226. virtual const wxColour& GetHolidayColourBg() const { return wxNullColour; }
  227. // mark the given day of the current month as being a holiday
  228. virtual void SetHoliday(size_t WXUNUSED(day)) { }
  229. // customizing the colours of the controls
  230. //
  231. // most of the methods in this section are only implemented by the native
  232. // version of the control and do nothing in the native ones
  233. // set/get the colours to use for the display of the week day names at the
  234. // top of the controls
  235. virtual void SetHeaderColours(const wxColour& WXUNUSED(colFg),
  236. const wxColour& WXUNUSED(colBg)) { }
  237. virtual const wxColour& GetHeaderColourFg() const { return wxNullColour; }
  238. virtual const wxColour& GetHeaderColourBg() const { return wxNullColour; }
  239. // set/get the colours used for the currently selected date
  240. virtual void SetHighlightColours(const wxColour& WXUNUSED(colFg),
  241. const wxColour& WXUNUSED(colBg)) { }
  242. virtual const wxColour& GetHighlightColourFg() const { return wxNullColour; }
  243. virtual const wxColour& GetHighlightColourBg() const { return wxNullColour; }
  244. // implementation only from now on
  245. // generate the given calendar event, return true if it was processed
  246. //
  247. // NB: this is public because it's used from GTK+ callbacks
  248. bool GenerateEvent(wxEventType type)
  249. {
  250. wxCalendarEvent event(this, GetDate(), type);
  251. return HandleWindowEvent(event);
  252. }
  253. protected:
  254. // generate all the events for the selection change from dateOld to current
  255. // date: SEL_CHANGED, PAGE_CHANGED if necessary and also one of (deprecated)
  256. // YEAR/MONTH/DAY_CHANGED ones
  257. //
  258. // returns true if page changed event was generated, false if the new date
  259. // is still in the same month as before
  260. bool GenerateAllChangeEvents(const wxDateTime& dateOld);
  261. // call SetHoliday() for all holidays in the current month
  262. //
  263. // should be called on month change, does nothing if wxCAL_SHOW_HOLIDAYS is
  264. // not set and returns false in this case, true if we do show them
  265. bool SetHolidayAttrs();
  266. // called by SetHolidayAttrs() to forget the previously set holidays
  267. virtual void ResetHolidayAttrs() { }
  268. // called by EnableHolidayDisplay()
  269. virtual void RefreshHolidays() { }
  270. };
  271. // ----------------------------------------------------------------------------
  272. // wxCalendarCtrl
  273. // ----------------------------------------------------------------------------
  274. #define wxCalendarNameStr "CalendarCtrl"
  275. #ifndef __WXUNIVERSAL__
  276. #if defined(__WXGTK20__)
  277. #define wxHAS_NATIVE_CALENDARCTRL
  278. #include "wx/gtk/calctrl.h"
  279. #define wxCalendarCtrl wxGtkCalendarCtrl
  280. #elif defined(__WXMSW__)
  281. #define wxHAS_NATIVE_CALENDARCTRL
  282. #include "wx/msw/calctrl.h"
  283. #endif
  284. #endif // !__WXUNIVERSAL__
  285. #ifndef wxHAS_NATIVE_CALENDARCTRL
  286. #include "wx/generic/calctrlg.h"
  287. #define wxCalendarCtrl wxGenericCalendarCtrl
  288. #endif
  289. // ----------------------------------------------------------------------------
  290. // calendar event types and macros for handling them
  291. // ----------------------------------------------------------------------------
  292. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_SEL_CHANGED, wxCalendarEvent );
  293. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_PAGE_CHANGED, wxCalendarEvent );
  294. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_DOUBLECLICKED, wxCalendarEvent );
  295. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_WEEKDAY_CLICKED, wxCalendarEvent );
  296. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_WEEK_CLICKED, wxCalendarEvent );
  297. // deprecated events
  298. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_DAY_CHANGED, wxCalendarEvent );
  299. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_MONTH_CHANGED, wxCalendarEvent );
  300. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_CALENDAR_YEAR_CHANGED, wxCalendarEvent );
  301. typedef void (wxEvtHandler::*wxCalendarEventFunction)(wxCalendarEvent&);
  302. #define wxCalendarEventHandler(func) \
  303. wxEVENT_HANDLER_CAST(wxCalendarEventFunction, func)
  304. #define wx__DECLARE_CALEVT(evt, id, fn) \
  305. wx__DECLARE_EVT1(wxEVT_CALENDAR_ ## evt, id, wxCalendarEventHandler(fn))
  306. #define EVT_CALENDAR(id, fn) wx__DECLARE_CALEVT(DOUBLECLICKED, id, fn)
  307. #define EVT_CALENDAR_SEL_CHANGED(id, fn) wx__DECLARE_CALEVT(SEL_CHANGED, id, fn)
  308. #define EVT_CALENDAR_PAGE_CHANGED(id, fn) wx__DECLARE_CALEVT(PAGE_CHANGED, id, fn)
  309. #define EVT_CALENDAR_WEEKDAY_CLICKED(id, fn) wx__DECLARE_CALEVT(WEEKDAY_CLICKED, id, fn)
  310. #define EVT_CALENDAR_WEEK_CLICKED(id, fn) wx__DECLARE_CALEVT(WEEK_CLICKED, id, fn)
  311. // deprecated events
  312. #define EVT_CALENDAR_DAY(id, fn) wx__DECLARE_CALEVT(DAY_CHANGED, id, fn)
  313. #define EVT_CALENDAR_MONTH(id, fn) wx__DECLARE_CALEVT(MONTH_CHANGED, id, fn)
  314. #define EVT_CALENDAR_YEAR(id, fn) wx__DECLARE_CALEVT(YEAR_CHANGED, id, fn)
  315. #endif // wxUSE_CALENDARCTRL
  316. #endif // _WX_CALCTRL_H_