compilation fix for PCH-less build
[wxWidgets.git] / src / msw / calctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/calctrl.cpp
3 // Purpose: wxCalendarCtrl implementation
4 // Author: Vadim Zeitlin
5 // Created: 2008-04-04
6 // RCS-ID: $Id$
7 // Copyright: (C) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_CALENDARCTRL
26
27 #ifndef WX_PRECOMP
28 #include "wx/msw/wrapwin.h"
29 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
30 #include "wx/msw/private.h"
31 #endif
32
33 #include "wx/calctrl.h"
34
35 #include "wx/msw/private/datecontrols.h"
36
37 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // wxCalendarCtrl creation
45 // ----------------------------------------------------------------------------
46
47 bool
48 wxCalendarCtrl::Create(wxWindow *parent,
49 wxWindowID id,
50 const wxDateTime& dt,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name)
55 {
56 if ( !wxMSWDateControls::CheckInitialization() )
57 return false;
58
59 // initialize the base class
60 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
61 return false;
62
63 // create the native control
64 if ( !MSWCreateControl(MONTHCAL_CLASS, wxEmptyString, pos, size) )
65 return false;
66
67 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
68
69 return true;
70 }
71
72 WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
73 {
74 WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle);
75
76 // right now we don't support any native styles but we should add wx styles
77 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
78 // probably (TODO)
79
80 // for compatibility with the other versions, just turn off today display
81 // unconditionally for now
82 styleMSW |= MCS_NOTODAY;
83
84 return styleMSW;
85 }
86
87 // TODO: handle WM_WININICHANGE
88
89 // ----------------------------------------------------------------------------
90 // wxCalendarCtrl geometry
91 // ----------------------------------------------------------------------------
92
93 wxSize wxCalendarCtrl::DoGetBestSize() const
94 {
95 RECT rc;
96 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc) )
97 {
98 return wxCalendarCtrlBase::DoGetBestSize();
99 }
100
101 const wxSize best = wxRectFromRECT(rc).GetSize() + GetWindowBorderSize();
102 CacheBestSize(best);
103 return best;
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxCalendarCtrl operations
108 // ----------------------------------------------------------------------------
109
110 bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
111 {
112 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
113
114 SYSTEMTIME st;
115 wxMSWDateControls::ToSystemTime(&st, dt);
116 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
117 {
118 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
119
120 return false;
121 }
122
123 return true;
124 }
125
126 wxDateTime wxCalendarCtrl::GetDate() const
127 {
128 SYSTEMTIME st;
129 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
130 return wxDefaultDateTime;
131
132 wxDateTime dt;
133 wxMSWDateControls::FromSystemTime(&dt, st);
134 return dt;
135 }
136
137 bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
138 {
139 SYSTEMTIME st[2];
140
141 DWORD flags = 0;
142 if ( dt1.IsValid() )
143 {
144 wxMSWDateControls::ToSystemTime(&st[0], dt1);
145 flags |= GDTR_MIN;
146 }
147
148 if ( dt2.IsValid() )
149 {
150 wxMSWDateControls::ToSystemTime(&st[1], dt2);
151 flags |= GDTR_MAX;
152 }
153
154 if ( !MonthCal_SetRange(GetHwnd(), flags, st) )
155 {
156 wxLogDebug(_T("MonthCal_SetRange() failed"));
157 }
158
159 return flags != 0;
160 }
161
162 bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const
163 {
164 SYSTEMTIME st[2];
165
166 DWORD flags = MonthCal_GetRange(GetHwnd(), st);
167 if ( dt1 )
168 {
169 if ( flags & GDTR_MIN )
170 wxMSWDateControls::FromSystemTime(dt1, st[0]);
171 else
172 *dt1 = wxDefaultDateTime;
173 }
174
175 if ( dt2 )
176 {
177 if ( flags & GDTR_MAX )
178 wxMSWDateControls::FromSystemTime(dt2, st[1]);
179 else
180 *dt2 = wxDefaultDateTime;
181 }
182
183 return flags != 0;
184 }
185
186 // ----------------------------------------------------------------------------
187 // other wxCalendarCtrl operations
188 // ----------------------------------------------------------------------------
189
190 bool wxCalendarCtrl::EnableMonthChange(bool enable)
191 {
192 wxFAIL_MSG( "not implemented" );
193
194 return false;
195 }
196
197 void wxCalendarCtrl::Mark(size_t day, bool mark)
198 {
199 wxFAIL_MSG( "not implemented" );
200 }
201
202 // ----------------------------------------------------------------------------
203 // wxCalendarCtrl events
204 // ----------------------------------------------------------------------------
205
206 bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
207 {
208 NMHDR* hdr = (NMHDR *)lParam;
209 switch ( hdr->code )
210 {
211 case MCN_SELECT:
212 NMSELCHANGE *sch = (NMSELCHANGE *)hdr;
213 GenerateEvent(wxEVT_CALENDAR_SEL_CHANGED);
214 *result = 0;
215 return true;
216 }
217
218 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
219 }
220
221 #endif // wxUSE_CALENDARCTRL