]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/datectlg.cpp | |
3 | // Purpose: generic wxDatePickerCtrlGeneric implementation | |
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 | ||
26 | #if wxUSE_DATEPICKCTRL | |
27 | ||
28 | #include "wx/datectrl.h" | |
29 | ||
30 | // use this version if we're explicitly requested to do it or if it's the only | |
31 | // one we have | |
32 | #if !defined(wxHAS_NATIVE_DATEPICKCTRL) || \ | |
33 | (defined(wxUSE_DATEPICKCTRL_GENERIC) && wxUSE_DATEPICKCTRL_GENERIC) | |
34 | ||
35 | #ifndef WX_PRECOMP | |
36 | #include "wx/bmpbuttn.h" | |
37 | #include "wx/dialog.h" | |
38 | #include "wx/dcmemory.h" | |
39 | #include "wx/panel.h" | |
40 | #include "wx/textctrl.h" | |
41 | #include "wx/valtext.h" | |
42 | #endif | |
43 | ||
44 | #ifdef wxHAS_NATIVE_DATEPICKCTRL | |
45 | // this header is not included from wx/datectrl.h if we have a native | |
46 | // version, but we do need it here | |
47 | #include "wx/generic/datectrl.h" | |
48 | #else | |
49 | // we need to define _WX_DEFINE_DATE_EVENTS_ before including wx/dateevt.h to | |
50 | // define the event types we use if we're the only date picker control version | |
51 | // being compiled -- otherwise it's defined in the native version implementation | |
52 | #define _WX_DEFINE_DATE_EVENTS_ | |
53 | #endif | |
54 | ||
55 | #include "wx/dateevt.h" | |
56 | ||
57 | #include "wx/calctrl.h" | |
58 | #include "wx/renderer.h" | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // constants | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | enum | |
65 | { | |
66 | CTRLID_TXT = 101, | |
67 | CTRLID_CAL, | |
68 | CTRLID_BTN, | |
69 | CTRLID_PAN | |
70 | }; | |
71 | ||
72 | #ifndef DEFAULT_ITEM_WIDTH | |
73 | #define DEFAULT_ITEM_WIDTH 100 | |
74 | #endif | |
75 | ||
76 | #if defined(__WXMSW__) | |
77 | #undef wxUSE_POPUPWIN | |
78 | #define wxUSE_POPUPWIN 0 // Popup not working | |
79 | #define TXTCTRL_FLAGS wxNO_BORDER | |
80 | #define CALBORDER 0 | |
81 | #define TXTPOSY 1 | |
82 | #elif defined(__WXGTK__) | |
83 | #define TXTCTRL_FLAGS 0 | |
84 | #define CALBORDER 4 | |
85 | #define TXTPOSY 0 | |
86 | #else | |
87 | #define TXTCTRL_FLAGS 0 | |
88 | #define CALBORDER 4 | |
89 | #define TXTPOSY 0 | |
90 | #endif | |
91 | ||
92 | // ---------------------------------------------------------------------------- | |
93 | // global variables | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | // this should have been a flag in wxDatePickerCtrlGeneric itself but adding it | |
97 | // there now would break backwards compatibility, so put it here as a global: | |
98 | // this shouldn't be a big problem as only one (GUI) thread normally can call | |
99 | // wxDatePickerCtrlGeneric::SetValue() and so it can be only ever used for one | |
100 | // control at a time | |
101 | // | |
102 | // if the value is not NULL, it points to the control which is inside SetValue() | |
103 | static wxDatePickerCtrlGeneric *gs_inSetValue = NULL; | |
104 | ||
105 | // ---------------------------------------------------------------------------- | |
106 | // local classes | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
109 | // This flag indicates that combo box style drop button is to be created | |
110 | #define wxBU_COMBO 0x0400 | |
111 | ||
112 | ||
113 | class wxDropdownButton : public wxBitmapButton | |
114 | { | |
115 | public: | |
116 | wxDropdownButton() { Init(); } | |
117 | wxDropdownButton(wxWindow *parent, | |
118 | wxWindowID id, | |
119 | const wxPoint& pos = wxDefaultPosition, | |
120 | const wxSize& size = wxDefaultSize, | |
121 | long style=0, | |
122 | const wxValidator& validator = wxDefaultValidator); | |
123 | ||
124 | bool Create(wxWindow *parent, | |
125 | wxWindowID id, | |
126 | const wxPoint& pos = wxDefaultPosition, | |
127 | const wxSize& size = wxDefaultSize, | |
128 | long style = 0, | |
129 | const wxValidator& validator = wxDefaultValidator); | |
130 | ||
131 | protected: | |
132 | virtual void DoMoveWindow(int x, int y, int w, int h); | |
133 | ||
134 | void OnSize(wxSizeEvent& event); | |
135 | void OnMouseEnter(wxMouseEvent& event); | |
136 | void OnMouseLeave(wxMouseEvent& event); | |
137 | ||
138 | void RecreateBitmaps(int w, int h); | |
139 | ||
140 | wxBitmap m_bmpNormal; | |
141 | wxBitmap m_bmpHot; | |
142 | ||
143 | int m_borderX, m_borderY; | |
144 | ||
145 | // True if DrawDropArrow should be used instead of DrawComboBoxDropButton | |
146 | bool m_useDropArrow; | |
147 | ||
148 | private: | |
149 | ||
150 | void Init() | |
151 | { | |
152 | m_borderX = -1; | |
153 | m_borderY = -1; | |
154 | } | |
155 | ||
156 | DECLARE_EVENT_TABLE() | |
157 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxDropdownButton) | |
158 | }; | |
159 | ||
160 | ||
161 | // Below, macro DROPBUT_USEDROPARROW should return false when | |
162 | // DrawComboBoxDropButton is to be used to render the entire button. | |
163 | // COMBOST is non-zero if wxBU_COMBO was set. | |
164 | ||
165 | #if defined(__WXMSW__) | |
166 | ||
167 | #define DROPBUT_USEDROPARROW(COMBOST) (COMBOST?false:true) | |
168 | #define DROPBUT_DEFAULT_WIDTH 17 | |
169 | ||
170 | #elif defined(__WXGTK__) | |
171 | ||
172 | #define DROPBUT_USEDROPARROW(COMBOST) true | |
173 | #define DROPBUT_DEFAULT_WIDTH 19 | |
174 | ||
175 | #else | |
176 | ||
177 | #define DROPBUT_USEDROPARROW(COMBOST) true | |
178 | #define DROPBUT_DEFAULT_WIDTH 17 | |
179 | ||
180 | #endif | |
181 | ||
182 | ||
183 | IMPLEMENT_DYNAMIC_CLASS(wxDropdownButton, wxBitmapButton) | |
184 | ||
185 | ||
186 | BEGIN_EVENT_TABLE(wxDropdownButton,wxBitmapButton) | |
187 | EVT_ENTER_WINDOW(wxDropdownButton::OnMouseEnter) | |
188 | EVT_LEAVE_WINDOW(wxDropdownButton::OnMouseLeave) | |
189 | EVT_SIZE(wxDropdownButton::OnSize) | |
190 | END_EVENT_TABLE() | |
191 | ||
192 | ||
193 | wxDropdownButton::wxDropdownButton(wxWindow *parent, | |
194 | wxWindowID id, | |
195 | const wxPoint& pos, | |
196 | const wxSize& size, | |
197 | long style, | |
198 | const wxValidator& validator) | |
199 | { | |
200 | Init(); | |
201 | Create(parent, id, pos, size, style, validator); | |
202 | } | |
203 | ||
204 | ||
205 | bool wxDropdownButton::Create(wxWindow *parent, | |
206 | wxWindowID id, | |
207 | const wxPoint& pos, | |
208 | const wxSize& size, | |
209 | long style, | |
210 | const wxValidator& validator) | |
211 | { | |
212 | m_marginX = 0; | |
213 | m_marginY = 0; | |
214 | ||
215 | m_useDropArrow = DROPBUT_USEDROPARROW(style & wxBU_COMBO); | |
216 | ||
217 | wxBitmap chkBmp(15,15); // arbitrary | |
218 | if ( !wxBitmapButton::Create(parent, id, chkBmp, | |
219 | pos, wxDefaultSize, | |
220 | style | (m_useDropArrow ? wxBU_AUTODRAW : wxNO_BORDER), | |
221 | validator) ) | |
222 | return false; | |
223 | ||
224 | const wxSize sz = GetSize(); | |
225 | int w = chkBmp.GetWidth(), | |
226 | h = chkBmp.GetHeight(); | |
227 | m_borderX = sz.x - m_marginX - w; | |
228 | m_borderY = sz.y - m_marginY - h; | |
229 | ||
230 | DoMoveWindow(pos.x, pos.y, size.x, size.y); | |
231 | ||
232 | return true; | |
233 | } | |
234 | ||
235 | ||
236 | void wxDropdownButton::RecreateBitmaps(int w, int h) | |
237 | { | |
238 | wxMemoryDC dc; | |
239 | ||
240 | int borderX = m_marginX + m_borderX; | |
241 | int borderY = m_marginY + m_borderY; | |
242 | int bw = w - borderX; | |
243 | int bh = h - borderY; | |
244 | ||
245 | wxBitmap bmp(bw, bh); | |
246 | wxBitmap bmpSel(bw, bh); | |
247 | wxRect r(0,0,w,h); | |
248 | ||
249 | wxRendererNative& renderer = wxRendererNative::Get(); | |
250 | ||
251 | dc.SelectObject(bmp); | |
252 | ||
253 | if ( m_useDropArrow ) | |
254 | { | |
255 | // Use DrawDropArrow on transparent background. | |
256 | ||
257 | wxColour magic(255,0,255); | |
258 | wxBrush magicBrush(magic); | |
259 | r.x = -(borderX/2); | |
260 | r.y = -(borderY/2); | |
261 | ||
262 | dc.SetBrush( magicBrush ); | |
263 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
264 | dc.DrawRectangle(0,0,bw,bh); | |
265 | renderer.DrawDropArrow(this, dc, r); | |
266 | dc.SelectObject( wxNullBitmap ); | |
267 | wxMask *mask = new wxMask( bmp, magic ); | |
268 | bmp.SetMask( mask ); | |
269 | ||
270 | dc.SelectObject(bmpSel); | |
271 | ||
272 | dc.SetBrush( magicBrush ); | |
273 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
274 | dc.DrawRectangle(0,0,bw,bh); | |
275 | renderer.DrawDropArrow(this, dc, r, wxCONTROL_PRESSED); | |
276 | dc.SelectObject( wxNullBitmap ); | |
277 | mask = new wxMask( bmpSel, magic ); | |
278 | bmpSel.SetMask( mask ); | |
279 | } | |
280 | else | |
281 | { | |
282 | // Use DrawComboBoxDropButton for the entire button | |
283 | // (also render extra "hot" button state). | |
284 | ||
285 | renderer.DrawComboBoxDropButton(this, dc, r); | |
286 | ||
287 | dc.SelectObject(bmpSel); | |
288 | ||
289 | renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_PRESSED); | |
290 | ||
291 | wxBitmap bmpHot(bw,bh); | |
292 | dc.SelectObject(bmpHot); | |
293 | renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_CURRENT); | |
294 | ||
295 | m_bmpNormal = bmp; | |
296 | m_bmpHot = bmpHot; | |
297 | } | |
298 | ||
299 | SetBitmapLabel(bmp); | |
300 | SetBitmapSelected(bmpSel); | |
301 | } | |
302 | ||
303 | ||
304 | void wxDropdownButton::DoMoveWindow(int x, int y, int w, int h) | |
305 | { | |
306 | if (w < 0) | |
307 | w = DROPBUT_DEFAULT_WIDTH; | |
308 | ||
309 | wxBitmapButton::DoMoveWindow(x, y, w, h); | |
310 | } | |
311 | ||
312 | ||
313 | void wxDropdownButton::OnSize(wxSizeEvent& event) | |
314 | { | |
315 | if ( m_borderX >= 0 && m_borderY >= 0 ) | |
316 | { | |
317 | int w, h; | |
318 | GetClientSize(&w,&h); | |
319 | ||
320 | if ( w > 1 && h > 1 ) | |
321 | RecreateBitmaps(w,h); | |
322 | } | |
323 | event.Skip(); | |
324 | } | |
325 | ||
326 | ||
327 | void wxDropdownButton::OnMouseEnter(wxMouseEvent& event) | |
328 | { | |
329 | if ( !m_useDropArrow ) | |
330 | SetBitmapLabel(m_bmpHot); | |
331 | ||
332 | event.Skip(); | |
333 | } | |
334 | ||
335 | ||
336 | void wxDropdownButton::OnMouseLeave(wxMouseEvent& event) | |
337 | { | |
338 | if ( !m_useDropArrow ) | |
339 | SetBitmapLabel(m_bmpNormal); | |
340 | ||
341 | event.Skip(); | |
342 | } | |
343 | ||
344 | ||
345 | #if wxUSE_POPUPWIN | |
346 | ||
347 | #include "wx/popupwin.h" | |
348 | ||
349 | class wxDatePopupInternal : public wxPopupTransientWindow | |
350 | { | |
351 | public: | |
352 | wxDatePopupInternal(wxWindow *parent) : wxPopupTransientWindow(parent) { } | |
353 | ||
354 | void ShowAt(int x, int y) | |
355 | { | |
356 | Position(wxPoint(x, y), wxSize(0, 0)); | |
357 | Popup(); | |
358 | } | |
359 | ||
360 | void Hide() | |
361 | { | |
362 | Dismiss(); | |
363 | } | |
364 | }; | |
365 | ||
366 | #else // !wxUSE_POPUPWIN | |
367 | ||
368 | class wxDatePopupInternal : public wxDialog | |
369 | { | |
370 | public: | |
371 | wxDatePopupInternal(wxWindow *parent) | |
372 | : wxDialog(parent, | |
373 | wxID_ANY, | |
374 | wxEmptyString, | |
375 | wxDefaultPosition, | |
376 | wxDefaultSize, | |
377 | wxSIMPLE_BORDER) | |
378 | { | |
379 | } | |
380 | ||
381 | void ShowAt(int x, int y) | |
382 | { | |
383 | Show(); | |
384 | Move(x, y); | |
385 | } | |
386 | ||
387 | void Hide() | |
388 | { | |
389 | wxDialog::Hide(); | |
390 | } | |
391 | }; | |
392 | ||
393 | #endif // wxUSE_POPUPWIN/!wxUSE_POPUPWIN | |
394 | ||
395 | // ============================================================================ | |
396 | // wxDatePickerCtrlGeneric implementation | |
397 | // ============================================================================ | |
398 | ||
399 | BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase) | |
400 | EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick) | |
401 | EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText) | |
402 | EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus) | |
403 | EVT_SIZE(wxDatePickerCtrlGeneric::OnSize) | |
404 | END_EVENT_TABLE() | |
405 | ||
406 | #ifndef wxHAS_NATIVE_DATEPICKCTRL | |
407 | IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl) | |
408 | #endif | |
409 | ||
410 | // ---------------------------------------------------------------------------- | |
411 | // creation | |
412 | // ---------------------------------------------------------------------------- | |
413 | ||
414 | bool wxDatePickerCtrlGeneric::Create(wxWindow *parent, | |
415 | wxWindowID id, | |
416 | const wxDateTime& date, | |
417 | const wxPoint& pos, | |
418 | const wxSize& size, | |
419 | long style, | |
420 | const wxValidator& validator, | |
421 | const wxString& name) | |
422 | { | |
423 | wxASSERT_MSG( !(style & wxDP_SPIN), | |
424 | _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") ); | |
425 | ||
426 | if ( !wxControl::Create(parent, id, pos, size, | |
427 | style | wxCLIP_CHILDREN | wxWANTS_CHARS, | |
428 | validator, name) ) | |
429 | ||
430 | { | |
431 | return false; | |
432 | } | |
433 | ||
434 | InheritAttributes(); | |
435 | ||
436 | m_txt = new wxTextCtrl(this, CTRLID_TXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, TXTCTRL_FLAGS); | |
437 | ||
438 | m_txt->Connect(wxEVT_KEY_DOWN, | |
439 | wxKeyEventHandler(wxDatePickerCtrlGeneric::OnEditKey), | |
440 | NULL, this); | |
441 | m_txt->Connect(wxEVT_KILL_FOCUS, | |
442 | wxFocusEventHandler(wxDatePickerCtrlGeneric::OnKillFocus), | |
443 | NULL, this); | |
444 | ||
445 | m_btn = new wxDropdownButton(this, CTRLID_BTN, wxDefaultPosition, wxDefaultSize, wxBU_COMBO); | |
446 | ||
447 | m_popup = new wxDatePopupInternal(this); | |
448 | m_popup->SetFont(GetFont()); | |
449 | ||
450 | wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN, | |
451 | wxPoint(0, 0), wxDefaultSize, | |
452 | wxSUNKEN_BORDER); | |
453 | m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime, | |
454 | wxPoint(0, 0), wxDefaultSize, | |
455 | wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER); | |
456 | m_cal->Connect(wxEVT_CALENDAR_SEL_CHANGED, | |
457 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
458 | NULL, this); | |
459 | m_cal->Connect(wxEVT_KEY_DOWN, | |
460 | wxKeyEventHandler(wxDatePickerCtrlGeneric::OnCalKey), | |
461 | NULL, this); | |
462 | m_cal->Connect(wxEVT_CALENDAR_DOUBLECLICKED, | |
463 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
464 | NULL, this); | |
465 | m_cal->Connect(wxEVT_CALENDAR_DAY_CHANGED, | |
466 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
467 | NULL, this); | |
468 | m_cal->Connect(wxEVT_CALENDAR_MONTH_CHANGED, | |
469 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
470 | NULL, this); | |
471 | m_cal->Connect(wxEVT_CALENDAR_YEAR_CHANGED, | |
472 | wxCalendarEventHandler(wxDatePickerCtrlGeneric::OnSelChange), | |
473 | NULL, this); | |
474 | ||
475 | wxWindow *yearControl = m_cal->GetYearControl(); | |
476 | ||
477 | Connect(wxEVT_SET_FOCUS, | |
478 | wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus)); | |
479 | ||
480 | wxClientDC dc(yearControl); | |
481 | dc.SetFont(yearControl->GetFont()); | |
482 | wxCoord width, dummy; | |
483 | dc.GetTextExtent(wxT("2000"), &width, &dummy); | |
484 | width += ConvertDialogToPixels(wxSize(20, 0)).x; | |
485 | ||
486 | wxSize calSize = m_cal->GetBestSize(); | |
487 | wxSize yearSize = yearControl->GetSize(); | |
488 | yearSize.x = width; | |
489 | ||
490 | wxPoint yearPosition = yearControl->GetPosition(); | |
491 | ||
492 | SetFormat(wxT("%x")); | |
493 | ||
494 | width = yearPosition.x + yearSize.x+2+CALBORDER/2; | |
495 | if (width < calSize.x-4) | |
496 | width = calSize.x-4; | |
497 | ||
498 | int calPos = (width-calSize.x)/2; | |
499 | if (calPos == -1) | |
500 | { | |
501 | calPos = 0; | |
502 | width += 2; | |
503 | } | |
504 | m_cal->SetSize(calPos, 0, calSize.x, calSize.y); | |
505 | yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y, | |
506 | yearSize.x, yearSize.y); | |
507 | m_cal->GetMonthControl()->Move(0, 0); | |
508 | ||
509 | ||
510 | ||
511 | panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER); | |
512 | m_popup->SetClientSize(panel->GetSize()); | |
513 | m_popup->Hide(); | |
514 | ||
515 | SetValue(date.IsValid() ? date : wxDateTime::Today()); | |
516 | ||
517 | SetBestFittingSize(size); | |
518 | ||
519 | SetBackgroundColour(m_txt->GetBackgroundColour()); | |
520 | ||
521 | return true; | |
522 | } | |
523 | ||
524 | ||
525 | void wxDatePickerCtrlGeneric::Init() | |
526 | { | |
527 | m_popup = NULL; | |
528 | m_txt = NULL; | |
529 | m_cal = NULL; | |
530 | m_btn = NULL; | |
531 | ||
532 | m_dropped = false; | |
533 | m_ignoreDrop = false; | |
534 | } | |
535 | ||
536 | wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric() | |
537 | { | |
538 | m_popup = NULL; | |
539 | m_txt = NULL; | |
540 | m_cal = NULL; | |
541 | m_btn = NULL; | |
542 | } | |
543 | ||
544 | bool wxDatePickerCtrlGeneric::Destroy() | |
545 | { | |
546 | if (m_cal) | |
547 | m_cal->Destroy(); | |
548 | if (m_popup) | |
549 | m_popup->Destroy(); | |
550 | if (m_txt) | |
551 | m_txt->Destroy(); | |
552 | if (m_btn) | |
553 | m_btn->Destroy(); | |
554 | ||
555 | m_popup = NULL; | |
556 | m_txt = NULL; | |
557 | m_cal = NULL; | |
558 | m_btn = NULL; | |
559 | ||
560 | return wxControl::Destroy(); | |
561 | } | |
562 | ||
563 | // ---------------------------------------------------------------------------- | |
564 | // overridden base class methods | |
565 | // ---------------------------------------------------------------------------- | |
566 | ||
567 | void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h) | |
568 | { | |
569 | wxControl::DoMoveWindow(x, y, w, h); | |
570 | ||
571 | if (m_dropped) | |
572 | DropDown(false); | |
573 | } | |
574 | ||
575 | wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const | |
576 | { | |
577 | if (m_btn && m_txt) | |
578 | { | |
579 | int bh=m_btn->GetBestSize().y; | |
580 | int eh=m_txt->GetBestSize().y; | |
581 | return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh); | |
582 | } | |
583 | return wxControl::DoGetBestSize(); | |
584 | } | |
585 | ||
586 | ||
587 | bool wxDatePickerCtrlGeneric::Show(bool show) | |
588 | { | |
589 | if ( !wxControl::Show(show) ) | |
590 | { | |
591 | return false; | |
592 | } | |
593 | ||
594 | if ( !show ) | |
595 | { | |
596 | if ( m_popup ) | |
597 | { | |
598 | m_popup->Hide(); | |
599 | m_dropped = false; | |
600 | } | |
601 | } | |
602 | ||
603 | return true; | |
604 | } | |
605 | ||
606 | ||
607 | bool wxDatePickerCtrlGeneric::Enable(bool enable) | |
608 | { | |
609 | if ( !wxControl::Enable(enable) ) | |
610 | { | |
611 | return false; | |
612 | } | |
613 | ||
614 | if ( !enable ) | |
615 | { | |
616 | if ( m_popup ) | |
617 | m_popup->Hide(); | |
618 | } | |
619 | ||
620 | if ( m_btn ) | |
621 | m_btn->Enable(enable); | |
622 | ||
623 | return true; | |
624 | } | |
625 | ||
626 | // ---------------------------------------------------------------------------- | |
627 | // wxDatePickerCtrlGeneric API | |
628 | // ---------------------------------------------------------------------------- | |
629 | ||
630 | bool | |
631 | wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate, | |
632 | const wxDateTime& upperdate) | |
633 | { | |
634 | return m_cal->SetDateRange(lowerdate, upperdate); | |
635 | } | |
636 | ||
637 | bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt) | |
638 | { | |
639 | m_format.clear(); | |
640 | ||
641 | wxDateTime dt; | |
642 | dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d")); | |
643 | wxString str(dt.Format(fmt)); | |
644 | ||
645 | const wxChar *p = str.c_str(); | |
646 | while ( *p ) | |
647 | { | |
648 | int n=wxAtoi(p); | |
649 | if (n == dt.GetDay()) | |
650 | { | |
651 | m_format.Append(wxT("%d")); | |
652 | p += 2; | |
653 | } | |
654 | else if (n == (int)dt.GetMonth()+1) | |
655 | { | |
656 | m_format.Append(wxT("%m")); | |
657 | p += 2; | |
658 | } | |
659 | else if (n == dt.GetYear()) | |
660 | { | |
661 | m_format.Append(wxT("%Y")); | |
662 | p += 4; | |
663 | } | |
664 | else if (n == (dt.GetYear() % 100)) | |
665 | { | |
666 | if (GetWindowStyle() & wxDP_SHOWCENTURY) | |
667 | m_format.Append(wxT("%Y")); | |
668 | else | |
669 | m_format.Append(wxT("%y")); | |
670 | p += 2; | |
671 | } | |
672 | else | |
673 | m_format.Append(*p++); | |
674 | } | |
675 | ||
676 | if ( m_txt ) | |
677 | { | |
678 | wxArrayString allowedChars; | |
679 | for ( wxChar c = _T('0'); c <= _T('9'); c++ ) | |
680 | allowedChars.Add(wxString(c, 1)); | |
681 | ||
682 | const wxChar *p2 = m_format.c_str(); | |
683 | while ( *p2 ) | |
684 | { | |
685 | if ( *p2 == '%') | |
686 | p2 += 2; | |
687 | else | |
688 | allowedChars.Add(wxString(*p2++, 1)); | |
689 | } | |
690 | ||
691 | #if wxUSE_VALIDATORS | |
692 | wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST); | |
693 | tv.SetIncludes(allowedChars); | |
694 | m_txt->SetValidator(tv); | |
695 | #endif | |
696 | ||
697 | if (m_currentDate.IsValid()) | |
698 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
699 | } | |
700 | ||
701 | return true; | |
702 | } | |
703 | ||
704 | ||
705 | wxDateTime wxDatePickerCtrlGeneric::GetValue() const | |
706 | { | |
707 | return m_currentDate; | |
708 | } | |
709 | ||
710 | ||
711 | void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date) | |
712 | { | |
713 | if ( !m_cal ) | |
714 | return; | |
715 | ||
716 | // we need to suppress the event sent from wxTextCtrl as calling our | |
717 | // SetValue() should not result in an event being sent (wxTextCtrl is | |
718 | // an exception to this rule) | |
719 | gs_inSetValue = this; | |
720 | ||
721 | if ( date.IsValid() ) | |
722 | { | |
723 | m_txt->SetValue(date.Format(m_format)); | |
724 | } | |
725 | else // invalid date | |
726 | { | |
727 | wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE), | |
728 | _T("this control must have a valid date") ); | |
729 | ||
730 | m_txt->SetValue(wxEmptyString); | |
731 | } | |
732 | ||
733 | gs_inSetValue = NULL; | |
734 | ||
735 | m_currentDate = date; | |
736 | } | |
737 | ||
738 | ||
739 | bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const | |
740 | { | |
741 | if (dt1) | |
742 | *dt1 = m_cal->GetLowerDateLimit(); | |
743 | if (dt2) | |
744 | *dt2 = m_cal->GetUpperDateLimit(); | |
745 | return true; | |
746 | } | |
747 | ||
748 | ||
749 | void | |
750 | wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2) | |
751 | { | |
752 | m_cal->SetDateRange(dt1, dt2); | |
753 | } | |
754 | ||
755 | // ---------------------------------------------------------------------------- | |
756 | // event handlers | |
757 | // ---------------------------------------------------------------------------- | |
758 | ||
759 | void wxDatePickerCtrlGeneric::DropDown(bool down) | |
760 | { | |
761 | if (m_popup) | |
762 | { | |
763 | if (down) | |
764 | { | |
765 | wxDateTime dt; | |
766 | if (!m_txt->GetValue().empty()) | |
767 | dt.ParseFormat(m_txt->GetValue(), m_format); | |
768 | ||
769 | if (dt.IsValid()) | |
770 | m_cal->SetDate(dt); | |
771 | else | |
772 | m_cal->SetDate(wxDateTime::Today()); | |
773 | ||
774 | wxPoint pos=GetParent()->ClientToScreen(GetPosition()); | |
775 | m_popup->ShowAt(pos.x, pos.y + GetSize().y); | |
776 | m_dropped = true; | |
777 | m_cal->SetFocus(); | |
778 | } | |
779 | else | |
780 | { | |
781 | if (m_dropped) | |
782 | m_popup->Hide(); | |
783 | m_dropped = false; | |
784 | } | |
785 | } | |
786 | } | |
787 | ||
788 | ||
789 | void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event) | |
790 | { | |
791 | if ( m_btn ) | |
792 | { | |
793 | wxSize sz = GetClientSize(); | |
794 | ||
795 | wxSize bs=m_btn->GetSize(); | |
796 | int eh=m_txt->GetBestSize().y; | |
797 | ||
798 | m_txt->SetSize(0, TXTPOSY, sz.x-bs.x, sz.y > eh ? eh-TXTPOSY : sz.y-TXTPOSY); | |
799 | m_btn->SetSize(sz.x - bs.x, 0, bs.x, sz.y); | |
800 | } | |
801 | ||
802 | event.Skip(); | |
803 | } | |
804 | ||
805 | ||
806 | void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev) | |
807 | { | |
808 | ev.Skip(); | |
809 | m_ignoreDrop = false; | |
810 | ||
811 | wxWindow *w=(wxWindow*)ev.GetEventObject(); | |
812 | while (w) | |
813 | { | |
814 | if (w == m_popup) | |
815 | return; | |
816 | w = w->GetParent(); | |
817 | } | |
818 | ||
819 | if (m_dropped) | |
820 | { | |
821 | DropDown(false); | |
822 | if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn) | |
823 | m_ignoreDrop = true; | |
824 | } | |
825 | } | |
826 | ||
827 | ||
828 | void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event)) | |
829 | { | |
830 | if (m_ignoreDrop) | |
831 | { | |
832 | m_ignoreDrop = false; | |
833 | m_txt->SetFocus(); | |
834 | } | |
835 | else | |
836 | { | |
837 | DropDown(); | |
838 | m_cal->SetFocus(); | |
839 | } | |
840 | } | |
841 | ||
842 | ||
843 | void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev)) | |
844 | { | |
845 | if (m_txt) | |
846 | { | |
847 | m_txt->SetFocus(); | |
848 | m_txt->SetSelection(-1, -1); // select everything | |
849 | } | |
850 | } | |
851 | ||
852 | ||
853 | void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev) | |
854 | { | |
855 | if (!m_txt) | |
856 | return; | |
857 | ||
858 | ev.Skip(); | |
859 | ||
860 | wxDateTime dt; | |
861 | dt.ParseFormat(m_txt->GetValue(), m_format); | |
862 | if ( !dt.IsValid() ) | |
863 | { | |
864 | if ( !HasFlag(wxDP_ALLOWNONE) ) | |
865 | dt = m_currentDate; | |
866 | } | |
867 | ||
868 | if(dt.IsValid()) | |
869 | m_txt->SetValue(dt.Format(m_format)); | |
870 | else | |
871 | m_txt->SetValue(wxEmptyString); | |
872 | ||
873 | // notify that we had to change the date after validation | |
874 | if ( (dt.IsValid() && (!m_currentDate.IsValid() || m_currentDate != dt)) || | |
875 | (!dt.IsValid() && m_currentDate.IsValid()) ) | |
876 | { | |
877 | m_currentDate = dt; | |
878 | wxDateEvent event(this, dt, wxEVT_DATE_CHANGED); | |
879 | GetEventHandler()->ProcessEvent(event); | |
880 | } | |
881 | } | |
882 | ||
883 | ||
884 | void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev) | |
885 | { | |
886 | if (m_cal) | |
887 | { | |
888 | m_currentDate = m_cal->GetDate(); | |
889 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
890 | if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED) | |
891 | { | |
892 | DropDown(false); | |
893 | m_txt->SetFocus(); | |
894 | } | |
895 | } | |
896 | ev.SetEventObject(this); | |
897 | ev.SetId(GetId()); | |
898 | GetParent()->ProcessEvent(ev); | |
899 | ||
900 | wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED); | |
901 | GetParent()->ProcessEvent(dev); | |
902 | } | |
903 | ||
904 | ||
905 | void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev) | |
906 | { | |
907 | if ( gs_inSetValue ) | |
908 | { | |
909 | // artificial event resulting from our own SetValue() call, ignore it | |
910 | return; | |
911 | } | |
912 | ||
913 | ev.SetEventObject(this); | |
914 | ev.SetId(GetId()); | |
915 | GetParent()->ProcessEvent(ev); | |
916 | ||
917 | // We'll create an additional event if the date is valid. | |
918 | // If the date isn't valid, the user's probably in the middle of typing | |
919 | wxString txt = m_txt->GetValue(); | |
920 | wxDateTime dt; | |
921 | if (!txt.empty()) | |
922 | { | |
923 | dt.ParseFormat(txt, m_format); | |
924 | if (!dt.IsValid()) | |
925 | return; | |
926 | } | |
927 | ||
928 | wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED); | |
929 | cev.SetEventObject(this); | |
930 | cev.SetId(GetId()); | |
931 | cev.SetDate(dt); | |
932 | ||
933 | GetParent()->ProcessEvent(cev); | |
934 | ||
935 | wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED); | |
936 | GetParent()->ProcessEvent(dev); | |
937 | } | |
938 | ||
939 | ||
940 | void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev) | |
941 | { | |
942 | if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers()) | |
943 | DropDown(true); | |
944 | else | |
945 | ev.Skip(); | |
946 | } | |
947 | ||
948 | ||
949 | void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev) | |
950 | { | |
951 | if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers()) | |
952 | DropDown(false); | |
953 | else | |
954 | ev.Skip(); | |
955 | } | |
956 | ||
957 | #endif // wxUSE_DATEPICKCTRL_GENERIC | |
958 | ||
959 | #endif // wxUSE_DATEPICKCTRL |