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
77 #define wxUSE_POPUPWIN 0 // Popup not working
78 #define TXTCTRL_FLAGS wxNO_BORDER
79 #define BTN_FLAGS wxNO_BORDER
81 #define RIGHTBUTTONBORDER 3
82 #define TOPBUTTONBORDER 0
83 #define BUTTONBORDER 3
86 #define TXTCTRL_FLAGS 0
87 #define BTN_FLAGS wxBU_AUTODRAW
89 #define RIGHTBUTTONBORDER 0
90 #define TOPBUTTONBORDER 0
91 #define BUTTONBORDER 0
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
101 class wxDropdownButton
: public wxBitmapButton
104 wxDropdownButton() { Init(); }
105 wxDropdownButton(wxWindow
*parent
,
107 const wxPoint
& pos
= wxDefaultPosition
,
108 const wxSize
& size
= wxDefaultSize
,
110 const wxValidator
& validator
= wxDefaultValidator
);
117 bool Create(wxWindow
*parent
,
119 const wxPoint
& pos
= wxDefaultPosition
,
120 const wxSize
& size
= wxDefaultSize
,
122 const wxValidator
& validator
= wxDefaultValidator
);
124 void DoMoveWindow(int x
, int y
, int w
, int h
);
127 int m_borderX
, m_borderY
;
131 wxDropdownButton::wxDropdownButton(wxWindow
*parent
,
136 const wxValidator
& validator
)
139 Create(parent
, id
, pos
, size
, style
, validator
);
143 bool wxDropdownButton::Create(wxWindow
*parent
,
147 long WXUNUSED(style
),
148 const wxValidator
& validator
)
153 wxBitmap
chkBmp(15,15); // arbitrary
154 if ( !wxBitmapButton::Create(parent
, id
, chkBmp
,
155 pos
, wxDefaultSize
, BTN_FLAGS
, validator
) )
158 const wxSize sz
= GetSize();
159 int w
= chkBmp
.GetWidth(),
160 h
= chkBmp
.GetHeight();
161 m_borderX
= sz
.x
- m_marginX
- w
;
162 m_borderY
= sz
.y
- m_marginY
- h
;
164 w
= size
.x
> 0 ? size
.x
: sz
.x
;
165 h
= size
.y
> 0 ? size
.y
: sz
.y
;
167 DoMoveWindow(pos
.x
, pos
.y
, w
, h
);
173 void wxDropdownButton::DoMoveWindow(int x
, int y
, int w
, int h
)
175 if (m_borderX
>= 0 && m_borderY
>= 0 && (w
>= 0 || h
>= 0))
182 w
= m_marginX
+ m_borderX
+ 15; // GTK magic size
187 int bw
= w
- m_marginX
- m_borderX
;
188 int bh
= h
- m_marginY
- m_borderY
;
192 wxBitmap
bmp(bw
, bh
);
193 dc
.SelectObject(bmp
);
195 wxRect
r(0,0,bw
, bh
);
196 wxRendererNative
& renderer
= wxRendererNative::Get();
199 wxColour
magic(255,0,255);
200 dc
.SetBrush( wxBrush( magic
) );
201 dc
.SetPen( *wxTRANSPARENT_PEN
);
202 dc
.DrawRectangle(0,0,bw
,bh
);
203 renderer
.DrawComboBoxDropButton(this, dc
, r
);
204 wxMask
*mask
= new wxMask( bmp
, magic
);
207 renderer
.DrawComboBoxDropButton(this, dc
, r
);
211 wxBitmap
bmpSel(bw
, bh
);
212 dc
.SelectObject(bmpSel
);
215 dc
.SetBrush( wxBrush( magic
) );
216 dc
.SetPen( *wxTRANSPARENT_PEN
);
217 dc
.DrawRectangle(0,0,bw
,bh
);
218 renderer
.DrawComboBoxDropButton(this, dc
, r
, wxCONTROL_PRESSED
);
219 mask
= new wxMask( bmpSel
, magic
);
220 bmpSel
.SetMask( mask
);
222 renderer
.DrawComboBoxDropButton(this, dc
, r
, wxCONTROL_PRESSED
);
224 SetBitmapSelected(bmpSel
);
227 wxBitmapButton::DoMoveWindow(x
, y
, w
, h
);
233 #include "wx/popupwin.h"
235 class wxDatePopupInternal
: public wxPopupTransientWindow
238 wxDatePopupInternal(wxWindow
*parent
) : wxPopupTransientWindow(parent
) { }
240 void ShowAt(int x
, int y
)
242 Position(wxPoint(x
, y
), wxSize(0, 0));
252 #else // !wxUSE_POPUPWIN
254 class wxDatePopupInternal
: public wxDialog
257 wxDatePopupInternal(wxWindow
*parent
)
267 void ShowAt(int x
, int y
)
279 #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN
281 // ============================================================================
282 // wxDatePickerCtrlGeneric implementation
283 // ============================================================================
285 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
286 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrlGeneric::OnClick
)
287 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrlGeneric::OnText
)
288 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus
)
291 #ifndef wxHAS_NATIVE_DATEPICKCTRL
292 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
301 const wxDateTime
& date
,
305 const wxValidator
& validator
,
306 const wxString
& name
)
308 wxASSERT_MSG( !(style
& wxDP_SPIN
),
309 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
311 if ( !wxControl::Create(parent
, id
, pos
, size
,
312 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
321 m_txt
= new wxTextCtrl(this, CTRLID_TXT
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, TXTCTRL_FLAGS
);
323 m_txt
->Connect(wxEVT_KEY_DOWN
,
324 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey
),
326 m_txt
->Connect(wxEVT_KILL_FOCUS
,
327 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus
),
330 const int height
= m_txt
->GetBestSize().y
- BUTTONBORDER
;
332 m_btn
= new wxDropdownButton(this, CTRLID_BTN
, wxDefaultPosition
, wxSize(height
, height
));
334 m_popup
= new wxDatePopupInternal(this);
335 m_popup
->SetFont(GetFont());
337 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
338 wxPoint(0, 0), wxDefaultSize
,
340 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
341 wxPoint(0, 0), wxDefaultSize
,
342 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
343 m_cal
->Connect(wxEVT_CALENDAR_SEL_CHANGED
,
344 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
346 m_cal
->Connect(wxEVT_KEY_DOWN
,
347 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey
),
349 m_cal
->Connect(wxEVT_CALENDAR_DOUBLECLICKED
,
350 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
352 m_cal
->Connect(wxEVT_CALENDAR_DAY_CHANGED
,
353 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
355 m_cal
->Connect(wxEVT_CALENDAR_MONTH_CHANGED
,
356 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
358 m_cal
->Connect(wxEVT_CALENDAR_YEAR_CHANGED
,
359 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
362 wxWindow
*yearControl
= m_cal
->GetYearControl();
364 Connect(wxEVT_SET_FOCUS
,
365 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus
));
367 wxClientDC
dc(yearControl
);
368 dc
.SetFont(yearControl
->GetFont());
369 wxCoord width
, dummy
;
370 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
371 width
+= ConvertDialogToPixels(wxSize(20, 0)).x
;
373 wxSize calSize
= m_cal
->GetBestSize();
374 wxSize yearSize
= yearControl
->GetSize();
377 wxPoint yearPosition
= yearControl
->GetPosition();
379 SetFormat(wxT("%x"));
381 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
382 if (width
< calSize
.x
-4)
385 int calPos
= (width
-calSize
.x
)/2;
391 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
392 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
393 yearSize
.x
, yearSize
.y
);
394 m_cal
->GetMonthControl()->Move(0, 0);
398 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
399 m_popup
->SetClientSize(panel
->GetSize());
402 SetValue(date
.IsValid() ? date
: wxDateTime::Today());
404 SetBestFittingSize(size
);
410 void wxDatePickerCtrlGeneric::Init()
418 m_ignoreDrop
= false;
422 bool wxDatePickerCtrlGeneric::Destroy()
438 return wxControl::Destroy();
441 // ----------------------------------------------------------------------------
442 // overridden base class methods
443 // ----------------------------------------------------------------------------
445 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
447 wxControl::DoMoveWindow(x
, y
, w
, h
);
448 wxSize bs
=m_btn
->GetBestSize();
449 int eh
=m_txt
->GetBestSize().y
;
451 m_txt
->SetSize(0, TXTPOSY
, w
-bs
.x
-RIGHTBUTTONBORDER
, h
> eh
? eh
-TXTPOSY
: h
-TXTPOSY
);
452 m_btn
->SetSize(w
- bs
.x
-RIGHTBUTTONBORDER
, TOPBUTTONBORDER
, bs
.x
, h
> bs
.y
? bs
.y
: h
);
458 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
460 int bh
=m_btn
->GetBestSize().y
;
461 int eh
=m_txt
->GetBestSize().y
;
462 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
466 bool wxDatePickerCtrlGeneric::Show(bool show
)
468 if ( !wxControl::Show(show
) )
486 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
488 if ( !wxControl::Enable(enable
) )
500 m_btn
->Enable(enable
);
505 // ----------------------------------------------------------------------------
506 // wxDatePickerCtrlGeneric API
507 // ----------------------------------------------------------------------------
510 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
511 const wxDateTime
& upperdate
)
513 return m_cal
->SetDateRange(lowerdate
, upperdate
);
516 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
519 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
520 wxString str
=dt
.Format(fmt
);
521 wxChar
*p
=(wxChar
*)str
.c_str();
523 m_format
=wxEmptyString
;
528 if (n
== dt
.GetDay())
530 m_format
.Append(wxT("%d"));
533 else if (n
== (int)dt
.GetMonth()+1)
535 m_format
.Append(wxT("%m"));
538 else if (n
== dt
.GetYear())
540 m_format
.Append(wxT("%Y"));
543 else if (n
== (dt
.GetYear() % 100))
545 if (GetWindowStyle() & wxDP_SHOWCENTURY
)
546 m_format
.Append(wxT("%Y"));
548 m_format
.Append(wxT("%y"));
552 m_format
.Append(*p
++);
557 wxArrayString allowedChars
;
558 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
559 allowedChars
.Add(wxString(c
, 1));
561 const wxChar
*p
= m_format
.c_str();
567 allowedChars
.Add(wxString(*p
++, 1));
571 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
572 tv
.SetIncludes(allowedChars
);
573 m_txt
->SetValidator(tv
);
576 if (m_currentDate
.IsValid())
577 m_txt
->SetValue(m_currentDate
.Format(m_format
));
584 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
586 return m_currentDate
;
590 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
595 m_txt
->SetValue(date
.Format(m_format
));
598 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE
),
599 _T("this control must have a valid date") );
601 m_txt
->SetValue(wxEmptyString
);
604 m_currentDate
= date
;
609 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
612 *dt1
= m_cal
->GetLowerDateLimit();
614 *dt2
= m_cal
->GetUpperDateLimit();
620 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
622 m_cal
->SetDateRange(dt1
, dt2
);
625 // ----------------------------------------------------------------------------
627 // ----------------------------------------------------------------------------
629 void wxDatePickerCtrlGeneric::DropDown(bool down
)
636 if (!m_txt
->GetValue().empty())
637 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
642 m_cal
->SetDate(wxDateTime::Today());
644 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
645 m_popup
->ShowAt(pos
.x
, pos
.y
+ GetSize().y
);
659 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
662 m_ignoreDrop
= false;
664 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
675 if (ev
.GetEventObject() == m_btn
)
681 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
685 m_ignoreDrop
= false;
696 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
701 m_txt
->SetSelection(-1, -1); // select everything
706 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
711 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
714 if ( !HasFlag(wxDP_ALLOWNONE
) )
719 m_txt
->SetValue(dt
.Format(m_format
));
721 m_txt
->SetValue(wxEmptyString
);
723 // notify that we had to change the date after validation
724 if ( (dt
.IsValid() && m_currentDate
!= dt
) ||
725 (!dt
.IsValid() && m_currentDate
.IsValid()) )
728 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
729 GetEventHandler()->ProcessEvent(event
);
734 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
738 m_currentDate
= m_cal
->GetDate();
739 m_txt
->SetValue(m_currentDate
.Format(m_format
));
740 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
746 ev
.SetEventObject(this);
748 GetParent()->ProcessEvent(ev
);
750 wxDateEvent
dev(this, ev
.GetDate(), wxEVT_DATE_CHANGED
);
751 GetParent()->ProcessEvent(dev
);
755 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
757 ev
.SetEventObject(this);
759 GetParent()->ProcessEvent(ev
);
761 // We'll create an additional event if the date is valid.
762 // If the date isn't valid, the user's probably in the middle of typing
763 wxString txt
= m_txt
->GetValue();
767 dt
.ParseFormat(txt
, m_format
);
772 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
773 cev
.SetEventObject(this);
777 GetParent()->ProcessEvent(cev
);
779 wxDateEvent
dev(this, dt
, wxEVT_DATE_CHANGED
);
780 GetParent()->ProcessEvent(dev
);
784 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
786 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
793 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
795 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
801 #endif // wxUSE_DATEPICKCTRL_GENERIC
803 #endif // wxUSE_DATEPICKCTRL