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"
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 #ifndef wxHAS_NATIVE_DATEPICKCTRL
53 #define _WX_DEFINE_DATE_EVENTS_
56 #include "wx/dateevt.h"
58 #include "wx/calctrl.h"
59 #include "wx/renderer.h"
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
73 #ifndef DEFAULT_ITEM_WIDTH
74 #define DEFAULT_ITEM_WIDTH 100
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
82 class wxDropdownButton
: public wxBitmapButton
85 wxDropdownButton() { Init(); }
86 wxDropdownButton(wxWindow
*parent
,
88 const wxPoint
& pos
= wxDefaultPosition
,
89 const wxSize
& size
= wxDefaultSize
,
91 const wxValidator
& validator
= wxDefaultValidator
);
98 void Create(wxWindow
*parent
,
100 const wxPoint
& pos
= wxDefaultPosition
,
101 const wxSize
& size
= wxDefaultSize
,
103 const wxValidator
& validator
= wxDefaultValidator
);
105 void DoMoveWindow(int x
, int y
, int w
, int h
);
108 int m_borderX
, m_borderY
;
112 wxDropdownButton::wxDropdownButton(wxWindow
*parent
,
117 const wxValidator
& validator
)
120 Create(parent
, id
, pos
, size
, style
, validator
);
124 void wxDropdownButton::Create(wxWindow
*parent
,
129 const wxValidator
& validator
)
131 wxBitmap
chkBmp(15,15); // arbitrary
132 wxBitmapButton::Create(parent
, id
, chkBmp
, pos
, wxDefaultSize
, wxBU_AUTODRAW
, validator
);
137 h
=chkBmp
.GetHeight();
138 m_borderX
= GetSize().x
- m_marginX
- w
;
139 m_borderY
= GetSize().y
- m_marginY
- h
;
141 w
= (size
.x
> 0 ? size
.x
: GetSize().x
);
142 h
= (size
.y
> 0 ? size
.y
: GetSize().y
);
144 DoMoveWindow(pos
.x
, pos
.y
, w
, h
);
148 void wxDropdownButton::DoMoveWindow(int x
, int y
, int w
, int h
)
150 if (m_borderX
>= 0 && m_borderY
>= 0 && (w
>= 0 || h
>= 0))
157 w
= m_marginX
+ m_borderX
+ 15; // GTK magic size
162 int bw
= w
- m_marginX
- m_borderX
;
163 int bh
= h
- m_marginY
- m_borderY
;
167 wxBitmap
bmp(bw
, bh
);
168 dc
.SelectObject(bmp
);
170 wxRendererNative::Get().DrawComboBoxDropButton(this, dc
, wxRect(0,0,bw
, bh
));
175 wxBitmapButton::DoMoveWindow(x
, y
, w
, h
);
181 #include "wx/popupwin.h"
183 class wxDatePopupInternal
: public wxPopupTransientWindow
186 wxDatePopupInternal(wxWindow
*parent
) : wxPopupTransientWindow(parent
) { }
188 void ShowAt(int x
, int y
)
190 Position(wxPoint(x
, y
), wxSize(0, 0));
200 #else // !wxUSE_POPUPWIN
202 class wxDatePopupInternal
: public wxDialog
205 wxDatePopupInternal(wxWindow
*parent
)
215 void ShowAt(int x
, int y
)
227 #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN
229 // ============================================================================
230 // wxDatePickerCtrlGeneric implementation
231 // ============================================================================
233 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
234 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrlGeneric::OnClick
)
235 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrlGeneric::OnText
)
236 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus
)
239 #ifndef wxHAS_NATIVE_DATEPICKCTRL
240 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
249 const wxDateTime
& date
,
253 const wxValidator
& validator
,
254 const wxString
& name
)
256 wxASSERT_MSG( !(style
& wxDP_SPIN
),
257 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
259 if ( !wxControl::Create(parent
, id
, pos
, size
,
260 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
269 m_txt
= new wxTextCtrl(this, CTRLID_TXT
);
271 m_txt
->Connect(wxEVT_KEY_DOWN
,
272 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey
),
274 m_txt
->Connect(wxEVT_KILL_FOCUS
,
275 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus
),
278 const int height
= m_txt
->GetBestSize().y
;
280 m_btn
= new wxDropdownButton(this, CTRLID_BTN
, wxDefaultPosition
, wxSize(height
, height
));
282 m_popup
= new wxDatePopupInternal(this);
283 m_popup
->SetFont(GetFont());
285 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
286 wxPoint(0, 0), wxDefaultSize
,
288 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
289 wxPoint(0, 0), wxDefaultSize
,
290 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
291 m_cal
->Connect(wxEVT_CALENDAR_SEL_CHANGED
,
292 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
294 m_cal
->Connect(wxEVT_KEY_DOWN
,
295 wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey
),
297 m_cal
->Connect(wxEVT_CALENDAR_DOUBLECLICKED
,
298 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
300 m_cal
->Connect(wxEVT_CALENDAR_DAY_CHANGED
,
301 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
303 m_cal
->Connect(wxEVT_CALENDAR_MONTH_CHANGED
,
304 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
306 m_cal
->Connect(wxEVT_CALENDAR_YEAR_CHANGED
,
307 wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange
),
310 wxWindow
*yearControl
= m_cal
->GetYearControl();
312 Connect(wxEVT_SET_FOCUS
,
313 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus
));
315 wxClientDC
dc(yearControl
);
317 wxCoord width
, dummy
;
318 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
319 width
+= ConvertDialogToPixels(wxSize(20, 0)).x
;
321 wxSize calSize
= m_cal
->GetBestSize();
322 wxSize yearSize
= yearControl
->GetSize();
325 wxPoint yearPosition
= yearControl
->GetPosition();
327 SetFormat(wxT("%x"));
332 #define RIGHTBUTTONBORDER 2
333 #define TOPBUTTONBORDER 1
336 #define RIGHTBUTTONBORDER 0
337 #define TOPBUTTONBORDER 0
340 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
341 if (width
< calSize
.x
-4)
344 int calPos
= (width
-calSize
.x
)/2;
350 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
351 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
352 yearSize
.x
, yearSize
.y
);
353 m_cal
->GetMonthControl()->Move(0, 0);
357 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
358 m_popup
->SetClientSize(panel
->GetSize());
370 void wxDatePickerCtrlGeneric::Init()
378 m_ignoreDrop
= false;
382 bool wxDatePickerCtrlGeneric::Destroy()
398 return wxControl::Destroy();
401 // ----------------------------------------------------------------------------
402 // overridden base class methods
403 // ----------------------------------------------------------------------------
405 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
407 wxControl::DoMoveWindow(x
, y
, w
, h
);
408 wxSize bs
=m_btn
->GetBestSize();
409 int eh
=m_txt
->GetBestSize().y
;
411 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
412 m_btn
->SetSize(w
- bs
.x
-RIGHTBUTTONBORDER
, TOPBUTTONBORDER
, bs
.x
, h
> bs
.y
? bs
.y
: h
);
418 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
420 int bh
=m_btn
->GetBestSize().y
;
421 int eh
=m_txt
->GetBestSize().y
;
422 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
426 bool wxDatePickerCtrlGeneric::Show(bool show
)
428 if ( !wxControl::Show(show
) )
446 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
448 if ( !wxControl::Enable(enable
) )
460 m_btn
->Enable(enable
);
465 // ----------------------------------------------------------------------------
466 // wxDatePickerCtrlGeneric API
467 // ----------------------------------------------------------------------------
470 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
471 const wxDateTime
& upperdate
)
473 return m_cal
->SetDateRange(lowerdate
, upperdate
);
476 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
479 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
480 wxString str
=dt
.Format(fmt
);
481 wxChar
*p
=(wxChar
*)str
.c_str();
483 m_format
=wxEmptyString
;
488 if (n
== dt
.GetDay())
490 m_format
.Append(wxT("%d"));
493 else if (n
== (int)dt
.GetMonth()+1)
495 m_format
.Append(wxT("%m"));
498 else if (n
== dt
.GetYear())
500 m_format
.Append(wxT("%Y"));
503 else if (n
== (dt
.GetYear() % 100))
505 if (GetWindowStyle() & wxDP_SHOWCENTURY
)
506 m_format
.Append(wxT("%Y"));
508 m_format
.Append(wxT("%y"));
512 m_format
.Append(*p
++);
517 wxArrayString allowedChars
;
518 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
519 allowedChars
.Add(wxString(c
, 1));
521 const wxChar
*p
= m_format
.c_str();
527 allowedChars
.Add(wxString(*p
++, 1));
530 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
531 tv
.SetIncludes(allowedChars
);
533 m_txt
->SetValidator(tv
);
535 if (m_currentDate
.IsValid())
536 m_txt
->SetValue(m_currentDate
.Format(m_format
));
543 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
545 return m_currentDate
;
549 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
554 m_txt
->SetValue(date
.Format(m_format
));
557 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE
),
558 _T("this control must have a valid date") );
560 m_txt
->SetValue(wxEmptyString
);
563 m_currentDate
= date
;
568 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
571 *dt1
= m_cal
->GetLowerDateLimit();
573 *dt2
= m_cal
->GetUpperDateLimit();
579 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
581 m_cal
->SetDateRange(dt1
, dt2
);
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 void wxDatePickerCtrlGeneric::DropDown(bool down
)
595 if (!m_txt
->GetValue().empty())
596 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
601 m_cal
->SetDate(wxDateTime::Today());
603 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
604 m_popup
->ShowAt(pos
.x
, pos
.y
+ GetSize().y
);
618 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
621 m_ignoreDrop
= false;
623 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
634 if (ev
.GetEventObject() == m_btn
)
640 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
644 m_ignoreDrop
= false;
655 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
660 m_txt
->SetSelection(-1, -1); // select everything
665 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
670 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
673 if ( !HasFlag(wxDP_ALLOWNONE
) )
678 m_txt
->SetValue(dt
.Format(m_format
));
680 m_txt
->SetValue(wxEmptyString
);
682 // notify that we had to change the date after validation
683 if ( (dt
.IsValid() && m_currentDate
!= dt
) ||
684 (!dt
.IsValid() && m_currentDate
.IsValid()) )
687 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
688 GetEventHandler()->ProcessEvent(event
);
693 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
697 m_currentDate
= m_cal
->GetDate();
698 m_txt
->SetValue(m_currentDate
.Format(m_format
));
699 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
705 ev
.SetEventObject(this);
707 GetParent()->ProcessEvent(ev
);
709 wxDateEvent
dev(this, ev
.GetDate(), wxEVT_DATE_CHANGED
);
710 GetParent()->ProcessEvent(dev
);
714 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
716 ev
.SetEventObject(this);
718 GetParent()->ProcessEvent(ev
);
720 // We'll create an additional event if the date is valid.
721 // If the date isn't valid, the user's probably in the middle of typing
722 wxString txt
= m_txt
->GetValue();
726 dt
.ParseFormat(txt
, m_format
);
731 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
732 cev
.SetEventObject(this);
736 GetParent()->ProcessEvent(cev
);
738 wxDateEvent
dev(this, dt
, wxEVT_DATE_CHANGED
);
739 GetParent()->ProcessEvent(dev
);
743 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
745 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
752 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
754 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
760 #endif // wxUSE_DATEPICKCTRL_GENERIC
762 #endif // wxUSE_DATEPICKCTRL