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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
22 class WXDLLEXPORT wxComboBox
;
23 class WXDLLEXPORT wxStaticText
;
24 class WXDLLEXPORT wxSpinCtrl
;
26 #define wxCalendarNameStr _T("CalendarCtrl")
28 // ----------------------------------------------------------------------------
29 // wxCalendarCtrl: a control allowing the user to pick a date interactively
30 // ----------------------------------------------------------------------------
32 class WXDLLIMPEXP_ADV 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
);
48 bool Create(wxWindow
*parent
,
50 const wxDateTime
& date
= wxDefaultDateTime
,
51 const wxPoint
& pos
= wxDefaultPosition
,
52 const wxSize
& size
= wxDefaultSize
,
53 long style
= wxCAL_SHOW_HOLIDAYS
| wxWANTS_CHARS
,
54 const wxString
& name
= wxCalendarNameStr
);
56 virtual ~wxCalendarCtrl();
58 virtual bool Destroy();
60 // set/get the current date
61 // ------------------------
63 bool SetDate(const wxDateTime
& date
); // we need to be able to control if the event should be sent in SetDateAndNotify(...)
64 const wxDateTime
& GetDate() const { return m_date
; }
66 // set/get the range in which selection can occur
67 // ---------------------------------------------
69 bool SetLowerDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
70 const wxDateTime
& GetLowerDateLimit() const { return m_lowdate
; }
71 bool SetUpperDateLimit(const wxDateTime
& date
= wxDefaultDateTime
);
72 const wxDateTime
& GetUpperDateLimit() const { return m_highdate
; }
74 bool SetDateRange(const wxDateTime
& lowerdate
= wxDefaultDateTime
, const wxDateTime
& upperdate
= wxDefaultDateTime
);
79 // some calendar styles can't be changed after the control creation by
80 // just using SetWindowStyle() and Refresh() and the functions below
81 // should be used instead for them
83 // corresponds to wxCAL_NO_YEAR_CHANGE bit
84 void EnableYearChange(bool enable
= TRUE
);
86 // corresponds to wxCAL_NO_MONTH_CHANGE bit
87 void EnableMonthChange(bool enable
= TRUE
);
89 // corresponds to wxCAL_SHOW_HOLIDAYS bit
90 void EnableHolidayDisplay(bool display
= TRUE
);
95 // header colours are used for painting the weekdays at the top
96 void SetHeaderColours(const wxColour
& colFg
, const wxColour
& colBg
)
98 m_colHeaderFg
= colFg
;
99 m_colHeaderBg
= colBg
;
102 const wxColour
& GetHeaderColourFg() const { return m_colHeaderFg
; }
103 const wxColour
& GetHeaderColourBg() const { return m_colHeaderBg
; }
105 // highlight colour is used for the currently selected date
106 void SetHighlightColours(const wxColour
& colFg
, const wxColour
& colBg
)
108 m_colHighlightFg
= colFg
;
109 m_colHighlightBg
= colBg
;
112 const wxColour
& GetHighlightColourFg() const { return m_colHighlightFg
; }
113 const wxColour
& GetHighlightColourBg() const { return m_colHighlightBg
; }
115 // holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
116 void SetHolidayColours(const wxColour
& colFg
, const wxColour
& colBg
)
118 m_colHolidayFg
= colFg
;
119 m_colHolidayBg
= colBg
;
122 const wxColour
& GetHolidayColourFg() const { return m_colHolidayFg
; }
123 const wxColour
& GetHolidayColourBg() const { return m_colHolidayBg
; }
125 // an item without custom attributes is drawn with the default colours and
126 // font and without border, setting custom attributes allows to modify this
128 // the day parameter should be in 1..31 range, for days 29, 30, 31 the
129 // corresponding attribute is just unused if there is no such day in the
132 wxCalendarDateAttr
*GetAttr(size_t day
) const
134 wxCHECK_MSG( day
> 0 && day
< 32, NULL
, _T("invalid day") );
136 return m_attrs
[day
- 1];
139 void SetAttr(size_t day
, wxCalendarDateAttr
*attr
)
141 wxCHECK_RET( day
> 0 && day
< 32, _T("invalid day") );
143 delete m_attrs
[day
- 1];
144 m_attrs
[day
- 1] = attr
;
147 void SetHoliday(size_t day
);
149 void ResetAttr(size_t day
) { SetAttr(day
, (wxCalendarDateAttr
*)NULL
); }
151 // returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
152 // with the corresponding value (none for NOWHERE, the date for DAY and wd
154 wxCalendarHitTestResult
HitTest(const wxPoint
& pos
,
155 wxDateTime
*date
= NULL
,
156 wxDateTime::WeekDay
*wd
= NULL
);
158 // implementation only from now on
159 // -------------------------------
161 // forward these functions to all subcontrols
162 virtual bool Enable(bool enable
= TRUE
);
163 virtual bool Show(bool show
= TRUE
);
166 // common part of all ctors
170 void OnPaint(wxPaintEvent
& event
);
171 void OnClick(wxMouseEvent
& event
);
172 void OnDClick(wxMouseEvent
& event
);
173 void OnChar(wxKeyEvent
& event
);
174 void OnMonthChange(wxCommandEvent
& event
);
175 void OnYearChange(wxCommandEvent
& event
);
177 // override some base class virtuals
178 virtual wxSize
DoGetBestSize() const;
179 virtual void DoGetPosition(int *x
, int *y
) const;
180 virtual void DoGetSize(int *width
, int *height
) const;
181 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
182 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
184 // (re)calc m_widthCol and m_heightRow
185 void RecalcGeometry();
187 // set the date and send the notification
188 void SetDateAndNotify(const wxDateTime
& date
);
190 // get the week (row, in range 1..6) for the given date
191 size_t GetWeek(const wxDateTime
& date
) const;
193 // get the date from which we start drawing days
194 wxDateTime
GetStartDate() const;
196 // is this date shown?
197 bool IsDateShown(const wxDateTime
& date
) const;
199 // is this date in the given range?
200 bool IsDateInRange(const wxDateTime
& date
) const;
203 bool ChangeYear(wxDateTime
* target
) const;
204 bool ChangeMonth(wxDateTime
* target
) const;
206 // redraw the given date
207 void RefreshDate(const wxDateTime
& date
);
209 // change the date inside the same month/year
210 void ChangeDay(const wxDateTime
& date
);
212 // set the attributes for the holidays if needed
213 void SetHolidayAttrs();
215 // reset all holidays
216 void ResetHolidayAttrs();
218 // generate the given calendar event(s)
219 void GenerateEvent(wxEventType type
)
221 wxCalendarEvent
event(this, type
);
222 (void)GetEventHandler()->ProcessEvent(event
);
225 void GenerateEvents(wxEventType type1
, wxEventType type2
)
227 GenerateEvent(type1
);
228 GenerateEvent(type2
);
231 // do we allow changing the month/year?
232 bool AllowMonthChange() const
234 return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE
)
235 != wxCAL_NO_MONTH_CHANGE
;
237 bool AllowYearChange() const
239 return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE
);
242 // show the correct controls
243 void ShowCurrentControls();
246 // get the currently shown control for month/year
247 wxControl
*GetMonthControl() const;
248 wxControl
*GetYearControl() const;
251 // OnPaint helper-methods
253 // Highlight the [fromdate : todate] range using pen and brush
254 void HighlightRange(wxPaintDC
* dc
, const wxDateTime
& fromdate
, const wxDateTime
& todate
, wxPen
* pen
, wxBrush
* brush
);
256 // Get the "coordinates" for the date relative to the month currently displayed.
257 // using (day, week): upper left coord is (1, 1), lower right coord is (7, 6)
258 // if the date isn't visible (-1, -1) is put in (day, week) and false is returned
259 bool GetDateCoord(const wxDateTime
& date
, int *day
, int *week
) const;
261 // Set the flag for SetDate(): otherwise it would overwrite the year
262 // typed in by the user
263 void SetUserChangedYear() { m_userChangedYear
= TRUE
; }
266 wxStaticText
*m_staticMonth
;
267 wxComboBox
*m_comboMonth
;
269 wxStaticText
*m_staticYear
;
270 wxSpinCtrl
*m_spinYear
;
272 // the current selection
276 wxDateTime m_lowdate
;
277 wxDateTime m_highdate
;
279 // default attributes
280 wxColour m_colHighlightFg
,
287 // the attributes for each of the month days
288 wxCalendarDateAttr
*m_attrs
[31];
290 // the width and height of one column/row in the calendar
295 wxRect m_leftArrowRect
,
298 // the week day names
299 wxString m_weekdays
[7];
301 // TRUE if SetDate() is being called as the result of changing the year in
303 bool m_userChangedYear
;
305 DECLARE_DYNAMIC_CLASS(wxCalendarCtrl
)
306 DECLARE_EVENT_TABLE()
307 DECLARE_NO_COPY_CLASS(wxCalendarCtrl
)
310 #endif // _WX_GENERIC_CALCTRL_H