1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.h
3 // Purpose: generic implementation of date-picker control
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma interface "calctrl.h"
16 #ifndef _WX_GENERIC_CALCTRL_H
17 #define _WX_GENERIC_CALCTRL_H
19 #include "wx/control.h" // the base class
20 #include "wx/dcclient.h" // for wxPaintDC
21 #include "wx/spinctrl.h" // for wxSpinEvent
23 class WXDLLEXPORT wxComboBox
;
24 class WXDLLEXPORT wxStaticText
;
26 #define wxCalendarNameStr _T("CalendarCtrl")
28 // ----------------------------------------------------------------------------
29 // wxCalendarCtrl: a control allowing the user to pick a date interactively
30 // ----------------------------------------------------------------------------
32 class WXDLLEXPORT wxCalendarCtrl
: public wxControl
34 friend class wxMonthComboBox
;
35 friend class wxYearSpinCtrl
;
39 wxCalendarCtrl() { Init(); }
40 wxCalendarCtrl(wxWindow
*parent
,
42 const wxDateTime
& date
= wxDefaultDateTime
,
43 const wxPoint
& pos
= wxDefaultPosition
,
44 const wxSize
& size
= wxDefaultSize
,
45 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
46 const wxString
& name
= wxCalendarNameStr
)
50 (void)Create(parent
, id
, date
, pos
, size
, style
, name
);
53 bool Create(wxWindow
*parent
,
55 const wxDateTime
& date
= wxDefaultDateTime
,
56 const wxPoint
& pos
= wxDefaultPosition
,
57 const wxSize
& size
= wxDefaultSize
,
58 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
59 const wxString
& name
= wxCalendarNameStr
);
61 virtual ~wxCalendarCtrl();
63 // set/get the current date
64 // ------------------------
66 bool SetDate(const wxDateTime
& date
); // we need to be able to control if the event should be sent in SetDateAndNotify(...)
67 const wxDateTime
& GetDate() const { return m_date
; }
69 // set/get the range in which selection can occur
70 // ---------------------------------------------
72 bool SetLowerDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
73 const wxDateTime
& GetLowerDateLimit() const { return m_lowdate
; }
74 bool SetUpperDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
75 const wxDateTime
& GetUpperDateLimit() const { return m_highdate
; }
77 bool SetDateRange(const wxDateTime
& lowerdate
= wxDefaultDateTime
, const wxDateTime
& upperdate
= wxDefaultDateTime
);
82 // some calendar styles can't be changed after the control creation by
83 // just using SetWindowStyle() and Refresh() and the functions below
84 // should be used instead for them
86 // corresponds to wxCAL_NO_YEAR_CHANGE bit
87 void EnableYearChange(bool enable
= TRUE
);
89 // corresponds to wxCAL_NO_MONTH_CHANGE bit
90 void EnableMonthChange(bool enable
= TRUE
);
92 // corresponds to wxCAL_SHOW_HOLIDAYS bit
93 void EnableHolidayDisplay(bool display
= TRUE
);
98 // header colours are used for painting the weekdays at the top
99 void SetHeaderColours(const wxColour
& colFg
, const wxColour
& colBg
)
101 m_colHeaderFg
= colFg
;
102 m_colHeaderBg
= colBg
;
105 const wxColour
& GetHeaderColourFg() const { return m_colHeaderFg
; }
106 const wxColour
& GetHeaderColourBg() const { return m_colHeaderBg
; }
108 // highlight colour is used for the currently selected date
109 void SetHighlightColours(const wxColour
& colFg
, const wxColour
& colBg
)
111 m_colHighlightFg
= colFg
;
112 m_colHighlightBg
= colBg
;
115 const wxColour
& GetHighlightColourFg() const { return m_colHighlightFg
; }
116 const wxColour
& GetHighlightColourBg() const { return m_colHighlightBg
; }
118 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
119 void SetHolidayColours(const wxColour
& colFg
, const wxColour
& colBg
)
121 m_colHolidayFg
= colFg
;
122 m_colHolidayBg
= colBg
;
125 const wxColour
& GetHolidayColourFg() const { return m_colHolidayFg
; }
126 const wxColour
& GetHolidayColourBg() const { return m_colHolidayBg
; }
128 // an item without custom attributes is drawn with the default colours and
129 // font and without border, setting custom attributes allows to modify this
131 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
132 // corresponding attribute is just unused if there is no such day in the
135 wxCalendarDateAttr
*GetAttr(size_t day
) const
137 wxCHECK_MSG( day
> 0 && day
< 32, NULL
, _T("invalid day") );
139 return m_attrs
[day
- 1];
142 void SetAttr(size_t day
, wxCalendarDateAttr
*attr
)
144 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day") );
146 delete m_attrs
[day
- 1];
147 m_attrs
[day
- 1] = attr
;
150 void SetHoliday(size_t day
);
152 void ResetAttr(size_t day
) { SetAttr(day
, (wxCalendarDateAttr
*)NULL
); }
154 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
155 // with the corresponding value (none for NOWHERE, the date for DAY and wd
157 wxCalendarHitTestResult
HitTest(const wxPoint
& pos
,
158 wxDateTime
*date
= NULL
,
159 wxDateTime::WeekDay
*wd
= NULL
);
161 // implementation only from now on
162 // -------------------------------
164 // forward these functions to all subcontrols
165 virtual bool Enable(bool enable
= TRUE
);
166 virtual bool Show(bool show
= TRUE
);
169 // common part of all ctors
173 void OnPaint(wxPaintEvent
& event
);
174 void OnClick(wxMouseEvent
& event
);
175 void OnDClick(wxMouseEvent
& event
);
176 void OnChar(wxKeyEvent
& event
);
177 void OnMonthChange(wxCommandEvent
& event
);
178 void OnYearChange(wxSpinEvent
& event
);
180 // override some base class virtuals
181 virtual wxSize
DoGetBestSize() const;
182 virtual void DoGetPosition(int *x
, int *y
) const;
183 virtual void DoGetSize(int *width
, int *height
) const;
184 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
185 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
187 // (re)calc m_widthCol and m_heightRow
188 void RecalcGeometry();
190 // set the date and send the notification
191 void SetDateAndNotify(const wxDateTime
& date
);
193 // get the week (row, in range 1..6) for the given date
194 size_t GetWeek(const wxDateTime
& date
) const;
196 // get the date from which we start drawing days
197 wxDateTime
GetStartDate() const;
199 // is this date shown?
200 bool IsDateShown(const wxDateTime
& date
) const;
202 // is this date in the given range?
203 bool IsDateInRange(const wxDateTime
& date
) const;
206 bool ChangeYear(wxDateTime
* target
) const;
207 bool ChangeMonth(wxDateTime
* target
) const;
209 // redraw the given date
210 void RefreshDate(const wxDateTime
& date
);
212 // change the date inside the same month/year
213 void ChangeDay(const wxDateTime
& date
);
215 // set the attributes for the holidays if needed
216 void SetHolidayAttrs();
218 // reset all holidays
219 void ResetHolidayAttrs();
221 // generate the given calendar event(s)
222 void GenerateEvent(wxEventType type
)
224 wxCalendarEvent
event(this, type
);
225 (void)GetEventHandler()->ProcessEvent(event
);
228 void GenerateEvents(wxEventType type1
, wxEventType type2
)
230 GenerateEvent(type1
);
231 GenerateEvent(type2
);
234 // do we allow changing the month/year?
235 bool AllowMonthChange() const
237 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
238 != wxCAL_NO_MONTH_CHANGE
;
240 bool AllowYearChange() const
242 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
245 // show the correct controls
246 void ShowCurrentControls();
248 // get the currently shown control for month/year
249 wxControl
*GetMonthControl() const;
250 wxControl
*GetYearControl() const;
252 // OnPaint helper-methods
254 // Highlight the [fromdate : todate] range using pen and brush
255 void HighlightRange(wxPaintDC
* dc
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pen
, wxBrush
* brush
);
257 // Get the "coordinates" for the date relative to the month currently displayed.
258 // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6)
259 // if the date isn't visible (-1, -1) is put in (day, week) and false is returned
260 bool GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const;
263 wxStaticText
*m_staticMonth
;
264 wxComboBox
*m_comboMonth
;
266 wxStaticText
*m_staticYear
;
267 wxSpinCtrl
*m_spinYear
;
269 // the current selection
273 wxDateTime m_lowdate
;
274 wxDateTime m_highdate
;
276 // default attributes
277 wxColour m_colHighlightFg
,
284 // the attributes for each of the month days
285 wxCalendarDateAttr
*m_attrs
[31];
287 // the width and height of one column/row in the calendar
292 wxRect m_leftArrowRect
,
295 // the week day names
296 wxString m_weekdays
[7];
298 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
299 DECLARE_EVENT_TABLE()
302 #endif // _WX_GENERIC_CALCTRL_H