]>
Commit | Line | Data |
---|---|---|
39df3acd | 1 | ///////////////////////////////////////////////////////////////////////////// |
d5ae99f5 | 2 | // Name: generic/datectlg.cpp |
7ae712f5 | 3 | // Purpose: generic wxDatePickerCtrlGeneric implementation |
39df3acd VZ |
4 | // Author: Andreas Pflug |
5 | // Modified by: | |
6 | // Created: 2005-01-19 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Andreas Pflug <pgadmin@pse-consulting.de> | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
7ae712f5 VZ |
26 | #if wxUSE_DATEPICKCTRL |
27 | ||
15c86b39 | 28 | #include "wx/datectrl.h" |
7ae712f5 VZ |
29 | |
30 | // use this version if we're explicitly requested to do it or if it's the only | |
31 | // one we have | |
15c86b39 | 32 | #if wxUSE_DATEPICKCTRL_GENERIC || !defined(wxHAS_NATIVE_DATEPICKCTRL) |
7ae712f5 | 33 | |
a962d4e0 KH |
34 | #ifndef WX_PRECOMP |
35 | #include "wx/bmpbuttn.h" | |
36 | #include "wx/dialog.h" | |
37 | #include "wx/dcmemory.h" | |
38 | #include "wx/panel.h" | |
39 | #include "wx/textctrl.h" | |
40 | #include "wx/valtext.h" | |
41 | #endif | |
42 | ||
1721a8c0 VZ |
43 | #ifdef wxHAS_NATIVE_DATEPICKCTRL |
44 | // this header is not included from wx/datectrl.h if we have a native | |
45 | // version, but we do need it here | |
46 | #include "wx/generic/datectrl.h" | |
930fb29e VZ |
47 | #else |
48 | // we need to define _WX_DEFINE_DATE_EVENTS_ before including wx/dateevt.h to | |
49 | // define the event types we use if we're the only date picker control version | |
50 | // being compiled -- otherwise it's defined in the native version implementation | |
7ae712f5 VZ |
51 | #define _WX_DEFINE_DATE_EVENTS_ |
52 | #endif | |
53 | ||
91edf16c VZ |
54 | #include "wx/dateevt.h" |
55 | ||
4b134bb2 | 56 | #include "wx/calctrl.h" |
4221a0e5 | 57 | #include "wx/renderer.h" |
39df3acd VZ |
58 | |
59 | // ---------------------------------------------------------------------------- | |
60 | // constants | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | enum | |
64 | { | |
65 | CTRLID_TXT = 101, | |
4b134bb2 VZ |
66 | CTRLID_CAL, |
67 | CTRLID_BTN, | |
39df3acd VZ |
68 | CTRLID_PAN |
69 | }; | |
70 | ||
71 | #ifndef DEFAULT_ITEM_WIDTH | |
72 | #define DEFAULT_ITEM_WIDTH 100 | |
73 | #endif | |
74 | ||
930fb29e VZ |
75 | #ifdef __WXMSW__ |
76 | #undef wxUSE_POPUPWIN | |
77 | #define wxUSE_POPUPWIN 0 // Popup not working | |
78 | #define TXTCTRL_FLAGS wxNO_BORDER | |
79 | #define BTN_FLAGS wxNO_BORDER | |
80 | #define CALBORDER 0 | |
81 | #define RIGHTBUTTONBORDER 3 | |
82 | #define TOPBUTTONBORDER 0 | |
83 | #define BUTTONBORDER 3 | |
84 | #define TXTPOSY 1 | |
85 | #else | |
86 | #define TXTCTRL_FLAGS 0 | |
87 | #define BTN_FLAGS wxBU_AUTODRAW | |
88 | #define CALBORDER 4 | |
89 | #define RIGHTBUTTONBORDER 0 | |
90 | #define TOPBUTTONBORDER 0 | |
91 | #define BUTTONBORDER 0 | |
92 | #define TXTPOSY 0 | |
93 | #endif | |
94 | ||
95 | ||
1721a8c0 VZ |
96 | // ---------------------------------------------------------------------------- |
97 | // local classes | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
38511687 VZ |
100 | |
101 | class wxDropdownButton : public wxBitmapButton | |
102 | { | |
103 | public: | |
104 | wxDropdownButton() { Init(); } | |
105 | wxDropdownButton(wxWindow *parent, | |
106 | wxWindowID id, | |
107 | const wxPoint& pos = wxDefaultPosition, | |
108 | const wxSize& size = wxDefaultSize, | |
109 | long style=0, | |
110 | const wxValidator& validator = wxDefaultValidator); | |
111 | ||
112 | void Init() | |
113 | { | |
114 | m_borderX = -1; | |
115 | m_borderY = -1; | |
116 | } | |
94ab4d92 | 117 | bool Create(wxWindow *parent, |
38511687 VZ |
118 | wxWindowID id, |
119 | const wxPoint& pos = wxDefaultPosition, | |
120 | const wxSize& size = wxDefaultSize, | |
930fb29e | 121 | long style = 0, |
38511687 VZ |
122 | const wxValidator& validator = wxDefaultValidator); |
123 | ||
124 | void DoMoveWindow(int x, int y, int w, int h); | |
125 | ||
126 | protected: | |
127 | int m_borderX, m_borderY; | |
128 | }; | |
129 | ||
130 | ||
131 | wxDropdownButton::wxDropdownButton(wxWindow *parent, | |
132 | wxWindowID id, | |
133 | const wxPoint& pos, | |
134 | const wxSize& size, | |
135 | long style, | |
136 | const wxValidator& validator) | |
137 | { | |
138 | Init(); | |
139 | Create(parent, id, pos, size, style, validator); | |
140 | } | |
141 | ||
142 | ||
94ab4d92 | 143 | bool wxDropdownButton::Create(wxWindow *parent, |
38511687 VZ |
144 | wxWindowID id, |
145 | const wxPoint& pos, | |
146 | const wxSize& size, | |
94ab4d92 | 147 | long WXUNUSED(style), |
38511687 VZ |
148 | const wxValidator& validator) |
149 | { | |
930fb29e VZ |
150 | m_marginX = 0; |
151 | m_marginY = 0; | |
152 | ||
38511687 | 153 | wxBitmap chkBmp(15,15); // arbitrary |
94ab4d92 | 154 | if ( !wxBitmapButton::Create(parent, id, chkBmp, |
930fb29e | 155 | pos, wxDefaultSize, BTN_FLAGS, validator) ) |
94ab4d92 | 156 | return false; |
38511687 | 157 | |
94ab4d92 VZ |
158 | const wxSize sz = GetSize(); |
159 | int w = chkBmp.GetWidth(), | |
160 | h = chkBmp.GetHeight(); | |
161 | m_borderX = sz.x - m_marginX - w; | |
162 | m_borderY = sz.y - m_marginY - h; | |
38511687 | 163 | |
94ab4d92 VZ |
164 | w = size.x > 0 ? size.x : sz.x; |
165 | h = size.y > 0 ? size.y : sz.y; | |
38511687 VZ |
166 | |
167 | DoMoveWindow(pos.x, pos.y, w, h); | |
94ab4d92 VZ |
168 | |
169 | return true; | |
38511687 VZ |
170 | } |
171 | ||
172 | ||
173 | void wxDropdownButton::DoMoveWindow(int x, int y, int w, int h) | |
174 | { | |
175 | if (m_borderX >= 0 && m_borderY >= 0 && (w >= 0 || h >= 0)) | |
176 | { | |
177 | wxMemoryDC dc; | |
178 | if (w < 0) | |
179 | w = GetSize().x; | |
180 | #ifdef __WXGTK__ | |
181 | else | |
182 | w = m_marginX + m_borderX + 15; // GTK magic size | |
183 | #endif | |
184 | if (h < 0) | |
185 | h = GetSize().y; | |
186 | ||
187 | int bw = w - m_marginX - m_borderX; | |
188 | int bh = h - m_marginY - m_borderY; | |
189 | if (bh < 11) bh=11; | |
190 | if (bw < 9) bw=9; | |
191 | ||
192 | wxBitmap bmp(bw, bh); | |
193 | dc.SelectObject(bmp); | |
194 | ||
89448b5d VZ |
195 | wxRect r(0,0,bw, bh); |
196 | wxRendererNative& renderer = wxRendererNative::Get(); | |
83dc6964 RR |
197 | |
198 | #ifdef __WXGTK__ | |
199 | wxColour magic(255,0,255); | |
200 | dc.SetBrush( wxBrush( magic ) ); | |
201 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
202 | dc.DrawRectangle(0,0,bw,bh); | |
203 | renderer.DrawComboBoxDropButton(this, dc, r); | |
204 | wxMask *mask = new wxMask( bmp, magic ); | |
205 | bmp.SetMask( mask ); | |
206 | #else | |
89448b5d | 207 | renderer.DrawComboBoxDropButton(this, dc, r); |
83dc6964 | 208 | #endif |
38511687 | 209 | SetBitmapLabel(bmp); |
89448b5d VZ |
210 | |
211 | wxBitmap bmpSel(bw, bh); | |
212 | dc.SelectObject(bmpSel); | |
83dc6964 RR |
213 | |
214 | #ifdef __WXGTK__ | |
215 | dc.SetBrush( wxBrush( magic ) ); | |
216 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
217 | dc.DrawRectangle(0,0,bw,bh); | |
218 | renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_PRESSED); | |
219 | mask = new wxMask( bmpSel, magic ); | |
220 | bmpSel.SetMask( mask ); | |
221 | #else | |
89448b5d | 222 | renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_PRESSED); |
83dc6964 | 223 | #endif |
89448b5d | 224 | SetBitmapSelected(bmpSel); |
38511687 VZ |
225 | } |
226 | ||
227 | wxBitmapButton::DoMoveWindow(x, y, w, h); | |
228 | } | |
229 | ||
230 | ||
1721a8c0 VZ |
231 | #if wxUSE_POPUPWIN |
232 | ||
233 | #include "wx/popupwin.h" | |
234 | ||
235 | class wxDatePopupInternal : public wxPopupTransientWindow | |
236 | { | |
237 | public: | |
238 | wxDatePopupInternal(wxWindow *parent) : wxPopupTransientWindow(parent) { } | |
239 | ||
240 | void ShowAt(int x, int y) | |
241 | { | |
242 | Position(wxPoint(x, y), wxSize(0, 0)); | |
243 | Popup(); | |
244 | } | |
245 | ||
246 | void Hide() | |
247 | { | |
248 | Dismiss(); | |
249 | } | |
250 | }; | |
251 | ||
252 | #else // !wxUSE_POPUPWIN | |
253 | ||
254 | class wxDatePopupInternal : public wxDialog | |
255 | { | |
256 | public: | |
257 | wxDatePopupInternal(wxWindow *parent) | |
258 | : wxDialog(parent, | |
259 | wxID_ANY, | |
260 | wxEmptyString, | |
261 | wxDefaultPosition, | |
262 | wxDefaultSize, | |
263 | wxSIMPLE_BORDER) | |
264 | { | |
265 | } | |
266 | ||
267 | void ShowAt(int x, int y) | |
268 | { | |
269 | Show(); | |
270 | Move(x, y); | |
271 | } | |
272 | ||
273 | void Hide() | |
274 | { | |
275 | wxDialog::Hide(); | |
276 | } | |
277 | }; | |
278 | ||
279 | #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN | |
280 | ||
39df3acd | 281 | // ============================================================================ |
7ae712f5 | 282 | // wxDatePickerCtrlGeneric implementation |
39df3acd VZ |
283 | // ============================================================================ |
284 | ||
7ae712f5 VZ |
285 | BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase) |
286 | EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick) | |
287 | EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText) | |
288 | EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus) | |
39df3acd VZ |
289 | END_EVENT_TABLE() |
290 | ||
b6292ff0 | 291 | #ifndef wxHAS_NATIVE_DATEPICKCTRL |
94ab4d92 | 292 | IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl) |
b6292ff0 | 293 | #endif |
39df3acd VZ |
294 | |
295 | // ---------------------------------------------------------------------------- | |
296 | // creation | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
7ae712f5 VZ |
299 | bool wxDatePickerCtrlGeneric::Create(wxWindow *parent, |
300 | wxWindowID id, | |
301 | const wxDateTime& date, | |
302 | const wxPoint& pos, | |
303 | const wxSize& size, | |
304 | long style, | |
807f5038 | 305 | const wxValidator& validator, |
7ae712f5 | 306 | const wxString& name) |
39df3acd | 307 | { |
29c86948 VZ |
308 | wxASSERT_MSG( !(style & wxDP_SPIN), |
309 | _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") ); | |
310 | ||
39df3acd VZ |
311 | if ( !wxControl::Create(parent, id, pos, size, |
312 | style | wxCLIP_CHILDREN | wxWANTS_CHARS, | |
807f5038 | 313 | validator, name) ) |
39df3acd VZ |
314 | |
315 | { | |
316 | return false; | |
317 | } | |
318 | ||
39df3acd VZ |
319 | InheritAttributes(); |
320 | ||
930fb29e | 321 | m_txt = new wxTextCtrl(this, CTRLID_TXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, TXTCTRL_FLAGS); |
38511687 | 322 | |
b6261304 VZ |
323 | m_txt->Connect(wxEVT_KEY_DOWN, |
324 | wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey), | |
325 | NULL, this); | |
326 | m_txt->Connect(wxEVT_KILL_FOCUS, | |
327 | wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus), | |
328 | NULL, this); | |
39df3acd | 329 | |
930fb29e | 330 | const int height = m_txt->GetBestSize().y - BUTTONBORDER; |
39df3acd | 331 | |
38511687 | 332 | m_btn = new wxDropdownButton(this, CTRLID_BTN, wxDefaultPosition, wxSize(height, height)); |
930fb29e | 333 | |
1721a8c0 | 334 | m_popup = new wxDatePopupInternal(this); |
d5ae99f5 | 335 | m_popup->SetFont(GetFont()); |
39df3acd | 336 | |
d5ae99f5 | 337 | wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN, |
1721a8c0 | 338 | wxPoint(0, 0), wxDefaultSize, |
4b134bb2 VZ |
339 | wxSUNKEN_BORDER); |
340 | m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime, | |
1721a8c0 | 341 | wxPoint(0, 0), wxDefaultSize, |
4b134bb2 | 342 | wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER); |
b6261304 VZ |
343 | m_cal->Connect(wxEVT_CALENDAR_SEL_CHANGED, |
344 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
345 | NULL, this); | |
346 | m_cal->Connect(wxEVT_KEY_DOWN, | |
347 | wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey), | |
348 | NULL, this); | |
349 | m_cal->Connect(wxEVT_CALENDAR_DOUBLECLICKED, | |
350 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
351 | NULL, this); | |
352 | m_cal->Connect(wxEVT_CALENDAR_DAY_CHANGED, | |
353 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
354 | NULL, this); | |
355 | m_cal->Connect(wxEVT_CALENDAR_MONTH_CHANGED, | |
356 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
357 | NULL, this); | |
358 | m_cal->Connect(wxEVT_CALENDAR_YEAR_CHANGED, | |
359 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
360 | NULL, this); | |
39df3acd VZ |
361 | |
362 | wxWindow *yearControl = m_cal->GetYearControl(); | |
363 | ||
b6261304 VZ |
364 | Connect(wxEVT_SET_FOCUS, |
365 | wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus)); | |
39df3acd VZ |
366 | |
367 | wxClientDC dc(yearControl); | |
9d9e32a0 | 368 | dc.SetFont(yearControl->GetFont()); |
39df3acd VZ |
369 | wxCoord width, dummy; |
370 | dc.GetTextExtent(wxT("2000"), &width, &dummy); | |
1721a8c0 | 371 | width += ConvertDialogToPixels(wxSize(20, 0)).x; |
39df3acd VZ |
372 | |
373 | wxSize calSize = m_cal->GetBestSize(); | |
374 | wxSize yearSize = yearControl->GetSize(); | |
375 | yearSize.x = width; | |
376 | ||
377 | wxPoint yearPosition = yearControl->GetPosition(); | |
378 | ||
d5ae99f5 VZ |
379 | SetFormat(wxT("%x")); |
380 | ||
39df3acd VZ |
381 | width = yearPosition.x + yearSize.x+2+CALBORDER/2; |
382 | if (width < calSize.x-4) | |
383 | width = calSize.x-4; | |
384 | ||
385 | int calPos = (width-calSize.x)/2; | |
386 | if (calPos == -1) | |
387 | { | |
388 | calPos = 0; | |
389 | width += 2; | |
390 | } | |
391 | m_cal->SetSize(calPos, 0, calSize.x, calSize.y); | |
4b134bb2 VZ |
392 | yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y, |
393 | yearSize.x, yearSize.y); | |
39df3acd VZ |
394 | m_cal->GetMonthControl()->Move(0, 0); |
395 | ||
396 | ||
397 | ||
398 | panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER); | |
d5ae99f5 VZ |
399 | m_popup->SetClientSize(panel->GetSize()); |
400 | m_popup->Hide(); | |
39df3acd | 401 | |
964f23b7 | 402 | SetValue(date.IsValid() ? date : wxDateTime::Today()); |
1721a8c0 | 403 | |
d8b49350 | 404 | SetBestFittingSize(size); |
930fb29e | 405 | |
3a0c6181 | 406 | return true; |
39df3acd VZ |
407 | } |
408 | ||
409 | ||
7ae712f5 | 410 | void wxDatePickerCtrlGeneric::Init() |
39df3acd | 411 | { |
d5ae99f5 | 412 | m_popup = NULL; |
39df3acd VZ |
413 | m_txt = NULL; |
414 | m_cal = NULL; | |
415 | m_btn = NULL; | |
416 | ||
417 | m_dropped = false; | |
418 | m_ignoreDrop = false; | |
419 | } | |
420 | ||
421 | ||
7ae712f5 | 422 | bool wxDatePickerCtrlGeneric::Destroy() |
39df3acd VZ |
423 | { |
424 | if (m_cal) | |
425 | m_cal->Destroy(); | |
d5ae99f5 VZ |
426 | if (m_popup) |
427 | m_popup->Destroy(); | |
39df3acd VZ |
428 | if (m_txt) |
429 | m_txt->Destroy(); | |
430 | if (m_btn) | |
431 | m_btn->Destroy(); | |
432 | ||
d5ae99f5 | 433 | m_popup = NULL; |
39df3acd VZ |
434 | m_txt = NULL; |
435 | m_cal = NULL; | |
436 | m_btn = NULL; | |
437 | ||
438 | return wxControl::Destroy(); | |
439 | } | |
440 | ||
441 | // ---------------------------------------------------------------------------- | |
442 | // overridden base class methods | |
443 | // ---------------------------------------------------------------------------- | |
444 | ||
7ae712f5 | 445 | void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h) |
39df3acd VZ |
446 | { |
447 | wxControl::DoMoveWindow(x, y, w, h); | |
448 | wxSize bs=m_btn->GetBestSize(); | |
449 | int eh=m_txt->GetBestSize().y; | |
450 | ||
930fb29e | 451 | m_txt->SetSize(0, TXTPOSY, w-bs.x-RIGHTBUTTONBORDER, h > eh ? eh-TXTPOSY : h-TXTPOSY); |
1721a8c0 | 452 | m_btn->SetSize(w - bs.x-RIGHTBUTTONBORDER, TOPBUTTONBORDER, bs.x, h > bs.y ? bs.y : h); |
39df3acd VZ |
453 | |
454 | if (m_dropped) | |
1721a8c0 | 455 | DropDown(true); |
39df3acd VZ |
456 | } |
457 | ||
7ae712f5 | 458 | wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const |
39df3acd VZ |
459 | { |
460 | int bh=m_btn->GetBestSize().y; | |
461 | int eh=m_txt->GetBestSize().y; | |
462 | return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh); | |
463 | } | |
464 | ||
465 | ||
7ae712f5 | 466 | bool wxDatePickerCtrlGeneric::Show(bool show) |
39df3acd VZ |
467 | { |
468 | if ( !wxControl::Show(show) ) | |
469 | { | |
3a0c6181 | 470 | return false; |
39df3acd VZ |
471 | } |
472 | ||
1721a8c0 | 473 | if ( !show ) |
39df3acd | 474 | { |
1721a8c0 | 475 | if ( m_popup ) |
39df3acd | 476 | { |
d5ae99f5 | 477 | m_popup->Hide(); |
39df3acd VZ |
478 | m_dropped = false; |
479 | } | |
480 | } | |
481 | ||
3a0c6181 | 482 | return true; |
39df3acd VZ |
483 | } |
484 | ||
485 | ||
7ae712f5 | 486 | bool wxDatePickerCtrlGeneric::Enable(bool enable) |
39df3acd VZ |
487 | { |
488 | if ( !wxControl::Enable(enable) ) | |
489 | { | |
3a0c6181 | 490 | return false; |
39df3acd VZ |
491 | } |
492 | ||
1721a8c0 | 493 | if ( !enable ) |
39df3acd | 494 | { |
2eda91db VZ |
495 | if ( m_popup ) |
496 | m_popup->Hide(); | |
39df3acd | 497 | } |
1721a8c0 VZ |
498 | |
499 | if ( m_btn ) | |
39df3acd | 500 | m_btn->Enable(enable); |
1721a8c0 | 501 | |
3a0c6181 | 502 | return true; |
39df3acd VZ |
503 | } |
504 | ||
505 | // ---------------------------------------------------------------------------- | |
7ae712f5 | 506 | // wxDatePickerCtrlGeneric API |
39df3acd VZ |
507 | // ---------------------------------------------------------------------------- |
508 | ||
4b134bb2 | 509 | bool |
7ae712f5 VZ |
510 | wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate, |
511 | const wxDateTime& upperdate) | |
4b134bb2 VZ |
512 | { |
513 | return m_cal->SetDateRange(lowerdate, upperdate); | |
514 | } | |
515 | ||
7ae712f5 | 516 | bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt) |
39df3acd VZ |
517 | { |
518 | wxDateTime dt; | |
519 | dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d")); | |
520 | wxString str=dt.Format(fmt); | |
521 | wxChar *p=(wxChar*)str.c_str(); | |
522 | ||
523 | m_format=wxEmptyString; | |
524 | ||
525 | while (*p) | |
526 | { | |
527 | int n=wxAtoi(p); | |
528 | if (n == dt.GetDay()) | |
529 | { | |
530 | m_format.Append(wxT("%d")); | |
531 | p += 2; | |
532 | } | |
533 | else if (n == (int)dt.GetMonth()+1) | |
534 | { | |
535 | m_format.Append(wxT("%m")); | |
536 | p += 2; | |
537 | } | |
538 | else if (n == dt.GetYear()) | |
539 | { | |
540 | m_format.Append(wxT("%Y")); | |
541 | p += 4; | |
542 | } | |
d5ae99f5 VZ |
543 | else if (n == (dt.GetYear() % 100)) |
544 | { | |
1721a8c0 VZ |
545 | if (GetWindowStyle() & wxDP_SHOWCENTURY) |
546 | m_format.Append(wxT("%Y")); | |
547 | else | |
548 | m_format.Append(wxT("%y")); | |
d5ae99f5 VZ |
549 | p += 2; |
550 | } | |
39df3acd VZ |
551 | else |
552 | m_format.Append(*p++); | |
553 | } | |
554 | ||
555 | if (m_txt) | |
556 | { | |
1721a8c0 VZ |
557 | wxArrayString allowedChars; |
558 | for ( wxChar c = _T('0'); c <= _T('9'); c++ ) | |
559 | allowedChars.Add(wxString(c, 1)); | |
560 | ||
561 | const wxChar *p = m_format.c_str(); | |
39df3acd VZ |
562 | while (*p) |
563 | { | |
564 | if (*p == '%') | |
565 | p += 2; | |
566 | else | |
1721a8c0 | 567 | allowedChars.Add(wxString(*p++, 1)); |
39df3acd | 568 | } |
1721a8c0 | 569 | |
39df3acd | 570 | wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST); |
1721a8c0 | 571 | tv.SetIncludes(allowedChars); |
39df3acd VZ |
572 | |
573 | m_txt->SetValidator(tv); | |
d5ae99f5 | 574 | |
1721a8c0 VZ |
575 | if (m_currentDate.IsValid()) |
576 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
39df3acd | 577 | } |
1721a8c0 | 578 | |
39df3acd VZ |
579 | return true; |
580 | } | |
581 | ||
582 | ||
7ae712f5 | 583 | wxDateTime wxDatePickerCtrlGeneric::GetValue() const |
39df3acd | 584 | { |
1721a8c0 | 585 | return m_currentDate; |
39df3acd VZ |
586 | } |
587 | ||
588 | ||
7ae712f5 | 589 | void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date) |
39df3acd VZ |
590 | { |
591 | if (m_cal) | |
592 | { | |
593 | if (date.IsValid()) | |
d5ae99f5 | 594 | m_txt->SetValue(date.Format(m_format)); |
39df3acd | 595 | else |
1721a8c0 VZ |
596 | { |
597 | wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE), | |
598 | _T("this control must have a valid date") ); | |
599 | ||
39df3acd | 600 | m_txt->SetValue(wxEmptyString); |
1721a8c0 VZ |
601 | } |
602 | ||
603 | m_currentDate = date; | |
39df3acd VZ |
604 | } |
605 | } | |
606 | ||
607 | ||
7ae712f5 | 608 | bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const |
39df3acd VZ |
609 | { |
610 | if (dt1) | |
4b134bb2 | 611 | *dt1 = m_cal->GetLowerDateLimit(); |
39df3acd | 612 | if (dt1) |
4b134bb2 | 613 | *dt2 = m_cal->GetUpperDateLimit(); |
39df3acd VZ |
614 | return true; |
615 | } | |
616 | ||
617 | ||
7ae712f5 VZ |
618 | void |
619 | wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2) | |
39df3acd | 620 | { |
4b134bb2 | 621 | m_cal->SetDateRange(dt1, dt2); |
39df3acd VZ |
622 | } |
623 | ||
624 | // ---------------------------------------------------------------------------- | |
625 | // event handlers | |
626 | // ---------------------------------------------------------------------------- | |
627 | ||
7ae712f5 | 628 | void wxDatePickerCtrlGeneric::DropDown(bool down) |
39df3acd | 629 | { |
d5ae99f5 | 630 | if (m_popup) |
39df3acd VZ |
631 | { |
632 | if (down) | |
633 | { | |
d5ae99f5 | 634 | wxDateTime dt; |
a7c58211 | 635 | if (!m_txt->GetValue().empty()) |
39df3acd | 636 | dt.ParseFormat(m_txt->GetValue(), m_format); |
d5ae99f5 VZ |
637 | |
638 | if (dt.IsValid()) | |
39df3acd | 639 | m_cal->SetDate(dt); |
d5ae99f5 VZ |
640 | else |
641 | m_cal->SetDate(wxDateTime::Today()); | |
39df3acd | 642 | |
d5ae99f5 | 643 | wxPoint pos=GetParent()->ClientToScreen(GetPosition()); |
1721a8c0 | 644 | m_popup->ShowAt(pos.x, pos.y + GetSize().y); |
39df3acd | 645 | m_dropped = true; |
1721a8c0 | 646 | m_cal->SetFocus(); |
39df3acd VZ |
647 | } |
648 | else | |
649 | { | |
650 | if (m_dropped) | |
d5ae99f5 | 651 | m_popup->Hide(); |
39df3acd VZ |
652 | m_dropped = false; |
653 | } | |
654 | } | |
655 | } | |
656 | ||
657 | ||
7ae712f5 | 658 | void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev) |
39df3acd VZ |
659 | { |
660 | ev.Skip(); | |
661 | m_ignoreDrop = false; | |
662 | ||
663 | wxWindow *w=(wxWindow*)ev.GetEventObject(); | |
664 | while (w) | |
665 | { | |
d5ae99f5 | 666 | if (w == m_popup) |
39df3acd VZ |
667 | return; |
668 | w = w->GetParent(); | |
669 | } | |
670 | ||
671 | if (m_dropped) | |
672 | { | |
673 | DropDown(false); | |
674 | if (ev.GetEventObject() == m_btn) | |
675 | m_ignoreDrop = true; | |
676 | } | |
677 | } | |
678 | ||
679 | ||
b6292ff0 | 680 | void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event)) |
39df3acd VZ |
681 | { |
682 | if (m_ignoreDrop) | |
683 | { | |
684 | m_ignoreDrop = false; | |
685 | m_txt->SetFocus(); | |
686 | } | |
687 | else | |
688 | { | |
689 | DropDown(); | |
690 | m_cal->SetFocus(); | |
691 | } | |
692 | } | |
693 | ||
694 | ||
b6292ff0 | 695 | void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev)) |
39df3acd VZ |
696 | { |
697 | if (m_txt) | |
698 | { | |
699 | m_txt->SetFocus(); | |
1721a8c0 | 700 | m_txt->SetSelection(-1, -1); // select everything |
39df3acd VZ |
701 | } |
702 | } | |
703 | ||
704 | ||
7ae712f5 | 705 | void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev) |
39df3acd VZ |
706 | { |
707 | ev.Skip(); | |
708 | ||
709 | wxDateTime dt; | |
710 | dt.ParseFormat(m_txt->GetValue(), m_format); | |
2436cac7 | 711 | if ( !dt.IsValid() ) |
1721a8c0 VZ |
712 | { |
713 | if ( !HasFlag(wxDP_ALLOWNONE) ) | |
714 | dt = m_currentDate; | |
715 | } | |
716 | ||
a7c58211 WS |
717 | if(dt.IsValid()) |
718 | m_txt->SetValue(dt.Format(m_format)); | |
719 | else | |
720 | m_txt->SetValue(wxEmptyString); | |
1721a8c0 VZ |
721 | |
722 | // notify that we had to change the date after validation | |
2436cac7 VZ |
723 | if ( (dt.IsValid() && m_currentDate != dt) || |
724 | (!dt.IsValid() && m_currentDate.IsValid()) ) | |
1721a8c0 VZ |
725 | { |
726 | m_currentDate = dt; | |
727 | wxDateEvent event(this, dt, wxEVT_DATE_CHANGED); | |
728 | GetEventHandler()->ProcessEvent(event); | |
729 | } | |
39df3acd VZ |
730 | } |
731 | ||
732 | ||
7ae712f5 | 733 | void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev) |
39df3acd VZ |
734 | { |
735 | if (m_cal) | |
736 | { | |
1721a8c0 VZ |
737 | m_currentDate = m_cal->GetDate(); |
738 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
39df3acd VZ |
739 | if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED) |
740 | { | |
741 | DropDown(false); | |
742 | m_txt->SetFocus(); | |
743 | } | |
744 | } | |
745 | ev.SetEventObject(this); | |
746 | ev.SetId(GetId()); | |
747 | GetParent()->ProcessEvent(ev); | |
1721a8c0 VZ |
748 | |
749 | wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED); | |
750 | GetParent()->ProcessEvent(dev); | |
39df3acd VZ |
751 | } |
752 | ||
753 | ||
7ae712f5 | 754 | void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev) |
39df3acd VZ |
755 | { |
756 | ev.SetEventObject(this); | |
757 | ev.SetId(GetId()); | |
758 | GetParent()->ProcessEvent(ev); | |
759 | ||
760 | // We'll create an additional event if the date is valid. | |
1721a8c0 | 761 | // If the date isn't valid, the user's probably in the middle of typing |
a7c58211 | 762 | wxString txt = m_txt->GetValue(); |
39df3acd | 763 | wxDateTime dt; |
a7c58211 | 764 | if (!txt.empty()) |
39df3acd VZ |
765 | { |
766 | dt.ParseFormat(txt, m_format); | |
767 | if (!dt.IsValid()) | |
768 | return; | |
769 | } | |
770 | ||
771 | wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED); | |
772 | cev.SetEventObject(this); | |
773 | cev.SetId(GetId()); | |
774 | cev.SetDate(dt); | |
775 | ||
776 | GetParent()->ProcessEvent(cev); | |
1721a8c0 VZ |
777 | |
778 | wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED); | |
779 | GetParent()->ProcessEvent(dev); | |
39df3acd VZ |
780 | } |
781 | ||
782 | ||
7ae712f5 | 783 | void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev) |
39df3acd VZ |
784 | { |
785 | if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers()) | |
1721a8c0 | 786 | DropDown(true); |
39df3acd VZ |
787 | else |
788 | ev.Skip(); | |
789 | } | |
790 | ||
791 | ||
7ae712f5 | 792 | void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev) |
39df3acd VZ |
793 | { |
794 | if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers()) | |
795 | DropDown(false); | |
796 | else | |
797 | ev.Skip(); | |
798 | } | |
799 | ||
7ae712f5 VZ |
800 | #endif // wxUSE_DATEPICKCTRL_GENERIC |
801 | ||
802 | #endif // wxUSE_DATEPICKCTRL | |
803 |