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