]> git.saurik.com Git - wxWidgets.git/blob - src/msw/datetimectrl.cpp
Refactor wxDatePickerCtrl to derive from wxDateTimePickerCtrl.
[wxWidgets.git] / src / msw / datetimectrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/datetimectrl.cpp
3 // Purpose: Implementation of wxDateTimePickerCtrl for MSW.
4 // Author: Vadim Zeitlin
5 // Created: 2011-09-22 (extracted from src/msw/datectrl.cpp)
6 // RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2005-2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/datetimectrl.h"
27
28 #ifdef wxNEEDS_DATETIMEPICKCTRL
29
30 #ifndef WX_PRECOMP
31 #endif // WX_PRECOMP
32
33 #include "wx/msw/private/datecontrols.h"
34
35 // apparently some versions of mingw define these macros erroneously
36 #ifndef DateTime_GetSystemtime
37 #define DateTime_GetSystemtime DateTime_GetSystemTime
38 #endif
39
40 #ifndef DateTime_SetSystemtime
41 #define DateTime_SetSystemtime DateTime_SetSystemTime
42 #endif
43
44 // ============================================================================
45 // wxDateTimePickerCtrl implementation
46 // ============================================================================
47
48 bool
49 wxDateTimePickerCtrl::MSWCreateDateTimePicker(wxWindow *parent,
50 wxWindowID id,
51 const wxDateTime& dt,
52 const wxPoint& pos,
53 const wxSize& size,
54 long style,
55 const wxValidator& validator,
56 const wxString& name)
57 {
58 if ( !wxMSWDateControls::CheckInitialization() )
59 return false;
60
61 // initialize the base class
62 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
63 return false;
64
65 // create the native control
66 if ( !MSWCreateControl(DATETIMEPICK_CLASS, wxString(), pos, size) )
67 return false;
68
69 if ( dt.IsValid() || MSWAllowsNone() )
70 SetValue(dt);
71 else
72 SetValue(wxDateTime::Now());
73
74 return true;
75 }
76
77 void wxDateTimePickerCtrl::SetValue(const wxDateTime& dt)
78 {
79 wxCHECK_RET( dt.IsValid() || MSWAllowsNone(),
80 wxT("this control requires a valid date") );
81
82 SYSTEMTIME st;
83 if ( dt.IsValid() )
84 dt.GetAsMSWSysTime(&st);
85
86 if ( !DateTime_SetSystemtime(GetHwnd(),
87 dt.IsValid() ? GDT_VALID : GDT_NONE,
88 &st) )
89 {
90 // The only expected failure is when the date is out of range but we
91 // already checked for this above.
92 wxFAIL_MSG( wxT("Setting the calendar date unexpectedly failed.") );
93
94 // In any case, skip updating m_date below.
95 return;
96 }
97
98 m_date = dt;
99 }
100
101 wxDateTime wxDateTimePickerCtrl::GetValue() const
102 {
103 return m_date;
104 }
105
106 wxSize wxDateTimePickerCtrl::DoGetBestSize() const
107 {
108 wxClientDC dc(const_cast<wxDateTimePickerCtrl *>(this));
109
110 // Use the same native format as this as the underlying native control.
111 wxString s = wxDateTime::Now().Format(wxLocale::GetInfo(MSWGetFormat()));
112
113 // the best size for the control is bigger than just the string
114 // representation of the current value because the control must accommodate
115 // any date and while the widths of all digits are usually about the same,
116 // the width of the month string varies a lot, so try to account for it
117 s += wxT("WW");
118
119 int x, y;
120 dc.GetTextExtent(s, &x, &y);
121
122 // account for the drop-down arrow or spin arrows
123 x += wxSystemSettings::GetMetric(wxSYS_HSCROLL_ARROW_X);
124
125 // and for the checkbox if we have it
126 if ( MSWAllowsNone() )
127 x += 3*GetCharWidth();
128
129 wxSize best(x, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
130 CacheBestSize(best);
131 return best;
132 }
133
134 bool
135 wxDateTimePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
136 {
137 NMHDR* hdr = (NMHDR *)lParam;
138 switch ( hdr->code )
139 {
140 case DTN_DATETIMECHANGE:
141 if ( MSWOnDateTimeChange(*(NMDATETIMECHANGE*)(hdr)) )
142 {
143 *result = 0;
144 return true;
145 }
146 break;
147 }
148
149 return wxDateTimePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
150 }
151
152 #endif // wxNEEDS_DATETIMEPICKCTRL