]> git.saurik.com Git - wxWidgets.git/blob - src/msw/datetimectrl.cpp
320da622b0685512850f38ceaf967a9557cf0579
[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 #include "wx/msw/wrapwin.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/private.h"
34 #include "wx/dcclient.h"
35 #endif // WX_PRECOMP
36
37 #include "wx/msw/private/datecontrols.h"
38
39 // apparently some versions of mingw define these macros erroneously
40 #ifndef DateTime_GetSystemtime
41 #define DateTime_GetSystemtime DateTime_GetSystemTime
42 #endif
43
44 #ifndef DateTime_SetSystemtime
45 #define DateTime_SetSystemtime DateTime_SetSystemTime
46 #endif
47
48 // ============================================================================
49 // wxDateTimePickerCtrl implementation
50 // ============================================================================
51
52 bool
53 wxDateTimePickerCtrl::MSWCreateDateTimePicker(wxWindow *parent,
54 wxWindowID id,
55 const wxDateTime& dt,
56 const wxPoint& pos,
57 const wxSize& size,
58 long style,
59 const wxValidator& validator,
60 const wxString& name)
61 {
62 if ( !wxMSWDateControls::CheckInitialization() )
63 return false;
64
65 // initialize the base class
66 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
67 return false;
68
69 // create the native control
70 if ( !MSWCreateControl(DATETIMEPICK_CLASS, wxString(), pos, size) )
71 return false;
72
73 if ( dt.IsValid() || MSWAllowsNone() )
74 SetValue(dt);
75 else
76 SetValue(wxDateTime::Now());
77
78 return true;
79 }
80
81 void wxDateTimePickerCtrl::SetValue(const wxDateTime& dt)
82 {
83 wxCHECK_RET( dt.IsValid() || MSWAllowsNone(),
84 wxT("this control requires a valid date") );
85
86 SYSTEMTIME st;
87 if ( dt.IsValid() )
88 dt.GetAsMSWSysTime(&st);
89
90 if ( !DateTime_SetSystemtime(GetHwnd(),
91 dt.IsValid() ? GDT_VALID : GDT_NONE,
92 &st) )
93 {
94 // The only expected failure is when the date is out of range but we
95 // already checked for this above.
96 wxFAIL_MSG( wxT("Setting the calendar date unexpectedly failed.") );
97
98 // In any case, skip updating m_date below.
99 return;
100 }
101
102 m_date = dt;
103 }
104
105 wxDateTime wxDateTimePickerCtrl::GetValue() const
106 {
107 return m_date;
108 }
109
110 wxSize wxDateTimePickerCtrl::DoGetBestSize() const
111 {
112 wxClientDC dc(const_cast<wxDateTimePickerCtrl *>(this));
113
114 // Use the same native format as this as the underlying native control.
115 wxString s = wxDateTime::Now().Format(wxLocale::GetInfo(MSWGetFormat()));
116
117 // the best size for the control is bigger than just the string
118 // representation of the current value because the control must accommodate
119 // any date and while the widths of all digits are usually about the same,
120 // the width of the month string varies a lot, so try to account for it
121 s += wxT("WW");
122
123 int x, y;
124 dc.GetTextExtent(s, &x, &y);
125
126 // account for the drop-down arrow or spin arrows
127 x += wxSystemSettings::GetMetric(wxSYS_HSCROLL_ARROW_X);
128
129 // and for the checkbox if we have it
130 if ( MSWAllowsNone() )
131 x += 3*GetCharWidth();
132
133 wxSize best(x, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
134 CacheBestSize(best);
135 return best;
136 }
137
138 bool
139 wxDateTimePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
140 {
141 NMHDR* hdr = (NMHDR *)lParam;
142 switch ( hdr->code )
143 {
144 case DTN_DATETIMECHANGE:
145 if ( MSWOnDateTimeChange(*(NMDATETIMECHANGE*)(hdr)) )
146 {
147 *result = 0;
148 return true;
149 }
150 break;
151 }
152
153 return wxDateTimePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
154 }
155
156 #endif // wxNEEDS_DATETIMEPICKCTRL