]>
Commit | Line | Data |
---|---|---|
feb72429 VZ |
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 | ||
9b877d18 WS |
29 | #if wxUSE_DATEPICKCTRL |
30 | ||
feb72429 | 31 | #include "wx/datectrl.h" |
0aa7cb54 VZ |
32 | #include "wx/app.h" |
33 | #include "wx/intl.h" | |
34 | #include "wx/dynlib.h" | |
feb72429 VZ |
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" | |
4510fc80 | 41 | #include "wx/msw/private.h" |
feb72429 VZ |
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 | { | |
0aa7cb54 VZ |
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 | ||
5385747e VZ |
121 | // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style |
122 | if ( !(style & wxDP_DROPDOWN) ) | |
123 | style |= wxDP_SPIN; | |
124 | ||
feb72429 VZ |
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 | ||
0aa7cb54 VZ |
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) ) | |
29c86948 VZ |
146 | styleMSW |= DTS_UPDOWN; |
147 | //else: drop down by default | |
148 | ||
149 | styleMSW |= DTS_SHORTDATEFORMAT; | |
feb72429 VZ |
150 | |
151 | return styleMSW; | |
152 | } | |
153 | ||
154 | // TODO: handle WM_WININICHANGE | |
155 | ||
156 | // ---------------------------------------------------------------------------- | |
157 | // wxDatePickerCtrl geometry | |
158 | // ---------------------------------------------------------------------------- | |
159 | ||
160 | wxSize wxDatePickerCtrl::DoGetBestSize() const | |
161 | { | |
162 | const int y = GetCharHeight(); | |
163 | ||
164 | return wxSize(DEFAULT_ITEM_WIDTH, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y)); | |
165 | } | |
166 | ||
167 | // ---------------------------------------------------------------------------- | |
168 | // wxDatePickerCtrl operations | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
171 | void wxDatePickerCtrl::SetValue(const wxDateTime& dt) | |
172 | { | |
173 | // as we don't support DTS_SHOWNONE style so far, we don't allow setting | |
174 | // the control to an invalid date, but this restriction may be lifted in | |
175 | // the future | |
176 | wxCHECK_RET( dt.IsValid(), _T("invalid date") ); | |
177 | ||
178 | SYSTEMTIME st; | |
179 | wxToSystemTime(&st, dt); | |
180 | if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID, &st) ) | |
181 | { | |
182 | wxLogDebug(_T("DateTime_SetSystemtime() failed")); | |
183 | } | |
184 | } | |
185 | ||
186 | wxDateTime wxDatePickerCtrl::GetValue() const | |
187 | { | |
188 | wxDateTime dt; | |
189 | SYSTEMTIME st; | |
190 | if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID ) | |
191 | { | |
192 | wxFromSystemTime(&dt, st); | |
193 | } | |
194 | ||
195 | return dt; | |
196 | } | |
197 | ||
198 | void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2) | |
199 | { | |
200 | SYSTEMTIME st[2]; | |
201 | ||
202 | DWORD flags = 0; | |
203 | if ( dt1.IsValid() ) | |
204 | { | |
205 | wxToSystemTime(&st[0], dt1); | |
206 | flags |= GDTR_MIN; | |
207 | } | |
208 | ||
209 | if ( dt2.IsValid() ) | |
210 | { | |
211 | wxToSystemTime(&st[1], dt2); | |
212 | flags |= GDTR_MAX; | |
213 | } | |
214 | ||
215 | if ( !DateTime_SetRange(GetHwnd(), flags, st) ) | |
216 | { | |
217 | wxLogDebug(_T("DateTime_SetRange() failed")); | |
218 | } | |
219 | } | |
220 | ||
221 | bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const | |
222 | { | |
223 | SYSTEMTIME st[2]; | |
224 | ||
225 | DWORD flags = DateTime_GetRange(GetHwnd(), st); | |
226 | if ( dt1 ) | |
227 | { | |
228 | if ( flags & GDTR_MIN ) | |
229 | wxFromSystemTime(dt1, st[0]); | |
230 | else | |
231 | *dt1 = wxDefaultDateTime; | |
232 | } | |
233 | ||
234 | if ( dt2 ) | |
235 | { | |
236 | if ( flags & GDTR_MAX ) | |
237 | wxFromSystemTime(dt2, st[1]); | |
238 | else | |
239 | *dt2 = wxDefaultDateTime; | |
240 | } | |
241 | ||
242 | return flags != 0; | |
243 | } | |
244 | ||
245 | // ---------------------------------------------------------------------------- | |
246 | // wxDatePickerCtrl events | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
249 | bool | |
250 | wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
251 | { | |
252 | NMHDR* hdr = (NMHDR *)lParam; | |
253 | switch ( hdr->code ) | |
254 | { | |
255 | case DTN_DATETIMECHANGE: | |
256 | NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr; | |
257 | wxDateTime dt; | |
258 | if ( dtch->dwFlags == GDT_VALID ) | |
259 | wxFromSystemTime(&dt, dtch->st); | |
260 | ||
261 | wxDateEvent event(this, dt, wxEVT_DATE_CHANGED); | |
262 | if ( GetEventHandler()->ProcessEvent(event) ) | |
263 | { | |
264 | *result = 0; | |
265 | return true; | |
266 | } | |
267 | } | |
268 | ||
269 | return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
270 | } | |
271 | ||
9b877d18 | 272 | #endif // wxUSE_DATEPICKCTRL |
29c86948 | 273 |