1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/datectlg.cpp
3 // Purpose: generic wxDatePickerCtrlGeneric implementation
4 // Author: Andreas Pflug
8 // Copyright: (c) 2005 Andreas Pflug <pgadmin@pse-consulting.de>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_DATEPICKCTRL
28 #include "wx/datectrl.h"
30 // use this version if we're explicitly requested to do it or if it's the only
32 #if wxUSE_DATEPICKCTRL_GENERIC || !defined(wxHAS_NATIVE_DATEPICKCTRL)
35 #include "wx/bmpbuttn.h"
36 #include "wx/dialog.h"
37 #include "wx/dcmemory.h"
39 #include "wx/textctrl.h"
40 #include "wx/valtext.h"
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"
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_
54 #include "wx/dateevt.h"
56 #include "wx/calctrl.h"
57 #include "wx/renderer.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
71 #ifndef DEFAULT_ITEM_WIDTH
72 #define DEFAULT_ITEM_WIDTH 100
75 #if defined(__WXMSW__)
77 #define wxUSE_POPUPWIN 0 // Popup not working
78 #define TXTCTRL_FLAGS wxNO_BORDER
81 #elif defined(__WXGTK__)
82 #define TXTCTRL_FLAGS 0
86 #define TXTCTRL_FLAGS 0
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // this should have been a flag in wxDatePickerCtrlGeneric itself but adding it
96 // there now would break backwards compatibility, so put it here as a global:
97 // this shouldn't be a big problem as only one (GUI) thread normally can call
98 // wxDatePickerCtrlGeneric::SetValue() and so it can be only ever used for one
101 // if the value is not NULL, it points to the control which is inside SetValue()
102 static wxDatePickerCtrlGeneric
*gs_inSetValue
= NULL
;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // This flag indicates that combo box style drop button is to be created
109 #define wxBU_COMBO 0x0400
112 class wxDropdownButton
: public wxBitmapButton
115 wxDropdownButton() { Init(); }
116 wxDropdownButton(wxWindow
*parent
,
118 const wxPoint
& pos
= wxDefaultPosition
,
119 const wxSize
& size
= wxDefaultSize
,
121 const wxValidator
& validator
= wxDefaultValidator
);
123 bool Create(wxWindow
*parent
,
125 const wxPoint
& pos
= wxDefaultPosition
,
126 const wxSize
& size
= wxDefaultSize
,
128 const wxValidator
& validator
= wxDefaultValidator
);
130 virtual void DoMoveWindow(int x
, int y
, int w
, int h
);
134 void OnSize(wxSizeEvent
& event
);
135 void OnMouseEnter(wxMouseEvent
& event
);
136 void OnMouseLeave(wxMouseEvent
& event
);
138 void RecreateBitmaps(int w
, int h
);
140 wxBitmap m_bmpNormal
;
143 int m_borderX
, m_borderY
;
145 // True if DrawDropArrow should be used instead of DrawComboBoxDropButton
156 DECLARE_EVENT_TABLE()
157 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDropdownButton
)
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.
165 #if defined(__WXMSW__)
167 #define DROPBUT_USEDROPARROW(COMBOST) (COMBOST?false:true)
168 #define DROPBUT_DEFAULT_WIDTH 17
170 #elif defined(__WXGTK__)
172 #define DROPBUT_USEDROPARROW(COMBOST) true
173 #define DROPBUT_DEFAULT_WIDTH 19
177 #define DROPBUT_USEDROPARROW(COMBOST) true
178 #define DROPBUT_DEFAULT_WIDTH 17
183 IMPLEMENT_DYNAMIC_CLASS(wxDropdownButton
, wxBitmapButton
)
186 BEGIN_EVENT_TABLE(wxDropdownButton
,wxBitmapButton
)
187 EVT_ENTER_WINDOW(wxDropdownButton::OnMouseEnter
)
188 EVT_LEAVE_WINDOW(wxDropdownButton::OnMouseLeave
)
189 EVT_SIZE(wxDropdownButton::OnSize
)
193 wxDropdownButton::wxDropdownButton(wxWindow
*parent
,
198 const wxValidator
& validator
)
201 Create(parent
, id
, pos
, size
, style
, validator
);
205 bool wxDropdownButton::Create(wxWindow
*parent
,
210 const wxValidator
& validator
)
215 m_useDropArrow
= DROPBUT_USEDROPARROW(style
& wxBU_COMBO
);
217 wxBitmap
chkBmp(15,15); // arbitrary
218 if ( !wxBitmapButton::Create(parent
, id
, chkBmp
,
220 style
| (m_useDropArrow
? wxBU_AUTODRAW
: wxNO_BORDER
),
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
;
230 DoMoveWindow(pos
.x
, pos
.y
, size
.x
, size
.y
);
236 void wxDropdownButton::RecreateBitmaps(int w
, int h
)
240 int borderX
= m_marginX
+ m_borderX
;
241 int borderY
= m_marginY
+ m_borderY
;
242 int bw
= w
- borderX
;
243 int bh
= h
- borderY
;
245 wxBitmap
bmp(bw
, bh
);
246 wxBitmap
bmpSel(bw
, bh
);
249 wxRendererNative
& renderer
= wxRendererNative::Get();
251 dc
.SelectObject(bmp
);
253 if ( m_useDropArrow
)
255 // Use DrawDropArrow on transparent background.
257 wxColour
magic(255,0,255);
258 wxBrush
magicBrush(magic
);
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
);
270 dc
.SelectObject(bmpSel
);
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
);
282 // Use DrawComboBoxDropButton for the entire button
283 // (also render extra "hot" button state).
285 renderer
.DrawComboBoxDropButton(this, dc
, r
);
287 dc
.SelectObject(bmpSel
);
289 renderer
.DrawComboBoxDropButton(this, dc
, r
, wxCONTROL_PRESSED
);
291 wxBitmap
bmpHot(bw
,bh
);
292 dc
.SelectObject(bmpHot
);
293 renderer
.DrawComboBoxDropButton(this, dc
, r
, wxCONTROL_CURRENT
);
300 SetBitmapSelected(bmpSel
);
304 void wxDropdownButton::DoMoveWindow(int x
, int y
, int w
, int h
)
307 w
= DROPBUT_DEFAULT_WIDTH
;
309 wxBitmapButton::DoMoveWindow(x
, y
, w
, h
);
313 void wxDropdownButton::OnSize(wxSizeEvent
& event
)
315 if ( m_borderX
>= 0 && m_borderY
>= 0 )
318 GetClientSize(&w
,&h
);
320 if ( w
> 1 && h
> 1 )
321 RecreateBitmaps(w
,h
);
327 void wxDropdownButton::OnMouseEnter(wxMouseEvent
& event
)
329 if ( !m_useDropArrow
)
330 SetBitmapLabel(m_bmpHot
);
336 void wxDropdownButton::OnMouseLeave(wxMouseEvent
& event
)
338 if ( !m_useDropArrow
)
339 SetBitmapLabel(m_bmpNormal
);
347 #include "wx/popupwin.h"
349 class wxDatePopupInternal
: public wxPopupTransientWindow
352 wxDatePopupInternal(wxWindow
*parent
) : wxPopupTransientWindow(parent
) { }
354 void ShowAt(int x
, int y
)
356 Position(wxPoint(x
, y
), wxSize(0, 0));
366 #else // !wxUSE_POPUPWIN
368 class wxDatePopupInternal
: public wxDialog
371 wxDatePopupInternal(wxWindow
*parent
)
381 void ShowAt(int x
, int y
)
393 #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN
395 // ============================================================================
396 // wxDatePickerCtrlGeneric implementation
397 // ============================================================================
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
)
406 #ifndef wxHAS_NATIVE_DATEPICKCTRL
407 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
416 const wxDateTime
& date
,
420 const wxValidator
& validator
,
421 const wxString
& name
)
423 wxASSERT_MSG( !(style
& wxDP_SPIN
),
424 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
426 if ( !wxControl::Create(parent
, id
, pos
, size
,
427 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
436 m_txt
= new wxTextCtrl(this, CTRLID_TXT
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, TXTCTRL_FLAGS
);
438 m_txt
->Connect(wxEVT_KEY_DOWN
,
439 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey
),
441 m_txt
->Connect(wxEVT_KILL_FOCUS
,
442 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus
),
445 m_btn
= new wxDropdownButton(this, CTRLID_BTN
, wxDefaultPosition
, wxDefaultSize
, wxBU_COMBO
);
447 m_popup
= new wxDatePopupInternal(this);
448 m_popup
->SetFont(GetFont());
450 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
451 wxPoint(0, 0), wxDefaultSize
,
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
),
459 m_cal
->Connect(wxEVT_KEY_DOWN
,
460 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey
),
462 m_cal
->Connect(wxEVT_CALENDAR_DOUBLECLICKED
,
463 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
465 m_cal
->Connect(wxEVT_CALENDAR_DAY_CHANGED
,
466 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
468 m_cal
->Connect(wxEVT_CALENDAR_MONTH_CHANGED
,
469 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
471 m_cal
->Connect(wxEVT_CALENDAR_YEAR_CHANGED
,
472 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
475 wxWindow
*yearControl
= m_cal
->GetYearControl();
477 Connect(wxEVT_SET_FOCUS
,
478 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus
));
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
;
486 wxSize calSize
= m_cal
->GetBestSize();
487 wxSize yearSize
= yearControl
->GetSize();
490 wxPoint yearPosition
= yearControl
->GetPosition();
492 SetFormat(wxT("%x"));
494 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
495 if (width
< calSize
.x
-4)
498 int calPos
= (width
-calSize
.x
)/2;
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);
511 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
512 m_popup
->SetClientSize(panel
->GetSize());
515 SetValue(date
.IsValid() ? date
: wxDateTime::Today());
517 SetBestFittingSize(size
);
519 SetBackgroundColour(m_txt
->GetBackgroundColour());
525 void wxDatePickerCtrlGeneric::Init()
533 m_ignoreDrop
= false;
536 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
544 bool wxDatePickerCtrlGeneric::Destroy()
560 return wxControl::Destroy();
563 // ----------------------------------------------------------------------------
564 // overridden base class methods
565 // ----------------------------------------------------------------------------
567 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
569 wxControl::DoMoveWindow(x
, y
, w
, h
);
575 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
577 int bh
=m_btn
->GetBestSize().y
;
578 int eh
=m_txt
->GetBestSize().y
;
579 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
583 bool wxDatePickerCtrlGeneric::Show(bool show
)
585 if ( !wxControl::Show(show
) )
603 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
605 if ( !wxControl::Enable(enable
) )
617 m_btn
->Enable(enable
);
622 // ----------------------------------------------------------------------------
623 // wxDatePickerCtrlGeneric API
624 // ----------------------------------------------------------------------------
627 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
628 const wxDateTime
& upperdate
)
630 return m_cal
->SetDateRange(lowerdate
, upperdate
);
633 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
636 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
637 wxString str
=dt
.Format(fmt
);
638 wxChar
*p
=(wxChar
*)str
.c_str();
640 m_format
=wxEmptyString
;
645 if (n
== dt
.GetDay())
647 m_format
.Append(wxT("%d"));
650 else if (n
== (int)dt
.GetMonth()+1)
652 m_format
.Append(wxT("%m"));
655 else if (n
== dt
.GetYear())
657 m_format
.Append(wxT("%Y"));
660 else if (n
== (dt
.GetYear() % 100))
662 if (GetWindowStyle() & wxDP_SHOWCENTURY
)
663 m_format
.Append(wxT("%Y"));
665 m_format
.Append(wxT("%y"));
669 m_format
.Append(*p
++);
674 wxArrayString allowedChars
;
675 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
676 allowedChars
.Add(wxString(c
, 1));
678 const wxChar
*p
= m_format
.c_str();
684 allowedChars
.Add(wxString(*p
++, 1));
688 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
689 tv
.SetIncludes(allowedChars
);
690 m_txt
->SetValidator(tv
);
693 if (m_currentDate
.IsValid())
694 m_txt
->SetValue(m_currentDate
.Format(m_format
));
701 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
703 return m_currentDate
;
707 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
712 // we need to suppress the event sent from wxTextCtrl as calling our
713 // SetValue() should not result in an event being sent (wxTextCtrl is
714 // an exception to this rule)
715 gs_inSetValue
= this;
717 if ( date
.IsValid() )
719 m_txt
->SetValue(date
.Format(m_format
));
723 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE
),
724 _T("this control must have a valid date") );
726 m_txt
->SetValue(wxEmptyString
);
729 gs_inSetValue
= NULL
;
731 m_currentDate
= date
;
735 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
738 *dt1
= m_cal
->GetLowerDateLimit();
740 *dt2
= m_cal
->GetUpperDateLimit();
746 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
748 m_cal
->SetDateRange(dt1
, dt2
);
751 // ----------------------------------------------------------------------------
753 // ----------------------------------------------------------------------------
755 void wxDatePickerCtrlGeneric::DropDown(bool down
)
762 if (!m_txt
->GetValue().empty())
763 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
768 m_cal
->SetDate(wxDateTime::Today());
770 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
771 m_popup
->ShowAt(pos
.x
, pos
.y
+ GetSize().y
);
785 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
789 wxSize sz
= GetClientSize();
791 wxSize bs
=m_btn
->GetSize();
792 int eh
=m_txt
->GetBestSize().y
;
794 m_txt
->SetSize(0, TXTPOSY
, sz
.x
-bs
.x
, sz
.y
> eh
? eh
-TXTPOSY
: sz
.y
-TXTPOSY
);
795 m_btn
->SetSize(sz
.x
- bs
.x
, 0, bs
.x
, sz
.y
);
802 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
805 m_ignoreDrop
= false;
807 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
818 if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn
)
824 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
828 m_ignoreDrop
= false;
839 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
844 m_txt
->SetSelection(-1, -1); // select everything
849 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
857 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
860 if ( !HasFlag(wxDP_ALLOWNONE
) )
865 m_txt
->SetValue(dt
.Format(m_format
));
867 m_txt
->SetValue(wxEmptyString
);
869 // notify that we had to change the date after validation
870 if ( (dt
.IsValid() && (!m_currentDate
.IsValid() || m_currentDate
!= dt
)) ||
871 (!dt
.IsValid() && m_currentDate
.IsValid()) )
874 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
875 GetEventHandler()->ProcessEvent(event
);
880 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
884 m_currentDate
= m_cal
->GetDate();
885 m_txt
->SetValue(m_currentDate
.Format(m_format
));
886 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
892 ev
.SetEventObject(this);
894 GetParent()->ProcessEvent(ev
);
896 wxDateEvent
dev(this, ev
.GetDate(), wxEVT_DATE_CHANGED
);
897 GetParent()->ProcessEvent(dev
);
901 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
905 // artificial event resulting from our own SetValue() call, ignore it
909 ev
.SetEventObject(this);
911 GetParent()->ProcessEvent(ev
);
913 // We'll create an additional event if the date is valid.
914 // If the date isn't valid, the user's probably in the middle of typing
915 wxString txt
= m_txt
->GetValue();
919 dt
.ParseFormat(txt
, m_format
);
924 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
925 cev
.SetEventObject(this);
929 GetParent()->ProcessEvent(cev
);
931 wxDateEvent
dev(this, dt
, wxEVT_DATE_CHANGED
);
932 GetParent()->ProcessEvent(dev
);
936 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
938 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
945 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
947 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
953 #endif // wxUSE_DATEPICKCTRL_GENERIC
955 #endif // wxUSE_DATEPICKCTRL