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