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));
570 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
571 tv
.SetIncludes(allowedChars
);
573 m_txt
->SetValidator(tv
);
575 if (m_currentDate
.IsValid())
576 m_txt
->SetValue(m_currentDate
.Format(m_format
));
583 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
585 return m_currentDate
;
589 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
594 m_txt
->SetValue(date
.Format(m_format
));
597 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE
),
598 _T("this control must have a valid date") );
600 m_txt
->SetValue(wxEmptyString
);
603 m_currentDate
= date
;
608 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
611 *dt1
= m_cal
->GetLowerDateLimit();
613 *dt2
= m_cal
->GetUpperDateLimit();
619 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
621 m_cal
->SetDateRange(dt1
, dt2
);
624 // ----------------------------------------------------------------------------
626 // ----------------------------------------------------------------------------
628 void wxDatePickerCtrlGeneric::DropDown(bool down
)
635 if (!m_txt
->GetValue().empty())
636 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
641 m_cal
->SetDate(wxDateTime::Today());
643 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
644 m_popup
->ShowAt(pos
.x
, pos
.y
+ GetSize().y
);
658 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
661 m_ignoreDrop
= false;
663 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
674 if (ev
.GetEventObject() == m_btn
)
680 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
684 m_ignoreDrop
= false;
695 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
700 m_txt
->SetSelection(-1, -1); // select everything
705 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
710 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
713 if ( !HasFlag(wxDP_ALLOWNONE
) )
718 m_txt
->SetValue(dt
.Format(m_format
));
720 m_txt
->SetValue(wxEmptyString
);
722 // notify that we had to change the date after validation
723 if ( (dt
.IsValid() && m_currentDate
!= dt
) ||
724 (!dt
.IsValid() && m_currentDate
.IsValid()) )
727 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
728 GetEventHandler()->ProcessEvent(event
);
733 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
737 m_currentDate
= m_cal
->GetDate();
738 m_txt
->SetValue(m_currentDate
.Format(m_format
));
739 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
745 ev
.SetEventObject(this);
747 GetParent()->ProcessEvent(ev
);
749 wxDateEvent
dev(this, ev
.GetDate(), wxEVT_DATE_CHANGED
);
750 GetParent()->ProcessEvent(dev
);
754 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
756 ev
.SetEventObject(this);
758 GetParent()->ProcessEvent(ev
);
760 // We'll create an additional event if the date is valid.
761 // If the date isn't valid, the user's probably in the middle of typing
762 wxString txt
= m_txt
->GetValue();
766 dt
.ParseFormat(txt
, m_format
);
771 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
772 cev
.SetEventObject(this);
776 GetParent()->ProcessEvent(cev
);
778 wxDateEvent
dev(this, dt
, wxEVT_DATE_CHANGED
);
779 GetParent()->ProcessEvent(dev
);
783 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
785 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
792 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
794 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
800 #endif // wxUSE_DATEPICKCTRL_GENERIC
802 #endif // wxUSE_DATEPICKCTRL