Fix for limited build required by headers of free DMC.
[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
33 #define _WX_DEFINE_DATE_EVENTS_
34 #include "wx/dateevt.h"
35
36 #include "wx/msw/wrapwin.h"
37 #include "wx/msw/wrapcctl.h"
38 #include "wx/msw/private.h"
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // helpers for wxDateTime <-> SYSTEMTIME conversion
46 // ----------------------------------------------------------------------------
47
48 static inline void wxFromSystemTime(wxDateTime *dt, const SYSTEMTIME& st)
49 {
50 dt->Set(st.wDay,
51 wx_static_cast(wxDateTime::Month, wxDateTime::Jan + st.wMonth - 1),
52 st.wYear,
53 0, 0, 0);
54 }
55
56 static inline void wxToSystemTime(SYSTEMTIME *st, const wxDateTime& dt)
57 {
58 const wxDateTime::Tm tm(dt.GetTm());
59
60 st->wYear = tm.year;
61 st->wMonth = tm.mon - wxDateTime::Jan + 1;
62 st->wDay = tm.mday;
63
64 st->wDayOfWeek =
65 st->wHour =
66 st->wMinute =
67 st->wSecond =
68 st->wMilliseconds = 0;
69 }
70
71 // ----------------------------------------------------------------------------
72 // wxDatePickerCtrl creation
73 // ----------------------------------------------------------------------------
74
75 bool
76 wxDatePickerCtrl::Create(wxWindow *parent,
77 wxWindowID id,
78 const wxDateTime& dt,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxValidator& validator,
83 const wxString& name)
84 {
85 // initialize the base class
86 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
87 return false;
88
89 // create the native control
90 if ( !MSWCreateControl(DATETIMEPICK_CLASS, _T(""), pos, size) )
91 return false;
92
93 if ( dt.IsValid() )
94 SetValue(dt);
95
96 return true;
97 }
98
99 WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
100 {
101 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
102
103 // for now this is unconditional, but we should support drop down control
104 // style as well later
105 styleMSW |= DTS_UPDOWN | DTS_SHORTDATEFORMAT;
106
107 return styleMSW;
108 }
109
110 // TODO: handle WM_WININICHANGE
111
112 // ----------------------------------------------------------------------------
113 // wxDatePickerCtrl geometry
114 // ----------------------------------------------------------------------------
115
116 wxSize wxDatePickerCtrl::DoGetBestSize() const
117 {
118 const int y = GetCharHeight();
119
120 return wxSize(DEFAULT_ITEM_WIDTH, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
121 }
122
123 // ----------------------------------------------------------------------------
124 // wxDatePickerCtrl operations
125 // ----------------------------------------------------------------------------
126
127 void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
128 {
129 // as we don't support DTS_SHOWNONE style so far, we don't allow setting
130 // the control to an invalid date, but this restriction may be lifted in
131 // the future
132 wxCHECK_RET( dt.IsValid(), _T("invalid date") );
133
134 SYSTEMTIME st;
135 wxToSystemTime(&st, dt);
136 if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID, &st) )
137 {
138 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
139 }
140 }
141
142 wxDateTime wxDatePickerCtrl::GetValue() const
143 {
144 wxDateTime dt;
145 SYSTEMTIME st;
146 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
147 {
148 wxFromSystemTime(&dt, st);
149 }
150
151 return dt;
152 }
153
154 void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
155 {
156 SYSTEMTIME st[2];
157
158 DWORD flags = 0;
159 if ( dt1.IsValid() )
160 {
161 wxToSystemTime(&st[0], dt1);
162 flags |= GDTR_MIN;
163 }
164
165 if ( dt2.IsValid() )
166 {
167 wxToSystemTime(&st[1], dt2);
168 flags |= GDTR_MAX;
169 }
170
171 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
172 {
173 wxLogDebug(_T("DateTime_SetRange() failed"));
174 }
175 }
176
177 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
178 {
179 SYSTEMTIME st[2];
180
181 DWORD flags = DateTime_GetRange(GetHwnd(), st);
182 if ( dt1 )
183 {
184 if ( flags & GDTR_MIN )
185 wxFromSystemTime(dt1, st[0]);
186 else
187 *dt1 = wxDefaultDateTime;
188 }
189
190 if ( dt2 )
191 {
192 if ( flags & GDTR_MAX )
193 wxFromSystemTime(dt2, st[1]);
194 else
195 *dt2 = wxDefaultDateTime;
196 }
197
198 return flags != 0;
199 }
200
201 // ----------------------------------------------------------------------------
202 // wxDatePickerCtrl events
203 // ----------------------------------------------------------------------------
204
205 bool
206 wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
207 {
208 NMHDR* hdr = (NMHDR *)lParam;
209 switch ( hdr->code )
210 {
211 case DTN_DATETIMECHANGE:
212 NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr;
213 wxDateTime dt;
214 if ( dtch->dwFlags == GDT_VALID )
215 wxFromSystemTime(&dt, dtch->st);
216
217 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
218 if ( GetEventHandler()->ProcessEvent(event) )
219 {
220 *result = 0;
221 return true;
222 }
223 }
224
225 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
226 }
227
228 #endif // wxUSE_DATEPICKCTRL