]> git.saurik.com Git - wxWidgets.git/blob - src/generic/calctrl.cpp
1. some minor compilation fixes in datetime.cppm
[wxWidgets.git] / src / generic / calctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/calctrl.cpp
3 // Purpose: implementation fo the generic wxCalendarCtrl
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29.12.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "calctrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #endif //WX_PRECOMP
33
34 #include "wx/generic/calctrl.h"
35
36 // ----------------------------------------------------------------------------
37 // wxWin macros
38 // ----------------------------------------------------------------------------
39
40 BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
41 EVT_PAINT(wxCalendarCtrl::OnPaint)
42
43 EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
44 END_EVENT_TABLE()
45
46 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
47
48 // ============================================================================
49 // implementation
50 // ============================================================================
51
52 // ----------------------------------------------------------------------------
53 // wxCalendarCtrl
54 // ----------------------------------------------------------------------------
55
56 void wxCalendarCtrl::Init()
57 {
58 m_comboMonth = NULL;
59 m_spinYear = NULL;
60
61 m_widthCol =
62 m_heightRow = 0;
63 }
64
65 bool wxCalendarCtrl::Create(wxWindow *parent,
66 wxWindowID id,
67 const wxDateTime& date,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73 m_date = date.IsValid() ? date : wxDateTime::Today();
74
75 wxSize sizeReal;
76 if ( size.x == -1 || size.y == -1 )
77 {
78 sizeReal = DoGetBestSize();
79 if ( size.x != -1 )
80 sizeReal.x = size.x;
81 if ( size.y != -1 )
82 sizeReal.y = size.y;
83 }
84 else
85 {
86 sizeReal = size;
87 }
88
89 SetSize(sizeReal);
90
91 SetBackgroundColour(*wxWHITE);
92
93 return TRUE;
94 }
95
96 // ----------------------------------------------------------------------------
97 // date helpers
98 // ----------------------------------------------------------------------------
99
100 wxDateTime wxCalendarCtrl::GetStartDate() const
101 {
102 wxDateTime::Tm tm = m_date.GetTm();
103
104 wxDateTime date = wxDateTime(1, tm.mon, tm.year);
105 date.SetToPrevWeekDay(wxDateTime::Sun);
106
107 return date;
108 }
109
110 bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const
111 {
112 return date.GetMonth() == m_date.GetMonth();
113 }
114
115 // ----------------------------------------------------------------------------
116 // size management
117 // ----------------------------------------------------------------------------
118
119 wxSize wxCalendarCtrl::DoGetBestSize() const
120 {
121 return wxSize(230, 200);
122 }
123
124 void wxCalendarCtrl::DoSetSize(int x, int y,
125 int width, int height,
126 int sizeFlags)
127 {
128 wxControl::DoSetSize(x, y, width, height, sizeFlags);
129 }
130
131 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
132 {
133 wxControl::DoMoveWindow(x, y, width, height);
134 }
135
136 // ----------------------------------------------------------------------------
137 // drawing
138 // ----------------------------------------------------------------------------
139
140 void wxCalendarCtrl::OnPaint(wxPaintEvent& event)
141 {
142 wxPaintDC dc(this);
143
144 wxDateTime::WeekDay wd;
145 wxString weekdays[7];
146
147 dc.SetFont(*wxSWISS_FONT);
148
149 // determine the column width (we assume that the weekday names are always
150 // wider (in any language) than the numbers)
151 m_widthCol = 0;
152 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
153 {
154 weekdays[wd] = wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr);
155
156 wxCoord width;
157 dc.GetTextExtent(weekdays[wd], &width, &m_heightRow);
158 if ( width > m_widthCol )
159 {
160 m_widthCol = width;
161 }
162 }
163
164 // leave some margins
165 m_widthCol += 2;
166 m_heightRow += 2;
167
168 // first draw the week days
169 dc.SetTextForeground(*wxBLUE);
170 dc.SetBrush(wxBrush(*wxLIGHT_GREY, wxSOLID));
171 dc.SetBackgroundMode(wxTRANSPARENT);
172 dc.SetPen(*wxWHITE_PEN);
173 dc.DrawRectangle(0, 0, 7*m_widthCol - 1, m_heightRow);
174 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
175 {
176 dc.DrawText(weekdays[wd], wd*m_widthCol + 1, 0);
177 }
178
179 // then the calendar itself
180 dc.SetTextForeground(*wxBLACK);
181 //dc.SetFont(*wxNORMAL_FONT);
182
183 wxCoord y = m_heightRow;
184
185 wxDateTime date = GetStartDate();
186 dc.SetBackgroundMode(wxSOLID);
187 for ( size_t nWeek = 0; nWeek < 6; nWeek++ )
188 {
189 for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
190 {
191 if ( IsDateShown(date) )
192 {
193 wxString day = wxString::Format(_T("%u"), date.GetDay());
194 wxCoord width;
195 dc.GetTextExtent(day, &width, (wxCoord *)NULL);
196
197 bool isSel = m_date == date;
198 if ( isSel )
199 {
200 dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
201 dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT));
202 }
203
204 dc.DrawText(day, wd*m_widthCol + (m_widthCol - width) / 2, y);
205
206 if ( isSel )
207 {
208 dc.SetTextForeground(*wxBLACK);
209 dc.SetTextBackground(m_backgroundColour);
210 }
211 }
212 //else: just don't draw it
213
214 date += wxDateSpan::Day();
215 }
216
217 y += m_heightRow;
218 }
219 }
220
221 // ----------------------------------------------------------------------------
222 // mouse handling
223 // ----------------------------------------------------------------------------
224
225 void wxCalendarCtrl::OnClick(wxMouseEvent& event)
226 {
227 wxDateTime date;
228 if ( !HitTest(event.GetPosition(), &date) )
229 {
230 event.Skip();
231 }
232 else
233 {
234 m_date = date;
235
236 Refresh();
237 }
238 }
239
240 bool wxCalendarCtrl::HitTest(const wxPoint& pos, wxDateTime *date)
241 {
242 wxCoord y = pos.y;
243 if ( y < m_heightRow )
244 return FALSE;
245
246 y -= m_heightRow;
247 int week = y / m_heightRow,
248 wday = pos.x / m_widthCol;
249
250 if ( week >= 6 || wday >= 7 )
251 return FALSE;
252
253 wxCHECK_MSG( date, FALSE, _T("bad pointer in wxCalendarCtrl::HitTest") );
254
255 *date = GetStartDate();
256 *date += wxDateSpan::Days(7*week + wday);
257 return IsDateShown(*date);
258 }