]> git.saurik.com Git - wxWidgets.git/blame - src/generic/datectlg.cpp
only change the bitmap size for the borderless controls
[wxWidgets.git] / src / generic / datectlg.cpp
CommitLineData
39df3acd 1/////////////////////////////////////////////////////////////////////////////
68f2155b 2// Name: src/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
38511687 131protected:
6f02a879 132 virtual void DoMoveWindow(int x, int y, int w, int h);
caad7637
JS
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
148private:
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)
38511687
VZ
158};
159
160
caad7637
JS
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
183IMPLEMENT_DYNAMIC_CLASS(wxDropdownButton, wxBitmapButton)
184
185
186BEGIN_EVENT_TABLE(wxDropdownButton,wxBitmapButton)
187 EVT_ENTER_WINDOW(wxDropdownButton::OnMouseEnter)
188 EVT_LEAVE_WINDOW(wxDropdownButton::OnMouseLeave)
189 EVT_SIZE(wxDropdownButton::OnSize)
190END_EVENT_TABLE()
191
192
38511687
VZ
193wxDropdownButton::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
94ab4d92 205bool wxDropdownButton::Create(wxWindow *parent,
38511687
VZ
206 wxWindowID id,
207 const wxPoint& pos,
208 const wxSize& size,
caad7637 209 long style,
38511687
VZ
210 const wxValidator& validator)
211{
930fb29e
VZ
212 m_marginX = 0;
213 m_marginY = 0;
214
caad7637
JS
215 m_useDropArrow = DROPBUT_USEDROPARROW(style & wxBU_COMBO);
216
38511687 217 wxBitmap chkBmp(15,15); // arbitrary
94ab4d92 218 if ( !wxBitmapButton::Create(parent, id, chkBmp,
caad7637
JS
219 pos, wxDefaultSize,
220 style | (m_useDropArrow ? wxBU_AUTODRAW : wxNO_BORDER),
221 validator) )
94ab4d92 222 return false;
38511687 223
94ab4d92
VZ
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;
38511687 229
caad7637 230 DoMoveWindow(pos.x, pos.y, size.x, size.y);
94ab4d92
VZ
231
232 return true;
38511687
VZ
233}
234
235
caad7637 236void wxDropdownButton::RecreateBitmaps(int w, int h)
38511687 237{
caad7637 238 wxMemoryDC dc;
38511687 239
caad7637
JS
240 int borderX = m_marginX + m_borderX;
241 int borderY = m_marginY + m_borderY;
242 int bw = w - borderX;
243 int bh = h - borderY;
38511687 244
caad7637
JS
245 wxBitmap bmp(bw, bh);
246 wxBitmap bmpSel(bw, bh);
247 wxRect r(0,0,w,h);
38511687 248
caad7637
JS
249 wxRendererNative& renderer = wxRendererNative::Get();
250
251 dc.SelectObject(bmp);
252
253 if ( m_useDropArrow )
254 {
255 // Use DrawDropArrow on transparent background.
f5c479cc 256
83dc6964 257 wxColour magic(255,0,255);
caad7637
JS
258 wxBrush magicBrush(magic);
259 r.x = -(borderX/2);
260 r.y = -(borderY/2);
261
262 dc.SetBrush( magicBrush );
83dc6964
RR
263 dc.SetPen( *wxTRANSPARENT_PEN );
264 dc.DrawRectangle(0,0,bw,bh);
7e24348e 265 renderer.DrawDropArrow(this, dc, r);
caad7637 266 dc.SelectObject( wxNullBitmap );
83dc6964
RR
267 wxMask *mask = new wxMask( bmp, magic );
268 bmp.SetMask( mask );
89448b5d 269
89448b5d 270 dc.SelectObject(bmpSel);
f5c479cc 271
caad7637 272 dc.SetBrush( magicBrush );
83dc6964
RR
273 dc.SetPen( *wxTRANSPARENT_PEN );
274 dc.DrawRectangle(0,0,bw,bh);
7e24348e 275 renderer.DrawDropArrow(this, dc, r, wxCONTROL_PRESSED);
caad7637 276 dc.SelectObject( wxNullBitmap );
83dc6964
RR
277 mask = new wxMask( bmpSel, magic );
278 bmpSel.SetMask( mask );
caad7637
JS
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
89448b5d 289 renderer.DrawComboBoxDropButton(this, dc, r, wxCONTROL_PRESSED);
caad7637
JS
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;
38511687
VZ
297 }
298
caad7637
JS
299 SetBitmapLabel(bmp);
300 SetBitmapSelected(bmpSel);
301}
302
303
304void wxDropdownButton::DoMoveWindow(int x, int y, int w, int h)
305{
306 if (w < 0)
307 w = DROPBUT_DEFAULT_WIDTH;
308
38511687
VZ
309 wxBitmapButton::DoMoveWindow(x, y, w, h);
310}
311
312
caad7637
JS
313void 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
327void wxDropdownButton::OnMouseEnter(wxMouseEvent& event)
328{
329 if ( !m_useDropArrow )
330 SetBitmapLabel(m_bmpHot);
331
332 event.Skip();
333}
334
335
336void wxDropdownButton::OnMouseLeave(wxMouseEvent& event)
337{
338 if ( !m_useDropArrow )
339 SetBitmapLabel(m_bmpNormal);
340
341 event.Skip();
342}
343
344
1721a8c0
VZ
345#if wxUSE_POPUPWIN
346
347#include "wx/popupwin.h"
348
349class wxDatePopupInternal : public wxPopupTransientWindow
350{
351public:
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
368class wxDatePopupInternal : public wxDialog
369{
370public:
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
39df3acd 395// ============================================================================
7ae712f5 396// wxDatePickerCtrlGeneric implementation
39df3acd
VZ
397// ============================================================================
398
7ae712f5
VZ
399BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase)
400 EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrlGeneric::OnClick)
401 EVT_TEXT(CTRLID_TXT, wxDatePickerCtrlGeneric::OnText)
402 EVT_CHILD_FOCUS(wxDatePickerCtrlGeneric::OnChildSetFocus)
caad7637 403 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize)
39df3acd
VZ
404END_EVENT_TABLE()
405
b6292ff0 406#ifndef wxHAS_NATIVE_DATEPICKCTRL
94ab4d92 407 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
b6292ff0 408#endif
39df3acd
VZ
409
410// ----------------------------------------------------------------------------
411// creation
412// ----------------------------------------------------------------------------
413
7ae712f5
VZ
414bool wxDatePickerCtrlGeneric::Create(wxWindow *parent,
415 wxWindowID id,
416 const wxDateTime& date,
417 const wxPoint& pos,
418 const wxSize& size,
419 long style,
807f5038 420 const wxValidator& validator,
7ae712f5 421 const wxString& name)
39df3acd 422{
29c86948
VZ
423 wxASSERT_MSG( !(style & wxDP_SPIN),
424 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
425
39df3acd
VZ
426 if ( !wxControl::Create(parent, id, pos, size,
427 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
807f5038 428 validator, name) )
39df3acd
VZ
429
430 {
431 return false;
432 }
433
39df3acd
VZ
434 InheritAttributes();
435
930fb29e 436 m_txt = new wxTextCtrl(this, CTRLID_TXT, wxEmptyString, wxDefaultPosition, wxDefaultSize, TXTCTRL_FLAGS);
38511687 437
b6261304
VZ
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);
39df3acd 444
caad7637 445 m_btn = new wxDropdownButton(this, CTRLID_BTN, wxDefaultPosition, wxDefaultSize, wxBU_COMBO);
930fb29e 446
1721a8c0 447 m_popup = new wxDatePopupInternal(this);
d5ae99f5 448 m_popup->SetFont(GetFont());
39df3acd 449
d5ae99f5 450 wxPanel *panel=new wxPanel(m_popup, CTRLID_PAN,
1721a8c0 451 wxPoint(0, 0), wxDefaultSize,
4b134bb2
VZ
452 wxSUNKEN_BORDER);
453 m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime,
1721a8c0 454 wxPoint(0, 0), wxDefaultSize,
4b134bb2 455 wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER);
b6261304
VZ
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);
39df3acd
VZ
474
475 wxWindow *yearControl = m_cal->GetYearControl();
476
b6261304
VZ
477 Connect(wxEVT_SET_FOCUS,
478 wxFocusEventHandler(wxDatePickerCtrlGeneric::OnSetFocus));
39df3acd
VZ
479
480 wxClientDC dc(yearControl);
9d9e32a0 481 dc.SetFont(yearControl->GetFont());
39df3acd
VZ
482 wxCoord width, dummy;
483 dc.GetTextExtent(wxT("2000"), &width, &dummy);
1721a8c0 484 width += ConvertDialogToPixels(wxSize(20, 0)).x;
39df3acd
VZ
485
486 wxSize calSize = m_cal->GetBestSize();
487 wxSize yearSize = yearControl->GetSize();
488 yearSize.x = width;
489
490 wxPoint yearPosition = yearControl->GetPosition();
491
d5ae99f5
VZ
492 SetFormat(wxT("%x"));
493
39df3acd
VZ
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);
4b134bb2
VZ
505 yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y,
506 yearSize.x, yearSize.y);
39df3acd
VZ
507 m_cal->GetMonthControl()->Move(0, 0);
508
509
510
511 panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER);
d5ae99f5
VZ
512 m_popup->SetClientSize(panel->GetSize());
513 m_popup->Hide();
39df3acd 514
964f23b7 515 SetValue(date.IsValid() ? date : wxDateTime::Today());
1721a8c0 516
d8b49350 517 SetBestFittingSize(size);
930fb29e 518
caad7637
JS
519 SetBackgroundColour(m_txt->GetBackgroundColour());
520
3a0c6181 521 return true;
39df3acd
VZ
522}
523
524
7ae712f5 525void wxDatePickerCtrlGeneric::Init()
39df3acd 526{
d5ae99f5 527 m_popup = NULL;
39df3acd
VZ
528 m_txt = NULL;
529 m_cal = NULL;
530 m_btn = NULL;
531
532 m_dropped = false;
533 m_ignoreDrop = false;
534}
535
a9d13e15
JS
536wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
537{
538 m_popup = NULL;
539 m_txt = NULL;
540 m_cal = NULL;
541 m_btn = NULL;
542}
39df3acd 543
7ae712f5 544bool wxDatePickerCtrlGeneric::Destroy()
39df3acd
VZ
545{
546 if (m_cal)
547 m_cal->Destroy();
d5ae99f5
VZ
548 if (m_popup)
549 m_popup->Destroy();
39df3acd
VZ
550 if (m_txt)
551 m_txt->Destroy();
552 if (m_btn)
553 m_btn->Destroy();
554
d5ae99f5 555 m_popup = NULL;
39df3acd
VZ
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
7ae712f5 567void wxDatePickerCtrlGeneric::DoMoveWindow(int x, int y, int w, int h)
39df3acd
VZ
568{
569 wxControl::DoMoveWindow(x, y, w, h);
39df3acd
VZ
570
571 if (m_dropped)
005eba5a 572 DropDown(false);
39df3acd
VZ
573}
574
7ae712f5 575wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const
39df3acd 576{
40a33c46
SN
577 if (m_btn && m_txt)
578 {
579 int bh=m_btn->GetBestSize().y;
580 int eh=m_txt->GetBestSize().y;
581 return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh);
582 }
68f2155b 583 return wxControl::DoGetBestSize();
39df3acd
VZ
584}
585
586
7ae712f5 587bool wxDatePickerCtrlGeneric::Show(bool show)
39df3acd
VZ
588{
589 if ( !wxControl::Show(show) )
590 {
3a0c6181 591 return false;
39df3acd
VZ
592 }
593
1721a8c0 594 if ( !show )
39df3acd 595 {
1721a8c0 596 if ( m_popup )
39df3acd 597 {
d5ae99f5 598 m_popup->Hide();
39df3acd
VZ
599 m_dropped = false;
600 }
601 }
602
3a0c6181 603 return true;
39df3acd
VZ
604}
605
606
7ae712f5 607bool wxDatePickerCtrlGeneric::Enable(bool enable)
39df3acd
VZ
608{
609 if ( !wxControl::Enable(enable) )
610 {
3a0c6181 611 return false;
39df3acd
VZ
612 }
613
1721a8c0 614 if ( !enable )
39df3acd 615 {
2eda91db
VZ
616 if ( m_popup )
617 m_popup->Hide();
39df3acd 618 }
1721a8c0
VZ
619
620 if ( m_btn )
39df3acd 621 m_btn->Enable(enable);
1721a8c0 622
3a0c6181 623 return true;
39df3acd
VZ
624}
625
626// ----------------------------------------------------------------------------
7ae712f5 627// wxDatePickerCtrlGeneric API
39df3acd
VZ
628// ----------------------------------------------------------------------------
629
4b134bb2 630bool
7ae712f5
VZ
631wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate,
632 const wxDateTime& upperdate)
4b134bb2
VZ
633{
634 return m_cal->SetDateRange(lowerdate, upperdate);
635}
636
7ae712f5 637bool wxDatePickerCtrlGeneric::SetFormat(const wxChar *fmt)
39df3acd 638{
17a1ebd1
VZ
639 m_format.clear();
640
39df3acd
VZ
641 wxDateTime dt;
642 dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
17a1ebd1 643 wxString str(dt.Format(fmt));
39df3acd 644
17a1ebd1
VZ
645 const wxChar *p = str.c_str();
646 while ( *p )
39df3acd
VZ
647 {
648 int n=wxAtoi(p);
649 if (n == dt.GetDay())
650 {
651 m_format.Append(wxT("%d"));
652 p += 2;
653 }
654 else if (n == (int)dt.GetMonth()+1)
655 {
656 m_format.Append(wxT("%m"));
657 p += 2;
658 }
659 else if (n == dt.GetYear())
660 {
661 m_format.Append(wxT("%Y"));
662 p += 4;
663 }
d5ae99f5
VZ
664 else if (n == (dt.GetYear() % 100))
665 {
1721a8c0
VZ
666 if (GetWindowStyle() & wxDP_SHOWCENTURY)
667 m_format.Append(wxT("%Y"));
668 else
669 m_format.Append(wxT("%y"));
d5ae99f5
VZ
670 p += 2;
671 }
39df3acd
VZ
672 else
673 m_format.Append(*p++);
674 }
675
17a1ebd1 676 if ( m_txt )
39df3acd 677 {
1721a8c0
VZ
678 wxArrayString allowedChars;
679 for ( wxChar c = _T('0'); c <= _T('9'); c++ )
680 allowedChars.Add(wxString(c, 1));
681
17a1ebd1
VZ
682 const wxChar *p2 = m_format.c_str();
683 while ( *p2 )
39df3acd 684 {
17a1ebd1
VZ
685 if ( *p2 == '%')
686 p2 += 2;
39df3acd 687 else
17a1ebd1 688 allowedChars.Add(wxString(*p2++, 1));
39df3acd 689 }
1721a8c0 690
f5c479cc 691#if wxUSE_VALIDATORS
39df3acd 692 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
1721a8c0 693 tv.SetIncludes(allowedChars);
39df3acd 694 m_txt->SetValidator(tv);
f5c479cc 695#endif
d5ae99f5 696
1721a8c0
VZ
697 if (m_currentDate.IsValid())
698 m_txt->SetValue(m_currentDate.Format(m_format));
39df3acd 699 }
1721a8c0 700
39df3acd
VZ
701 return true;
702}
703
704
7ae712f5 705wxDateTime wxDatePickerCtrlGeneric::GetValue() const
39df3acd 706{
1721a8c0 707 return m_currentDate;
39df3acd
VZ
708}
709
710
7ae712f5 711void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date)
39df3acd 712{
aa74ad5b
VZ
713 if ( !m_cal )
714 return;
1721a8c0 715
aa74ad5b
VZ
716 // we need to suppress the event sent from wxTextCtrl as calling our
717 // SetValue() should not result in an event being sent (wxTextCtrl is
718 // an exception to this rule)
719 gs_inSetValue = this;
720
721 if ( date.IsValid() )
722 {
723 m_txt->SetValue(date.Format(m_format));
724 }
725 else // invalid date
726 {
727 wxASSERT_MSG( HasFlag(wxDP_ALLOWNONE),
728 _T("this control must have a valid date") );
1721a8c0 729
aa74ad5b 730 m_txt->SetValue(wxEmptyString);
39df3acd 731 }
aa74ad5b
VZ
732
733 gs_inSetValue = NULL;
734
735 m_currentDate = date;
39df3acd
VZ
736}
737
738
7ae712f5 739bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
39df3acd
VZ
740{
741 if (dt1)
4b134bb2 742 *dt1 = m_cal->GetLowerDateLimit();
40a33c46 743 if (dt2)
4b134bb2 744 *dt2 = m_cal->GetUpperDateLimit();
39df3acd
VZ
745 return true;
746}
747
748
7ae712f5
VZ
749void
750wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
39df3acd 751{
4b134bb2 752 m_cal->SetDateRange(dt1, dt2);
39df3acd
VZ
753}
754
755// ----------------------------------------------------------------------------
756// event handlers
757// ----------------------------------------------------------------------------
758
7ae712f5 759void wxDatePickerCtrlGeneric::DropDown(bool down)
39df3acd 760{
d5ae99f5 761 if (m_popup)
39df3acd
VZ
762 {
763 if (down)
764 {
d5ae99f5 765 wxDateTime dt;
a7c58211 766 if (!m_txt->GetValue().empty())
39df3acd 767 dt.ParseFormat(m_txt->GetValue(), m_format);
d5ae99f5
VZ
768
769 if (dt.IsValid())
39df3acd 770 m_cal->SetDate(dt);
d5ae99f5
VZ
771 else
772 m_cal->SetDate(wxDateTime::Today());
39df3acd 773
d5ae99f5 774 wxPoint pos=GetParent()->ClientToScreen(GetPosition());
1721a8c0 775 m_popup->ShowAt(pos.x, pos.y + GetSize().y);
39df3acd 776 m_dropped = true;
1721a8c0 777 m_cal->SetFocus();
39df3acd
VZ
778 }
779 else
780 {
781 if (m_dropped)
d5ae99f5 782 m_popup->Hide();
39df3acd
VZ
783 m_dropped = false;
784 }
785 }
786}
787
788
caad7637
JS
789void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event)
790{
791 if ( m_btn )
792 {
793 wxSize sz = GetClientSize();
794
795 wxSize bs=m_btn->GetSize();
796 int eh=m_txt->GetBestSize().y;
797
798 m_txt->SetSize(0, TXTPOSY, sz.x-bs.x, sz.y > eh ? eh-TXTPOSY : sz.y-TXTPOSY);
799 m_btn->SetSize(sz.x - bs.x, 0, bs.x, sz.y);
800 }
801
802 event.Skip();
803}
804
805
7ae712f5 806void wxDatePickerCtrlGeneric::OnChildSetFocus(wxChildFocusEvent &ev)
39df3acd
VZ
807{
808 ev.Skip();
809 m_ignoreDrop = false;
810
811 wxWindow *w=(wxWindow*)ev.GetEventObject();
812 while (w)
813 {
d5ae99f5 814 if (w == m_popup)
39df3acd
VZ
815 return;
816 w = w->GetParent();
817 }
818
819 if (m_dropped)
820 {
821 DropDown(false);
caad7637 822 if (::wxFindWindowAtPoint(::wxGetMousePosition()) == m_btn)
39df3acd
VZ
823 m_ignoreDrop = true;
824 }
825}
826
827
b6292ff0 828void wxDatePickerCtrlGeneric::OnClick(wxCommandEvent& WXUNUSED(event))
39df3acd
VZ
829{
830 if (m_ignoreDrop)
831 {
832 m_ignoreDrop = false;
833 m_txt->SetFocus();
834 }
835 else
836 {
837 DropDown();
838 m_cal->SetFocus();
839 }
840}
841
842
b6292ff0 843void wxDatePickerCtrlGeneric::OnSetFocus(wxFocusEvent& WXUNUSED(ev))
39df3acd
VZ
844{
845 if (m_txt)
846 {
847 m_txt->SetFocus();
1721a8c0 848 m_txt->SetSelection(-1, -1); // select everything
39df3acd
VZ
849 }
850}
851
852
7ae712f5 853void wxDatePickerCtrlGeneric::OnKillFocus(wxFocusEvent &ev)
39df3acd 854{
a9d13e15
JS
855 if (!m_txt)
856 return;
857
39df3acd
VZ
858 ev.Skip();
859
860 wxDateTime dt;
861 dt.ParseFormat(m_txt->GetValue(), m_format);
2436cac7 862 if ( !dt.IsValid() )
1721a8c0
VZ
863 {
864 if ( !HasFlag(wxDP_ALLOWNONE) )
865 dt = m_currentDate;
866 }
867
a7c58211
WS
868 if(dt.IsValid())
869 m_txt->SetValue(dt.Format(m_format));
870 else
871 m_txt->SetValue(wxEmptyString);
1721a8c0
VZ
872
873 // notify that we had to change the date after validation
58e041b7 874 if ( (dt.IsValid() && (!m_currentDate.IsValid() || m_currentDate != dt)) ||
2436cac7 875 (!dt.IsValid() && m_currentDate.IsValid()) )
1721a8c0
VZ
876 {
877 m_currentDate = dt;
878 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
879 GetEventHandler()->ProcessEvent(event);
880 }
39df3acd
VZ
881}
882
883
7ae712f5 884void wxDatePickerCtrlGeneric::OnSelChange(wxCalendarEvent &ev)
39df3acd
VZ
885{
886 if (m_cal)
887 {
1721a8c0
VZ
888 m_currentDate = m_cal->GetDate();
889 m_txt->SetValue(m_currentDate.Format(m_format));
39df3acd
VZ
890 if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED)
891 {
892 DropDown(false);
893 m_txt->SetFocus();
894 }
895 }
896 ev.SetEventObject(this);
897 ev.SetId(GetId());
898 GetParent()->ProcessEvent(ev);
1721a8c0
VZ
899
900 wxDateEvent dev(this, ev.GetDate(), wxEVT_DATE_CHANGED);
901 GetParent()->ProcessEvent(dev);
39df3acd
VZ
902}
903
904
7ae712f5 905void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev)
39df3acd 906{
aa74ad5b
VZ
907 if ( gs_inSetValue )
908 {
909 // artificial event resulting from our own SetValue() call, ignore it
910 return;
911 }
912
39df3acd
VZ
913 ev.SetEventObject(this);
914 ev.SetId(GetId());
915 GetParent()->ProcessEvent(ev);
916
917 // We'll create an additional event if the date is valid.
1721a8c0 918 // If the date isn't valid, the user's probably in the middle of typing
a7c58211 919 wxString txt = m_txt->GetValue();
39df3acd 920 wxDateTime dt;
a7c58211 921 if (!txt.empty())
39df3acd
VZ
922 {
923 dt.ParseFormat(txt, m_format);
924 if (!dt.IsValid())
925 return;
926 }
927
928 wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED);
929 cev.SetEventObject(this);
930 cev.SetId(GetId());
931 cev.SetDate(dt);
932
933 GetParent()->ProcessEvent(cev);
1721a8c0
VZ
934
935 wxDateEvent dev(this, dt, wxEVT_DATE_CHANGED);
936 GetParent()->ProcessEvent(dev);
39df3acd
VZ
937}
938
939
7ae712f5 940void wxDatePickerCtrlGeneric::OnEditKey(wxKeyEvent & ev)
39df3acd
VZ
941{
942 if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers())
1721a8c0 943 DropDown(true);
39df3acd
VZ
944 else
945 ev.Skip();
946}
947
948
7ae712f5 949void wxDatePickerCtrlGeneric::OnCalKey(wxKeyEvent & ev)
39df3acd
VZ
950{
951 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
952 DropDown(false);
953 else
954 ev.Skip();
955}
956
7ae712f5
VZ
957#endif // wxUSE_DATEPICKCTRL_GENERIC
958
959#endif // wxUSE_DATEPICKCTRL