]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datectlg.cpp
Another fix for PCH-less build in markup code.
[wxWidgets.git] / src / generic / datectlg.cpp
CommitLineData
39df3acd 1/////////////////////////////////////////////////////////////////////////////
68f2155b 2// Name: src/generic/datectlg.cpp
7ae712f5 3// Purpose: generic wxDatePickerCtrlGeneric implementation
39df3acd
VZ
4// Author: Andreas Pflug
5// Modified by:
6// Created: 2005-01-19
7// RCS-ID: $Id$
8// Copyright: (c) 2005 Andreas Pflug <pgadmin@pse-consulting.de>
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
7ae712f5
VZ
26#if wxUSE_DATEPICKCTRL
27
a962d4e0 28#ifndef WX_PRECOMP
a962d4e0
KH
29 #include "wx/dialog.h"
30 #include "wx/dcmemory.h"
4d4c704c 31 #include "wx/intl.h"
a962d4e0
KH
32 #include "wx/panel.h"
33 #include "wx/textctrl.h"
34 #include "wx/valtext.h"
35#endif
36
4a40cd9b
VZ
37#include "wx/calctrl.h"
38#include "wx/combo.h"
91edf16c 39
4a40cd9b 40#include "wx/datectrl.h"
df16cb09 41#include "wx/generic/datectrl.h"
39df3acd
VZ
42
43// ----------------------------------------------------------------------------
44// constants
45// ----------------------------------------------------------------------------
46
930fb29e 47
aa74ad5b
VZ
48// ----------------------------------------------------------------------------
49// global variables
50// ----------------------------------------------------------------------------
51
930fb29e 52
1721a8c0
VZ
53// ----------------------------------------------------------------------------
54// local classes
55// ----------------------------------------------------------------------------
56
c245a012 57class wxCalendarComboPopup : public wxCalendarCtrl,
85fa9d60 58 public wxComboPopup
38511687
VZ
59{
60public:
38511687 61
c245a012 62 wxCalendarComboPopup() : wxCalendarCtrl(),
85fa9d60 63 wxComboPopup()
caad7637 64 {
caad7637
JS
65 }
66
85fa9d60
VZ
67 virtual void Init()
68 {
69 }
caad7637 70
85fa9d60
VZ
71 // NB: Don't create lazily since it didn't work that way before
72 // wxComboCtrl was used, and changing behaviour would almost
73 // certainly introduce new bugs.
74 virtual bool Create(wxWindow* parent)
75 {
c245a012 76 if ( !wxCalendarCtrl::Create(parent, wxID_ANY, wxDefaultDateTime,
85fa9d60 77 wxPoint(0, 0), wxDefaultSize,
6c70323f 78 wxCAL_SEQUENTIAL_MONTH_SELECTION
c245a012 79 | wxCAL_SHOW_HOLIDAYS | wxBORDER_SUNKEN) )
85fa9d60 80 return false;
caad7637 81
6c70323f 82 SetFormat(GetLocaleDateFormat());
caad7637 83
c245a012 84 m_useSize = wxCalendarCtrl::GetBestSize();
caad7637 85
85fa9d60
VZ
86 wxWindow* tx = m_combo->GetTextCtrl();
87 if ( !tx )
88 tx = m_combo;
caad7637 89
85fa9d60
VZ
90 tx->Connect(wxEVT_KILL_FOCUS,
91 wxFocusEventHandler(wxCalendarComboPopup::OnKillTextFocus),
92 NULL, this);
caad7637 93
85fa9d60
VZ
94 return true;
95 }
38511687 96
85fa9d60
VZ
97 virtual wxSize GetAdjustedSize(int WXUNUSED(minWidth),
98 int WXUNUSED(prefHeight),
99 int WXUNUSED(maxHeight))
100 {
101 return m_useSize;
102 }
38511687 103
85fa9d60 104 virtual wxWindow *GetControl() { return this; }
930fb29e 105
85fa9d60
VZ
106 void SetDateValue(const wxDateTime& date)
107 {
108 if ( date.IsValid() )
109 {
110 m_combo->SetText(date.Format(m_format));
d6781628 111 SetDate(date);
85fa9d60
VZ
112 }
113 else // invalid date
114 {
115 wxASSERT_MSG( HasDPFlag(wxDP_ALLOWNONE),
9a83f860 116 wxT("this control must have a valid date") );
caad7637 117
85fa9d60
VZ
118 m_combo->SetText(wxEmptyString);
119 }
85fa9d60 120 }
94ab4d92 121
2ccc6650
VZ
122 bool IsTextEmpty() const
123 {
124 return m_combo->GetTextCtrl()->IsEmpty();
125 }
126
85fa9d60
VZ
127 bool ParseDateTime(const wxString& s, wxDateTime* pDt)
128 {
129 wxASSERT(pDt);
38511687 130
85fa9d60
VZ
131 if ( !s.empty() )
132 {
e0a050e3 133 pDt->ParseFormat(s.c_str(), m_format);
85fa9d60
VZ
134 if ( !pDt->IsValid() )
135 return false;
136 }
38511687 137
85fa9d60
VZ
138 return true;
139 }
38511687 140
85fa9d60
VZ
141 void SendDateEvent(const wxDateTime& dt)
142 {
85fa9d60
VZ
143 // Sends both wxCalendarEvent and wxDateEvent
144 wxWindow* datePicker = m_combo->GetParent();
38511687 145
628e155d 146 wxCalendarEvent cev(datePicker, dt, wxEVT_CALENDAR_SEL_CHANGED);
d1b736b7 147 datePicker->GetEventHandler()->ProcessEvent(cev);
38511687 148
85fa9d60 149 wxDateEvent event(datePicker, dt, wxEVT_DATE_CHANGED);
d1b736b7 150 datePicker->GetEventHandler()->ProcessEvent(event);
85fa9d60 151 }
caad7637 152
85fa9d60 153private:
caad7637 154
85fa9d60 155 void OnCalKey(wxKeyEvent & ev)
caad7637 156 {
85fa9d60
VZ
157 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
158 Dismiss();
159 else
160 ev.Skip();
caad7637 161 }
caad7637 162
85fa9d60
VZ
163 void OnSelChange(wxCalendarEvent &ev)
164 {
2fda1fe5 165 m_combo->SetText(GetDate().Format(m_format));
caad7637 166
85fa9d60
VZ
167 if ( ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED )
168 {
169 Dismiss();
170 }
caad7637 171
2fda1fe5 172 SendDateEvent(GetDate());
38511687
VZ
173 }
174
85fa9d60
VZ
175 void OnKillTextFocus(wxFocusEvent &ev)
176 {
177 ev.Skip();
caad7637 178
2fda1fe5
VZ
179 const wxDateTime& dtOld = GetDate();
180
85fa9d60
VZ
181 wxDateTime dt;
182 wxString value = m_combo->GetValue();
183 if ( !ParseDateTime(value, &dt) )
184 {
185 if ( !HasDPFlag(wxDP_ALLOWNONE) )
2fda1fe5 186 dt = dtOld;
85fa9d60 187 }
caad7637 188
2fda1fe5 189 m_combo->SetText(GetStringValueFor(dt));
38511687 190
d6781628
RD
191 if ( !dt.IsValid() && HasDPFlag(wxDP_ALLOWNONE) )
192 return;
6c70323f 193
85fa9d60 194 // notify that we had to change the date after validation
2fda1fe5
VZ
195 if ( (dt.IsValid() && (!dtOld.IsValid() || dt != dtOld)) ||
196 (!dt.IsValid() && dtOld.IsValid()) )
85fa9d60 197 {
2fda1fe5 198 SetDate(dt);
85fa9d60
VZ
199 SendDateEvent(dt);
200 }
201 }
38511687 202
6c70323f 203 bool HasDPFlag(int flag) const
caad7637 204 {
85fa9d60 205 return m_combo->GetParent()->HasFlag(flag);
caad7637 206 }
caad7637 207
4d4c704c
VZ
208 // Return the format to be used for the dates shown by the control. This
209 // functions honours wxDP_SHOWCENTURY flag.
6c70323f 210 wxString GetLocaleDateFormat() const
85fa9d60 211 {
4d4c704c
VZ
212 wxString fmt = wxLocale::GetInfo(wxLOCALE_SHORT_DATE_FMT);
213 if ( HasDPFlag(wxDP_SHOWCENTURY) )
214 fmt.Replace("%y", "%Y");
caad7637 215
4d4c704c 216 return fmt;
6c70323f
VZ
217 }
218
219 bool SetFormat(const wxString& fmt)
220 {
221 m_format = fmt;
222
85fa9d60
VZ
223 if ( m_combo )
224 {
225 wxArrayString allowedChars;
9a83f860 226 for ( wxChar c = wxT('0'); c <= wxT('9'); c++ )
85fa9d60
VZ
227 allowedChars.Add(wxString(c, 1));
228
229 const wxChar *p2 = m_format.c_str();
230 while ( *p2 )
231 {
232 if ( *p2 == '%')
233 p2 += 2;
234 else
235 allowedChars.Add(wxString(*p2++, 1));
236 }
237
238 #if wxUSE_VALIDATORS
239 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
240 tv.SetIncludes(allowedChars);
241 m_combo->SetValidator(tv);
242 #endif
243
2fda1fe5
VZ
244 if ( GetDate().IsValid() )
245 m_combo->SetText(GetDate().Format(m_format));
85fa9d60 246 }
1721a8c0 247
85fa9d60 248 return true;
1721a8c0
VZ
249 }
250
85fa9d60 251 virtual void SetStringValue(const wxString& s)
1721a8c0 252 {
85fa9d60 253 wxDateTime dt;
d6781628 254 if ( !s.empty() && ParseDateTime(s, &dt) )
2fda1fe5 255 SetDate(dt);
d6781628 256 //else: keep the old value
1721a8c0 257 }
1721a8c0 258
85fa9d60 259 virtual wxString GetStringValue() const
1721a8c0 260 {
2fda1fe5 261 return GetStringValueFor(GetDate());
1721a8c0
VZ
262 }
263
85fa9d60 264private:
2fda1fe5
VZ
265 // returns either the given date representation using the current format or
266 // an empty string if it's invalid
267 wxString GetStringValueFor(const wxDateTime& dt) const
268 {
269 wxString val;
270 if ( dt.IsValid() )
271 val = dt.Format(m_format);
272
273 return val;
274 }
85fa9d60
VZ
275
276 wxSize m_useSize;
277 wxString m_format;
85fa9d60
VZ
278
279 DECLARE_EVENT_TABLE()
1721a8c0
VZ
280};
281
85fa9d60 282
c245a012 283BEGIN_EVENT_TABLE(wxCalendarComboPopup, wxCalendarCtrl)
85fa9d60
VZ
284 EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey)
285 EVT_CALENDAR_SEL_CHANGED(wxID_ANY, wxCalendarComboPopup::OnSelChange)
628e155d 286 EVT_CALENDAR_PAGE_CHANGED(wxID_ANY, wxCalendarComboPopup::OnSelChange)
85fa9d60
VZ
287 EVT_CALENDAR(wxID_ANY, wxCalendarComboPopup::OnSelChange)
288END_EVENT_TABLE()
289
1721a8c0 290
39df3acd 291// ============================================================================
7ae712f5 292// wxDatePickerCtrlGeneric implementation
39df3acd
VZ
293// ============================================================================
294
7ae712f5 295BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase)
85fa9d60 296 EVT_TEXT(wxID_ANY, wxDatePickerCtrlGeneric::OnText)
caad7637 297 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize)
4427c0a3 298 EVT_SET_FOCUS(wxDatePickerCtrlGeneric::OnFocus)
39df3acd
VZ
299END_EVENT_TABLE()
300
b6292ff0 301#ifndef wxHAS_NATIVE_DATEPICKCTRL
94ab4d92 302 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
b6292ff0 303#endif
39df3acd
VZ
304
305// ----------------------------------------------------------------------------
306// creation
307// ----------------------------------------------------------------------------
308
7ae712f5
VZ
309bool wxDatePickerCtrlGeneric::Create(wxWindow *parent,
310 wxWindowID id,
311 const wxDateTime& date,
312 const wxPoint& pos,
313 const wxSize& size,
314 long style,
807f5038 315 const wxValidator& validator,
7ae712f5 316 const wxString& name)
39df3acd 317{
29c86948 318 wxASSERT_MSG( !(style & wxDP_SPIN),
9a83f860 319 wxT("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
29c86948 320
39df3acd 321 if ( !wxControl::Create(parent, id, pos, size,
85fa9d60 322 style | wxCLIP_CHILDREN | wxWANTS_CHARS | wxBORDER_NONE,
807f5038 323 validator, name) )
39df3acd
VZ
324 {
325 return false;
326 }
327
39df3acd
VZ
328 InheritAttributes();
329
85fa9d60
VZ
330 m_combo = new wxComboCtrl(this, -1, wxEmptyString,
331 wxDefaultPosition, wxDefaultSize);
39df3acd 332
4427c0a3
RR
333 m_combo->SetCtrlMainWnd(this);
334
85fa9d60 335 m_popup = new wxCalendarComboPopup();
39df3acd 336
06077aaf
VZ
337#if defined(__WXMSW__)
338 // without this keyboard navigation in month control doesn't work
339 m_combo->UseAltPopupWindow();
340#endif
85fa9d60 341 m_combo->SetPopupControl(m_popup);
39df3acd 342
85fa9d60 343 m_popup->SetDateValue(date.IsValid() ? date : wxDateTime::Today());
1721a8c0 344
170acdc9 345 SetInitialSize(size);
930fb29e 346
3a0c6181 347 return true;
39df3acd
VZ
348}
349
350
7ae712f5 351void wxDatePickerCtrlGeneric::Init()
39df3acd 352{
85fa9d60 353 m_combo = NULL;
85fa9d60 354 m_popup = NULL;
39df3acd
VZ
355}
356
a9d13e15
JS
357wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
358{
a9d13e15 359}
39df3acd 360
7ae712f5 361bool wxDatePickerCtrlGeneric::Destroy()
39df3acd 362{
85fa9d60
VZ
363 if ( m_combo )
364 m_combo->Destroy();
39df3acd 365
85fa9d60 366 m_combo = NULL;
85fa9d60 367 m_popup = NULL;
39df3acd
VZ
368
369 return wxControl::Destroy();
370}
371
372// ----------------------------------------------------------------------------
373// overridden base class methods
374// ----------------------------------------------------------------------------
375
7ae712f5 376wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const
39df3acd 377{
85fa9d60 378 return m_combo->GetBestSize();
39df3acd
VZ
379}
380
a9e41db7
VZ
381wxWindowList wxDatePickerCtrlGeneric::GetCompositeWindowParts() const
382{
383 wxWindowList parts;
809d6928
SC
384 if (m_combo)
385 parts.push_back(m_combo);
386 if (m_popup)
387 parts.push_back(m_popup);
a9e41db7
VZ
388 return parts;
389}
390
39df3acd 391// ----------------------------------------------------------------------------
7ae712f5 392// wxDatePickerCtrlGeneric API
39df3acd
VZ
393// ----------------------------------------------------------------------------
394
4b134bb2 395bool
7ae712f5
VZ
396wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate,
397 const wxDateTime& upperdate)
4b134bb2 398{
2fda1fe5 399 return m_popup->SetDateRange(lowerdate, upperdate);
4b134bb2
VZ
400}
401
39df3acd 402
7ae712f5 403wxDateTime wxDatePickerCtrlGeneric::GetValue() const
39df3acd 404{
2ccc6650
VZ
405 if ( HasFlag(wxDP_ALLOWNONE) && m_popup->IsTextEmpty() )
406 return wxInvalidDateTime;
2fda1fe5 407 return m_popup->GetDate();
39df3acd
VZ
408}
409
410
7ae712f5 411void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date)
39df3acd 412{
85fa9d60 413 m_popup->SetDateValue(date);
39df3acd
VZ
414}
415
416
7ae712f5 417bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
39df3acd 418{
c245a012 419 return m_popup->GetDateRange(dt1, dt2);
39df3acd
VZ
420}
421
422
7ae712f5
VZ
423void
424wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
39df3acd 425{
2fda1fe5
VZ
426 m_popup->SetDateRange(dt1, dt2);
427}
428
c245a012 429wxCalendarCtrl *wxDatePickerCtrlGeneric::GetCalendar() const
2fda1fe5
VZ
430{
431 return m_popup;
39df3acd
VZ
432}
433
434// ----------------------------------------------------------------------------
435// event handlers
436// ----------------------------------------------------------------------------
437
39df3acd 438
caad7637
JS
439void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event)
440{
85fa9d60
VZ
441 if ( m_combo )
442 m_combo->SetSize(GetClientSize());
caad7637
JS
443
444 event.Skip();
445}
446
447
7ae712f5 448void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev)
39df3acd
VZ
449{
450 ev.SetEventObject(this);
451 ev.SetId(GetId());
eafd76b0 452 GetParent()->GetEventHandler()->ProcessEvent(ev);
39df3acd
VZ
453
454 // We'll create an additional event if the date is valid.
1721a8c0 455 // If the date isn't valid, the user's probably in the middle of typing
39df3acd 456 wxDateTime dt;
b0fb790b 457 if ( !m_popup || !m_popup->ParseDateTime(m_combo->GetValue(), &dt) )
85fa9d60 458 return;
39df3acd 459
85fa9d60 460 m_popup->SendDateEvent(dt);
39df3acd
VZ
461}
462
463
4427c0a3
RR
464void wxDatePickerCtrlGeneric::OnFocus(wxFocusEvent& WXUNUSED(event))
465{
466 m_combo->SetFocus();
467}
468
469
7ae712f5 470#endif // wxUSE_DATEPICKCTRL
85fa9d60 471