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