]>
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 | |
930fb29e VZ |
158 | #if (BTNFLAGS & wxBU_AUTODRAW ) == 0 |
159 | m_windowStyle |= wxBU_AUTODRAW; | |
160 | #endif | |
161 | ||
94ab4d92 VZ |
162 | const wxSize sz = GetSize(); |
163 | int w = chkBmp.GetWidth(), | |
164 | h = chkBmp.GetHeight(); | |
165 | m_borderX = sz.x - m_marginX - w; | |
166 | m_borderY = sz.y - m_marginY - h; | |
38511687 | 167 | |
94ab4d92 VZ |
168 | w = size.x > 0 ? size.x : sz.x; |
169 | h = size.y > 0 ? size.y : sz.y; | |
38511687 VZ |
170 | |
171 | DoMoveWindow(pos.x, pos.y, w, h); | |
94ab4d92 VZ |
172 | |
173 | return true; | |
38511687 VZ |
174 | } |
175 | ||
176 | ||
177 | void wxDropdownButton::DoMoveWindow(int x, int y, int w, int h) | |
178 | { | |
179 | if (m_borderX >= 0 && m_borderY >= 0 && (w >= 0 || h >= 0)) | |
180 | { | |
181 | wxMemoryDC dc; | |
182 | if (w < 0) | |
183 | w = GetSize().x; | |
184 | #ifdef __WXGTK__ | |
185 | else | |
186 | w = m_marginX + m_borderX + 15; // GTK magic size | |
187 | #endif | |
188 | if (h < 0) | |
189 | h = GetSize().y; | |
190 | ||
191 | int bw = w - m_marginX - m_borderX; | |
192 | int bh = h - m_marginY - m_borderY; | |
193 | if (bh < 11) bh=11; | |
194 | if (bw < 9) bw=9; | |
195 | ||
196 | wxBitmap bmp(bw, bh); | |
197 | dc.SelectObject(bmp); | |
198 | ||
199 | wxRendererNative::Get().DrawComboBoxDropButton(this, dc, wxRect(0,0,bw, bh)); | |
38511687 VZ |
200 | SetBitmapLabel(bmp); |
201 | } | |
202 | ||
203 | wxBitmapButton::DoMoveWindow(x, y, w, h); | |
204 | } | |
205 | ||
206 | ||
1721a8c0 VZ |
207 | #if wxUSE_POPUPWIN |
208 | ||
209 | #include "wx/popupwin.h" | |
210 | ||
211 | class wxDatePopupInternal : public wxPopupTransientWindow | |
212 | { | |
213 | public: | |
214 | wxDatePopupInternal(wxWindow *parent) : wxPopupTransientWindow(parent) { } | |
215 | ||
216 | void ShowAt(int x, int y) | |
217 | { | |
218 | Position(wxPoint(x, y), wxSize(0, 0)); | |
219 | Popup(); | |
220 | } | |
221 | ||
222 | void Hide() | |
223 | { | |
224 | Dismiss(); | |
225 | } | |
226 | }; | |
227 | ||
228 | #else // !wxUSE_POPUPWIN | |
229 | ||
230 | class wxDatePopupInternal : public wxDialog | |
231 | { | |
232 | public: | |
233 | wxDatePopupInternal(wxWindow *parent) | |
234 | : wxDialog(parent, | |
235 | wxID_ANY, | |
236 | wxEmptyString, | |
237 | wxDefaultPosition, | |
238 | wxDefaultSize, | |
239 | wxSIMPLE_BORDER) | |
240 | { | |
241 | } | |
242 | ||
243 | void ShowAt(int x, int y) | |
244 | { | |
245 | Show(); | |
246 | Move(x, y); | |
247 | } | |
248 | ||
249 | void Hide() | |
250 | { | |
251 | wxDialog::Hide(); | |
252 | } | |
253 | }; | |
254 | ||
255 | #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN | |
256 | ||
39df3acd | 257 | // ============================================================================ |
7ae712f5 | 258 | // wxDatePickerCtrlGeneric implementation |
39df3acd VZ |
259 | // ============================================================================ |
260 | ||
7ae712f5 VZ |
261 | BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase) |
262 | EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick) | |
263 | EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText) | |
264 | EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus) | |
39df3acd VZ |
265 | END_EVENT_TABLE() |
266 | ||
b6292ff0 | 267 | #ifndef wxHAS_NATIVE_DATEPICKCTRL |
94ab4d92 | 268 | IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl) |
b6292ff0 | 269 | #endif |
39df3acd VZ |
270 | |
271 | // ---------------------------------------------------------------------------- | |
272 | // creation | |
273 | // ---------------------------------------------------------------------------- | |
274 | ||
7ae712f5 VZ |
275 | bool wxDatePickerCtrlGeneric::Create(wxWindow *parent, |
276 | wxWindowID id, | |
277 | const wxDateTime& date, | |
278 | const wxPoint& pos, | |
279 | const wxSize& size, | |
280 | long style, | |
807f5038 | 281 | const wxValidator& validator, |
7ae712f5 | 282 | const wxString& name) |
39df3acd | 283 | { |
29c86948 VZ |
284 | wxASSERT_MSG( !(style & wxDP_SPIN), |
285 | _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") ); | |
286 | ||
39df3acd VZ |
287 | if ( !wxControl::Create(parent, id, pos, size, |
288 | style | wxCLIP_CHILDREN | wxWANTS_CHARS, | |
807f5038 | 289 | validator, name) ) |
39df3acd VZ |
290 | |
291 | { | |
292 | return false; | |
293 | } | |
294 | ||
39df3acd VZ |
295 | InheritAttributes(); |
296 | ||
930fb29e | 297 | m_txt = new wxTextCtrl(this, CTRLID_TXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, TXTCTRL_FLAGS); |
38511687 | 298 | |
b6261304 VZ |
299 | m_txt->Connect(wxEVT_KEY_DOWN, |
300 | wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey), | |
301 | NULL, this); | |
302 | m_txt->Connect(wxEVT_KILL_FOCUS, | |
303 | wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus), | |
304 | NULL, this); | |
39df3acd | 305 | |
930fb29e | 306 | const int height = m_txt->GetBestSize().y - BUTTONBORDER; |
39df3acd | 307 | |
38511687 | 308 | m_btn = new wxDropdownButton(this, CTRLID_BTN, wxDefaultPosition, wxSize(height, height)); |
930fb29e | 309 | |
1721a8c0 | 310 | m_popup = new wxDatePopupInternal(this); |
d5ae99f5 | 311 | m_popup->SetFont(GetFont()); |
39df3acd | 312 | |
d5ae99f5 | 313 | wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN, |
1721a8c0 | 314 | wxPoint(0, 0), wxDefaultSize, |
4b134bb2 VZ |
315 | wxSUNKEN_BORDER); |
316 | m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime, | |
1721a8c0 | 317 | wxPoint(0, 0), wxDefaultSize, |
4b134bb2 | 318 | wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER); |
b6261304 VZ |
319 | m_cal->Connect(wxEVT_CALENDAR_SEL_CHANGED, |
320 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
321 | NULL, this); | |
322 | m_cal->Connect(wxEVT_KEY_DOWN, | |
323 | wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey), | |
324 | NULL, this); | |
325 | m_cal->Connect(wxEVT_CALENDAR_DOUBLECLICKED, | |
326 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
327 | NULL, this); | |
328 | m_cal->Connect(wxEVT_CALENDAR_DAY_CHANGED, | |
329 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
330 | NULL, this); | |
331 | m_cal->Connect(wxEVT_CALENDAR_MONTH_CHANGED, | |
332 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
333 | NULL, this); | |
334 | m_cal->Connect(wxEVT_CALENDAR_YEAR_CHANGED, | |
335 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
336 | NULL, this); | |
39df3acd VZ |
337 | |
338 | wxWindow *yearControl = m_cal->GetYearControl(); | |
339 | ||
b6261304 VZ |
340 | Connect(wxEVT_SET_FOCUS, |
341 | wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus)); | |
39df3acd VZ |
342 | |
343 | wxClientDC dc(yearControl); | |
9d9e32a0 | 344 | dc.SetFont(yearControl->GetFont()); |
39df3acd VZ |
345 | wxCoord width, dummy; |
346 | dc.GetTextExtent(wxT("2000"), &width, &dummy); | |
1721a8c0 | 347 | width += ConvertDialogToPixels(wxSize(20, 0)).x; |
39df3acd VZ |
348 | |
349 | wxSize calSize = m_cal->GetBestSize(); | |
350 | wxSize yearSize = yearControl->GetSize(); | |
351 | yearSize.x = width; | |
352 | ||
353 | wxPoint yearPosition = yearControl->GetPosition(); | |
354 | ||
d5ae99f5 VZ |
355 | SetFormat(wxT("%x")); |
356 | ||
39df3acd VZ |
357 | width = yearPosition.x + yearSize.x+2+CALBORDER/2; |
358 | if (width < calSize.x-4) | |
359 | width = calSize.x-4; | |
360 | ||
361 | int calPos = (width-calSize.x)/2; | |
362 | if (calPos == -1) | |
363 | { | |
364 | calPos = 0; | |
365 | width += 2; | |
366 | } | |
367 | m_cal->SetSize(calPos, 0, calSize.x, calSize.y); | |
4b134bb2 VZ |
368 | yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y, |
369 | yearSize.x, yearSize.y); | |
39df3acd VZ |
370 | m_cal->GetMonthControl()->Move(0, 0); |
371 | ||
372 | ||
373 | ||
374 | panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER); | |
d5ae99f5 VZ |
375 | m_popup->SetClientSize(panel->GetSize()); |
376 | m_popup->Hide(); | |
39df3acd | 377 | |
964f23b7 | 378 | SetValue(date.IsValid() ? date : wxDateTime::Today()); |
1721a8c0 | 379 | |
d8b49350 | 380 | SetBestFittingSize(size); |
930fb29e | 381 | |
3a0c6181 | 382 | return true; |
39df3acd VZ |
383 | } |
384 | ||
385 | ||
7ae712f5 | 386 | void wxDatePickerCtrlGeneric::Init() |
39df3acd | 387 | { |
d5ae99f5 | 388 | m_popup = NULL; |
39df3acd VZ |
389 | m_txt = NULL; |
390 | m_cal = NULL; | |
391 | m_btn = NULL; | |
392 | ||
393 | m_dropped = false; | |
394 | m_ignoreDrop = false; | |
395 | } | |
396 | ||
397 | ||
7ae712f5 | 398 | bool wxDatePickerCtrlGeneric::Destroy() |
39df3acd VZ |
399 | { |
400 | if (m_cal) | |
401 | m_cal->Destroy(); | |
d5ae99f5 VZ |
402 | if (m_popup) |
403 | m_popup->Destroy(); | |
39df3acd VZ |
404 | if (m_txt) |
405 | m_txt->Destroy(); | |
406 | if (m_btn) | |
407 | m_btn->Destroy(); | |
408 | ||
d5ae99f5 | 409 | m_popup = NULL; |
39df3acd VZ |
410 | m_txt = NULL; |
411 | m_cal = NULL; | |
412 | m_btn = NULL; | |
413 | ||
414 | return wxControl::Destroy(); | |
415 | } | |
416 | ||
417 | // ---------------------------------------------------------------------------- | |
418 | // overridden base class methods | |
419 | // ---------------------------------------------------------------------------- | |
420 | ||
7ae712f5 | 421 | void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h) |
39df3acd VZ |
422 | { |
423 | wxControl::DoMoveWindow(x, y, w, h); | |
424 | wxSize bs=m_btn->GetBestSize(); | |
425 | int eh=m_txt->GetBestSize().y; | |
426 | ||
930fb29e | 427 | m_txt->SetSize(0, TXTPOSY, w-bs.x-RIGHTBUTTONBORDER, h > eh ? eh-TXTPOSY : h-TXTPOSY); |
1721a8c0 | 428 | m_btn->SetSize(w - bs.x-RIGHTBUTTONBORDER, TOPBUTTONBORDER, bs.x, h > bs.y ? bs.y : h); |
39df3acd VZ |
429 | |
430 | if (m_dropped) | |
1721a8c0 | 431 | DropDown(true); |
39df3acd VZ |
432 | } |
433 | ||
7ae712f5 | 434 | wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const |
39df3acd VZ |
435 | { |
436 | int bh=m_btn->GetBestSize().y; | |
437 | int eh=m_txt->GetBestSize().y; | |
438 | return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh); | |
439 | } | |
440 | ||
441 | ||
7ae712f5 | 442 | bool wxDatePickerCtrlGeneric::Show(bool show) |
39df3acd VZ |
443 | { |
444 | if ( !wxControl::Show(show) ) | |
445 | { | |
3a0c6181 | 446 | return false; |
39df3acd VZ |
447 | } |
448 | ||
1721a8c0 | 449 | if ( !show ) |
39df3acd | 450 | { |
1721a8c0 | 451 | if ( m_popup ) |
39df3acd | 452 | { |
d5ae99f5 | 453 | m_popup->Hide(); |
39df3acd VZ |
454 | m_dropped = false; |
455 | } | |
456 | } | |
457 | ||
3a0c6181 | 458 | return true; |
39df3acd VZ |
459 | } |
460 | ||
461 | ||
7ae712f5 | 462 | bool wxDatePickerCtrlGeneric::Enable(bool enable) |
39df3acd VZ |
463 | { |
464 | if ( !wxControl::Enable(enable) ) | |
465 | { | |
3a0c6181 | 466 | return false; |
39df3acd VZ |
467 | } |
468 | ||
1721a8c0 | 469 | if ( !enable ) |
39df3acd | 470 | { |
1721a8c0 | 471 | if ( m_cal ) |
39df3acd VZ |
472 | m_cal->Hide(); |
473 | } | |
1721a8c0 VZ |
474 | |
475 | if ( m_btn ) | |
39df3acd | 476 | m_btn->Enable(enable); |
1721a8c0 | 477 | |
3a0c6181 | 478 | return true; |
39df3acd VZ |
479 | } |
480 | ||
481 | // ---------------------------------------------------------------------------- | |
7ae712f5 | 482 | // wxDatePickerCtrlGeneric API |
39df3acd VZ |
483 | // ---------------------------------------------------------------------------- |
484 | ||
4b134bb2 | 485 | bool |
7ae712f5 VZ |
486 | wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate, |
487 | const wxDateTime& upperdate) | |
4b134bb2 VZ |
488 | { |
489 | return m_cal->SetDateRange(lowerdate, upperdate); | |
490 | } | |
491 | ||
7ae712f5 | 492 | bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt) |
39df3acd VZ |
493 | { |
494 | wxDateTime dt; | |
495 | dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d")); | |
496 | wxString str=dt.Format(fmt); | |
497 | wxChar *p=(wxChar*)str.c_str(); | |
498 | ||
499 | m_format=wxEmptyString; | |
500 | ||
501 | while (*p) | |
502 | { | |
503 | int n=wxAtoi(p); | |
504 | if (n == dt.GetDay()) | |
505 | { | |
506 | m_format.Append(wxT("%d")); | |
507 | p += 2; | |
508 | } | |
509 | else if (n == (int)dt.GetMonth()+1) | |
510 | { | |
511 | m_format.Append(wxT("%m")); | |
512 | p += 2; | |
513 | } | |
514 | else if (n == dt.GetYear()) | |
515 | { | |
516 | m_format.Append(wxT("%Y")); | |
517 | p += 4; | |
518 | } | |
d5ae99f5 VZ |
519 | else if (n == (dt.GetYear() % 100)) |
520 | { | |
1721a8c0 VZ |
521 | if (GetWindowStyle() & wxDP_SHOWCENTURY) |
522 | m_format.Append(wxT("%Y")); | |
523 | else | |
524 | m_format.Append(wxT("%y")); | |
d5ae99f5 VZ |
525 | p += 2; |
526 | } | |
39df3acd VZ |
527 | else |
528 | m_format.Append(*p++); | |
529 | } | |
530 | ||
531 | if (m_txt) | |
532 | { | |
1721a8c0 VZ |
533 | wxArrayString allowedChars; |
534 | for ( wxChar c = _T('0'); c <= _T('9'); c++ ) | |
535 | allowedChars.Add(wxString(c, 1)); | |
536 | ||
537 | const wxChar *p = m_format.c_str(); | |
39df3acd VZ |
538 | while (*p) |
539 | { | |
540 | if (*p == '%') | |
541 | p += 2; | |
542 | else | |
1721a8c0 | 543 | allowedChars.Add(wxString(*p++, 1)); |
39df3acd | 544 | } |
1721a8c0 | 545 | |
39df3acd | 546 | wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST); |
1721a8c0 | 547 | tv.SetIncludes(allowedChars); |
39df3acd VZ |
548 | |
549 | m_txt->SetValidator(tv); | |
d5ae99f5 | 550 | |
1721a8c0 VZ |
551 | if (m_currentDate.IsValid()) |
552 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
39df3acd | 553 | } |
1721a8c0 | 554 | |
39df3acd VZ |
555 | return true; |
556 | } | |
557 | ||
558 | ||
7ae712f5 | 559 | wxDateTime wxDatePickerCtrlGeneric::GetValue() const |
39df3acd | 560 | { |
1721a8c0 | 561 | return m_currentDate; |
39df3acd VZ |
562 | } |
563 | ||
564 | ||
7ae712f5 | 565 | void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date) |
39df3acd VZ |
566 | { |
567 | if (m_cal) | |
568 | { | |
569 | if (date.IsValid()) | |
d5ae99f5 | 570 | m_txt->SetValue(date.Format(m_format)); |
39df3acd | 571 | else |
1721a8c0 VZ |
572 | { |
573 | wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE), | |
574 | _T("this control must have a valid date") ); | |
575 | ||
39df3acd | 576 | m_txt->SetValue(wxEmptyString); |
1721a8c0 VZ |
577 | } |
578 | ||
579 | m_currentDate = date; | |
39df3acd VZ |
580 | } |
581 | } | |
582 | ||
583 | ||
7ae712f5 | 584 | bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const |
39df3acd VZ |
585 | { |
586 | if (dt1) | |
4b134bb2 | 587 | *dt1 = m_cal->GetLowerDateLimit(); |
39df3acd | 588 | if (dt1) |
4b134bb2 | 589 | *dt2 = m_cal->GetUpperDateLimit(); |
39df3acd VZ |
590 | return true; |
591 | } | |
592 | ||
593 | ||
7ae712f5 VZ |
594 | void |
595 | wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2) | |
39df3acd | 596 | { |
4b134bb2 | 597 | m_cal->SetDateRange(dt1, dt2); |
39df3acd VZ |
598 | } |
599 | ||
600 | // ---------------------------------------------------------------------------- | |
601 | // event handlers | |
602 | // ---------------------------------------------------------------------------- | |
603 | ||
7ae712f5 | 604 | void wxDatePickerCtrlGeneric::DropDown(bool down) |
39df3acd | 605 | { |
d5ae99f5 | 606 | if (m_popup) |
39df3acd VZ |
607 | { |
608 | if (down) | |
609 | { | |
d5ae99f5 | 610 | wxDateTime dt; |
a7c58211 | 611 | if (!m_txt->GetValue().empty()) |
39df3acd | 612 | dt.ParseFormat(m_txt->GetValue(), m_format); |
d5ae99f5 VZ |
613 | |
614 | if (dt.IsValid()) | |
39df3acd | 615 | m_cal->SetDate(dt); |
d5ae99f5 VZ |
616 | else |
617 | m_cal->SetDate(wxDateTime::Today()); | |
39df3acd | 618 | |
d5ae99f5 | 619 | wxPoint pos=GetParent()->ClientToScreen(GetPosition()); |
1721a8c0 | 620 | m_popup->ShowAt(pos.x, pos.y + GetSize().y); |
39df3acd | 621 | m_dropped = true; |
1721a8c0 | 622 | m_cal->SetFocus(); |
39df3acd VZ |
623 | } |
624 | else | |
625 | { | |
626 | if (m_dropped) | |
d5ae99f5 | 627 | m_popup->Hide(); |
39df3acd VZ |
628 | m_dropped = false; |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | ||
7ae712f5 | 634 | void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev) |
39df3acd VZ |
635 | { |
636 | ev.Skip(); | |
637 | m_ignoreDrop = false; | |
638 | ||
639 | wxWindow *w=(wxWindow*)ev.GetEventObject(); | |
640 | while (w) | |
641 | { | |
d5ae99f5 | 642 | if (w == m_popup) |
39df3acd VZ |
643 | return; |
644 | w = w->GetParent(); | |
645 | } | |
646 | ||
647 | if (m_dropped) | |
648 | { | |
649 | DropDown(false); | |
650 | if (ev.GetEventObject() == m_btn) | |
651 | m_ignoreDrop = true; | |
652 | } | |
653 | } | |
654 | ||
655 | ||
b6292ff0 | 656 | void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event)) |
39df3acd VZ |
657 | { |
658 | if (m_ignoreDrop) | |
659 | { | |
660 | m_ignoreDrop = false; | |
661 | m_txt->SetFocus(); | |
662 | } | |
663 | else | |
664 | { | |
665 | DropDown(); | |
666 | m_cal->SetFocus(); | |
667 | } | |
668 | } | |
669 | ||
670 | ||
b6292ff0 | 671 | void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev)) |
39df3acd VZ |
672 | { |
673 | if (m_txt) | |
674 | { | |
675 | m_txt->SetFocus(); | |
1721a8c0 | 676 | m_txt->SetSelection(-1, -1); // select everything |
39df3acd VZ |
677 | } |
678 | } | |
679 | ||
680 | ||
7ae712f5 | 681 | void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev) |
39df3acd VZ |
682 | { |
683 | ev.Skip(); | |
684 | ||
685 | wxDateTime dt; | |
686 | dt.ParseFormat(m_txt->GetValue(), m_format); | |
2436cac7 | 687 | if ( !dt.IsValid() ) |
1721a8c0 VZ |
688 | { |
689 | if ( !HasFlag(wxDP_ALLOWNONE) ) | |
690 | dt = m_currentDate; | |
691 | } | |
692 | ||
a7c58211 WS |
693 | if(dt.IsValid()) |
694 | m_txt->SetValue(dt.Format(m_format)); | |
695 | else | |
696 | m_txt->SetValue(wxEmptyString); | |
1721a8c0 VZ |
697 | |
698 | // notify that we had to change the date after validation | |
2436cac7 VZ |
699 | if ( (dt.IsValid() && m_currentDate != dt) || |
700 | (!dt.IsValid() && m_currentDate.IsValid()) ) | |
1721a8c0 VZ |
701 | { |
702 | m_currentDate = dt; | |
703 | wxDateEvent event(this, dt, wxEVT_DATE_CHANGED); | |
704 | GetEventHandler()->ProcessEvent(event); | |
705 | } | |
39df3acd VZ |
706 | } |
707 | ||
708 | ||
7ae712f5 | 709 | void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev) |
39df3acd VZ |
710 | { |
711 | if (m_cal) | |
712 | { | |
1721a8c0 VZ |
713 | m_currentDate = m_cal->GetDate(); |
714 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
39df3acd VZ |
715 | if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED) |
716 | { | |
717 | DropDown(false); | |
718 | m_txt->SetFocus(); | |
719 | } | |
720 | } | |
721 | ev.SetEventObject(this); | |
722 | ev.SetId(GetId()); | |
723 | GetParent()->ProcessEvent(ev); | |
1721a8c0 VZ |
724 | |
725 | wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED); | |
726 | GetParent()->ProcessEvent(dev); | |
39df3acd VZ |
727 | } |
728 | ||
729 | ||
7ae712f5 | 730 | void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev) |
39df3acd VZ |
731 | { |
732 | ev.SetEventObject(this); | |
733 | ev.SetId(GetId()); | |
734 | GetParent()->ProcessEvent(ev); | |
735 | ||
736 | // We'll create an additional event if the date is valid. | |
1721a8c0 | 737 | // If the date isn't valid, the user's probably in the middle of typing |
a7c58211 | 738 | wxString txt = m_txt->GetValue(); |
39df3acd | 739 | wxDateTime dt; |
a7c58211 | 740 | if (!txt.empty()) |
39df3acd VZ |
741 | { |
742 | dt.ParseFormat(txt, m_format); | |
743 | if (!dt.IsValid()) | |
744 | return; | |
745 | } | |
746 | ||
747 | wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED); | |
748 | cev.SetEventObject(this); | |
749 | cev.SetId(GetId()); | |
750 | cev.SetDate(dt); | |
751 | ||
752 | GetParent()->ProcessEvent(cev); | |
1721a8c0 VZ |
753 | |
754 | wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED); | |
755 | GetParent()->ProcessEvent(dev); | |
39df3acd VZ |
756 | } |
757 | ||
758 | ||
7ae712f5 | 759 | void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev) |
39df3acd VZ |
760 | { |
761 | if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers()) | |
1721a8c0 | 762 | DropDown(true); |
39df3acd VZ |
763 | else |
764 | ev.Skip(); | |
765 | } | |
766 | ||
767 | ||
7ae712f5 | 768 | void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev) |
39df3acd VZ |
769 | { |
770 | if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers()) | |
771 | DropDown(false); | |
772 | else | |
773 | ev.Skip(); | |
774 | } | |
775 | ||
7ae712f5 VZ |
776 | #endif // wxUSE_DATEPICKCTRL_GENERIC |
777 | ||
778 | #endif // wxUSE_DATEPICKCTRL | |
779 |