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 // otherwise it's defined in the native version implementation
44 #ifndef wxHAS_NATIVE_DATEPICKCTRL
45 #define _WX_DEFINE_DATE_EVENTS_
48 #include "wx/dateevt.h"
49 #include "wx/generic/datectrl.h"
51 #include "wx/calctrl.h"
52 #include "wx/popupwin.h"
53 #include "wx/renderer.h"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
67 #ifndef DEFAULT_ITEM_WIDTH
68 #define DEFAULT_ITEM_WIDTH 100
71 // ============================================================================
72 // wxDatePickerCtrlGeneric implementation
73 // ============================================================================
75 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
76 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrlGeneric::OnClick
)
77 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrlGeneric::OnText
)
78 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus
)
81 #ifndef wxHAS_NATIVE_DATEPICKCTRL
82 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
91 const wxDateTime
& date
,
95 const wxValidator
& validator
,
98 wxASSERT_MSG( !(style
& wxDP_SPIN
),
99 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
101 if ( !wxControl::Create(parent
, id
, pos
, size
,
102 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
111 m_txt
= new wxTextCtrl(this, CTRLID_TXT
);
112 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
113 (wxObjectEventFunction
)
115 (wxCharEventFunction
)
116 &wxDatePickerCtrlGeneric::OnEditKey
,
118 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
119 (wxObjectEventFunction
)
121 (wxFocusEventFunction
)
122 &wxDatePickerCtrlGeneric::OnKillFocus
,
125 const int height
= m_txt
->GetBestSize().y
- 4; // FIXME: fudge
126 wxBitmap
bmp(height
, height
);
129 dc
.SelectObject(bmp
);
130 wxRendererNative::Get().DrawComboBoxDropButton
134 wxRect(0, 0, height
, height
)
138 wxBitmapButton
*btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
139 btn
->SetMargins(0, 0);
142 m_popup
= new wxPopupWindow(this);
143 m_popup
->SetFont(GetFont());
145 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
146 wxPoint(0, 0), wxDefaultSize
,
148 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
149 wxPoint(0,0), wxDefaultSize
,
150 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
151 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
152 (wxObjectEventFunction
)
154 (wxCalendarEventFunction
)
155 &wxDatePickerCtrlGeneric::OnSelChange
,
157 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
158 (wxObjectEventFunction
)
160 (wxCharEventFunction
)
161 &wxDatePickerCtrlGeneric::OnCalKey
,
163 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
164 (wxObjectEventFunction
)
166 (wxCalendarEventFunction
)
167 &wxDatePickerCtrlGeneric::OnSelChange
,
169 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
170 (wxObjectEventFunction
)
172 (wxCalendarEventFunction
)
173 &wxDatePickerCtrlGeneric::OnSelChange
,
175 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
176 (wxObjectEventFunction
)
178 (wxCalendarEventFunction
)
179 &wxDatePickerCtrlGeneric::OnSelChange
,
181 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
182 (wxObjectEventFunction
)
184 (wxCalendarEventFunction
)
185 &wxDatePickerCtrlGeneric::OnSelChange
,
188 wxWindow
*yearControl
= m_cal
->GetYearControl();
190 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
191 (wxObjectEventFunction
)
193 (wxFocusEventFunction
)
194 &wxDatePickerCtrlGeneric::OnSetFocus
);
196 wxClientDC
dc(yearControl
);
198 wxCoord width
, dummy
;
199 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
200 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
202 wxSize calSize
= m_cal
->GetBestSize();
203 wxSize yearSize
= yearControl
->GetSize();
206 wxPoint yearPosition
= yearControl
->GetPosition();
208 SetFormat(wxT("%x"));
211 m_txt
->SetValue(date
.Format(m_format
));
220 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
221 if (width
< calSize
.x
-4)
224 int calPos
= (width
-calSize
.x
)/2;
230 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
231 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
232 yearSize
.x
, yearSize
.y
);
233 m_cal
->GetMonthControl()->Move(0, 0);
237 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
238 m_popup
->SetClientSize(panel
->GetSize());
245 void wxDatePickerCtrlGeneric::Init()
253 m_ignoreDrop
= false;
257 bool wxDatePickerCtrlGeneric::Destroy()
273 return wxControl::Destroy();
276 // ----------------------------------------------------------------------------
277 // overridden base class methods
278 // ----------------------------------------------------------------------------
280 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
282 wxControl::DoMoveWindow(x
, y
, w
, h
);
283 wxSize bs
=m_btn
->GetBestSize();
284 int eh
=m_txt
->GetBestSize().y
;
286 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
287 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
293 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
295 int bh
=m_btn
->GetBestSize().y
;
296 int eh
=m_txt
->GetBestSize().y
;
297 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
301 bool wxDatePickerCtrlGeneric::Show(bool show
)
303 if ( !wxControl::Show(show
) )
321 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
323 if ( !wxControl::Enable(enable
) )
334 m_btn
->Enable(enable
);
338 // ----------------------------------------------------------------------------
339 // wxDatePickerCtrlGeneric API
340 // ----------------------------------------------------------------------------
343 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
344 const wxDateTime
& upperdate
)
346 return m_cal
->SetDateRange(lowerdate
, upperdate
);
349 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
351 wxString currentText
;
352 wxDateTime currentDate
;
355 currentText
= m_txt
->GetValue();
356 if (!currentText
.empty())
357 currentDate
.ParseFormat(currentText
, m_format
);
360 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
361 wxString str
=dt
.Format(fmt
);
362 wxChar
*p
=(wxChar
*)str
.c_str();
364 m_format
=wxEmptyString
;
369 if (n
== dt
.GetDay())
371 m_format
.Append(wxT("%d"));
374 else if (n
== (int)dt
.GetMonth()+1)
376 m_format
.Append(wxT("%m"));
379 else if (n
== dt
.GetYear())
381 m_format
.Append(wxT("%Y"));
384 else if (n
== (dt
.GetYear() % 100))
386 m_format
.Append(wxT("%y"));
390 m_format
.Append(*p
++);
395 wxStringList valList
;
397 for (c
='0'; c
<= '9'; c
++)
398 valList
.Add(wxString(c
, 1));
399 wxChar
*p
=(wxChar
*)m_format
.c_str();
405 valList
.Add(wxString(*p
++, 1));
407 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
408 tv
.SetIncludeList(valList
);
410 m_txt
->SetValidator(tv
);
412 if (!currentText
.empty())
413 m_txt
->SetValue(currentDate
.Format(m_format
));
419 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
422 wxString txt
=m_txt
->GetValue();
425 dt
.ParseFormat(txt
, m_format
);
431 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
436 m_txt
->SetValue(date
.Format(m_format
));
438 m_txt
->SetValue(wxEmptyString
);
443 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
446 *dt1
= m_cal
->GetLowerDateLimit();
448 *dt2
= m_cal
->GetUpperDateLimit();
454 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
456 m_cal
->SetDateRange(dt1
, dt2
);
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 void wxDatePickerCtrlGeneric::DropDown(bool down
)
470 if (!m_txt
->GetValue().empty())
471 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
476 m_cal
->SetDate(wxDateTime::Today());
478 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
479 m_popup
->Move(pos
.x
, pos
.y
+ GetSize().y
);
493 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
496 m_ignoreDrop
= false;
498 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
509 if (ev
.GetEventObject() == m_btn
)
515 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
519 m_ignoreDrop
= false;
530 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
535 m_txt
->SetSelection(0, 100);
540 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
545 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
547 m_txt
->SetValue(wxEmptyString
);
549 m_txt
->SetValue(dt
.Format(m_format
));
553 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
557 m_txt
->SetValue(m_cal
->GetDate().Format(m_format
));
558 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
564 ev
.SetEventObject(this);
566 GetParent()->ProcessEvent(ev
);
570 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
572 ev
.SetEventObject(this);
574 GetParent()->ProcessEvent(ev
);
576 // We'll create an additional event if the date is valid.
577 // If the date isn't valid, the user's probable in the middle of typing
578 wxString txt
=m_txt
->GetValue();
582 dt
.ParseFormat(txt
, m_format
);
587 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
588 cev
.SetEventObject(this);
592 GetParent()->ProcessEvent(cev
);
596 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
598 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
605 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
607 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
613 #endif // wxUSE_DATEPICKCTRL_GENERIC
615 #endif // wxUSE_DATEPICKCTRL