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