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