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