]> git.saurik.com Git - wxWidgets.git/blame - src/msw/datectrl.cpp
Compilation fix for unix builds with cygwin
[wxWidgets.git] / src / msw / datectrl.cpp
CommitLineData
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 42
3200f37d
VZ
43// apparently some versions of mingw define these macros erroneously
44#ifndef DateTime_GetSystemtime
45 #define DateTime_GetSystemtime DateTime_GetSystemTime
46#endif
47
48#ifndef DateTime_SetSystemtime
49 #define DateTime_SetSystemtime DateTime_SetSystemTime
65ab1002
JS
50#endif
51
a69e2a0a
VZ
52IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
53
feb72429
VZ
54// ============================================================================
55// implementation
56// ============================================================================
57
58// ----------------------------------------------------------------------------
59// helpers for wxDateTime <-> SYSTEMTIME conversion
60// ----------------------------------------------------------------------------
61
62static inline void wxFromSystemTime(wxDateTime *dt, const SYSTEMTIME& st)
63{
64 dt->Set(st.wDay,
65 wx_static_cast(wxDateTime::Month, wxDateTime::Jan + st.wMonth - 1),
66 st.wYear,
67 0, 0, 0);
68}
69
70static inline void wxToSystemTime(SYSTEMTIME *st, const wxDateTime& dt)
71{
72 const wxDateTime::Tm tm(dt.GetTm());
73
3a0c6181
WS
74 st->wYear = (WXWORD)tm.year;
75 st->wMonth = (WXWORD)(tm.mon - wxDateTime::Jan + 1);
feb72429
VZ
76 st->wDay = tm.mday;
77
78 st->wDayOfWeek =
79 st->wHour =
80 st->wMinute =
81 st->wSecond =
82 st->wMilliseconds = 0;
83}
84
85// ----------------------------------------------------------------------------
86// wxDatePickerCtrl creation
87// ----------------------------------------------------------------------------
88
89bool
90wxDatePickerCtrl::Create(wxWindow *parent,
91 wxWindowID id,
92 const wxDateTime& dt,
93 const wxPoint& pos,
94 const wxSize& size,
95 long style,
96 const wxValidator& validator,
97 const wxString& name)
98{
0aa7cb54
VZ
99 // although we already call InitCommonControls() in app.cpp which is
100 // supposed to initialize all common controls, in comctl32.dll 4.72 (and
101 // presumably earlier versions 4.70 and 4.71, date time picker not being
102 // supported in < 4.70 anyhow) it does not do it and we have to initialize
3103e8a9 103 // it explicitly
0aa7cb54
VZ
104 static bool s_initDone = false; // MT-ok: used from GUI thread only
105 if ( !s_initDone )
106 {
2a1f999f 107 if ( wxApp::GetComCtl32Version() < 470 )
0aa7cb54
VZ
108 {
109 wxLogError(_("This system doesn't support date picker control, please upgrade your version of comctl32.dll"));
110
111 return false;
112 }
113
114 INITCOMMONCONTROLSEX icex;
115 icex.dwSize = sizeof(icex);
116 icex.dwICC = ICC_DATE_CLASSES;
117
118 wxDynamicLibrary dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM);
119
120 typedef BOOL (WINAPI *ICCEx_t)(INITCOMMONCONTROLSEX *);
121 wxDYNLIB_FUNCTION( ICCEx_t, InitCommonControlsEx, dllComCtl32 );
122
123 if ( pfnInitCommonControlsEx )
124 {
125 (*pfnInitCommonControlsEx)(&icex);
126 }
127
128 s_initDone = true;
129 }
130
131
5385747e
VZ
132 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
133 if ( !(style & wxDP_DROPDOWN) )
134 style |= wxDP_SPIN;
135
feb72429
VZ
136 // initialize the base class
137 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
138 return false;
139
140 // create the native control
f31a4098 141 if ( !MSWCreateControl(DATETIMEPICK_CLASS, wxEmptyString, pos, size) )
feb72429
VZ
142 return false;
143
bab3f3ea 144 if ( dt.IsValid() || (style & wxDP_ALLOWNONE) )
feb72429
VZ
145 SetValue(dt);
146
147 return true;
148}
149
150WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
151{
152 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
153
0aa7cb54
VZ
154 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
155 // comctl32.dll 4.72
2a1f999f 156 if ( wxApp::GetComCtl32Version() > 472 && (style & wxDP_SPIN) )
29c86948
VZ
157 styleMSW |= DTS_UPDOWN;
158 //else: drop down by default
159
2cfbeac8
VZ
160#ifdef DTS_SHORTDATECENTURYFORMAT
161 if ( style & wxDP_SHOWCENTURY )
162 styleMSW |= DTS_SHORTDATECENTURYFORMAT;
163 else
164#endif // DTS_SHORTDATECENTURYFORMAT
165 styleMSW |= DTS_SHORTDATEFORMAT;
feb72429 166
3200f37d
VZ
167 if ( style & wxDP_ALLOWNONE )
168 styleMSW |= DTS_SHOWNONE;
169
feb72429
VZ
170 return styleMSW;
171}
172
173// TODO: handle WM_WININICHANGE
174
175// ----------------------------------------------------------------------------
176// wxDatePickerCtrl geometry
177// ----------------------------------------------------------------------------
178
179wxSize wxDatePickerCtrl::DoGetBestSize() const
180{
181 const int y = GetCharHeight();
182
31582e4e
RD
183 wxSize best(DEFAULT_ITEM_WIDTH, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
184 CacheBestSize(best);
185 return best;
feb72429
VZ
186}
187
188// ----------------------------------------------------------------------------
189// wxDatePickerCtrl operations
190// ----------------------------------------------------------------------------
191
192void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
193{
3200f37d
VZ
194 wxCHECK_RET( dt.IsValid() || HasFlag(wxDP_ALLOWNONE),
195 _T("this control requires a valid date") );
feb72429
VZ
196
197 SYSTEMTIME st;
3200f37d
VZ
198 if ( dt.IsValid() )
199 wxToSystemTime(&st, dt);
200 if ( !DateTime_SetSystemtime(GetHwnd(),
201 dt.IsValid() ? GDT_VALID : GDT_NONE,
202 &st) )
feb72429
VZ
203 {
204 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
205 }
206}
207
208wxDateTime wxDatePickerCtrl::GetValue() const
209{
210 wxDateTime dt;
211 SYSTEMTIME st;
212 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
213 {
214 wxFromSystemTime(&dt, st);
215 }
216
217 return dt;
218}
219
220void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
221{
222 SYSTEMTIME st[2];
223
224 DWORD flags = 0;
225 if ( dt1.IsValid() )
226 {
227 wxToSystemTime(&st[0], dt1);
228 flags |= GDTR_MIN;
229 }
230
231 if ( dt2.IsValid() )
232 {
233 wxToSystemTime(&st[1], dt2);
234 flags |= GDTR_MAX;
235 }
236
237 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
238 {
239 wxLogDebug(_T("DateTime_SetRange() failed"));
240 }
241}
242
243bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
244{
245 SYSTEMTIME st[2];
246
247 DWORD flags = DateTime_GetRange(GetHwnd(), st);
248 if ( dt1 )
249 {
250 if ( flags & GDTR_MIN )
251 wxFromSystemTime(dt1, st[0]);
252 else
253 *dt1 = wxDefaultDateTime;
254 }
255
256 if ( dt2 )
257 {
258 if ( flags & GDTR_MAX )
259 wxFromSystemTime(dt2, st[1]);
260 else
261 *dt2 = wxDefaultDateTime;
262 }
263
264 return flags != 0;
265}
266
267// ----------------------------------------------------------------------------
268// wxDatePickerCtrl events
269// ----------------------------------------------------------------------------
270
271bool
272wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
273{
274 NMHDR* hdr = (NMHDR *)lParam;
275 switch ( hdr->code )
276 {
277 case DTN_DATETIMECHANGE:
278 NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr;
279 wxDateTime dt;
280 if ( dtch->dwFlags == GDT_VALID )
281 wxFromSystemTime(&dt, dtch->st);
282
283 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
284 if ( GetEventHandler()->ProcessEvent(event) )
285 {
286 *result = 0;
287 return true;
288 }
289 }
290
291 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
292}
293
9b877d18 294#endif // wxUSE_DATEPICKCTRL
29c86948 295