]> git.saurik.com Git - wxWidgets.git/blob - src/msw/datectrl.cpp
added wxDP_SPIN/DROPDOWN styles (only Win32 native version implements the former)
[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 if ( style & wxDP_SPIN )
104 styleMSW |= DTS_UPDOWN;
105 //else: drop down by default
106
107 styleMSW |= DTS_SHORTDATEFORMAT;
108
109 return styleMSW;
110 }
111
112 // TODO: handle WM_WININICHANGE
113
114 // ----------------------------------------------------------------------------
115 // wxDatePickerCtrl geometry
116 // ----------------------------------------------------------------------------
117
118 wxSize wxDatePickerCtrl::DoGetBestSize() const
119 {
120 const int y = GetCharHeight();
121
122 return wxSize(DEFAULT_ITEM_WIDTH, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
123 }
124
125 // ----------------------------------------------------------------------------
126 // wxDatePickerCtrl operations
127 // ----------------------------------------------------------------------------
128
129 void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
130 {
131 // as we don't support DTS_SHOWNONE style so far, we don't allow setting
132 // the control to an invalid date, but this restriction may be lifted in
133 // the future
134 wxCHECK_RET( dt.IsValid(), _T("invalid date") );
135
136 SYSTEMTIME st;
137 wxToSystemTime(&st, dt);
138 if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID, &st) )
139 {
140 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
141 }
142 }
143
144 wxDateTime wxDatePickerCtrl::GetValue() const
145 {
146 wxDateTime dt;
147 SYSTEMTIME st;
148 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
149 {
150 wxFromSystemTime(&dt, st);
151 }
152
153 return dt;
154 }
155
156 void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
157 {
158 SYSTEMTIME st[2];
159
160 DWORD flags = 0;
161 if ( dt1.IsValid() )
162 {
163 wxToSystemTime(&st[0], dt1);
164 flags |= GDTR_MIN;
165 }
166
167 if ( dt2.IsValid() )
168 {
169 wxToSystemTime(&st[1], dt2);
170 flags |= GDTR_MAX;
171 }
172
173 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
174 {
175 wxLogDebug(_T("DateTime_SetRange() failed"));
176 }
177 }
178
179 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
180 {
181 SYSTEMTIME st[2];
182
183 DWORD flags = DateTime_GetRange(GetHwnd(), st);
184 if ( dt1 )
185 {
186 if ( flags & GDTR_MIN )
187 wxFromSystemTime(dt1, st[0]);
188 else
189 *dt1 = wxDefaultDateTime;
190 }
191
192 if ( dt2 )
193 {
194 if ( flags & GDTR_MAX )
195 wxFromSystemTime(dt2, st[1]);
196 else
197 *dt2 = wxDefaultDateTime;
198 }
199
200 return flags != 0;
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxDatePickerCtrl events
205 // ----------------------------------------------------------------------------
206
207 bool
208 wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
209 {
210 NMHDR* hdr = (NMHDR *)lParam;
211 switch ( hdr->code )
212 {
213 case DTN_DATETIMECHANGE:
214 NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr;
215 wxDateTime dt;
216 if ( dtch->dwFlags == GDT_VALID )
217 wxFromSystemTime(&dt, dtch->st);
218
219 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
220 if ( GetEventHandler()->ProcessEvent(event) )
221 {
222 *result = 0;
223 return true;
224 }
225 }
226
227 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
228 }
229
230 #endif // wxUSE_DATEPICKCTRL
231