Build fix to make tinderbox work, needs asap change.
[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 !defined(wxHAS_NATIVE_DATEPICKCTRL) || \
33 (defined(wxUSE_DATEPICKCTRL_GENERIC) && wxUSE_DATEPICKCTRL_GENERIC)
34
35 #ifndef WX_PRECOMP
36 #include "wx/bmpbuttn.h"
37 #include "wx/dialog.h"
38 #include "wx/dcmemory.h"
39 #include "wx/panel.h"
40 #include "wx/textctrl.h"
41 #include "wx/valtext.h"
42 #endif
43
44 #ifdef wxHAS_NATIVE_DATEPICKCTRL
45 // this header is not included from wx/datectrl.h if we have a native
46 // version, but we do need it here
47 #include "wx/generic/datectrl.h"
48 #else
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 #define _WX_DEFINE_DATE_EVENTS_
53 #endif
54
55 #include "wx/dateevt.h"
56
57 #include "wx/calctrl.h"
58 #include "wx/renderer.h"
59
60 // ----------------------------------------------------------------------------
61 // constants
62 // ----------------------------------------------------------------------------
63
64 enum
65 {
66 CTRLID_TXT = 101,
67 CTRLID_CAL,
68 CTRLID_BTN,
69 CTRLID_PAN
70 };
71
72 #ifndef DEFAULT_ITEM_WIDTH
73 #define DEFAULT_ITEM_WIDTH 100
74 #endif
75
76 #if defined(__WXMSW__)
77 #undef wxUSE_POPUPWIN
78 #define wxUSE_POPUPWIN 0 // Popup not working
79 #define TXTCTRL_FLAGS wxNO_BORDER
80 #define CALBORDER 0
81 #define TXTPOSY 1
82 #elif defined(__WXGTK__)
83 #define TXTCTRL_FLAGS 0
84 #define CALBORDER 4
85 #define TXTPOSY 0
86 #else
87 #define TXTCTRL_FLAGS 0
88 #define CALBORDER 4
89 #define TXTPOSY 0
90 #endif
91
92 // ----------------------------------------------------------------------------
93 // global variables
94 // ----------------------------------------------------------------------------
95
96 // this should have been a flag in wxDatePickerCtrlGeneric itself but adding it
97 // there now would break backwards compatibility, so put it here as a global:
98 // this shouldn't be a big problem as only one (GUI) thread normally can call
99 // wxDatePickerCtrlGeneric::SetValue() and so it can be only ever used for one
100 // control at a time
101 //
102 // if the value is not NULL, it points to the control which is inside SetValue()
103 static wxDatePickerCtrlGeneric *gs_inSetValue = NULL;
104
105 // ----------------------------------------------------------------------------
106 // local classes
107 // ----------------------------------------------------------------------------
108
109 // This flag indicates that combo box style drop button is to be created
110 #define wxBU_COMBO 0x0400
111
112
113 class wxDropdownButton : public wxBitmapButton
114 {
115 public:
116 wxDropdownButton() { Init(); }
117 wxDropdownButton(wxWindow *parent,
118 wxWindowID id,
119 const wxPoint& pos = wxDefaultPosition,
120 const wxSize& size = wxDefaultSize,
121 long style=0,
122 const wxValidator& validator = wxDefaultValidator);
123
124 bool Create(wxWindow *parent,
125 wxWindowID id,
126 const wxPoint& pos = wxDefaultPosition,
127 const wxSize& size = wxDefaultSize,
128 long style = 0,
129 const wxValidator& validator = wxDefaultValidator);
130
131 protected:
132 virtual void DoMoveWindow(int x, int y, int w, int h);
133
134 void OnSize(wxSizeEvent& event);
135 void OnMouseEnter(wxMouseEvent& event);
136 void OnMouseLeave(wxMouseEvent& event);
137
138 void RecreateBitmaps(int w, int h);
139
140 wxBitmap m_bmpNormal;
141 wxBitmap m_bmpHot;
142
143 int m_borderX, m_borderY;
144
145 // True if DrawDropArrow should be used instead of DrawComboBoxDropButton
146 bool m_useDropArrow;
147
148 private:
149
150 void Init()
151 {
152 m_borderX = -1;
153 m_borderY = -1;
154 }
155
156 DECLARE_EVENT_TABLE()
157 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDropdownButton)
158 };
159
160
161 // Below, macro DROPBUT_USEDROPARROW should return false when
162 // DrawComboBoxDropButton is to be used to render the entire button.
163 // COMBOST is non-zero if wxBU_COMBO was set.
164
165 #if defined(__WXMSW__)
166
167 #define DROPBUT_USEDROPARROW(COMBOST) (COMBOST?false:true)
168 #define DROPBUT_DEFAULT_WIDTH 17
169
170 #elif defined(__WXGTK__)
171
172 #define DROPBUT_USEDROPARROW(COMBOST) true
173 #define DROPBUT_DEFAULT_WIDTH 19
174
175 #else
176
177 #define DROPBUT_USEDROPARROW(COMBOST) true
178 #define DROPBUT_DEFAULT_WIDTH 17
179
180 #endif
181
182
183 IMPLEMENT_DYNAMIC_CLASS(wxDropdownButton, wxBitmapButton)
184
185
186 BEGIN_EVENT_TABLE(wxDropdownButton,wxBitmapButton)
187 EVT_ENTER_WINDOW(wxDropdownButton::OnMouseEnter)
188 EVT_LEAVE_WINDOW(wxDropdownButton::OnMouseLeave)
189 EVT_SIZE(wxDropdownButton::OnSize)
190 END_EVENT_TABLE()
191
192
193 wxDropdownButton::wxDropdownButton(wxWindow *parent,
194 wxWindowID id,
195 const wxPoint& pos,
196 const wxSize& size,
197 long style,
198 const wxValidator& validator)
199 {
200 Init();
201 Create(parent, id, pos, size, style, validator);
202 }
203
204
205 bool wxDropdownButton::Create(wxWindow *parent,
206 wxWindowID id,
207 const wxPoint& pos,
208 const wxSize& size,
209 long style,
210 const wxValidator& validator)
211 {
212 m_marginX = 0;
213 m_marginY = 0;
214
215 m_useDropArrow = DROPBUT_USEDROPARROW(style & wxBU_COMBO);
216
217 wxBitmap chkBmp(15,15); // arbitrary
218 if ( !wxBitmapButton::Create(parent, id, chkBmp,
219 pos, wxDefaultSize,
220 style | (m_useDropArrow ? wxBU_AUTODRAW : wxNO_BORDER),
221 validator) )
222 return false;
223
224 const wxSize sz = GetSize();
225 int w = chkBmp.GetWidth(),
226 h = chkBmp.GetHeight();
227 m_borderX = sz.x - m_marginX - w;
228 m_borderY = sz.y - m_marginY - h;
229
230 DoMoveWindow(pos.x, pos.y, size.x, size.y);
231
232 return true;
233 }
234
235
236 void wxDropdownButton::RecreateBitmaps(int w, int h)
237 {
238 wxMemoryDC dc;
239
240 int borderX = m_marginX + m_borderX;
241 int borderY = m_marginY + m_borderY;
242 int bw = w - borderX;
243 int bh = h - borderY;
244
245 wxBitmap bmp(bw, bh);
246 wxBitmap bmpSel(bw, bh);
247 wxRect r(0,0,w,h);
248
249 wxRendererNative& renderer = wxRendererNative::Get();
250
251 dc.SelectObject(bmp);
252
253 if ( m_useDropArrow )
254 {
255 // Use DrawDropArrow on transparent background.
256
257 wxColour magic(255,0,255);
258 wxBrush magicBrush(magic);
259 r.x = -(borderX/2);
260 r.y = -(borderY/2);
261
262 dc.SetBrush( magicBrush );
263 dc.SetPen( *wxTRANSPARENT_PEN );
264 dc.DrawRectangle(0,0,bw,bh);
265 renderer.DrawDropArrow(this, dc, r);
266 dc.SelectObject( wxNullBitmap );
267 wxMask *mask = new wxMask( bmp, magic );
268 bmp.SetMask( mask );
269
270 dc.SelectObject(bmpSel);
271
272 dc.SetBrush( magicBrush );
273 dc.SetPen( *wxTRANSPARENT_PEN );
274 dc.DrawRectangle(0,0,bw,bh);
275 renderer.DrawDropArrow(this, dc, r, wxCONTROL_PRESSED);
276 dc.SelectObject( wxNullBitmap );
277 mask = new wxMask( bmpSel, magic );
278 bmpSel.SetMask( mask );
279 }
280 else
281 {
282 // Use DrawComboBoxDropButton for the entire button
283 // (also render extra "hot" button state).
284
285 renderer.DrawComboBoxDropButton(this, dc, r);
286
287 dc.SelectObject(bmpSel);
288
289 renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_PRESSED);
290
291 wxBitmap bmpHot(bw,bh);
292 dc.SelectObject(bmpHot);
293 renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_CURRENT);
294
295 m_bmpNormal = bmp;
296 m_bmpHot = bmpHot;
297 }
298
299 SetBitmapLabel(bmp);
300 SetBitmapSelected(bmpSel);
301 }
302
303
304 void wxDropdownButton::DoMoveWindow(int x, int y, int w, int h)
305 {
306 if (w < 0)
307 w = DROPBUT_DEFAULT_WIDTH;
308
309 wxBitmapButton::DoMoveWindow(x, y, w, h);
310 }
311
312
313 void wxDropdownButton::OnSize(wxSizeEvent& event)
314 {
315 if ( m_borderX >= 0 && m_borderY >= 0 )
316 {
317 int w, h;
318 GetClientSize(&w,&h);
319
320 if ( w > 1 && h > 1 )
321 RecreateBitmaps(w,h);
322 }
323 event.Skip();
324 }
325
326
327 void wxDropdownButton::OnMouseEnter(wxMouseEvent& event)
328 {
329 if ( !m_useDropArrow )
330 SetBitmapLabel(m_bmpHot);
331
332 event.Skip();
333 }
334
335
336 void wxDropdownButton::OnMouseLeave(wxMouseEvent& event)
337 {
338 if ( !m_useDropArrow )
339 SetBitmapLabel(m_bmpNormal);
340
341 event.Skip();
342 }
343
344
345 #if wxUSE_POPUPWIN
346
347 #include "wx/popupwin.h"
348
349 class wxDatePopupInternal : public wxPopupTransientWindow
350 {
351 public:
352 wxDatePopupInternal(wxWindow *parent) : wxPopupTransientWindow(parent) { }
353
354 void ShowAt(int x, int y)
355 {
356 Position(wxPoint(x, y), wxSize(0, 0));
357 Popup();
358 }
359
360 void Hide()
361 {
362 Dismiss();
363 }
364 };
365
366 #else // !wxUSE_POPUPWIN
367
368 class wxDatePopupInternal : public wxDialog
369 {
370 public:
371 wxDatePopupInternal(wxWindow *parent)
372 : wxDialog(parent,
373 wxID_ANY,
374 wxEmptyString,
375 wxDefaultPosition,
376 wxDefaultSize,
377 wxSIMPLE_BORDER)
378 {
379 }
380
381 void ShowAt(int x, int y)
382 {
383 Show();
384 Move(x, y);
385 }
386
387 void Hide()
388 {
389 wxDialog::Hide();
390 }
391 };
392
393 #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN
394
395 // ============================================================================
396 // wxDatePickerCtrlGeneric implementation
397 // ============================================================================
398
399 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase)
400 EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick)
401 EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText)
402 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus)
403 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize)
404 END_EVENT_TABLE()
405
406 #ifndef wxHAS_NATIVE_DATEPICKCTRL
407 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
408 #endif
409
410 // ----------------------------------------------------------------------------
411 // creation
412 // ----------------------------------------------------------------------------
413
414 bool wxDatePickerCtrlGeneric::Create(wxWindow *parent,
415 wxWindowID id,
416 const wxDateTime& date,
417 const wxPoint& pos,
418 const wxSize& size,
419 long style,
420 const wxValidator& validator,
421 const wxString& name)
422 {
423 wxASSERT_MSG( !(style & wxDP_SPIN),
424 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
425
426 if ( !wxControl::Create(parent, id, pos, size,
427 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
428 validator, name) )
429
430 {
431 return false;
432 }
433
434 InheritAttributes();
435
436 m_txt = new wxTextCtrl(this, CTRLID_TXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, TXTCTRL_FLAGS);
437
438 m_txt->Connect(wxEVT_KEY_DOWN,
439 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey),
440 NULL, this);
441 m_txt->Connect(wxEVT_KILL_FOCUS,
442 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus),
443 NULL, this);
444
445 m_btn = new wxDropdownButton(this, CTRLID_BTN, wxDefaultPosition, wxDefaultSize, wxBU_COMBO);
446
447 m_popup = new wxDatePopupInternal(this);
448 m_popup->SetFont(GetFont());
449
450 wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN,
451 wxPoint(0, 0), wxDefaultSize,
452 wxSUNKEN_BORDER);
453 m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime,
454 wxPoint(0, 0), wxDefaultSize,
455 wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER);
456 m_cal->Connect(wxEVT_CALENDAR_SEL_CHANGED,
457 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange),
458 NULL, this);
459 m_cal->Connect(wxEVT_KEY_DOWN,
460 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey),
461 NULL, this);
462 m_cal->Connect(wxEVT_CALENDAR_DOUBLECLICKED,
463 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange),
464 NULL, this);
465 m_cal->Connect(wxEVT_CALENDAR_DAY_CHANGED,
466 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange),
467 NULL, this);
468 m_cal->Connect(wxEVT_CALENDAR_MONTH_CHANGED,
469 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange),
470 NULL, this);
471 m_cal->Connect(wxEVT_CALENDAR_YEAR_CHANGED,
472 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange),
473 NULL, this);
474
475 wxWindow *yearControl = m_cal->GetYearControl();
476
477 Connect(wxEVT_SET_FOCUS,
478 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus));
479
480 wxClientDC dc(yearControl);
481 dc.SetFont(yearControl->GetFont());
482 wxCoord width, dummy;
483 dc.GetTextExtent(wxT("2000"), &width, &dummy);
484 width += ConvertDialogToPixels(wxSize(20, 0)).x;
485
486 wxSize calSize = m_cal->GetBestSize();
487 wxSize yearSize = yearControl->GetSize();
488 yearSize.x = width;
489
490 wxPoint yearPosition = yearControl->GetPosition();
491
492 SetFormat(wxT("%x"));
493
494 width = yearPosition.x + yearSize.x+2+CALBORDER/2;
495 if (width < calSize.x-4)
496 width = calSize.x-4;
497
498 int calPos = (width-calSize.x)/2;
499 if (calPos == -1)
500 {
501 calPos = 0;
502 width += 2;
503 }
504 m_cal->SetSize(calPos, 0, calSize.x, calSize.y);
505 yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y,
506 yearSize.x, yearSize.y);
507 m_cal->GetMonthControl()->Move(0, 0);
508
509
510
511 panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER);
512 m_popup->SetClientSize(panel->GetSize());
513 m_popup->Hide();
514
515 SetValue(date.IsValid() ? date : wxDateTime::Today());
516
517 SetBestFittingSize(size);
518
519 SetBackgroundColour(m_txt->GetBackgroundColour());
520
521 return true;
522 }
523
524
525 void wxDatePickerCtrlGeneric::Init()
526 {
527 m_popup = NULL;
528 m_txt = NULL;
529 m_cal = NULL;
530 m_btn = NULL;
531
532 m_dropped = false;
533 m_ignoreDrop = false;
534 }
535
536 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
537 {
538 m_popup = NULL;
539 m_txt = NULL;
540 m_cal = NULL;
541 m_btn = NULL;
542 }
543
544 bool wxDatePickerCtrlGeneric::Destroy()
545 {
546 if (m_cal)
547 m_cal->Destroy();
548 if (m_popup)
549 m_popup->Destroy();
550 if (m_txt)
551 m_txt->Destroy();
552 if (m_btn)
553 m_btn->Destroy();
554
555 m_popup = NULL;
556 m_txt = NULL;
557 m_cal = NULL;
558 m_btn = NULL;
559
560 return wxControl::Destroy();
561 }
562
563 // ----------------------------------------------------------------------------
564 // overridden base class methods
565 // ----------------------------------------------------------------------------
566
567 void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h)
568 {
569 wxControl::DoMoveWindow(x, y, w, h);
570
571 if (m_dropped)
572 DropDown(true);
573 }
574
575 wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const
576 {
577 if (m_btn && m_txt)
578 {
579 int bh=m_btn->GetBestSize().y;
580 int eh=m_txt->GetBestSize().y;
581 return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh);
582 }
583 return wxSize(DEFAULT_ITEM_WIDTH,
584 #if defined(__WXMSW__) || defined(__WXPM__)
585 DEFAULT_ITEM_HEIGHT
586 #else
587 8 // FIXME ASAP
588 #endif
589 );
590 }
591
592
593 bool wxDatePickerCtrlGeneric::Show(bool show)
594 {
595 if ( !wxControl::Show(show) )
596 {
597 return false;
598 }
599
600 if ( !show )
601 {
602 if ( m_popup )
603 {
604 m_popup->Hide();
605 m_dropped = false;
606 }
607 }
608
609 return true;
610 }
611
612
613 bool wxDatePickerCtrlGeneric::Enable(bool enable)
614 {
615 if ( !wxControl::Enable(enable) )
616 {
617 return false;
618 }
619
620 if ( !enable )
621 {
622 if ( m_popup )
623 m_popup->Hide();
624 }
625
626 if ( m_btn )
627 m_btn->Enable(enable);
628
629 return true;
630 }
631
632 // ----------------------------------------------------------------------------
633 // wxDatePickerCtrlGeneric API
634 // ----------------------------------------------------------------------------
635
636 bool
637 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate,
638 const wxDateTime& upperdate)
639 {
640 return m_cal->SetDateRange(lowerdate, upperdate);
641 }
642
643 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt)
644 {
645 m_format.clear();
646
647 wxDateTime dt;
648 dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
649 wxString str(dt.Format(fmt));
650
651 const wxChar *p = str.c_str();
652 while ( *p )
653 {
654 int n=wxAtoi(p);
655 if (n == dt.GetDay())
656 {
657 m_format.Append(wxT("%d"));
658 p += 2;
659 }
660 else if (n == (int)dt.GetMonth()+1)
661 {
662 m_format.Append(wxT("%m"));
663 p += 2;
664 }
665 else if (n == dt.GetYear())
666 {
667 m_format.Append(wxT("%Y"));
668 p += 4;
669 }
670 else if (n == (dt.GetYear() % 100))
671 {
672 if (GetWindowStyle() & wxDP_SHOWCENTURY)
673 m_format.Append(wxT("%Y"));
674 else
675 m_format.Append(wxT("%y"));
676 p += 2;
677 }
678 else
679 m_format.Append(*p++);
680 }
681
682 if ( m_txt )
683 {
684 wxArrayString allowedChars;
685 for ( wxChar c = _T('0'); c <= _T('9'); c++ )
686 allowedChars.Add(wxString(c, 1));
687
688 const wxChar *p2 = m_format.c_str();
689 while ( *p2 )
690 {
691 if ( *p2 == '%')
692 p2 += 2;
693 else
694 allowedChars.Add(wxString(*p2++, 1));
695 }
696
697 #if wxUSE_VALIDATORS
698 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
699 tv.SetIncludes(allowedChars);
700 m_txt->SetValidator(tv);
701 #endif
702
703 if (m_currentDate.IsValid())
704 m_txt->SetValue(m_currentDate.Format(m_format));
705 }
706
707 return true;
708 }
709
710
711 wxDateTime wxDatePickerCtrlGeneric::GetValue() const
712 {
713 return m_currentDate;
714 }
715
716
717 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date)
718 {
719 if ( !m_cal )
720 return;
721
722 // we need to suppress the event sent from wxTextCtrl as calling our
723 // SetValue() should not result in an event being sent (wxTextCtrl is
724 // an exception to this rule)
725 gs_inSetValue = this;
726
727 if ( date.IsValid() )
728 {
729 m_txt->SetValue(date.Format(m_format));
730 }
731 else // invalid date
732 {
733 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE),
734 _T("this control must have a valid date") );
735
736 m_txt->SetValue(wxEmptyString);
737 }
738
739 gs_inSetValue = NULL;
740
741 m_currentDate = date;
742 }
743
744
745 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
746 {
747 if (dt1)
748 *dt1 = m_cal->GetLowerDateLimit();
749 if (dt2)
750 *dt2 = m_cal->GetUpperDateLimit();
751 return true;
752 }
753
754
755 void
756 wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
757 {
758 m_cal->SetDateRange(dt1, dt2);
759 }
760
761 // ----------------------------------------------------------------------------
762 // event handlers
763 // ----------------------------------------------------------------------------
764
765 void wxDatePickerCtrlGeneric::DropDown(bool down)
766 {
767 if (m_popup)
768 {
769 if (down)
770 {
771 wxDateTime dt;
772 if (!m_txt->GetValue().empty())
773 dt.ParseFormat(m_txt->GetValue(), m_format);
774
775 if (dt.IsValid())
776 m_cal->SetDate(dt);
777 else
778 m_cal->SetDate(wxDateTime::Today());
779
780 wxPoint pos=GetParent()->ClientToScreen(GetPosition());
781 m_popup->ShowAt(pos.x, pos.y + GetSize().y);
782 m_dropped = true;
783 m_cal->SetFocus();
784 }
785 else
786 {
787 if (m_dropped)
788 m_popup->Hide();
789 m_dropped = false;
790 }
791 }
792 }
793
794
795 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event)
796 {
797 if ( m_btn )
798 {
799 wxSize sz = GetClientSize();
800
801 wxSize bs=m_btn->GetSize();
802 int eh=m_txt->GetBestSize().y;
803
804 m_txt->SetSize(0, TXTPOSY, sz.x-bs.x, sz.y > eh ? eh-TXTPOSY : sz.y-TXTPOSY);
805 m_btn->SetSize(sz.x - bs.x, 0, bs.x, sz.y);
806 }
807
808 event.Skip();
809 }
810
811
812 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev)
813 {
814 ev.Skip();
815 m_ignoreDrop = false;
816
817 wxWindow *w=(wxWindow*)ev.GetEventObject();
818 while (w)
819 {
820 if (w == m_popup)
821 return;
822 w = w->GetParent();
823 }
824
825 if (m_dropped)
826 {
827 DropDown(false);
828 if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn)
829 m_ignoreDrop = true;
830 }
831 }
832
833
834 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event))
835 {
836 if (m_ignoreDrop)
837 {
838 m_ignoreDrop = false;
839 m_txt->SetFocus();
840 }
841 else
842 {
843 DropDown();
844 m_cal->SetFocus();
845 }
846 }
847
848
849 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev))
850 {
851 if (m_txt)
852 {
853 m_txt->SetFocus();
854 m_txt->SetSelection(-1, -1); // select everything
855 }
856 }
857
858
859 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev)
860 {
861 if (!m_txt)
862 return;
863
864 ev.Skip();
865
866 wxDateTime dt;
867 dt.ParseFormat(m_txt->GetValue(), m_format);
868 if ( !dt.IsValid() )
869 {
870 if ( !HasFlag(wxDP_ALLOWNONE) )
871 dt = m_currentDate;
872 }
873
874 if(dt.IsValid())
875 m_txt->SetValue(dt.Format(m_format));
876 else
877 m_txt->SetValue(wxEmptyString);
878
879 // notify that we had to change the date after validation
880 if ( (dt.IsValid() && (!m_currentDate.IsValid() || m_currentDate != dt)) ||
881 (!dt.IsValid() && m_currentDate.IsValid()) )
882 {
883 m_currentDate = dt;
884 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
885 GetEventHandler()->ProcessEvent(event);
886 }
887 }
888
889
890 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev)
891 {
892 if (m_cal)
893 {
894 m_currentDate = m_cal->GetDate();
895 m_txt->SetValue(m_currentDate.Format(m_format));
896 if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED)
897 {
898 DropDown(false);
899 m_txt->SetFocus();
900 }
901 }
902 ev.SetEventObject(this);
903 ev.SetId(GetId());
904 GetParent()->ProcessEvent(ev);
905
906 wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED);
907 GetParent()->ProcessEvent(dev);
908 }
909
910
911 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev)
912 {
913 if ( gs_inSetValue )
914 {
915 // artificial event resulting from our own SetValue() call, ignore it
916 return;
917 }
918
919 ev.SetEventObject(this);
920 ev.SetId(GetId());
921 GetParent()->ProcessEvent(ev);
922
923 // We'll create an additional event if the date is valid.
924 // If the date isn't valid, the user's probably in the middle of typing
925 wxString txt = m_txt->GetValue();
926 wxDateTime dt;
927 if (!txt.empty())
928 {
929 dt.ParseFormat(txt, m_format);
930 if (!dt.IsValid())
931 return;
932 }
933
934 wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED);
935 cev.SetEventObject(this);
936 cev.SetId(GetId());
937 cev.SetDate(dt);
938
939 GetParent()->ProcessEvent(cev);
940
941 wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED);
942 GetParent()->ProcessEvent(dev);
943 }
944
945
946 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev)
947 {
948 if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers())
949 DropDown(true);
950 else
951 ev.Skip();
952 }
953
954
955 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev)
956 {
957 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
958 DropDown(false);
959 else
960 ev.Skip();
961 }
962
963 #endif // wxUSE_DATEPICKCTRL_GENERIC
964
965 #endif // wxUSE_DATEPICKCTRL
966