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