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"
27 #include "wx/bmpbuttn.h"
28 #include "wx/dialog.h"
29 #include "wx/dcmemory.h"
31 #include "wx/textctrl.h"
32 #include "wx/valtext.h"
35 #include "wx/datectrl.h"
36 #include "wx/calctrl.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
50 #ifndef DEFAULT_ITEM_WIDTH
51 #define DEFAULT_ITEM_WIDTH 100
54 // ============================================================================
55 // wxDatePickerCtrl implementation
56 // ============================================================================
58 BEGIN_EVENT_TABLE(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
59 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrl::OnClick
)
60 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrl::OnText
)
61 EVT_CHILD_FOCUS(wxDatePickerCtrl::OnChildSetFocus
)
64 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 wxDatePickerCtrl::wxDatePickerCtrl(wxWindow
*parent
,
72 const wxDateTime
& date
,
79 Create(parent
, id
, date
, pos
, size
, style
, name
);
83 bool wxDatePickerCtrl::Create(wxWindow
*parent
,
85 const wxDateTime
& date
,
91 wxASSERT_MSG( !(style
& wxDP_SPIN
),
92 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
96 txt
= date
.FormatDate();
98 if ( !wxControl::Create(parent
, id
, pos
, size
,
99 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
100 wxDefaultValidator
, name
) )
106 SetWindowStyle(style
| wxWANTS_CHARS
);
113 dc
.SelectObject(bmp
);
114 dc
.SetBrush(wxBrush(GetBackgroundColour()));
115 dc
.SetPen(wxPen(GetBackgroundColour()));
116 dc
.DrawRectangle(0,0, 8,4);
118 dc
.SetBrush(wxBrush(GetForegroundColour()));
119 dc
.SetPen(wxPen(GetForegroundColour()));
120 wxPoint pt
[3] = { wxPoint(0,0), wxPoint(6,0), wxPoint(3,3) };
121 dc
.DrawPolygon(3, pt
);
122 dc
.SelectObject(wxNullBitmap
);
125 m_txt
=new wxTextCtrl(this, CTRLID_TXT
, txt
);
126 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
127 (wxObjectEventFunction
)&wxDatePickerCtrl::OnEditKey
,
129 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
130 (wxObjectEventFunction
)&wxDatePickerCtrl::OnKillFocus
,
132 SetFormat(wxT("%x"));
134 m_btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
136 m_dlg
= new wxDialog(this, CTRLID_CAL
, wxEmptyString
,
137 wxDefaultPosition
, wxDefaultSize
,
139 m_dlg
->SetFont(GetFont());
141 wxPanel
*panel
=new wxPanel(m_dlg
, CTRLID_PAN
,
142 wxPoint(0, 0), wxDefaultSize
,
144 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
145 wxPoint(0,0), wxDefaultSize
,
146 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
147 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
148 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
150 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
151 (wxObjectEventFunction
)&wxDatePickerCtrl::OnCalKey
,
153 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
154 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
156 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
157 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
159 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
160 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
162 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
163 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSelChange
,
166 wxWindow
*yearControl
= m_cal
->GetYearControl();
168 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
169 (wxObjectEventFunction
)&wxDatePickerCtrl::OnSetFocus
);
171 wxClientDC
dc(yearControl
);
173 wxCoord width
, dummy
;
174 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
175 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
177 wxSize calSize
= m_cal
->GetBestSize();
178 wxSize yearSize
= yearControl
->GetSize();
181 wxPoint yearPosition
= yearControl
->GetPosition();
189 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
190 if (width
< calSize
.x
-4)
193 int calPos
= (width
-calSize
.x
)/2;
199 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
200 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
201 yearSize
.x
, yearSize
.y
);
202 m_cal
->GetMonthControl()->Move(0, 0);
206 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
207 m_dlg
->SetClientSize(panel
->GetSize());
213 void wxDatePickerCtrl::Init()
221 m_ignoreDrop
= false;
225 bool wxDatePickerCtrl::Destroy()
241 return wxControl::Destroy();
244 // ----------------------------------------------------------------------------
245 // overridden base class methods
246 // ----------------------------------------------------------------------------
248 void wxDatePickerCtrl::DoMoveWindow(int x
, int y
, int w
, int h
)
250 wxControl::DoMoveWindow(x
, y
, w
, h
);
251 wxSize bs
=m_btn
->GetBestSize();
252 int eh
=m_txt
->GetBestSize().y
;
254 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
255 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
261 wxSize
wxDatePickerCtrl::DoGetBestSize() const
263 int bh
=m_btn
->GetBestSize().y
;
264 int eh
=m_txt
->GetBestSize().y
;
265 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
269 bool wxDatePickerCtrl::Show(bool show
)
271 if ( !wxControl::Show(show
) )
289 bool wxDatePickerCtrl::Enable(bool enable
)
291 if ( !wxControl::Enable(enable
) )
302 m_btn
->Enable(enable
);
306 // ----------------------------------------------------------------------------
307 // wxDatePickerCtrl API
308 // ----------------------------------------------------------------------------
311 wxDatePickerCtrl::SetDateRange(const wxDateTime
& lowerdate
,
312 const wxDateTime
& upperdate
)
314 return m_cal
->SetDateRange(lowerdate
, upperdate
);
317 bool wxDatePickerCtrl::SetFormat(const wxChar
*fmt
)
320 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
321 wxString str
=dt
.Format(fmt
);
322 wxChar
*p
=(wxChar
*)str
.c_str();
324 m_format
=wxEmptyString
;
329 if (n
== dt
.GetDay())
331 m_format
.Append(wxT("%d"));
334 else if (n
== (int)dt
.GetMonth()+1)
336 m_format
.Append(wxT("%m"));
339 else if (n
== dt
.GetYear())
341 m_format
.Append(wxT("%Y"));
345 m_format
.Append(*p
++);
350 wxStringList valList
;
352 for (c
='0'; c
<= '9'; c
++)
353 valList
.Add(wxString(c
, 1));
354 wxChar
*p
=(wxChar
*)m_format
.c_str();
360 valList
.Add(wxString(*p
++, 1));
362 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
363 tv
.SetIncludeList(valList
);
365 m_txt
->SetValidator(tv
);
371 wxDateTime
wxDatePickerCtrl::GetValue() const
374 wxString txt
=m_txt
->GetValue();
377 dt
.ParseFormat(txt
, m_format
);
383 void wxDatePickerCtrl::SetValue(const wxDateTime
& date
)
388 m_txt
->SetValue(date
.FormatDate());
390 m_txt
->SetValue(wxEmptyString
);
395 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
398 *dt1
= m_cal
->GetLowerDateLimit();
400 *dt2
= m_cal
->GetUpperDateLimit();
405 void wxDatePickerCtrl::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
407 m_cal
->SetDateRange(dt1
, dt2
);
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void wxDatePickerCtrl::DropDown(bool down
)
420 if (m_txt
->GetValue().IsEmpty())
421 m_cal
->SetDate(wxDateTime::Today());
425 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
428 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
430 m_dlg
->Move(pos
.x
, pos
.y
+ GetSize().y
);
444 void wxDatePickerCtrl::OnChildSetFocus(wxChildFocusEvent
&ev
)
447 m_ignoreDrop
= false;
449 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
460 if (ev
.GetEventObject() == m_btn
)
466 void wxDatePickerCtrl::OnClick(wxCommandEvent
& event
)
470 m_ignoreDrop
= false;
481 void wxDatePickerCtrl::OnSetFocus(wxFocusEvent
&ev
)
486 m_txt
->SetSelection(0, 100);
491 void wxDatePickerCtrl::OnKillFocus(wxFocusEvent
&ev
)
496 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
498 m_txt
->SetValue(wxEmptyString
);
500 m_txt
->SetValue(dt
.Format(m_format
));
504 void wxDatePickerCtrl::OnSelChange(wxCalendarEvent
&ev
)
508 m_txt
->SetValue(m_cal
->GetDate().FormatDate());
509 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
515 ev
.SetEventObject(this);
517 GetParent()->ProcessEvent(ev
);
521 void wxDatePickerCtrl::OnText(wxCommandEvent
&ev
)
523 ev
.SetEventObject(this);
525 GetParent()->ProcessEvent(ev
);
527 // We'll create an additional event if the date is valid.
528 // If the date isn't valid, the user's probable in the middle of typing
529 wxString txt
=m_txt
->GetValue();
533 dt
.ParseFormat(txt
, m_format
);
538 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
539 cev
.SetEventObject(this);
543 GetParent()->ProcessEvent(cev
);
547 void wxDatePickerCtrl::OnEditKey(wxKeyEvent
& ev
)
549 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
556 void wxDatePickerCtrl::OnCalKey(wxKeyEvent
& ev
)
558 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())