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