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