added and implemented for MSW wxDP_SHOWCENTURY flag
[wxWidgets.git] / src / msw / datectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/datectrl.cpp
3 // Purpose: wxDatePickerCtrl implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2005-01-09
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #endif
28
29 #if wxUSE_DATEPICKCTRL
30
31 #include "wx/datectrl.h"
32 #include "wx/app.h"
33 #include "wx/intl.h"
34 #include "wx/dynlib.h"
35
36 #define _WX_DEFINE_DATE_EVENTS_
37 #include "wx/dateevt.h"
38
39 #include "wx/msw/wrapwin.h"
40 #include "wx/msw/wrapcctl.h"
41 #include "wx/msw/private.h"
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 // ----------------------------------------------------------------------------
48 // helpers for wxDateTime <-> SYSTEMTIME conversion
49 // ----------------------------------------------------------------------------
50
51 static inline void wxFromSystemTime(wxDateTime *dt, const SYSTEMTIME& st)
52 {
53 dt->Set(st.wDay,
54 wx_static_cast(wxDateTime::Month, wxDateTime::Jan + st.wMonth - 1),
55 st.wYear,
56 0, 0, 0);
57 }
58
59 static inline void wxToSystemTime(SYSTEMTIME *st, const wxDateTime& dt)
60 {
61 const wxDateTime::Tm tm(dt.GetTm());
62
63 st->wYear = tm.year;
64 st->wMonth = tm.mon - wxDateTime::Jan + 1;
65 st->wDay = tm.mday;
66
67 st->wDayOfWeek =
68 st->wHour =
69 st->wMinute =
70 st->wSecond =
71 st->wMilliseconds = 0;
72 }
73
74 // ----------------------------------------------------------------------------
75 // wxDatePickerCtrl creation
76 // ----------------------------------------------------------------------------
77
78 bool
79 wxDatePickerCtrl::Create(wxWindow *parent,
80 wxWindowID id,
81 const wxDateTime& dt,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxValidator& validator,
86 const wxString& name)
87 {
88 // although we already call InitCommonControls() in app.cpp which is
89 // supposed to initialize all common controls, in comctl32.dll 4.72 (and
90 // presumably earlier versions 4.70 and 4.71, date time picker not being
91 // supported in < 4.70 anyhow) it does not do it and we have to initialize
92 // it explicitely
93 static bool s_initDone = false; // MT-ok: used from GUI thread only
94 if ( !s_initDone )
95 {
96 if ( wxTheApp->GetComCtl32Version() < 470 )
97 {
98 wxLogError(_("This system doesn't support date picker control, please upgrade your version of comctl32.dll"));
99
100 return false;
101 }
102
103 INITCOMMONCONTROLSEX icex;
104 icex.dwSize = sizeof(icex);
105 icex.dwICC = ICC_DATE_CLASSES;
106
107 wxDynamicLibrary dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM);
108
109 typedef BOOL (WINAPI *ICCEx_t)(INITCOMMONCONTROLSEX *);
110 wxDYNLIB_FUNCTION( ICCEx_t, InitCommonControlsEx, dllComCtl32 );
111
112 if ( pfnInitCommonControlsEx )
113 {
114 (*pfnInitCommonControlsEx)(&icex);
115 }
116
117 s_initDone = true;
118 }
119
120
121 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
122 if ( !(style & wxDP_DROPDOWN) )
123 style |= wxDP_SPIN;
124
125 // initialize the base class
126 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
127 return false;
128
129 // create the native control
130 if ( !MSWCreateControl(DATETIMEPICK_CLASS, _T(""), pos, size) )
131 return false;
132
133 if ( dt.IsValid() )
134 SetValue(dt);
135
136 return true;
137 }
138
139 WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
140 {
141 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
142
143 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
144 // comctl32.dll 4.72
145 if ( wxTheApp->GetComCtl32Version() > 472 && (style & wxDP_SPIN) )
146 styleMSW |= DTS_UPDOWN;
147 //else: drop down by default
148
149 #ifdef DTS_SHORTDATECENTURYFORMAT
150 if ( style & wxDP_SHOWCENTURY )
151 styleMSW |= DTS_SHORTDATECENTURYFORMAT;
152 else
153 #endif // DTS_SHORTDATECENTURYFORMAT
154 styleMSW |= DTS_SHORTDATEFORMAT;
155
156 return styleMSW;
157 }
158
159 // TODO: handle WM_WININICHANGE
160
161 // ----------------------------------------------------------------------------
162 // wxDatePickerCtrl geometry
163 // ----------------------------------------------------------------------------
164
165 wxSize wxDatePickerCtrl::DoGetBestSize() const
166 {
167 const int y = GetCharHeight();
168
169 return wxSize(DEFAULT_ITEM_WIDTH, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
170 }
171
172 // ----------------------------------------------------------------------------
173 // wxDatePickerCtrl operations
174 // ----------------------------------------------------------------------------
175
176 void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
177 {
178 // as we don't support DTS_SHOWNONE style so far, we don't allow setting
179 // the control to an invalid date, but this restriction may be lifted in
180 // the future
181 wxCHECK_RET( dt.IsValid(), _T("invalid date") );
182
183 SYSTEMTIME st;
184 wxToSystemTime(&st, dt);
185 if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID, &st) )
186 {
187 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
188 }
189 }
190
191 wxDateTime wxDatePickerCtrl::GetValue() const
192 {
193 wxDateTime dt;
194 SYSTEMTIME st;
195 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
196 {
197 wxFromSystemTime(&dt, st);
198 }
199
200 return dt;
201 }
202
203 void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
204 {
205 SYSTEMTIME st[2];
206
207 DWORD flags = 0;
208 if ( dt1.IsValid() )
209 {
210 wxToSystemTime(&st[0], dt1);
211 flags |= GDTR_MIN;
212 }
213
214 if ( dt2.IsValid() )
215 {
216 wxToSystemTime(&st[1], dt2);
217 flags |= GDTR_MAX;
218 }
219
220 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
221 {
222 wxLogDebug(_T("DateTime_SetRange() failed"));
223 }
224 }
225
226 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
227 {
228 SYSTEMTIME st[2];
229
230 DWORD flags = DateTime_GetRange(GetHwnd(), st);
231 if ( dt1 )
232 {
233 if ( flags & GDTR_MIN )
234 wxFromSystemTime(dt1, st[0]);
235 else
236 *dt1 = wxDefaultDateTime;
237 }
238
239 if ( dt2 )
240 {
241 if ( flags & GDTR_MAX )
242 wxFromSystemTime(dt2, st[1]);
243 else
244 *dt2 = wxDefaultDateTime;
245 }
246
247 return flags != 0;
248 }
249
250 // ----------------------------------------------------------------------------
251 // wxDatePickerCtrl events
252 // ----------------------------------------------------------------------------
253
254 bool
255 wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
256 {
257 NMHDR* hdr = (NMHDR *)lParam;
258 switch ( hdr->code )
259 {
260 case DTN_DATETIMECHANGE:
261 NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr;
262 wxDateTime dt;
263 if ( dtch->dwFlags == GDT_VALID )
264 wxFromSystemTime(&dt, dtch->st);
265
266 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
267 if ( GetEventHandler()->ProcessEvent(event) )
268 {
269 *result = 0;
270 return true;
271 }
272 }
273
274 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
275 }
276
277 #endif // wxUSE_DATEPICKCTRL
278