bug fixes for using wxTransientPopupWindow and wxDP_ALLOWNONE support from Andreas...
[wxWidgets.git] / src / generic / datectlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/datectrl.h"
29
30 // use this version if we're explicitly requested to do it or if it's the only
31 // one we have
32 #if wxUSE_DATEPICKCTRL_GENERIC || !defined(wxHAS_NATIVE_DATEPICKCTRL)
33
34 #ifndef WX_PRECOMP
35 #include "wx/bmpbuttn.h"
36 #include "wx/dialog.h"
37 #include "wx/dcmemory.h"
38 #include "wx/panel.h"
39 #include "wx/textctrl.h"
40 #include "wx/valtext.h"
41 #endif
42
43 #ifdef wxHAS_NATIVE_DATEPICKCTRL
44 // this header is not included from wx/datectrl.h if we have a native
45 // version, but we do need it here
46 #include "wx/generic/datectrl.h"
47 #endif
48
49 // we need to define _WX_DEFINE_DATE_EVENTS_ before including wx/dateevt.h to
50 // define the event types we use if we're the only date picker control version
51 // being compiled -- otherwise it's defined in the native version implementation
52 #ifndef wxHAS_NATIVE_DATEPICKCTRL
53 #define _WX_DEFINE_DATE_EVENTS_
54 #endif
55
56 #include "wx/dateevt.h"
57
58 #include "wx/calctrl.h"
59 #include "wx/renderer.h"
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 enum
66 {
67 CTRLID_TXT = 101,
68 CTRLID_CAL,
69 CTRLID_BTN,
70 CTRLID_PAN
71 };
72
73 #ifndef DEFAULT_ITEM_WIDTH
74 #define DEFAULT_ITEM_WIDTH 100
75 #endif
76
77 // ----------------------------------------------------------------------------
78 // local classes
79 // ----------------------------------------------------------------------------
80
81 #if wxUSE_POPUPWIN
82
83 #include "wx/popupwin.h"
84
85 class wxDatePopupInternal : public wxPopupTransientWindow
86 {
87 public:
88 wxDatePopupInternal(wxWindow *parent) : wxPopupTransientWindow(parent) { }
89
90 void ShowAt(int x, int y)
91 {
92 Position(wxPoint(x, y), wxSize(0, 0));
93 Popup();
94 }
95
96 void Hide()
97 {
98 Dismiss();
99 }
100 };
101
102 #else // !wxUSE_POPUPWIN
103
104 class wxDatePopupInternal : public wxDialog
105 {
106 public:
107 wxDatePopupInternal(wxWindow *parent)
108 : wxDialog(parent,
109 wxID_ANY,
110 wxEmptyString,
111 wxDefaultPosition,
112 wxDefaultSize,
113 wxSIMPLE_BORDER)
114 {
115 }
116
117 void ShowAt(int x, int y)
118 {
119 Show();
120 Move(x, y);
121 }
122
123 void Hide()
124 {
125 wxDialog::Hide();
126 }
127 };
128
129 #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN
130
131 // ============================================================================
132 // wxDatePickerCtrlGeneric implementation
133 // ============================================================================
134
135 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase)
136 EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick)
137 EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText)
138 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus)
139 END_EVENT_TABLE()
140
141 #ifndef wxHAS_NATIVE_DATEPICKCTRL
142 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxDatePickerCtrlBase)
143 #endif
144
145 // ----------------------------------------------------------------------------
146 // creation
147 // ----------------------------------------------------------------------------
148
149 bool wxDatePickerCtrlGeneric::Create(wxWindow *parent,
150 wxWindowID id,
151 const wxDateTime& date,
152 const wxPoint& pos,
153 const wxSize& size,
154 long style,
155 const wxValidator& validator,
156 const wxString& name)
157 {
158 wxASSERT_MSG( !(style & wxDP_SPIN),
159 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
160
161 if ( !wxControl::Create(parent, id, pos, size,
162 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
163 validator, name) )
164
165 {
166 return false;
167 }
168
169 InheritAttributes();
170
171 m_txt = new wxTextCtrl(this, CTRLID_TXT);
172 m_txt->Connect(wxID_ANY, wxID_ANY, wxEVT_KEY_DOWN,
173 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnEditKey,
174 0, this);
175 m_txt->Connect(wxID_ANY, wxID_ANY, wxEVT_KILL_FOCUS,
176 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnKillFocus,
177 0, this);
178
179 const int height = m_txt->GetBestSize().y - 4; // FIXME: fudge
180 wxBitmap bmp(height, height);
181 {
182 wxMemoryDC dc;
183 dc.SelectObject(bmp);
184 wxRendererNative::Get().DrawComboBoxDropButton
185 (
186 this,
187 dc,
188 wxRect(0, 0, height, height)
189 );
190 }
191
192 wxBitmapButton *btn = new wxBitmapButton(this, CTRLID_BTN, bmp);
193 btn->SetMargins(0, 0);
194 m_btn = btn;
195
196 m_popup = new wxDatePopupInternal(this);
197 m_popup->SetFont(GetFont());
198
199 wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN,
200 wxPoint(0, 0), wxDefaultSize,
201 wxSUNKEN_BORDER);
202 m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime,
203 wxPoint(0, 0), wxDefaultSize,
204 wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER);
205 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_SEL_CHANGED,
206 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnSelChange,
207 0, this);
208 m_cal->Connect(wxID_ANY, wxID_ANY, wxEVT_KEY_DOWN,
209 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnCalKey,
210 0, this);
211 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_DOUBLECLICKED,
212 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnSelChange,
213 0, this);
214 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_DAY_CHANGED,
215 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnSelChange,
216 0, this);
217 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_MONTH_CHANGED,
218 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnSelChange,
219 0, this);
220 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_YEAR_CHANGED,
221 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnSelChange,
222 0, this);
223
224 wxWindow *yearControl = m_cal->GetYearControl();
225
226 Connect(wxID_ANY, wxID_ANY, wxEVT_SET_FOCUS,
227 (wxObjectEventFunction)&wxDatePickerCtrlGeneric::OnSetFocus);
228
229 wxClientDC dc(yearControl);
230 dc.SetFont(m_font);
231 wxCoord width, dummy;
232 dc.GetTextExtent(wxT("2000"), &width, &dummy);
233 width += ConvertDialogToPixels(wxSize(20, 0)).x;
234
235 wxSize calSize = m_cal->GetBestSize();
236 wxSize yearSize = yearControl->GetSize();
237 yearSize.x = width;
238
239 wxPoint yearPosition = yearControl->GetPosition();
240
241 SetFormat(wxT("%x"));
242
243
244 #ifdef __WXMSW__
245 #define CALBORDER 0
246 #define RIGHTBUTTONBORDER 2
247 #define TOPBUTTONBORDER 1
248 #else
249 #define CALBORDER 4
250 #define RIGHTBUTTONBORDER 0
251 #define TOPBUTTONBORDER 0
252 #endif
253
254 width = yearPosition.x + yearSize.x+2+CALBORDER/2;
255 if (width < calSize.x-4)
256 width = calSize.x-4;
257
258 int calPos = (width-calSize.x)/2;
259 if (calPos == -1)
260 {
261 calPos = 0;
262 width += 2;
263 }
264 m_cal->SetSize(calPos, 0, calSize.x, calSize.y);
265 yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y,
266 yearSize.x, yearSize.y);
267 m_cal->GetMonthControl()->Move(0, 0);
268
269
270
271 panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER);
272 m_popup->SetClientSize(panel->GetSize());
273 m_popup->Hide();
274
275 if (!date.IsValid())
276 date.Today();
277
278 SetValue(date);
279
280 return true;
281 }
282
283
284 void wxDatePickerCtrlGeneric::Init()
285 {
286 m_popup = NULL;
287 m_txt = NULL;
288 m_cal = NULL;
289 m_btn = NULL;
290
291 m_dropped = false;
292 m_ignoreDrop = false;
293 }
294
295
296 bool wxDatePickerCtrlGeneric::Destroy()
297 {
298 if (m_cal)
299 m_cal->Destroy();
300 if (m_popup)
301 m_popup->Destroy();
302 if (m_txt)
303 m_txt->Destroy();
304 if (m_btn)
305 m_btn->Destroy();
306
307 m_popup = NULL;
308 m_txt = NULL;
309 m_cal = NULL;
310 m_btn = NULL;
311
312 return wxControl::Destroy();
313 }
314
315 // ----------------------------------------------------------------------------
316 // overridden base class methods
317 // ----------------------------------------------------------------------------
318
319 void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h)
320 {
321 wxControl::DoMoveWindow(x, y, w, h);
322 wxSize bs=m_btn->GetBestSize();
323 int eh=m_txt->GetBestSize().y;
324
325 m_txt->SetSize(0, 0, w-bs.x-1, h > eh ? eh : h);
326 m_btn->SetSize(w - bs.x-RIGHTBUTTONBORDER, TOPBUTTONBORDER, bs.x, h > bs.y ? bs.y : h);
327
328 if (m_dropped)
329 DropDown(true);
330 }
331
332 wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const
333 {
334 int bh=m_btn->GetBestSize().y;
335 int eh=m_txt->GetBestSize().y;
336 return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh);
337 }
338
339
340 bool wxDatePickerCtrlGeneric::Show(bool show)
341 {
342 if ( !wxControl::Show(show) )
343 {
344 return false;
345 }
346
347 if ( !show )
348 {
349 if ( m_popup )
350 {
351 m_popup->Hide();
352 m_dropped = false;
353 }
354 }
355
356 return true;
357 }
358
359
360 bool wxDatePickerCtrlGeneric::Enable(bool enable)
361 {
362 if ( !wxControl::Enable(enable) )
363 {
364 return false;
365 }
366
367 if ( !enable )
368 {
369 if ( m_cal )
370 m_cal->Hide();
371 }
372
373 if ( m_btn )
374 m_btn->Enable(enable);
375
376 return true;
377 }
378
379 // ----------------------------------------------------------------------------
380 // wxDatePickerCtrlGeneric API
381 // ----------------------------------------------------------------------------
382
383 bool
384 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate,
385 const wxDateTime& upperdate)
386 {
387 return m_cal->SetDateRange(lowerdate, upperdate);
388 }
389
390 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt)
391 {
392 wxDateTime dt;
393 dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
394 wxString str=dt.Format(fmt);
395 wxChar *p=(wxChar*)str.c_str();
396
397 m_format=wxEmptyString;
398
399 while (*p)
400 {
401 int n=wxAtoi(p);
402 if (n == dt.GetDay())
403 {
404 m_format.Append(wxT("%d"));
405 p += 2;
406 }
407 else if (n == (int)dt.GetMonth()+1)
408 {
409 m_format.Append(wxT("%m"));
410 p += 2;
411 }
412 else if (n == dt.GetYear())
413 {
414 m_format.Append(wxT("%Y"));
415 p += 4;
416 }
417 else if (n == (dt.GetYear() % 100))
418 {
419 if (GetWindowStyle() & wxDP_SHOWCENTURY)
420 m_format.Append(wxT("%Y"));
421 else
422 m_format.Append(wxT("%y"));
423 p += 2;
424 }
425 else
426 m_format.Append(*p++);
427 }
428
429 if (m_txt)
430 {
431 wxArrayString allowedChars;
432 for ( wxChar c = _T('0'); c <= _T('9'); c++ )
433 allowedChars.Add(wxString(c, 1));
434
435 const wxChar *p = m_format.c_str();
436 while (*p)
437 {
438 if (*p == '%')
439 p += 2;
440 else
441 allowedChars.Add(wxString(*p++, 1));
442 }
443
444 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
445 tv.SetIncludes(allowedChars);
446
447 m_txt->SetValidator(tv);
448
449 if (m_currentDate.IsValid())
450 m_txt->SetValue(m_currentDate.Format(m_format));
451 }
452
453 return true;
454 }
455
456
457 wxDateTime wxDatePickerCtrlGeneric::GetValue() const
458 {
459 return m_currentDate;
460 }
461
462
463 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date)
464 {
465 if (m_cal)
466 {
467 if (date.IsValid())
468 m_txt->SetValue(date.Format(m_format));
469 else
470 {
471 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE),
472 _T("this control must have a valid date") );
473
474 m_txt->SetValue(wxEmptyString);
475 }
476
477 m_currentDate = date;
478 }
479 }
480
481
482 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
483 {
484 if (dt1)
485 *dt1 = m_cal->GetLowerDateLimit();
486 if (dt1)
487 *dt2 = m_cal->GetUpperDateLimit();
488 return true;
489 }
490
491
492 void
493 wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
494 {
495 m_cal->SetDateRange(dt1, dt2);
496 }
497
498 // ----------------------------------------------------------------------------
499 // event handlers
500 // ----------------------------------------------------------------------------
501
502 void wxDatePickerCtrlGeneric::DropDown(bool down)
503 {
504 if (m_popup)
505 {
506 if (down)
507 {
508 wxDateTime dt;
509 if (!m_txt->GetValue().IsEmpty())
510 dt.ParseFormat(m_txt->GetValue(), m_format);
511
512 if (dt.IsValid())
513 m_cal->SetDate(dt);
514 else
515 m_cal->SetDate(wxDateTime::Today());
516
517 wxPoint pos=GetParent()->ClientToScreen(GetPosition());
518 m_popup->ShowAt(pos.x, pos.y + GetSize().y);
519 m_dropped = true;
520 m_cal->SetFocus();
521 }
522 else
523 {
524 if (m_dropped)
525 m_popup->Hide();
526 m_dropped = false;
527 }
528 }
529 }
530
531
532 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev)
533 {
534 ev.Skip();
535 m_ignoreDrop = false;
536
537 wxWindow *w=(wxWindow*)ev.GetEventObject();
538 while (w)
539 {
540 if (w == m_popup)
541 return;
542 w = w->GetParent();
543 }
544
545 if (m_dropped)
546 {
547 DropDown(false);
548 if (ev.GetEventObject() == m_btn)
549 m_ignoreDrop = true;
550 }
551 }
552
553
554 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event))
555 {
556 if (m_ignoreDrop)
557 {
558 m_ignoreDrop = false;
559 m_txt->SetFocus();
560 }
561 else
562 {
563 DropDown();
564 m_cal->SetFocus();
565 }
566 }
567
568
569 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev))
570 {
571 if (m_txt)
572 {
573 m_txt->SetFocus();
574 m_txt->SetSelection(-1, -1); // select everything
575 }
576 }
577
578
579 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev)
580 {
581 ev.Skip();
582
583 wxDateTime dt;
584 dt.ParseFormat(m_txt->GetValue(), m_format);
585 if (!dt.IsValid())
586 {
587 if ( !HasFlag(wxDP_ALLOWNONE) )
588 dt = m_currentDate;
589 }
590
591 if (!dt.IsValid())
592 m_txt->SetValue(wxEmptyString);
593 else
594 m_txt->SetValue(dt.Format(m_format));
595
596 // notify that we had to change the date after validation
597 if (m_currentDate != dt)
598 {
599 m_currentDate = dt;
600 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
601 GetEventHandler()->ProcessEvent(event);
602 }
603 }
604
605
606 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev)
607 {
608 if (m_cal)
609 {
610 m_currentDate = m_cal->GetDate();
611 m_txt->SetValue(m_currentDate.Format(m_format));
612 if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED)
613 {
614 DropDown(false);
615 m_txt->SetFocus();
616 }
617 }
618 ev.SetEventObject(this);
619 ev.SetId(GetId());
620 GetParent()->ProcessEvent(ev);
621
622 wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED);
623 GetParent()->ProcessEvent(dev);
624 }
625
626
627 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev)
628 {
629 ev.SetEventObject(this);
630 ev.SetId(GetId());
631 GetParent()->ProcessEvent(ev);
632
633 // We'll create an additional event if the date is valid.
634 // If the date isn't valid, the user's probably in the middle of typing
635 wxString txt=m_txt->GetValue();
636 wxDateTime dt;
637 if (!txt.IsEmpty())
638 {
639 dt.ParseFormat(txt, m_format);
640 if (!dt.IsValid())
641 return;
642 }
643
644 wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED);
645 cev.SetEventObject(this);
646 cev.SetId(GetId());
647 cev.SetDate(dt);
648
649 GetParent()->ProcessEvent(cev);
650
651 wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED);
652 GetParent()->ProcessEvent(dev);
653 }
654
655
656 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev)
657 {
658 if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers())
659 DropDown(true);
660 else
661 ev.Skip();
662 }
663
664
665 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev)
666 {
667 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
668 DropDown(false);
669 else
670 ev.Skip();
671 }
672
673 #endif // wxUSE_DATEPICKCTRL_GENERIC
674
675 #endif // wxUSE_DATEPICKCTRL
676