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 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
29 #define HAS_NATIVE_VERSION
32 // use this version if we're explicitly requested to do it or if it's the only
34 #if wxUSE_DATEPICKCTRL_GENERIC || !defined(HAS_NATIVE_VERSION)
37 #include "wx/bmpbuttn.h"
38 #include "wx/dialog.h"
39 #include "wx/dcmemory.h"
41 #include "wx/textctrl.h"
42 #include "wx/valtext.h"
45 // otherwise it's defined in the native version implementation
46 #ifndef HAS_NATIVE_VERSION
47 #define _WX_DEFINE_DATE_EVENTS_
50 #include "wx/dateevt.h"
52 #include "wx/datectrl.h"
53 #include "wx/calctrl.h"
54 #include "wx/popupwin.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
68 #ifndef DEFAULT_ITEM_WIDTH
69 #define DEFAULT_ITEM_WIDTH 100
72 // ============================================================================
73 // wxDatePickerCtrlGeneric implementation
74 // ============================================================================
76 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
77 EVT_BUTTON(CTRLID_BTN
, wxDatePickerCtrlGeneric::OnClick
)
78 EVT_TEXT(CTRLID_TXT
, wxDatePickerCtrlGeneric::OnText
)
79 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus
)
82 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxDatePickerCtrlBase
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
90 const wxDateTime
& date
,
96 wxASSERT_MSG( !(style
& wxDP_SPIN
),
97 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
99 if ( !wxControl::Create(parent
, id
, pos
, size
,
100 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
,
101 wxDefaultValidator
, name
) )
107 SetWindowStyle(style
| wxWANTS_CHARS
);
114 dc
.SelectObject(bmp
);
115 dc
.SetBrush(wxBrush(GetBackgroundColour()));
116 dc
.SetPen(wxPen(GetBackgroundColour()));
117 dc
.DrawRectangle(0,0, 8,4);
119 dc
.SetBrush(wxBrush(GetForegroundColour()));
120 dc
.SetPen(wxPen(GetForegroundColour()));
121 wxPoint pt
[3] = { wxPoint(0,0), wxPoint(6,0), wxPoint(3,3) };
122 dc
.DrawPolygon(3, pt
);
123 dc
.SelectObject(wxNullBitmap
);
126 m_txt
=new wxTextCtrl(this, CTRLID_TXT
);
127 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
128 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnEditKey
,
130 m_txt
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KILL_FOCUS
,
131 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnKillFocus
,
134 m_btn
= new wxBitmapButton(this, CTRLID_BTN
, bmp
);
136 m_popup
= new wxPopupWindow(this);
137 m_popup
->SetFont(GetFont());
139 wxPanel
*panel
=new wxPanel(m_popup
, CTRLID_PAN
,
140 wxPoint(0, 0), wxDefaultSize
,
142 m_cal
= new wxCalendarCtrl(panel
, CTRLID_CAL
, wxDefaultDateTime
,
143 wxPoint(0,0), wxDefaultSize
,
144 wxCAL_SHOW_HOLIDAYS
| wxSUNKEN_BORDER
);
145 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_SEL_CHANGED
,
146 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
148 m_cal
->Connect(wxID_ANY
, wxID_ANY
, wxEVT_KEY_DOWN
,
149 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnCalKey
,
151 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DOUBLECLICKED
,
152 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
154 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_DAY_CHANGED
,
155 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
157 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_MONTH_CHANGED
,
158 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
160 m_cal
->Connect(CTRLID_CAL
, CTRLID_CAL
, wxEVT_CALENDAR_YEAR_CHANGED
,
161 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSelChange
,
164 wxWindow
*yearControl
= m_cal
->GetYearControl();
166 Connect(wxID_ANY
, wxID_ANY
, wxEVT_SET_FOCUS
,
167 (wxObjectEventFunction
)&wxDatePickerCtrlGeneric::OnSetFocus
);
169 wxClientDC
dc(yearControl
);
171 wxCoord width
, dummy
;
172 dc
.GetTextExtent(wxT("2000"), &width
, &dummy
);
173 width
+= ConvertDialogToPixels(wxSize(20,0)).x
;
175 wxSize calSize
= m_cal
->GetBestSize();
176 wxSize yearSize
= yearControl
->GetSize();
179 wxPoint yearPosition
= yearControl
->GetPosition();
181 SetFormat(wxT("%x"));
184 m_txt
->SetValue(date
.Format(m_format
));
193 width
= yearPosition
.x
+ yearSize
.x
+2+CALBORDER
/2;
194 if (width
< calSize
.x
-4)
197 int calPos
= (width
-calSize
.x
)/2;
203 m_cal
->SetSize(calPos
, 0, calSize
.x
, calSize
.y
);
204 yearControl
->SetSize(width
-yearSize
.x
-CALBORDER
/2, yearPosition
.y
,
205 yearSize
.x
, yearSize
.y
);
206 m_cal
->GetMonthControl()->Move(0, 0);
210 panel
->SetClientSize(width
+CALBORDER
/2, calSize
.y
-2+CALBORDER
);
211 m_popup
->SetClientSize(panel
->GetSize());
218 void wxDatePickerCtrlGeneric::Init()
226 m_ignoreDrop
= false;
230 bool wxDatePickerCtrlGeneric::Destroy()
246 return wxControl::Destroy();
249 // ----------------------------------------------------------------------------
250 // overridden base class methods
251 // ----------------------------------------------------------------------------
253 void wxDatePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int w
, int h
)
255 wxControl::DoMoveWindow(x
, y
, w
, h
);
256 wxSize bs
=m_btn
->GetBestSize();
257 int eh
=m_txt
->GetBestSize().y
;
259 m_txt
->SetSize(0, 0, w
-bs
.x
-1, h
> eh
? eh
: h
);
260 m_btn
->SetSize(w
- bs
.x
, 0, bs
.x
, h
> bs
.y
? bs
.y
: h
);
266 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
268 int bh
=m_btn
->GetBestSize().y
;
269 int eh
=m_txt
->GetBestSize().y
;
270 return wxSize(DEFAULT_ITEM_WIDTH
, bh
> eh
? bh
: eh
);
274 bool wxDatePickerCtrlGeneric::Show(bool show
)
276 if ( !wxControl::Show(show
) )
294 bool wxDatePickerCtrlGeneric::Enable(bool enable
)
296 if ( !wxControl::Enable(enable
) )
307 m_btn
->Enable(enable
);
311 // ----------------------------------------------------------------------------
312 // wxDatePickerCtrlGeneric API
313 // ----------------------------------------------------------------------------
316 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
317 const wxDateTime
& upperdate
)
319 return m_cal
->SetDateRange(lowerdate
, upperdate
);
322 bool wxDatePickerCtrlGeneric::SetFormat(const wxChar
*fmt
)
324 wxString currentText
;
325 wxDateTime currentDate
;
328 currentText
= m_txt
->GetValue();
329 if (!currentText
.IsEmpty())
330 currentDate
.ParseFormat(currentText
, m_format
);
333 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
334 wxString str
=dt
.Format(fmt
);
335 wxChar
*p
=(wxChar
*)str
.c_str();
337 m_format
=wxEmptyString
;
342 if (n
== dt
.GetDay())
344 m_format
.Append(wxT("%d"));
347 else if (n
== (int)dt
.GetMonth()+1)
349 m_format
.Append(wxT("%m"));
352 else if (n
== dt
.GetYear())
354 m_format
.Append(wxT("%Y"));
357 else if (n
== (dt
.GetYear() % 100))
359 m_format
.Append(wxT("%y"));
363 m_format
.Append(*p
++);
368 wxStringList valList
;
370 for (c
='0'; c
<= '9'; c
++)
371 valList
.Add(wxString(c
, 1));
372 wxChar
*p
=(wxChar
*)m_format
.c_str();
378 valList
.Add(wxString(*p
++, 1));
380 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
381 tv
.SetIncludeList(valList
);
383 m_txt
->SetValidator(tv
);
385 if (!currentText
.IsEmpty())
386 m_txt
->SetValue(currentDate
.Format(m_format
));
392 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
395 wxString txt
=m_txt
->GetValue();
398 dt
.ParseFormat(txt
, m_format
);
404 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
409 m_txt
->SetValue(date
.Format(m_format
));
411 m_txt
->SetValue(wxEmptyString
);
416 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
419 *dt1
= m_cal
->GetLowerDateLimit();
421 *dt2
= m_cal
->GetUpperDateLimit();
427 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
429 m_cal
->SetDateRange(dt1
, dt2
);
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 void wxDatePickerCtrlGeneric::DropDown(bool down
)
443 if (!m_txt
->GetValue().IsEmpty())
444 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
449 m_cal
->SetDate(wxDateTime::Today());
451 wxPoint pos
=GetParent()->ClientToScreen(GetPosition());
452 m_popup
->Move(pos
.x
, pos
.y
+ GetSize().y
);
466 void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent
&ev
)
469 m_ignoreDrop
= false;
471 wxWindow
*w
=(wxWindow
*)ev
.GetEventObject();
482 if (ev
.GetEventObject() == m_btn
)
488 void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent
& event
)
492 m_ignoreDrop
= false;
503 void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent
&ev
)
508 m_txt
->SetSelection(0, 100);
513 void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent
&ev
)
518 dt
.ParseFormat(m_txt
->GetValue(), m_format
);
520 m_txt
->SetValue(wxEmptyString
);
522 m_txt
->SetValue(dt
.Format(m_format
));
526 void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent
&ev
)
530 m_txt
->SetValue(m_cal
->GetDate().Format(m_format
));
531 if (ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
537 ev
.SetEventObject(this);
539 GetParent()->ProcessEvent(ev
);
543 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
545 ev
.SetEventObject(this);
547 GetParent()->ProcessEvent(ev
);
549 // We'll create an additional event if the date is valid.
550 // If the date isn't valid, the user's probable in the middle of typing
551 wxString txt
=m_txt
->GetValue();
555 dt
.ParseFormat(txt
, m_format
);
560 wxCalendarEvent
cev(m_cal
, wxEVT_CALENDAR_SEL_CHANGED
);
561 cev
.SetEventObject(this);
565 GetParent()->ProcessEvent(cev
);
569 void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent
& ev
)
571 if (ev
.GetKeyCode() == WXK_DOWN
&& !ev
.HasModifiers())
578 void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent
& ev
)
580 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
586 #endif // wxUSE_DATEPICKCTRL_GENERIC
588 #endif // wxUSE_DATEPICKCTRL