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
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // This flag indicates that combo box style drop button is to be created
97 #define wxBU_COMBO 0x0400
100 class wxDropdownButton
: public wxBitmapButton
103 wxDropdownButton() { Init(); }
104 wxDropdownButton(wxWindow
*parent
,
106 const wxPoint
& pos
= wxDefaultPosition
,
107 const wxSize
& size
= wxDefaultSize
,
109 const wxValidator
& validator
= wxDefaultValidator
);
111 bool Create(wxWindow
*parent
,
113 const wxPoint
& pos
= wxDefaultPosition
,
114 const wxSize
& size
= wxDefaultSize
,
116 const wxValidator
& validator
= wxDefaultValidator
);
118 virtual void DoMoveWindow(int x
, int y
, int w
, int h
);
122 void OnSize(wxSizeEvent
& event
);
123 void OnMouseEnter(wxMouseEvent
& event
);
124 void OnMouseLeave(wxMouseEvent
& event
);
126 void RecreateBitmaps(int w
, int h
);
128 wxBitmap m_bmpNormal
;
131 int m_borderX
, m_borderY
;
133 // True if DrawDropArrow should be used instead of DrawComboBoxDropButton
144 DECLARE_EVENT_TABLE()
145 DECLARE_DYNAMIC_CLASS_NO_COPY(wxDropdownButton
)
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.
153 #if defined(__WXMSW__)
155 #define DROPBUT_USEDROPARROW(COMBOST) (COMBOST?false:true)
156 #define DROPBUT_DEFAULT_WIDTH 17
158 #elif defined(__WXGTK__)
160 #define DROPBUT_USEDROPARROW(COMBOST) true
161 #define DROPBUT_DEFAULT_WIDTH 19
165 #define DROPBUT_USEDROPARROW(COMBOST) true
166 #define DROPBUT_DEFAULT_WIDTH 17
171 IMPLEMENT_DYNAMIC_CLASS(wxDropdownButton
, wxBitmapButton
)
174 BEGIN_EVENT_TABLE(wxDropdownButton
,wxBitmapButton
)
175 EVT_ENTER_WINDOW(wxDropdownButton::OnMouseEnter
)
176 EVT_LEAVE_WINDOW(wxDropdownButton::OnMouseLeave
)
177 EVT_SIZE(wxDropdownButton::OnSize
)
181 wxDropdownButton::wxDropdownButton(wxWindow
*parent
,
186 const wxValidator
& validator
)
189 Create(parent
, id
, pos
, size
, style
, validator
);
193 bool wxDropdownButton::Create(wxWindow
*parent
,
198 const wxValidator
& validator
)
203 m_useDropArrow
= DROPBUT_USEDROPARROW(style
& wxBU_COMBO
);
205 wxBitmap
chkBmp(15,15); // arbitrary
206 if ( !wxBitmapButton::Create(parent
, id
, chkBmp
,
208 style
| (m_useDropArrow
? wxBU_AUTODRAW
: wxNO_BORDER
),
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
;
218 DoMoveWindow(pos
.x
, pos
.y
, size
.x
, size
.y
);
224 void wxDropdownButton::RecreateBitmaps(int w
, int h
)
228 int borderX
= m_marginX
+ m_borderX
;
229 int borderY
= m_marginY
+ m_borderY
;
230 int bw
= w
- borderX
;
231 int bh
= h
- borderY
;
233 wxBitmap
bmp(bw
, bh
);
234 wxBitmap
bmpSel(bw
, bh
);
237 wxRendererNative
& renderer
= wxRendererNative::Get();
239 dc
.SelectObject(bmp
);
241 if ( m_useDropArrow
)
243 // Use DrawDropArrow on transparent background.
245 wxColour
magic(255,0,255);
246 wxBrush
magicBrush(magic
);
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
);
258 dc
.SelectObject(bmpSel
);
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
);
270 // Use DrawComboBoxDropButton for the entire button
271 // (also render extra "hot" button state).
273 renderer
.DrawComboBoxDropButton(this, dc
, r
);
275 dc
.SelectObject(bmpSel
);
277 renderer
.DrawComboBoxDropButton(this, dc
, r
, wxCONTROL_PRESSED
);
279 wxBitmap
bmpHot(bw
,bh
);
280 dc
.SelectObject(bmpHot
);
281 renderer
.DrawComboBoxDropButton(this, dc
, r
, wxCONTROL_CURRENT
);
288 SetBitmapSelected(bmpSel
);
292 void wxDropdownButton::DoMoveWindow(int x
, int y
, int w
, int h
)
295 w
= DROPBUT_DEFAULT_WIDTH
;
297 wxBitmapButton::DoMoveWindow(x
, y
, w
, h
);
301 void wxDropdownButton::OnSize(wxSizeEvent
& event
)
303 if ( m_borderX
>= 0 && m_borderY
>= 0 )
306 GetClientSize(&w
,&h
);
308 if ( w
> 1 && h
> 1 )
309 RecreateBitmaps(w
,h
);
315 void wxDropdownButton::OnMouseEnter(wxMouseEvent
& event
)
317 if ( !m_useDropArrow
)
318 SetBitmapLabel(m_bmpHot
);
324 void wxDropdownButton::OnMouseLeave(wxMouseEvent
& event
)
326 if ( !m_useDropArrow
)
327 SetBitmapLabel(m_bmpNormal
);
335 #include "wx/popupwin.h"
337 class wxDatePopupInternal
: public wxPopupTransientWindow
340 wxDatePopupInternal(wxWindow
*parent
) : wxPopupTransientWindow(parent
) { }
342 void ShowAt(int x
, int y
)
344 Position(wxPoint(x
, y
), wxSize(0, 0));
354 #else // !wxUSE_POPUPWIN
356 class wxDatePopupInternal
: public wxDialog
359 wxDatePopupInternal(wxWindow
*parent
)
369 void ShowAt(int x
, int y
)
381 #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN
383 // ============================================================================
384 // wxDatePickerCtrlGeneric implementation
385 // ============================================================================
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
)
394 #ifndef wxHAS_NATIVE_DATEPICKCTRL
395 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
404 const wxDateTime
& date
,
408 const wxValidator
& validator
,
409 const wxString
& name
)
411 wxASSERT_MSG( !(style
& wxDP_SPIN
),
412 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
414 if ( !wxControl::Create(parent
, id
, pos
, size
,
415 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
424 m_txt
= new wxTextCtrl(this, CTRLID_TXT
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, TXTCTRL_FLAGS
);
426 m_txt
->Connect(wxEVT_KEY_DOWN
,
427 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey
),
429 m_txt
->Connect(wxEVT_KILL_FOCUS
,
430 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus
),
433 m_btn
= new wxDropdownButton(this, CTRLID_BTN
, wxDefaultPosition
, wxDefaultSize
, wxBU_COMBO
);
435 m_popup
= new wxDatePopupInternal(this);
436 m_popup
->SetFont(GetFont());
438 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
439 wxPoint(0, 0), wxDefaultSize
,
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
),
447 m_cal
->Connect(wxEVT_KEY_DOWN
,
448 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey
),
450 m_cal
->Connect(wxEVT_CALENDAR_DOUBLECLICKED
,
451 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
453 m_cal
->Connect(wxEVT_CALENDAR_DAY_CHANGED
,
454 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
456 m_cal
->Connect(wxEVT_CALENDAR_MONTH_CHANGED
,
457 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
459 m_cal
->Connect(wxEVT_CALENDAR_YEAR_CHANGED
,
460 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
463 wxWindow
*yearControl
= m_cal
->GetYearControl();
465 Connect(wxEVT_SET_FOCUS
,
466 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus
));
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
;
474 wxSize calSize
= m_cal
->GetBestSize();
475 wxSize yearSize
= yearControl
->GetSize();
478 wxPoint yearPosition
= yearControl
->GetPosition();
480 SetFormat(wxT("%x"));
482 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
483 if (width
< calSize
.x
-4)
486 int calPos
= (width
-calSize
.x
)/2;
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);
499 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
500 m_popup
->SetClientSize(panel
->GetSize());
503 SetValue(date
.IsValid() ? date
: wxDateTime::Today());
505 SetBestFittingSize(size
);
507 SetBackgroundColour(m_txt
->GetBackgroundColour());
513 void wxDatePickerCtrlGeneric::Init()
521 m_ignoreDrop
= false;
524 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
532 bool wxDatePickerCtrlGeneric::Destroy()
548 return wxControl::Destroy();
551 // ----------------------------------------------------------------------------
552 // overridden base class methods
553 // ----------------------------------------------------------------------------
555 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
557 wxControl::DoMoveWindow(x
, y
, w
, h
);
563 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
565 int bh
=m_btn
->GetBestSize().y
;
566 int eh
=m_txt
->GetBestSize().y
;
567 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
571 bool wxDatePickerCtrlGeneric::Show(bool show
)
573 if ( !wxControl::Show(show
) )
591 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
593 if ( !wxControl::Enable(enable
) )
605 m_btn
->Enable(enable
);
610 // ----------------------------------------------------------------------------
611 // wxDatePickerCtrlGeneric API
612 // ----------------------------------------------------------------------------
615 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
616 const wxDateTime
& upperdate
)
618 return m_cal
->SetDateRange(lowerdate
, upperdate
);
621 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
624 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
625 wxString str
=dt
.Format(fmt
);
626 wxChar
*p
=(wxChar
*)str
.c_str();
628 m_format
=wxEmptyString
;
633 if (n
== dt
.GetDay())
635 m_format
.Append(wxT("%d"));
638 else if (n
== (int)dt
.GetMonth()+1)
640 m_format
.Append(wxT("%m"));
643 else if (n
== dt
.GetYear())
645 m_format
.Append(wxT("%Y"));
648 else if (n
== (dt
.GetYear() % 100))
650 if (GetWindowStyle() & wxDP_SHOWCENTURY
)
651 m_format
.Append(wxT("%Y"));
653 m_format
.Append(wxT("%y"));
657 m_format
.Append(*p
++);
662 wxArrayString allowedChars
;
663 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
664 allowedChars
.Add(wxString(c
, 1));
666 const wxChar
*p
= m_format
.c_str();
672 allowedChars
.Add(wxString(*p
++, 1));
676 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
677 tv
.SetIncludes(allowedChars
);
678 m_txt
->SetValidator(tv
);
681 if (m_currentDate
.IsValid())
682 m_txt
->SetValue(m_currentDate
.Format(m_format
));
689 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
691 return m_currentDate
;
695 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
700 m_txt
->SetValue(date
.Format(m_format
));
703 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE
),
704 _T("this control must have a valid date") );
706 m_txt
->SetValue(wxEmptyString
);
709 m_currentDate
= date
;
714 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
717 *dt1
= m_cal
->GetLowerDateLimit();
719 *dt2
= m_cal
->GetUpperDateLimit();
725 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
727 m_cal
->SetDateRange(dt1
, dt2
);
730 // ----------------------------------------------------------------------------
732 // ----------------------------------------------------------------------------
734 void wxDatePickerCtrlGeneric::DropDown(bool down
)
741 if (!m_txt
->GetValue().empty())
742 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
747 m_cal
->SetDate(wxDateTime::Today());
749 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
750 m_popup
->ShowAt(pos
.x
, pos
.y
+ GetSize().y
);
764 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
768 wxSize sz
= GetClientSize();
770 wxSize bs
=m_btn
->GetSize();
771 int eh
=m_txt
->GetBestSize().y
;
773 m_txt
->SetSize(0, TXTPOSY
, sz
.x
-bs
.x
, sz
.y
> eh
? eh
-TXTPOSY
: sz
.y
-TXTPOSY
);
774 m_btn
->SetSize(sz
.x
- bs
.x
, 0, bs
.x
, sz
.y
);
781 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
784 m_ignoreDrop
= false;
786 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
797 if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn
)
803 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
807 m_ignoreDrop
= false;
818 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
823 m_txt
->SetSelection(-1, -1); // select everything
828 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
836 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
839 if ( !HasFlag(wxDP_ALLOWNONE
) )
844 m_txt
->SetValue(dt
.Format(m_format
));
846 m_txt
->SetValue(wxEmptyString
);
848 // notify that we had to change the date after validation
849 if ( (dt
.IsValid() && m_currentDate
!= dt
) ||
850 (!dt
.IsValid() && m_currentDate
.IsValid()) )
853 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
854 GetEventHandler()->ProcessEvent(event
);
859 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
863 m_currentDate
= m_cal
->GetDate();
864 m_txt
->SetValue(m_currentDate
.Format(m_format
));
865 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
871 ev
.SetEventObject(this);
873 GetParent()->ProcessEvent(ev
);
875 wxDateEvent
dev(this, ev
.GetDate(), wxEVT_DATE_CHANGED
);
876 GetParent()->ProcessEvent(dev
);
880 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
882 ev
.SetEventObject(this);
884 GetParent()->ProcessEvent(ev
);
886 // We'll create an additional event if the date is valid.
887 // If the date isn't valid, the user's probably in the middle of typing
888 wxString txt
= m_txt
->GetValue();
892 dt
.ParseFormat(txt
, m_format
);
897 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
898 cev
.SetEventObject(this);
902 GetParent()->ProcessEvent(cev
);
904 wxDateEvent
dev(this, dt
, wxEVT_DATE_CHANGED
);
905 GetParent()->ProcessEvent(dev
);
909 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
911 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
918 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
920 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
926 #endif // wxUSE_DATEPICKCTRL_GENERIC
928 #endif // wxUSE_DATEPICKCTRL