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