1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/datectrl.cpp
3 // Purpose: generic wxDatePickerCtrl 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 #include "wx/datectrl.h"
27 #include "wx/calctrl.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
41 #ifndef DEFAULT_ITEM_WIDTH
42 #define DEFAULT_ITEM_WIDTH 100
45 // ============================================================================
46 // wxDatePickerCtrl implementation
47 // ============================================================================
49 BEGIN_EVENT_TABLE(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
50 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrl::OnClick
)
51 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrl::OnText
)
52 EVT_CHILD_FOCUS(wxDatePickerCtrl::OnChildSetFocus
)
55 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 wxDatePickerCtrl::wxDatePickerCtrl(wxWindow
*parent
,
63 const wxDateTime
& date
,
70 Create(parent
, id
, date
, pos
, size
, style
, name
);
74 bool wxDatePickerCtrl::Create(wxWindow
*parent
,
76 const wxDateTime
& date
,
82 wxASSERT_MSG( !(style
& wxDP_SPIN
),
83 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
87 txt
= date
.FormatDate();
89 if ( !wxControl::Create(parent
, id
, pos
, size
,
90 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
91 wxDefaultValidator
, name
) )
97 SetWindowStyle(style
| wxWANTS_CHARS
);
104 dc
.SelectObject(bmp
);
105 dc
.SetBrush(wxBrush(GetBackgroundColour()));
106 dc
.SetPen(wxPen(GetBackgroundColour()));
107 dc
.DrawRectangle(0,0, 8,4);
109 dc
.SetBrush(wxBrush(GetForegroundColour()));
110 dc
.SetPen(wxPen(GetForegroundColour()));
111 wxPoint pt
[3] = { wxPoint(0,0), wxPoint(6,0), wxPoint(3,3) };
112 dc
.DrawPolygon(3, pt
);
113 dc
.SelectObject(wxNullBitmap
);
116 m_txt
=new wxTextCtrl(this, CTRLID_TXT
, txt
);
117 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
118 (wxObjectEventFunction
)&wxDatePickerCtrl::OnEditKey
,
120 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
121 (wxObjectEventFunction
)&wxDatePickerCtrl::OnKillFocus
,
123 SetFormat(wxT("%x"));
125 m_btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
127 m_dlg
= new wxDialog(this, CTRLID_CAL
, wxEmptyString
,
128 wxDefaultPosition
, wxDefaultSize
,
130 m_dlg
->SetFont(GetFont());
132 wxPanel
*panel
=new wxPanel(m_dlg
, CTRLID_PAN
,
133 wxPoint(0, 0), wxDefaultSize
,
135 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
136 wxPoint(0,0), wxDefaultSize
,
137 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
138 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
139 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
141 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
142 (wxObjectEventFunction
)&wxDatePickerCtrl::OnCalKey
,
144 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
145 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
147 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
148 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
150 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
151 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
153 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
154 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
157 wxWindow
*yearControl
= m_cal
->GetYearControl();
159 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
160 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSetFocus
);
162 wxClientDC
dc(yearControl
);
164 wxCoord width
, dummy
;
165 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
166 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
168 wxSize calSize
= m_cal
->GetBestSize();
169 wxSize yearSize
= yearControl
->GetSize();
172 wxPoint yearPosition
= yearControl
->GetPosition();
180 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
181 if (width
< calSize
.x
-4)
184 int calPos
= (width
-calSize
.x
)/2;
190 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
191 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
192 yearSize
.x
, yearSize
.y
);
193 m_cal
->GetMonthControl()->Move(0, 0);
197 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
198 m_dlg
->SetClientSize(panel
->GetSize());
204 void wxDatePickerCtrl::Init()
212 m_ignoreDrop
= false;
216 bool wxDatePickerCtrl::Destroy()
232 return wxControl::Destroy();
235 // ----------------------------------------------------------------------------
236 // overridden base class methods
237 // ----------------------------------------------------------------------------
239 void wxDatePickerCtrl::DoMoveWindow(int x
, int y
, int w
, int h
)
241 wxControl::DoMoveWindow(x
, y
, w
, h
);
242 wxSize bs
=m_btn
->GetBestSize();
243 int eh
=m_txt
->GetBestSize().y
;
245 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
246 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
252 wxSize
wxDatePickerCtrl::DoGetBestSize() const
254 int bh
=m_btn
->GetBestSize().y
;
255 int eh
=m_txt
->GetBestSize().y
;
256 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
260 bool wxDatePickerCtrl::Show(bool show
)
262 if ( !wxControl::Show(show
) )
280 bool wxDatePickerCtrl::Enable(bool enable
)
282 if ( !wxControl::Enable(enable
) )
293 m_btn
->Enable(enable
);
297 // ----------------------------------------------------------------------------
298 // wxDatePickerCtrl API
299 // ----------------------------------------------------------------------------
302 wxDatePickerCtrl::SetDateRange(const wxDateTime
& lowerdate
,
303 const wxDateTime
& upperdate
)
305 return m_cal
->SetDateRange(lowerdate
, upperdate
);
308 bool wxDatePickerCtrl::SetFormat(const wxChar
*fmt
)
311 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
312 wxString str
=dt
.Format(fmt
);
313 wxChar
*p
=(wxChar
*)str
.c_str();
315 m_format
=wxEmptyString
;
320 if (n
== dt
.GetDay())
322 m_format
.Append(wxT("%d"));
325 else if (n
== (int)dt
.GetMonth()+1)
327 m_format
.Append(wxT("%m"));
330 else if (n
== dt
.GetYear())
332 m_format
.Append(wxT("%Y"));
336 m_format
.Append(*p
++);
341 wxStringList valList
;
343 for (c
='0'; c
<= '9'; c
++)
344 valList
.Add(wxString(c
, 1));
345 wxChar
*p
=(wxChar
*)m_format
.c_str();
351 valList
.Add(wxString(*p
++, 1));
353 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
354 tv
.SetIncludeList(valList
);
356 m_txt
->SetValidator(tv
);
362 wxDateTime
wxDatePickerCtrl::GetValue() const
365 wxString txt
=m_txt
->GetValue();
368 dt
.ParseFormat(txt
, m_format
);
374 void wxDatePickerCtrl::SetValue(const wxDateTime
& date
)
379 m_txt
->SetValue(date
.FormatDate());
381 m_txt
->SetValue(wxEmptyString
);
386 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
389 *dt1
= m_cal
->GetLowerDateLimit();
391 *dt2
= m_cal
->GetUpperDateLimit();
396 void wxDatePickerCtrl::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
398 m_cal
->SetDateRange(dt1
, dt2
);
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 void wxDatePickerCtrl::DropDown(bool down
)
411 if (m_txt
->GetValue().IsEmpty())
412 m_cal
->SetDate(wxDateTime::Today());
416 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
419 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
421 m_dlg
->Move(pos
.x
, pos
.y
+ GetSize().y
);
435 void wxDatePickerCtrl::OnChildSetFocus(wxChildFocusEvent
&ev
)
438 m_ignoreDrop
= false;
440 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
451 if (ev
.GetEventObject() == m_btn
)
457 void wxDatePickerCtrl::OnClick(wxCommandEvent
& event
)
461 m_ignoreDrop
= false;
472 void wxDatePickerCtrl::OnSetFocus(wxFocusEvent
&ev
)
477 m_txt
->SetSelection(0, 100);
482 void wxDatePickerCtrl::OnKillFocus(wxFocusEvent
&ev
)
487 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
489 m_txt
->SetValue(wxEmptyString
);
491 m_txt
->SetValue(dt
.Format(m_format
));
495 void wxDatePickerCtrl::OnSelChange(wxCalendarEvent
&ev
)
499 m_txt
->SetValue(m_cal
->GetDate().FormatDate());
500 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
506 ev
.SetEventObject(this);
508 GetParent()->ProcessEvent(ev
);
512 void wxDatePickerCtrl::OnText(wxCommandEvent
&ev
)
514 ev
.SetEventObject(this);
516 GetParent()->ProcessEvent(ev
);
518 // We'll create an additional event if the date is valid.
519 // If the date isn't valid, the user's probable in the middle of typing
520 wxString txt
=m_txt
->GetValue();
524 dt
.ParseFormat(txt
, m_format
);
529 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
530 cev
.SetEventObject(this);
534 GetParent()->ProcessEvent(cev
);
538 void wxDatePickerCtrl::OnEditKey(wxKeyEvent
& ev
)
540 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
547 void wxDatePickerCtrl::OnCalKey(wxKeyEvent
& ev
)
549 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())