]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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(true); | |
573 | } | |
574 | ||
575 | wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const | |
576 | { | |
577 | int bh=m_btn->GetBestSize().y; | |
578 | int eh=m_txt->GetBestSize().y; | |
579 | return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh); | |
580 | } | |
581 | ||
582 | ||
583 | bool wxDatePickerCtrlGeneric::Show(bool show) | |
584 | { | |
585 | if ( !wxControl::Show(show) ) | |
586 | { | |
587 | return false; | |
588 | } | |
589 | ||
590 | if ( !show ) | |
591 | { | |
592 | if ( m_popup ) | |
593 | { | |
594 | m_popup->Hide(); | |
595 | m_dropped = false; | |
596 | } | |
597 | } | |
598 | ||
599 | return true; | |
600 | } | |
601 | ||
602 | ||
603 | bool wxDatePickerCtrlGeneric::Enable(bool enable) | |
604 | { | |
605 | if ( !wxControl::Enable(enable) ) | |
606 | { | |
607 | return false; | |
608 | } | |
609 | ||
610 | if ( !enable ) | |
611 | { | |
612 | if ( m_popup ) | |
613 | m_popup->Hide(); | |
614 | } | |
615 | ||
616 | if ( m_btn ) | |
617 | m_btn->Enable(enable); | |
618 | ||
619 | return true; | |
620 | } | |
621 | ||
622 | // ---------------------------------------------------------------------------- | |
623 | // wxDatePickerCtrlGeneric API | |
624 | // ---------------------------------------------------------------------------- | |
625 | ||
626 | bool | |
627 | wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate, | |
628 | const wxDateTime& upperdate) | |
629 | { | |
630 | return m_cal->SetDateRange(lowerdate, upperdate); | |
631 | } | |
632 | ||
633 | bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt) | |
634 | { | |
635 | m_format.clear(); | |
636 | ||
637 | wxDateTime dt; | |
638 | dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d")); | |
639 | wxString str(dt.Format(fmt)); | |
640 | ||
641 | const wxChar *p = str.c_str(); | |
642 | while ( *p ) | |
643 | { | |
644 | int n=wxAtoi(p); | |
645 | if (n == dt.GetDay()) | |
646 | { | |
647 | m_format.Append(wxT("%d")); | |
648 | p += 2; | |
649 | } | |
650 | else if (n == (int)dt.GetMonth()+1) | |
651 | { | |
652 | m_format.Append(wxT("%m")); | |
653 | p += 2; | |
654 | } | |
655 | else if (n == dt.GetYear()) | |
656 | { | |
657 | m_format.Append(wxT("%Y")); | |
658 | p += 4; | |
659 | } | |
660 | else if (n == (dt.GetYear() % 100)) | |
661 | { | |
662 | if (GetWindowStyle() & wxDP_SHOWCENTURY) | |
663 | m_format.Append(wxT("%Y")); | |
664 | else | |
665 | m_format.Append(wxT("%y")); | |
666 | p += 2; | |
667 | } | |
668 | else | |
669 | m_format.Append(*p++); | |
670 | } | |
671 | ||
672 | if ( m_txt ) | |
673 | { | |
674 | wxArrayString allowedChars; | |
675 | for ( wxChar c = _T('0'); c <= _T('9'); c++ ) | |
676 | allowedChars.Add(wxString(c, 1)); | |
677 | ||
678 | const wxChar *p2 = m_format.c_str(); | |
679 | while ( *p2 ) | |
680 | { | |
681 | if ( *p2 == '%') | |
682 | p2 += 2; | |
683 | else | |
684 | allowedChars.Add(wxString(*p2++, 1)); | |
685 | } | |
686 | ||
687 | #if wxUSE_VALIDATORS | |
688 | wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST); | |
689 | tv.SetIncludes(allowedChars); | |
690 | m_txt->SetValidator(tv); | |
691 | #endif | |
692 | ||
693 | if (m_currentDate.IsValid()) | |
694 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
695 | } | |
696 | ||
697 | return true; | |
698 | } | |
699 | ||
700 | ||
701 | wxDateTime wxDatePickerCtrlGeneric::GetValue() const | |
702 | { | |
703 | return m_currentDate; | |
704 | } | |
705 | ||
706 | ||
707 | void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date) | |
708 | { | |
709 | if ( !m_cal ) | |
710 | return; | |
711 | ||
712 | // we need to suppress the event sent from wxTextCtrl as calling our | |
713 | // SetValue() should not result in an event being sent (wxTextCtrl is | |
714 | // an exception to this rule) | |
715 | gs_inSetValue = this; | |
716 | ||
717 | if ( date.IsValid() ) | |
718 | { | |
719 | m_txt->SetValue(date.Format(m_format)); | |
720 | } | |
721 | else // invalid date | |
722 | { | |
723 | wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE), | |
724 | _T("this control must have a valid date") ); | |
725 | ||
726 | m_txt->SetValue(wxEmptyString); | |
727 | } | |
728 | ||
729 | gs_inSetValue = NULL; | |
730 | ||
731 | m_currentDate = date; | |
732 | } | |
733 | ||
734 | ||
735 | bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const | |
736 | { | |
737 | if (dt1) | |
738 | *dt1 = m_cal->GetLowerDateLimit(); | |
739 | if (dt1) | |
740 | *dt2 = m_cal->GetUpperDateLimit(); | |
741 | return true; | |
742 | } | |
743 | ||
744 | ||
745 | void | |
746 | wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2) | |
747 | { | |
748 | m_cal->SetDateRange(dt1, dt2); | |
749 | } | |
750 | ||
751 | // ---------------------------------------------------------------------------- | |
752 | // event handlers | |
753 | // ---------------------------------------------------------------------------- | |
754 | ||
755 | void wxDatePickerCtrlGeneric::DropDown(bool down) | |
756 | { | |
757 | if (m_popup) | |
758 | { | |
759 | if (down) | |
760 | { | |
761 | wxDateTime dt; | |
762 | if (!m_txt->GetValue().empty()) | |
763 | dt.ParseFormat(m_txt->GetValue(), m_format); | |
764 | ||
765 | if (dt.IsValid()) | |
766 | m_cal->SetDate(dt); | |
767 | else | |
768 | m_cal->SetDate(wxDateTime::Today()); | |
769 | ||
770 | wxPoint pos=GetParent()->ClientToScreen(GetPosition()); | |
771 | m_popup->ShowAt(pos.x, pos.y + GetSize().y); | |
772 | m_dropped = true; | |
773 | m_cal->SetFocus(); | |
774 | } | |
775 | else | |
776 | { | |
777 | if (m_dropped) | |
778 | m_popup->Hide(); | |
779 | m_dropped = false; | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | ||
785 | void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event) | |
786 | { | |
787 | if ( m_btn ) | |
788 | { | |
789 | wxSize sz = GetClientSize(); | |
790 | ||
791 | wxSize bs=m_btn->GetSize(); | |
792 | int eh=m_txt->GetBestSize().y; | |
793 | ||
794 | m_txt->SetSize(0, TXTPOSY, sz.x-bs.x, sz.y > eh ? eh-TXTPOSY : sz.y-TXTPOSY); | |
795 | m_btn->SetSize(sz.x - bs.x, 0, bs.x, sz.y); | |
796 | } | |
797 | ||
798 | event.Skip(); | |
799 | } | |
800 | ||
801 | ||
802 | void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev) | |
803 | { | |
804 | ev.Skip(); | |
805 | m_ignoreDrop = false; | |
806 | ||
807 | wxWindow *w=(wxWindow*)ev.GetEventObject(); | |
808 | while (w) | |
809 | { | |
810 | if (w == m_popup) | |
811 | return; | |
812 | w = w->GetParent(); | |
813 | } | |
814 | ||
815 | if (m_dropped) | |
816 | { | |
817 | DropDown(false); | |
818 | if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn) | |
819 | m_ignoreDrop = true; | |
820 | } | |
821 | } | |
822 | ||
823 | ||
824 | void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event)) | |
825 | { | |
826 | if (m_ignoreDrop) | |
827 | { | |
828 | m_ignoreDrop = false; | |
829 | m_txt->SetFocus(); | |
830 | } | |
831 | else | |
832 | { | |
833 | DropDown(); | |
834 | m_cal->SetFocus(); | |
835 | } | |
836 | } | |
837 | ||
838 | ||
839 | void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev)) | |
840 | { | |
841 | if (m_txt) | |
842 | { | |
843 | m_txt->SetFocus(); | |
844 | m_txt->SetSelection(-1, -1); // select everything | |
845 | } | |
846 | } | |
847 | ||
848 | ||
849 | void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev) | |
850 | { | |
851 | if (!m_txt) | |
852 | return; | |
853 | ||
854 | ev.Skip(); | |
855 | ||
856 | wxDateTime dt; | |
857 | dt.ParseFormat(m_txt->GetValue(), m_format); | |
858 | if ( !dt.IsValid() ) | |
859 | { | |
860 | if ( !HasFlag(wxDP_ALLOWNONE) ) | |
861 | dt = m_currentDate; | |
862 | } | |
863 | ||
864 | if(dt.IsValid()) | |
865 | m_txt->SetValue(dt.Format(m_format)); | |
866 | else | |
867 | m_txt->SetValue(wxEmptyString); | |
868 | ||
869 | // notify that we had to change the date after validation | |
870 | if ( (dt.IsValid() && (!m_currentDate.IsValid() || m_currentDate != dt)) || | |
871 | (!dt.IsValid() && m_currentDate.IsValid()) ) | |
872 | { | |
873 | m_currentDate = dt; | |
874 | wxDateEvent event(this, dt, wxEVT_DATE_CHANGED); | |
875 | GetEventHandler()->ProcessEvent(event); | |
876 | } | |
877 | } | |
878 | ||
879 | ||
880 | void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev) | |
881 | { | |
882 | if (m_cal) | |
883 | { | |
884 | m_currentDate = m_cal->GetDate(); | |
885 | m_txt->SetValue(m_currentDate.Format(m_format)); | |
886 | if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED) | |
887 | { | |
888 | DropDown(false); | |
889 | m_txt->SetFocus(); | |
890 | } | |
891 | } | |
892 | ev.SetEventObject(this); | |
893 | ev.SetId(GetId()); | |
894 | GetParent()->ProcessEvent(ev); | |
895 | ||
896 | wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED); | |
897 | GetParent()->ProcessEvent(dev); | |
898 | } | |
899 | ||
900 | ||
901 | void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev) | |
902 | { | |
903 | if ( gs_inSetValue ) | |
904 | { | |
905 | // artificial event resulting from our own SetValue() call, ignore it | |
906 | return; | |
907 | } | |
908 | ||
909 | ev.SetEventObject(this); | |
910 | ev.SetId(GetId()); | |
911 | GetParent()->ProcessEvent(ev); | |
912 | ||
913 | // We'll create an additional event if the date is valid. | |
914 | // If the date isn't valid, the user's probably in the middle of typing | |
915 | wxString txt = m_txt->GetValue(); | |
916 | wxDateTime dt; | |
917 | if (!txt.empty()) | |
918 | { | |
919 | dt.ParseFormat(txt, m_format); | |
920 | if (!dt.IsValid()) | |
921 | return; | |
922 | } | |
923 | ||
924 | wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED); | |
925 | cev.SetEventObject(this); | |
926 | cev.SetId(GetId()); | |
927 | cev.SetDate(dt); | |
928 | ||
929 | GetParent()->ProcessEvent(cev); | |
930 | ||
931 | wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED); | |
932 | GetParent()->ProcessEvent(dev); | |
933 | } | |
934 | ||
935 | ||
936 | void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev) | |
937 | { | |
938 | if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers()) | |
939 | DropDown(true); | |
940 | else | |
941 | ev.Skip(); | |
942 | } | |
943 | ||
944 | ||
945 | void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev) | |
946 | { | |
947 | if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers()) | |
948 | DropDown(false); | |
949 | else | |
950 | ev.Skip(); | |
951 | } | |
952 | ||
953 | #endif // wxUSE_DATEPICKCTRL_GENERIC | |
954 | ||
955 | #endif // wxUSE_DATEPICKCTRL | |
956 |