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