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
)&wxDatePickerCtrlGeneric::OnEditKey
,
114 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
115 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnKillFocus
,
118 const int height
= m_txt
->GetBestSize().y
- 4; // FIXME: fudge
119 wxBitmap
bmp(height
, height
);
122 dc
.SelectObject(bmp
);
123 wxRendererNative::Get().DrawComboBoxDropButton
127 wxRect(0, 0, height
, height
)
131 wxBitmapButton
*btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
132 btn
->SetMargins(0, 0);
135 m_popup
= new wxPopupWindow(this);
136 m_popup
->SetFont(GetFont());
138 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
139 wxPoint(0, 0), wxDefaultSize
,
141 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
142 wxPoint(0,0), wxDefaultSize
,
143 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
144 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
145 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
147 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
148 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnCalKey
,
150 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
151 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
153 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
154 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
156 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
157 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
159 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
160 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
163 wxWindow
*yearControl
= m_cal
->GetYearControl();
165 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
166 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSetFocus
);
168 wxClientDC
dc(yearControl
);
170 wxCoord width
, dummy
;
171 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
172 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
174 wxSize calSize
= m_cal
->GetBestSize();
175 wxSize yearSize
= yearControl
->GetSize();
178 wxPoint yearPosition
= yearControl
->GetPosition();
180 SetFormat(wxT("%x"));
183 m_txt
->SetValue(date
.Format(m_format
));
192 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
193 if (width
< calSize
.x
-4)
196 int calPos
= (width
-calSize
.x
)/2;
202 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
203 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
204 yearSize
.x
, yearSize
.y
);
205 m_cal
->GetMonthControl()->Move(0, 0);
209 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
210 m_popup
->SetClientSize(panel
->GetSize());
217 void wxDatePickerCtrlGeneric::Init()
225 m_ignoreDrop
= false;
229 bool wxDatePickerCtrlGeneric::Destroy()
245 return wxControl::Destroy();
248 // ----------------------------------------------------------------------------
249 // overridden base class methods
250 // ----------------------------------------------------------------------------
252 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
254 wxControl::DoMoveWindow(x
, y
, w
, h
);
255 wxSize bs
=m_btn
->GetBestSize();
256 int eh
=m_txt
->GetBestSize().y
;
258 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
259 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
265 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
267 int bh
=m_btn
->GetBestSize().y
;
268 int eh
=m_txt
->GetBestSize().y
;
269 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
273 bool wxDatePickerCtrlGeneric::Show(bool show
)
275 if ( !wxControl::Show(show
) )
293 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
295 if ( !wxControl::Enable(enable
) )
306 m_btn
->Enable(enable
);
310 // ----------------------------------------------------------------------------
311 // wxDatePickerCtrlGeneric API
312 // ----------------------------------------------------------------------------
315 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
316 const wxDateTime
& upperdate
)
318 return m_cal
->SetDateRange(lowerdate
, upperdate
);
321 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
323 wxString currentText
;
324 wxDateTime currentDate
;
327 currentText
= m_txt
->GetValue();
328 if (!currentText
.IsEmpty())
329 currentDate
.ParseFormat(currentText
, m_format
);
332 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
333 wxString str
=dt
.Format(fmt
);
334 wxChar
*p
=(wxChar
*)str
.c_str();
336 m_format
=wxEmptyString
;
341 if (n
== dt
.GetDay())
343 m_format
.Append(wxT("%d"));
346 else if (n
== (int)dt
.GetMonth()+1)
348 m_format
.Append(wxT("%m"));
351 else if (n
== dt
.GetYear())
353 m_format
.Append(wxT("%Y"));
356 else if (n
== (dt
.GetYear() % 100))
358 m_format
.Append(wxT("%y"));
362 m_format
.Append(*p
++);
367 wxStringList valList
;
369 for (c
='0'; c
<= '9'; c
++)
370 valList
.Add(wxString(c
, 1));
371 wxChar
*p
=(wxChar
*)m_format
.c_str();
377 valList
.Add(wxString(*p
++, 1));
379 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
380 tv
.SetIncludeList(valList
);
382 m_txt
->SetValidator(tv
);
384 if (!currentText
.IsEmpty())
385 m_txt
->SetValue(currentDate
.Format(m_format
));
391 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
394 wxString txt
=m_txt
->GetValue();
397 dt
.ParseFormat(txt
, m_format
);
403 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
408 m_txt
->SetValue(date
.Format(m_format
));
410 m_txt
->SetValue(wxEmptyString
);
415 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
418 *dt1
= m_cal
->GetLowerDateLimit();
420 *dt2
= m_cal
->GetUpperDateLimit();
426 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
428 m_cal
->SetDateRange(dt1
, dt2
);
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 void wxDatePickerCtrlGeneric::DropDown(bool down
)
442 if (!m_txt
->GetValue().IsEmpty())
443 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
448 m_cal
->SetDate(wxDateTime::Today());
450 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
451 m_popup
->Move(pos
.x
, pos
.y
+ GetSize().y
);
465 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
468 m_ignoreDrop
= false;
470 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
481 if (ev
.GetEventObject() == m_btn
)
487 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& WXUNUSED(event
))
491 m_ignoreDrop
= false;
502 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
& WXUNUSED(ev
))
507 m_txt
->SetSelection(0, 100);
512 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
517 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
519 m_txt
->SetValue(wxEmptyString
);
521 m_txt
->SetValue(dt
.Format(m_format
));
525 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
529 m_txt
->SetValue(m_cal
->GetDate().Format(m_format
));
530 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
536 ev
.SetEventObject(this);
538 GetParent()->ProcessEvent(ev
);
542 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
544 ev
.SetEventObject(this);
546 GetParent()->ProcessEvent(ev
);
548 // We'll create an additional event if the date is valid.
549 // If the date isn't valid, the user's probable in the middle of typing
550 wxString txt
=m_txt
->GetValue();
554 dt
.ParseFormat(txt
, m_format
);
559 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
560 cev
.SetEventObject(this);
564 GetParent()->ProcessEvent(cev
);
568 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
570 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
577 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
579 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
585 #endif // wxUSE_DATEPICKCTRL_GENERIC
587 #endif // wxUSE_DATEPICKCTRL