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 !defined(wxHAS_NATIVE_DATEPICKCTRL) || \
33 (defined(wxUSE_DATEPICKCTRL_GENERIC) && wxUSE_DATEPICKCTRL_GENERIC)
36 #include "wx/bmpbuttn.h"
37 #include "wx/dialog.h"
38 #include "wx/dcmemory.h"
40 #include "wx/textctrl.h"
41 #include "wx/valtext.h"
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"
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_
55 #include "wx/dateevt.h"
57 #include "wx/calctrl.h"
58 #include "wx/renderer.h"
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
72 #ifndef DEFAULT_ITEM_WIDTH
73 #define DEFAULT_ITEM_WIDTH 100
76 #if defined(__WXMSW__)
78 #define wxUSE_POPUPWIN 0 // Popup not working
79 #define TXTCTRL_FLAGS wxNO_BORDER
82 #elif defined(__WXGTK__)
83 #define TXTCTRL_FLAGS 0
87 #define TXTCTRL_FLAGS 0
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
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
102 // if the value is not NULL, it points to the control which is inside SetValue()
103 static wxDatePickerCtrlGeneric
*gs_inSetValue
= NULL
;
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // This flag indicates that combo box style drop button is to be created
110 #define wxBU_COMBO 0x0400
113 class wxDropdownButton
: public wxBitmapButton
116 wxDropdownButton() { Init(); }
117 wxDropdownButton(wxWindow
*parent
,
119 const wxPoint
& pos
= wxDefaultPosition
,
120 const wxSize
& size
= wxDefaultSize
,
122 const wxValidator
& validator
= wxDefaultValidator
);
124 bool Create(wxWindow
*parent
,
126 const wxPoint
& pos
= wxDefaultPosition
,
127 const wxSize
& size
= wxDefaultSize
,
129 const wxValidator
& validator
= wxDefaultValidator
);
132 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
579 int bh
=m_btn
->GetBestSize().y
;
580 int eh
=m_txt
->GetBestSize().y
;
581 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
583 return wxSize(DEFAULT_ITEM_WIDTH
,DEFAULT_ITEM_HEIGHT
);
587 bool wxDatePickerCtrlGeneric::Show(bool show
)
589 if ( !wxControl::Show(show
) )
607 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
609 if ( !wxControl::Enable(enable
) )
621 m_btn
->Enable(enable
);
626 // ----------------------------------------------------------------------------
627 // wxDatePickerCtrlGeneric API
628 // ----------------------------------------------------------------------------
631 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
632 const wxDateTime
& upperdate
)
634 return m_cal
->SetDateRange(lowerdate
, upperdate
);
637 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
642 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
643 wxString
str(dt
.Format(fmt
));
645 const wxChar
*p
= str
.c_str();
649 if (n
== dt
.GetDay())
651 m_format
.Append(wxT("%d"));
654 else if (n
== (int)dt
.GetMonth()+1)
656 m_format
.Append(wxT("%m"));
659 else if (n
== dt
.GetYear())
661 m_format
.Append(wxT("%Y"));
664 else if (n
== (dt
.GetYear() % 100))
666 if (GetWindowStyle() & wxDP_SHOWCENTURY
)
667 m_format
.Append(wxT("%Y"));
669 m_format
.Append(wxT("%y"));
673 m_format
.Append(*p
++);
678 wxArrayString allowedChars
;
679 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
680 allowedChars
.Add(wxString(c
, 1));
682 const wxChar
*p2
= m_format
.c_str();
688 allowedChars
.Add(wxString(*p2
++, 1));
692 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
693 tv
.SetIncludes(allowedChars
);
694 m_txt
->SetValidator(tv
);
697 if (m_currentDate
.IsValid())
698 m_txt
->SetValue(m_currentDate
.Format(m_format
));
705 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
707 return m_currentDate
;
711 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
716 // we need to suppress the event sent from wxTextCtrl as calling our
717 // SetValue() should not result in an event being sent (wxTextCtrl is
718 // an exception to this rule)
719 gs_inSetValue
= this;
721 if ( date
.IsValid() )
723 m_txt
->SetValue(date
.Format(m_format
));
727 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE
),
728 _T("this control must have a valid date") );
730 m_txt
->SetValue(wxEmptyString
);
733 gs_inSetValue
= NULL
;
735 m_currentDate
= date
;
739 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
742 *dt1
= m_cal
->GetLowerDateLimit();
744 *dt2
= m_cal
->GetUpperDateLimit();
750 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
752 m_cal
->SetDateRange(dt1
, dt2
);
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
759 void wxDatePickerCtrlGeneric::DropDown(bool down
)
766 if (!m_txt
->GetValue().empty())
767 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
772 m_cal
->SetDate(wxDateTime::Today());
774 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
775 m_popup
->ShowAt(pos
.x
, pos
.y
+ GetSize().y
);
789 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
793 wxSize sz
= GetClientSize();
795 wxSize bs
=m_btn
->GetSize();
796 int eh
=m_txt
->GetBestSize().y
;
798 m_txt
->SetSize(0, TXTPOSY
, sz
.x
-bs
.x
, sz
.y
> eh
? eh
-TXTPOSY
: sz
.y
-TXTPOSY
);
799 m_btn
->SetSize(sz
.x
- bs
.x
, 0, bs
.x
, sz
.y
);
806 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
809 m_ignoreDrop
= false;
811 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
822 if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn
)
828 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
832 m_ignoreDrop
= false;
843 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
848 m_txt
->SetSelection(-1, -1); // select everything
853 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
861 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
864 if ( !HasFlag(wxDP_ALLOWNONE
) )
869 m_txt
->SetValue(dt
.Format(m_format
));
871 m_txt
->SetValue(wxEmptyString
);
873 // notify that we had to change the date after validation
874 if ( (dt
.IsValid() && (!m_currentDate
.IsValid() || m_currentDate
!= dt
)) ||
875 (!dt
.IsValid() && m_currentDate
.IsValid()) )
878 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
879 GetEventHandler()->ProcessEvent(event
);
884 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
888 m_currentDate
= m_cal
->GetDate();
889 m_txt
->SetValue(m_currentDate
.Format(m_format
));
890 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
896 ev
.SetEventObject(this);
898 GetParent()->ProcessEvent(ev
);
900 wxDateEvent
dev(this, ev
.GetDate(), wxEVT_DATE_CHANGED
);
901 GetParent()->ProcessEvent(dev
);
905 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
909 // artificial event resulting from our own SetValue() call, ignore it
913 ev
.SetEventObject(this);
915 GetParent()->ProcessEvent(ev
);
917 // We'll create an additional event if the date is valid.
918 // If the date isn't valid, the user's probably in the middle of typing
919 wxString txt
= m_txt
->GetValue();
923 dt
.ParseFormat(txt
, m_format
);
928 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
929 cev
.SetEventObject(this);
933 GetParent()->ProcessEvent(cev
);
935 wxDateEvent
dev(this, dt
, wxEVT_DATE_CHANGED
);
936 GetParent()->ProcessEvent(dev
);
940 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
942 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
949 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
951 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
957 #endif // wxUSE_DATEPICKCTRL_GENERIC
959 #endif // wxUSE_DATEPICKCTRL