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
,
97 wxASSERT_MSG( !(style
& wxDP_SPIN
),
98 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
100 if ( !wxControl::Create(parent
, id
, pos
, size
,
101 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
102 wxDefaultValidator
, name
) )
110 m_txt
= new wxTextCtrl(this, CTRLID_TXT
);
111 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
112 (wxObjectEventFunction
)
114 (wxCharEventFunction
)
115 &wxDatePickerCtrlGeneric::OnEditKey
,
117 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
118 (wxObjectEventFunction
)
120 (wxFocusEventFunction
)
121 &wxDatePickerCtrlGeneric::OnKillFocus
,
124 const int height
= m_txt
->GetBestSize().y
- 4; // FIXME: fudge
125 wxBitmap
bmp(height
, height
);
128 dc
.SelectObject(bmp
);
129 wxRendererNative::Get().DrawComboBoxDropButton
133 wxRect(0, 0, height
, height
)
137 wxBitmapButton
*btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
138 btn
->SetMargins(0, 0);
141 m_popup
= new wxPopupWindow(this);
142 m_popup
->SetFont(GetFont());
144 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
145 wxPoint(0, 0), wxDefaultSize
,
147 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
148 wxPoint(0,0), wxDefaultSize
,
149 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
150 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
151 (wxObjectEventFunction
)
153 (wxCalendarEventFunction
)
154 &wxDatePickerCtrlGeneric::OnSelChange
,
156 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
157 (wxObjectEventFunction
)
159 (wxCharEventFunction
)
160 &wxDatePickerCtrlGeneric::OnCalKey
,
162 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
163 (wxObjectEventFunction
)
165 (wxCalendarEventFunction
)
166 &wxDatePickerCtrlGeneric::OnSelChange
,
168 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
169 (wxObjectEventFunction
)
171 (wxCalendarEventFunction
)
172 &wxDatePickerCtrlGeneric::OnSelChange
,
174 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
175 (wxObjectEventFunction
)
177 (wxCalendarEventFunction
)
178 &wxDatePickerCtrlGeneric::OnSelChange
,
180 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
181 (wxObjectEventFunction
)
183 (wxCalendarEventFunction
)
184 &wxDatePickerCtrlGeneric::OnSelChange
,
187 wxWindow
*yearControl
= m_cal
->GetYearControl();
189 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
190 (wxObjectEventFunction
)
192 (wxFocusEventFunction
)
193 &wxDatePickerCtrlGeneric::OnSetFocus
);
195 wxClientDC
dc(yearControl
);
197 wxCoord width
, dummy
;
198 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
199 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
201 wxSize calSize
= m_cal
->GetBestSize();
202 wxSize yearSize
= yearControl
->GetSize();
205 wxPoint yearPosition
= yearControl
->GetPosition();
207 SetFormat(wxT("%x"));
210 m_txt
->SetValue(date
.Format(m_format
));
219 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
220 if (width
< calSize
.x
-4)
223 int calPos
= (width
-calSize
.x
)/2;
229 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
230 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
231 yearSize
.x
, yearSize
.y
);
232 m_cal
->GetMonthControl()->Move(0, 0);
236 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
237 m_popup
->SetClientSize(panel
->GetSize());
244 void wxDatePickerCtrlGeneric::Init()
252 m_ignoreDrop
= false;
256 bool wxDatePickerCtrlGeneric::Destroy()
272 return wxControl::Destroy();
275 // ----------------------------------------------------------------------------
276 // overridden base class methods
277 // ----------------------------------------------------------------------------
279 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
281 wxControl::DoMoveWindow(x
, y
, w
, h
);
282 wxSize bs
=m_btn
->GetBestSize();
283 int eh
=m_txt
->GetBestSize().y
;
285 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
286 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
292 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
294 int bh
=m_btn
->GetBestSize().y
;
295 int eh
=m_txt
->GetBestSize().y
;
296 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
300 bool wxDatePickerCtrlGeneric::Show(bool show
)
302 if ( !wxControl::Show(show
) )
320 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
322 if ( !wxControl::Enable(enable
) )
333 m_btn
->Enable(enable
);
337 // ----------------------------------------------------------------------------
338 // wxDatePickerCtrlGeneric API
339 // ----------------------------------------------------------------------------
342 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
343 const wxDateTime
& upperdate
)
345 return m_cal
->SetDateRange(lowerdate
, upperdate
);
348 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
350 wxString currentText
;
351 wxDateTime currentDate
;
354 currentText
= m_txt
->GetValue();
355 if (!currentText
.empty())
356 currentDate
.ParseFormat(currentText
, m_format
);
359 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
360 wxString str
=dt
.Format(fmt
);
361 wxChar
*p
=(wxChar
*)str
.c_str();
363 m_format
=wxEmptyString
;
368 if (n
== dt
.GetDay())
370 m_format
.Append(wxT("%d"));
373 else if (n
== (int)dt
.GetMonth()+1)
375 m_format
.Append(wxT("%m"));
378 else if (n
== dt
.GetYear())
380 m_format
.Append(wxT("%Y"));
383 else if (n
== (dt
.GetYear() % 100))
385 m_format
.Append(wxT("%y"));
389 m_format
.Append(*p
++);
394 wxStringList valList
;
396 for (c
='0'; c
<= '9'; c
++)
397 valList
.Add(wxString(c
, 1));
398 wxChar
*p
=(wxChar
*)m_format
.c_str();
404 valList
.Add(wxString(*p
++, 1));
406 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
407 tv
.SetIncludeList(valList
);
409 m_txt
->SetValidator(tv
);
411 if (!currentText
.empty())
412 m_txt
->SetValue(currentDate
.Format(m_format
));
418 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
421 wxString txt
=m_txt
->GetValue();
424 dt
.ParseFormat(txt
, m_format
);
430 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
435 m_txt
->SetValue(date
.Format(m_format
));
437 m_txt
->SetValue(wxEmptyString
);
442 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
445 *dt1
= m_cal
->GetLowerDateLimit();
447 *dt2
= m_cal
->GetUpperDateLimit();
453 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
455 m_cal
->SetDateRange(dt1
, dt2
);
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 void wxDatePickerCtrlGeneric::DropDown(bool down
)
469 if (!m_txt
->GetValue().empty())
470 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
475 m_cal
->SetDate(wxDateTime::Today());
477 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
478 m_popup
->Move(pos
.x
, pos
.y
+ GetSize().y
);
492 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
495 m_ignoreDrop
= false;
497 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
508 if (ev
.GetEventObject() == m_btn
)
514 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
518 m_ignoreDrop
= false;
529 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
534 m_txt
->SetSelection(0, 100);
539 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
544 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
546 m_txt
->SetValue(wxEmptyString
);
548 m_txt
->SetValue(dt
.Format(m_format
));
552 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
556 m_txt
->SetValue(m_cal
->GetDate().Format(m_format
));
557 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
563 ev
.SetEventObject(this);
565 GetParent()->ProcessEvent(ev
);
569 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
571 ev
.SetEventObject(this);
573 GetParent()->ProcessEvent(ev
);
575 // We'll create an additional event if the date is valid.
576 // If the date isn't valid, the user's probable in the middle of typing
577 wxString txt
=m_txt
->GetValue();
581 dt
.ParseFormat(txt
, m_format
);
586 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
587 cev
.SetEventObject(this);
591 GetParent()->ProcessEvent(cev
);
595 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
597 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
604 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
606 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
612 #endif // wxUSE_DATEPICKCTRL_GENERIC
614 #endif // wxUSE_DATEPICKCTRL