]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datectlg.cpp
This should make the GTK statictext control wrap
[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
15c86b39 32#if wxUSE_DATEPICKCTRL_GENERIC || !defined(wxHAS_NATIVE_DATEPICKCTRL)
7ae712f5 33
a962d4e0
KH
34#ifndef WX_PRECOMP
35 #include "wx/bmpbuttn.h"
36 #include "wx/dialog.h"
37 #include "wx/dcmemory.h"
38 #include "wx/panel.h"
39 #include "wx/textctrl.h"
40 #include "wx/valtext.h"
41#endif
42
1721a8c0
VZ
43#ifdef wxHAS_NATIVE_DATEPICKCTRL
44 // this header is not included from wx/datectrl.h if we have a native
45 // version, but we do need it here
46 #include "wx/generic/datectrl.h"
930fb29e
VZ
47#else
48 // we need to define _WX_DEFINE_DATE_EVENTS_ before including wx/dateevt.h to
49 // define the event types we use if we're the only date picker control version
50 // being compiled -- otherwise it's defined in the native version implementation
7ae712f5
VZ
51 #define _WX_DEFINE_DATE_EVENTS_
52#endif
53
91edf16c
VZ
54#include "wx/dateevt.h"
55
4b134bb2 56#include "wx/calctrl.h"
4221a0e5 57#include "wx/renderer.h"
39df3acd
VZ
58
59// ----------------------------------------------------------------------------
60// constants
61// ----------------------------------------------------------------------------
62
63enum
64{
65 CTRLID_TXT = 101,
4b134bb2
VZ
66 CTRLID_CAL,
67 CTRLID_BTN,
39df3acd
VZ
68 CTRLID_PAN
69};
70
71#ifndef DEFAULT_ITEM_WIDTH
72 #define DEFAULT_ITEM_WIDTH 100
73#endif
74
caad7637 75#if defined(__WXMSW__)
930fb29e
VZ
76 #undef wxUSE_POPUPWIN
77 #define wxUSE_POPUPWIN 0 // Popup not working
78 #define TXTCTRL_FLAGS wxNO_BORDER
930fb29e 79 #define CALBORDER 0
930fb29e 80 #define TXTPOSY 1
caad7637
JS
81#elif defined(__WXGTK__)
82 #define TXTCTRL_FLAGS 0
83 #define CALBORDER 4
84 #define TXTPOSY 0
930fb29e
VZ
85#else
86 #define TXTCTRL_FLAGS 0
930fb29e 87 #define CALBORDER 4
930fb29e
VZ
88 #define TXTPOSY 0
89#endif
90
91
1721a8c0
VZ
92// ----------------------------------------------------------------------------
93// local classes
94// ----------------------------------------------------------------------------
95
caad7637
JS
96// This flag indicates that combo box style drop button is to be created
97#define wxBU_COMBO 0x0400
98
38511687
VZ
99
100class wxDropdownButton : public wxBitmapButton
101{
102public:
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
94ab4d92 111 bool Create(wxWindow *parent,
38511687
VZ
112 wxWindowID id,
113 const wxPoint& pos = wxDefaultPosition,
114 const wxSize& size = wxDefaultSize,
930fb29e 115 long style = 0,
38511687
VZ
116 const wxValidator& validator = wxDefaultValidator);
117
caad7637 118 virtual void DoMoveWindow(int x, int y, int w, int h);
38511687
VZ
119
120protected:
caad7637
JS
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
136private:
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)
38511687
VZ
146};
147
148
caad7637
JS
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
171IMPLEMENT_DYNAMIC_CLASS(wxDropdownButton, wxBitmapButton)
172
173
174BEGIN_EVENT_TABLE(wxDropdownButton,wxBitmapButton)
175 EVT_ENTER_WINDOW(wxDropdownButton::OnMouseEnter)
176 EVT_LEAVE_WINDOW(wxDropdownButton::OnMouseLeave)
177 EVT_SIZE(wxDropdownButton::OnSize)
178END_EVENT_TABLE()
179
180
38511687
VZ
181wxDropdownButton::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
94ab4d92 193bool wxDropdownButton::Create(wxWindow *parent,
38511687
VZ
194 wxWindowID id,
195 const wxPoint& pos,
196 const wxSize& size,
caad7637 197 long style,
38511687
VZ
198 const wxValidator& validator)
199{
930fb29e
VZ
200 m_marginX = 0;
201 m_marginY = 0;
202
caad7637
JS
203 m_useDropArrow = DROPBUT_USEDROPARROW(style & wxBU_COMBO);
204
38511687 205 wxBitmap chkBmp(15,15); // arbitrary
94ab4d92 206 if ( !wxBitmapButton::Create(parent, id, chkBmp,
caad7637
JS
207 pos, wxDefaultSize,
208 style | (m_useDropArrow ? wxBU_AUTODRAW : wxNO_BORDER),
209 validator) )
94ab4d92 210 return false;
38511687 211
94ab4d92
VZ
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;
38511687 217
caad7637 218 DoMoveWindow(pos.x, pos.y, size.x, size.y);
94ab4d92
VZ
219
220 return true;
38511687
VZ
221}
222
223
caad7637 224void wxDropdownButton::RecreateBitmaps(int w, int h)
38511687 225{
caad7637 226 wxMemoryDC dc;
38511687 227
caad7637
JS
228 int borderX = m_marginX + m_borderX;
229 int borderY = m_marginY + m_borderY;
230 int bw = w - borderX;
231 int bh = h - borderY;
38511687 232
caad7637
JS
233 wxBitmap bmp(bw, bh);
234 wxBitmap bmpSel(bw, bh);
235 wxRect r(0,0,w,h);
38511687 236
caad7637
JS
237 wxRendererNative& renderer = wxRendererNative::Get();
238
239 dc.SelectObject(bmp);
240
241 if ( m_useDropArrow )
242 {
243 // Use DrawDropArrow on transparent background.
f5c479cc 244
83dc6964 245 wxColour magic(255,0,255);
caad7637
JS
246 wxBrush magicBrush(magic);
247 r.x = -(borderX/2);
248 r.y = -(borderY/2);
249
250 dc.SetBrush( magicBrush );
83dc6964
RR
251 dc.SetPen( *wxTRANSPARENT_PEN );
252 dc.DrawRectangle(0,0,bw,bh);
7e24348e 253 renderer.DrawDropArrow(this, dc, r);
caad7637 254 dc.SelectObject( wxNullBitmap );
83dc6964
RR
255 wxMask *mask = new wxMask( bmp, magic );
256 bmp.SetMask( mask );
89448b5d 257
89448b5d 258 dc.SelectObject(bmpSel);
f5c479cc 259
caad7637 260 dc.SetBrush( magicBrush );
83dc6964
RR
261 dc.SetPen( *wxTRANSPARENT_PEN );
262 dc.DrawRectangle(0,0,bw,bh);
7e24348e 263 renderer.DrawDropArrow(this, dc, r, wxCONTROL_PRESSED);
caad7637 264 dc.SelectObject( wxNullBitmap );
83dc6964
RR
265 mask = new wxMask( bmpSel, magic );
266 bmpSel.SetMask( mask );
caad7637
JS
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
89448b5d 277 renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_PRESSED);
caad7637
JS
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;
38511687
VZ
285 }
286
caad7637
JS
287 SetBitmapLabel(bmp);
288 SetBitmapSelected(bmpSel);
289}
290
291
292void wxDropdownButton::DoMoveWindow(int x, int y, int w, int h)
293{
294 if (w < 0)
295 w = DROPBUT_DEFAULT_WIDTH;
296
38511687
VZ
297 wxBitmapButton::DoMoveWindow(x, y, w, h);
298}
299
300
caad7637
JS
301void 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
315void wxDropdownButton::OnMouseEnter(wxMouseEvent& event)
316{
317 if ( !m_useDropArrow )
318 SetBitmapLabel(m_bmpHot);
319
320 event.Skip();
321}
322
323
324void wxDropdownButton::OnMouseLeave(wxMouseEvent& event)
325{
326 if ( !m_useDropArrow )
327 SetBitmapLabel(m_bmpNormal);
328
329 event.Skip();
330}
331
332
1721a8c0
VZ
333#if wxUSE_POPUPWIN
334
335#include "wx/popupwin.h"
336
337class wxDatePopupInternal : public wxPopupTransientWindow
338{
339public:
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
356class wxDatePopupInternal : public wxDialog
357{
358public:
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
39df3acd 383// ============================================================================
7ae712f5 384// wxDatePickerCtrlGeneric implementation
39df3acd
VZ
385// ============================================================================
386
7ae712f5
VZ
387BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase)
388 EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick)
389 EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText)
390 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus)
caad7637 391 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize)
39df3acd
VZ
392END_EVENT_TABLE()
393
b6292ff0 394#ifndef wxHAS_NATIVE_DATEPICKCTRL
94ab4d92 395 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
b6292ff0 396#endif
39df3acd
VZ
397
398// ----------------------------------------------------------------------------
399// creation
400// ----------------------------------------------------------------------------
401
7ae712f5
VZ
402bool wxDatePickerCtrlGeneric::Create(wxWindow *parent,
403 wxWindowID id,
404 const wxDateTime& date,
405 const wxPoint& pos,
406 const wxSize& size,
407 long style,
807f5038 408 const wxValidator& validator,
7ae712f5 409 const wxString& name)
39df3acd 410{
29c86948
VZ
411 wxASSERT_MSG( !(style & wxDP_SPIN),
412 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
413
39df3acd
VZ
414 if ( !wxControl::Create(parent, id, pos, size,
415 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
807f5038 416 validator, name) )
39df3acd
VZ
417
418 {
419 return false;
420 }
421
39df3acd
VZ
422 InheritAttributes();
423
930fb29e 424 m_txt = new wxTextCtrl(this, CTRLID_TXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, TXTCTRL_FLAGS);
38511687 425
b6261304
VZ
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);
39df3acd 432
caad7637 433 m_btn = new wxDropdownButton(this, CTRLID_BTN, wxDefaultPosition, wxDefaultSize, wxBU_COMBO);
930fb29e 434
1721a8c0 435 m_popup = new wxDatePopupInternal(this);
d5ae99f5 436 m_popup->SetFont(GetFont());
39df3acd 437
d5ae99f5 438 wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN,
1721a8c0 439 wxPoint(0, 0), wxDefaultSize,
4b134bb2
VZ
440 wxSUNKEN_BORDER);
441 m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime,
1721a8c0 442 wxPoint(0, 0), wxDefaultSize,
4b134bb2 443 wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER);
b6261304
VZ
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);
39df3acd
VZ
462
463 wxWindow *yearControl = m_cal->GetYearControl();
464
b6261304
VZ
465 Connect(wxEVT_SET_FOCUS,
466 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus));
39df3acd
VZ
467
468 wxClientDC dc(yearControl);
9d9e32a0 469 dc.SetFont(yearControl->GetFont());
39df3acd
VZ
470 wxCoord width, dummy;
471 dc.GetTextExtent(wxT("2000"), &width, &dummy);
1721a8c0 472 width += ConvertDialogToPixels(wxSize(20, 0)).x;
39df3acd
VZ
473
474 wxSize calSize = m_cal->GetBestSize();
475 wxSize yearSize = yearControl->GetSize();
476 yearSize.x = width;
477
478 wxPoint yearPosition = yearControl->GetPosition();
479
d5ae99f5
VZ
480 SetFormat(wxT("%x"));
481
39df3acd
VZ
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);
4b134bb2
VZ
493 yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y,
494 yearSize.x, yearSize.y);
39df3acd
VZ
495 m_cal->GetMonthControl()->Move(0, 0);
496
497
498
499 panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER);
d5ae99f5
VZ
500 m_popup->SetClientSize(panel->GetSize());
501 m_popup->Hide();
39df3acd 502
964f23b7 503 SetValue(date.IsValid() ? date : wxDateTime::Today());
1721a8c0 504
d8b49350 505 SetBestFittingSize(size);
930fb29e 506
caad7637
JS
507 SetBackgroundColour(m_txt->GetBackgroundColour());
508
3a0c6181 509 return true;
39df3acd
VZ
510}
511
512
7ae712f5 513void wxDatePickerCtrlGeneric::Init()
39df3acd 514{
d5ae99f5 515 m_popup = NULL;
39df3acd
VZ
516 m_txt = NULL;
517 m_cal = NULL;
518 m_btn = NULL;
519
520 m_dropped = false;
521 m_ignoreDrop = false;
522}
523
a9d13e15
JS
524wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
525{
526 m_popup = NULL;
527 m_txt = NULL;
528 m_cal = NULL;
529 m_btn = NULL;
530}
39df3acd 531
7ae712f5 532bool wxDatePickerCtrlGeneric::Destroy()
39df3acd
VZ
533{
534 if (m_cal)
535 m_cal->Destroy();
d5ae99f5
VZ
536 if (m_popup)
537 m_popup->Destroy();
39df3acd
VZ
538 if (m_txt)
539 m_txt->Destroy();
540 if (m_btn)
541 m_btn->Destroy();
542
d5ae99f5 543 m_popup = NULL;
39df3acd
VZ
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
7ae712f5 555void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h)
39df3acd
VZ
556{
557 wxControl::DoMoveWindow(x, y, w, h);
39df3acd
VZ
558
559 if (m_dropped)
1721a8c0 560 DropDown(true);
39df3acd
VZ
561}
562
7ae712f5 563wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const
39df3acd
VZ
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
7ae712f5 571bool wxDatePickerCtrlGeneric::Show(bool show)
39df3acd
VZ
572{
573 if ( !wxControl::Show(show) )
574 {
3a0c6181 575 return false;
39df3acd
VZ
576 }
577
1721a8c0 578 if ( !show )
39df3acd 579 {
1721a8c0 580 if ( m_popup )
39df3acd 581 {
d5ae99f5 582 m_popup->Hide();
39df3acd
VZ
583 m_dropped = false;
584 }
585 }
586
3a0c6181 587 return true;
39df3acd
VZ
588}
589
590
7ae712f5 591bool wxDatePickerCtrlGeneric::Enable(bool enable)
39df3acd
VZ
592{
593 if ( !wxControl::Enable(enable) )
594 {
3a0c6181 595 return false;
39df3acd
VZ
596 }
597
1721a8c0 598 if ( !enable )
39df3acd 599 {
2eda91db
VZ
600 if ( m_popup )
601 m_popup->Hide();
39df3acd 602 }
1721a8c0
VZ
603
604 if ( m_btn )
39df3acd 605 m_btn->Enable(enable);
1721a8c0 606
3a0c6181 607 return true;
39df3acd
VZ
608}
609
610// ----------------------------------------------------------------------------
7ae712f5 611// wxDatePickerCtrlGeneric API
39df3acd
VZ
612// ----------------------------------------------------------------------------
613
4b134bb2 614bool
7ae712f5
VZ
615wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate,
616 const wxDateTime& upperdate)
4b134bb2
VZ
617{
618 return m_cal->SetDateRange(lowerdate, upperdate);
619}
620
7ae712f5 621bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt)
39df3acd
VZ
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 }
d5ae99f5
VZ
648 else if (n == (dt.GetYear() % 100))
649 {
1721a8c0
VZ
650 if (GetWindowStyle() & wxDP_SHOWCENTURY)
651 m_format.Append(wxT("%Y"));
652 else
653 m_format.Append(wxT("%y"));
d5ae99f5
VZ
654 p += 2;
655 }
39df3acd
VZ
656 else
657 m_format.Append(*p++);
658 }
659
660 if (m_txt)
661 {
1721a8c0
VZ
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();
39df3acd
VZ
667 while (*p)
668 {
669 if (*p == '%')
670 p += 2;
671 else
1721a8c0 672 allowedChars.Add(wxString(*p++, 1));
39df3acd 673 }
1721a8c0 674
f5c479cc 675#if wxUSE_VALIDATORS
39df3acd 676 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
1721a8c0 677 tv.SetIncludes(allowedChars);
39df3acd 678 m_txt->SetValidator(tv);
f5c479cc 679#endif
d5ae99f5 680
1721a8c0
VZ
681 if (m_currentDate.IsValid())
682 m_txt->SetValue(m_currentDate.Format(m_format));
39df3acd 683 }
1721a8c0 684
39df3acd
VZ
685 return true;
686}
687
688
7ae712f5 689wxDateTime wxDatePickerCtrlGeneric::GetValue() const
39df3acd 690{
1721a8c0 691 return m_currentDate;
39df3acd
VZ
692}
693
694
7ae712f5 695void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date)
39df3acd
VZ
696{
697 if (m_cal)
698 {
699 if (date.IsValid())
d5ae99f5 700 m_txt->SetValue(date.Format(m_format));
39df3acd 701 else
1721a8c0
VZ
702 {
703 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE),
704 _T("this control must have a valid date") );
705
39df3acd 706 m_txt->SetValue(wxEmptyString);
1721a8c0
VZ
707 }
708
709 m_currentDate = date;
39df3acd
VZ
710 }
711}
712
713
7ae712f5 714bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
39df3acd
VZ
715{
716 if (dt1)
4b134bb2 717 *dt1 = m_cal->GetLowerDateLimit();
39df3acd 718 if (dt1)
4b134bb2 719 *dt2 = m_cal->GetUpperDateLimit();
39df3acd
VZ
720 return true;
721}
722
723
7ae712f5
VZ
724void
725wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
39df3acd 726{
4b134bb2 727 m_cal->SetDateRange(dt1, dt2);
39df3acd
VZ
728}
729
730// ----------------------------------------------------------------------------
731// event handlers
732// ----------------------------------------------------------------------------
733
7ae712f5 734void wxDatePickerCtrlGeneric::DropDown(bool down)
39df3acd 735{
d5ae99f5 736 if (m_popup)
39df3acd
VZ
737 {
738 if (down)
739 {
d5ae99f5 740 wxDateTime dt;
a7c58211 741 if (!m_txt->GetValue().empty())
39df3acd 742 dt.ParseFormat(m_txt->GetValue(), m_format);
d5ae99f5
VZ
743
744 if (dt.IsValid())
39df3acd 745 m_cal->SetDate(dt);
d5ae99f5
VZ
746 else
747 m_cal->SetDate(wxDateTime::Today());
39df3acd 748
d5ae99f5 749 wxPoint pos=GetParent()->ClientToScreen(GetPosition());
1721a8c0 750 m_popup->ShowAt(pos.x, pos.y + GetSize().y);
39df3acd 751 m_dropped = true;
1721a8c0 752 m_cal->SetFocus();
39df3acd
VZ
753 }
754 else
755 {
756 if (m_dropped)
d5ae99f5 757 m_popup->Hide();
39df3acd
VZ
758 m_dropped = false;
759 }
760 }
761}
762
763
caad7637
JS
764void 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
7ae712f5 781void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev)
39df3acd
VZ
782{
783 ev.Skip();
784 m_ignoreDrop = false;
785
786 wxWindow *w=(wxWindow*)ev.GetEventObject();
787 while (w)
788 {
d5ae99f5 789 if (w == m_popup)
39df3acd
VZ
790 return;
791 w = w->GetParent();
792 }
793
794 if (m_dropped)
795 {
796 DropDown(false);
caad7637 797 if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn)
39df3acd
VZ
798 m_ignoreDrop = true;
799 }
800}
801
802
b6292ff0 803void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event))
39df3acd
VZ
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
b6292ff0 818void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev))
39df3acd
VZ
819{
820 if (m_txt)
821 {
822 m_txt->SetFocus();
1721a8c0 823 m_txt->SetSelection(-1, -1); // select everything
39df3acd
VZ
824 }
825}
826
827
7ae712f5 828void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev)
39df3acd 829{
a9d13e15
JS
830 if (!m_txt)
831 return;
832
39df3acd
VZ
833 ev.Skip();
834
835 wxDateTime dt;
836 dt.ParseFormat(m_txt->GetValue(), m_format);
2436cac7 837 if ( !dt.IsValid() )
1721a8c0
VZ
838 {
839 if ( !HasFlag(wxDP_ALLOWNONE) )
840 dt = m_currentDate;
841 }
842
a7c58211
WS
843 if(dt.IsValid())
844 m_txt->SetValue(dt.Format(m_format));
845 else
846 m_txt->SetValue(wxEmptyString);
1721a8c0
VZ
847
848 // notify that we had to change the date after validation
2436cac7
VZ
849 if ( (dt.IsValid() && m_currentDate != dt) ||
850 (!dt.IsValid() && m_currentDate.IsValid()) )
1721a8c0
VZ
851 {
852 m_currentDate = dt;
853 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
854 GetEventHandler()->ProcessEvent(event);
855 }
39df3acd
VZ
856}
857
858
7ae712f5 859void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev)
39df3acd
VZ
860{
861 if (m_cal)
862 {
1721a8c0
VZ
863 m_currentDate = m_cal->GetDate();
864 m_txt->SetValue(m_currentDate.Format(m_format));
39df3acd
VZ
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);
1721a8c0
VZ
874
875 wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED);
876 GetParent()->ProcessEvent(dev);
39df3acd
VZ
877}
878
879
7ae712f5 880void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev)
39df3acd
VZ
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.
1721a8c0 887 // If the date isn't valid, the user's probably in the middle of typing
a7c58211 888 wxString txt = m_txt->GetValue();
39df3acd 889 wxDateTime dt;
a7c58211 890 if (!txt.empty())
39df3acd
VZ
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);
1721a8c0
VZ
903
904 wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED);
905 GetParent()->ProcessEvent(dev);
39df3acd
VZ
906}
907
908
7ae712f5 909void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev)
39df3acd
VZ
910{
911 if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers())
1721a8c0 912 DropDown(true);
39df3acd
VZ
913 else
914 ev.Skip();
915}
916
917
7ae712f5 918void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev)
39df3acd
VZ
919{
920 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
921 DropDown(false);
922 else
923 ev.Skip();
924}
925
7ae712f5
VZ
926#endif // wxUSE_DATEPICKCTRL_GENERIC
927
928#endif // wxUSE_DATEPICKCTRL
929