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"
50 #include "wx/calctrl.h"
51 #include "wx/popupwin.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
65 #ifndef DEFAULT_ITEM_WIDTH
66 #define DEFAULT_ITEM_WIDTH 100
69 // ============================================================================
70 // wxDatePickerCtrlGeneric implementation
71 // ============================================================================
73 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
74 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrlGeneric::OnClick
)
75 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrlGeneric::OnText
)
76 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus
)
79 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
87 const wxDateTime
& date
,
93 wxASSERT_MSG( !(style
& wxDP_SPIN
),
94 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
96 if ( !wxControl::Create(parent
, id
, pos
, size
,
97 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
98 wxDefaultValidator
, name
) )
104 SetWindowStyle(style
| wxWANTS_CHARS
);
111 dc
.SelectObject(bmp
);
112 dc
.SetBrush(wxBrush(GetBackgroundColour()));
113 dc
.SetPen(wxPen(GetBackgroundColour()));
114 dc
.DrawRectangle(0,0, 8,4);
116 dc
.SetBrush(wxBrush(GetForegroundColour()));
117 dc
.SetPen(wxPen(GetForegroundColour()));
118 wxPoint pt
[3] = { wxPoint(0,0), wxPoint(6,0), wxPoint(3,3) };
119 dc
.DrawPolygon(3, pt
);
120 dc
.SelectObject(wxNullBitmap
);
123 m_txt
=new wxTextCtrl(this, CTRLID_TXT
);
124 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
125 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnEditKey
,
127 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
128 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnKillFocus
,
131 m_btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
133 m_popup
= new wxPopupWindow(this);
134 m_popup
->SetFont(GetFont());
136 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
137 wxPoint(0, 0), wxDefaultSize
,
139 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
140 wxPoint(0,0), wxDefaultSize
,
141 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
142 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
143 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
145 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
146 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnCalKey
,
148 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
149 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
151 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
152 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
154 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
155 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
157 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
158 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
161 wxWindow
*yearControl
= m_cal
->GetYearControl();
163 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
164 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSetFocus
);
166 wxClientDC
dc(yearControl
);
168 wxCoord width
, dummy
;
169 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
170 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
172 wxSize calSize
= m_cal
->GetBestSize();
173 wxSize yearSize
= yearControl
->GetSize();
176 wxPoint yearPosition
= yearControl
->GetPosition();
178 SetFormat(wxT("%x"));
181 m_txt
->SetValue(date
.Format(m_format
));
190 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
191 if (width
< calSize
.x
-4)
194 int calPos
= (width
-calSize
.x
)/2;
200 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
201 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
202 yearSize
.x
, yearSize
.y
);
203 m_cal
->GetMonthControl()->Move(0, 0);
207 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
208 m_popup
->SetClientSize(panel
->GetSize());
215 void wxDatePickerCtrlGeneric::Init()
223 m_ignoreDrop
= false;
227 bool wxDatePickerCtrlGeneric::Destroy()
243 return wxControl::Destroy();
246 // ----------------------------------------------------------------------------
247 // overridden base class methods
248 // ----------------------------------------------------------------------------
250 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
252 wxControl::DoMoveWindow(x
, y
, w
, h
);
253 wxSize bs
=m_btn
->GetBestSize();
254 int eh
=m_txt
->GetBestSize().y
;
256 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
257 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
263 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
265 int bh
=m_btn
->GetBestSize().y
;
266 int eh
=m_txt
->GetBestSize().y
;
267 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
271 bool wxDatePickerCtrlGeneric::Show(bool show
)
273 if ( !wxControl::Show(show
) )
291 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
293 if ( !wxControl::Enable(enable
) )
304 m_btn
->Enable(enable
);
308 // ----------------------------------------------------------------------------
309 // wxDatePickerCtrlGeneric API
310 // ----------------------------------------------------------------------------
313 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
314 const wxDateTime
& upperdate
)
316 return m_cal
->SetDateRange(lowerdate
, upperdate
);
319 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
321 wxString currentText
;
322 wxDateTime currentDate
;
325 currentText
= m_txt
->GetValue();
326 if (!currentText
.IsEmpty())
327 currentDate
.ParseFormat(currentText
, m_format
);
330 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
331 wxString str
=dt
.Format(fmt
);
332 wxChar
*p
=(wxChar
*)str
.c_str();
334 m_format
=wxEmptyString
;
339 if (n
== dt
.GetDay())
341 m_format
.Append(wxT("%d"));
344 else if (n
== (int)dt
.GetMonth()+1)
346 m_format
.Append(wxT("%m"));
349 else if (n
== dt
.GetYear())
351 m_format
.Append(wxT("%Y"));
354 else if (n
== (dt
.GetYear() % 100))
356 m_format
.Append(wxT("%y"));
360 m_format
.Append(*p
++);
365 wxStringList valList
;
367 for (c
='0'; c
<= '9'; c
++)
368 valList
.Add(wxString(c
, 1));
369 wxChar
*p
=(wxChar
*)m_format
.c_str();
375 valList
.Add(wxString(*p
++, 1));
377 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
378 tv
.SetIncludeList(valList
);
380 m_txt
->SetValidator(tv
);
382 if (!currentText
.IsEmpty())
383 m_txt
->SetValue(currentDate
.Format(m_format
));
389 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
392 wxString txt
=m_txt
->GetValue();
395 dt
.ParseFormat(txt
, m_format
);
401 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
406 m_txt
->SetValue(date
.Format(m_format
));
408 m_txt
->SetValue(wxEmptyString
);
413 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
416 *dt1
= m_cal
->GetLowerDateLimit();
418 *dt2
= m_cal
->GetUpperDateLimit();
424 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
426 m_cal
->SetDateRange(dt1
, dt2
);
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 void wxDatePickerCtrlGeneric::DropDown(bool down
)
440 if (!m_txt
->GetValue().IsEmpty())
441 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
446 m_cal
->SetDate(wxDateTime::Today());
448 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
449 m_popup
->Move(pos
.x
, pos
.y
+ GetSize().y
);
463 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
466 m_ignoreDrop
= false;
468 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
479 if (ev
.GetEventObject() == m_btn
)
485 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& event
)
489 m_ignoreDrop
= false;
500 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
&ev
)
505 m_txt
->SetSelection(0, 100);
510 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
515 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
517 m_txt
->SetValue(wxEmptyString
);
519 m_txt
->SetValue(dt
.Format(m_format
));
523 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
527 m_txt
->SetValue(m_cal
->GetDate().Format(m_format
));
528 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
534 ev
.SetEventObject(this);
536 GetParent()->ProcessEvent(ev
);
540 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
542 ev
.SetEventObject(this);
544 GetParent()->ProcessEvent(ev
);
546 // We'll create an additional event if the date is valid.
547 // If the date isn't valid, the user's probable in the middle of typing
548 wxString txt
=m_txt
->GetValue();
552 dt
.ParseFormat(txt
, m_format
);
557 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
558 cev
.SetEventObject(this);
562 GetParent()->ProcessEvent(cev
);
566 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
568 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
575 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
577 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
583 #endif // wxUSE_DATEPICKCTRL_GENERIC
585 #endif // wxUSE_DATEPICKCTRL