]> git.saurik.com Git - wxWidgets.git/blob - src/generic/datectlg.cpp
Make sure -DNO_GCC_PRAGMA is in wx-config output regardless of the reason.
[wxWidgets.git] / src / generic / datectlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/datectrl.cpp
3 // Purpose: generic wxDatePickerCtrl implementation
4 // Author: Andreas Pflug
5 // Modified by:
6 // Created: 2005-01-19
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 Andreas Pflug <pgadmin@pse-consulting.de>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/bmpbuttn.h"
28 #include "wx/dialog.h"
29 #include "wx/dcmemory.h"
30 #include "wx/panel.h"
31 #include "wx/textctrl.h"
32 #include "wx/valtext.h"
33 #endif
34
35 #include "wx/datectrl.h"
36 #include "wx/calctrl.h"
37
38 // ----------------------------------------------------------------------------
39 // constants
40 // ----------------------------------------------------------------------------
41
42 enum
43 {
44 CTRLID_TXT = 101,
45 CTRLID_CAL,
46 CTRLID_BTN,
47 CTRLID_PAN
48 };
49
50 #ifndef DEFAULT_ITEM_WIDTH
51 #define DEFAULT_ITEM_WIDTH 100
52 #endif
53
54 // ============================================================================
55 // wxDatePickerCtrl implementation
56 // ============================================================================
57
58 BEGIN_EVENT_TABLE(wxDatePickerCtrl, wxDatePickerCtrlBase)
59 EVT_BUTTON(CTRLID_BTN, wxDatePickerCtrl::OnClick)
60 EVT_TEXT(CTRLID_TXT, wxDatePickerCtrl::OnText)
61 EVT_CHILD_FOCUS(wxDatePickerCtrl::OnChildSetFocus)
62 END_EVENT_TABLE()
63
64 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxDatePickerCtrlBase)
65
66 // ----------------------------------------------------------------------------
67 // creation
68 // ----------------------------------------------------------------------------
69
70 wxDatePickerCtrl::wxDatePickerCtrl(wxWindow *parent,
71 wxWindowID id,
72 const wxDateTime& date,
73 const wxPoint& pos,
74 const wxSize& size,
75 long style,
76 const wxString& name)
77 {
78 Init();
79 Create(parent, id, date, pos, size, style, name);
80 }
81
82
83 bool wxDatePickerCtrl::Create(wxWindow *parent,
84 wxWindowID id,
85 const wxDateTime& date,
86 const wxPoint& pos,
87 const wxSize& size,
88 long style,
89 const wxString& name)
90 {
91 wxASSERT_MSG( !(style & wxDP_SPIN),
92 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
93
94 wxString txt;
95 if (date.IsValid())
96 txt = date.FormatDate();
97
98 if ( !wxControl::Create(parent, id, pos, size,
99 style | wxCLIP_CHILDREN | wxWANTS_CHARS,
100 wxDefaultValidator, name) )
101
102 {
103 return false;
104 }
105
106 SetWindowStyle(style | wxWANTS_CHARS);
107 InheritAttributes();
108
109 wxBitmap bmp(8, 4);
110 {
111 wxMemoryDC dc;
112
113 dc.SelectObject(bmp);
114 dc.SetBrush(wxBrush(GetBackgroundColour()));
115 dc.SetPen(wxPen(GetBackgroundColour()));
116 dc.DrawRectangle(0,0, 8,4);
117
118 dc.SetBrush(wxBrush(GetForegroundColour()));
119 dc.SetPen(wxPen(GetForegroundColour()));
120 wxPoint pt[3] = { wxPoint(0,0), wxPoint(6,0), wxPoint(3,3) };
121 dc.DrawPolygon(3, pt);
122 dc.SelectObject(wxNullBitmap);
123 }
124
125 m_txt=new wxTextCtrl(this, CTRLID_TXT, txt);
126 m_txt->Connect(wxID_ANY, wxID_ANY, wxEVT_KEY_DOWN,
127 (wxObjectEventFunction)&wxDatePickerCtrl::OnEditKey,
128 0, this);
129 m_txt->Connect(wxID_ANY, wxID_ANY, wxEVT_KILL_FOCUS,
130 (wxObjectEventFunction)&wxDatePickerCtrl::OnKillFocus,
131 0, this);
132 SetFormat(wxT("%x"));
133
134 m_btn = new wxBitmapButton(this, CTRLID_BTN, bmp);
135
136 m_dlg = new wxDialog(this, CTRLID_CAL, wxEmptyString,
137 wxDefaultPosition, wxDefaultSize,
138 wxSIMPLE_BORDER);
139 m_dlg->SetFont(GetFont());
140
141 wxPanel *panel=new wxPanel(m_dlg, CTRLID_PAN,
142 wxPoint(0, 0), wxDefaultSize,
143 wxSUNKEN_BORDER);
144 m_cal = new wxCalendarCtrl(panel, CTRLID_CAL, wxDefaultDateTime,
145 wxPoint(0,0), wxDefaultSize,
146 wxCAL_SHOW_HOLIDAYS | wxSUNKEN_BORDER);
147 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_SEL_CHANGED,
148 (wxObjectEventFunction)&wxDatePickerCtrl::OnSelChange,
149 0, this);
150 m_cal->Connect(wxID_ANY, wxID_ANY, wxEVT_KEY_DOWN,
151 (wxObjectEventFunction)&wxDatePickerCtrl::OnCalKey,
152 0, this);
153 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_DOUBLECLICKED,
154 (wxObjectEventFunction)&wxDatePickerCtrl::OnSelChange,
155 0, this);
156 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_DAY_CHANGED,
157 (wxObjectEventFunction)&wxDatePickerCtrl::OnSelChange,
158 0, this);
159 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_MONTH_CHANGED,
160 (wxObjectEventFunction)&wxDatePickerCtrl::OnSelChange,
161 0, this);
162 m_cal->Connect(CTRLID_CAL, CTRLID_CAL, wxEVT_CALENDAR_YEAR_CHANGED,
163 (wxObjectEventFunction)&wxDatePickerCtrl::OnSelChange,
164 0, this);
165
166 wxWindow *yearControl = m_cal->GetYearControl();
167
168 Connect(wxID_ANY, wxID_ANY, wxEVT_SET_FOCUS,
169 (wxObjectEventFunction)&wxDatePickerCtrl::OnSetFocus);
170
171 wxClientDC dc(yearControl);
172 dc.SetFont(m_font);
173 wxCoord width, dummy;
174 dc.GetTextExtent(wxT("2000"), &width, &dummy);
175 width += ConvertDialogToPixels(wxSize(20,0)).x;
176
177 wxSize calSize = m_cal->GetBestSize();
178 wxSize yearSize = yearControl->GetSize();
179 yearSize.x = width;
180
181 wxPoint yearPosition = yearControl->GetPosition();
182
183 #ifdef __WXMSW__
184 #define CALBORDER 0
185 #else
186 #define CALBORDER 4
187 #endif
188
189 width = yearPosition.x + yearSize.x+2+CALBORDER/2;
190 if (width < calSize.x-4)
191 width = calSize.x-4;
192
193 int calPos = (width-calSize.x)/2;
194 if (calPos == -1)
195 {
196 calPos = 0;
197 width += 2;
198 }
199 m_cal->SetSize(calPos, 0, calSize.x, calSize.y);
200 yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y,
201 yearSize.x, yearSize.y);
202 m_cal->GetMonthControl()->Move(0, 0);
203
204
205
206 panel->SetClientSize(width+CALBORDER/2, calSize.y-2+CALBORDER);
207 m_dlg->SetClientSize(panel->GetSize());
208
209 return TRUE;
210 }
211
212
213 void wxDatePickerCtrl::Init()
214 {
215 m_dlg = NULL;
216 m_txt = NULL;
217 m_cal = NULL;
218 m_btn = NULL;
219
220 m_dropped = false;
221 m_ignoreDrop = false;
222 }
223
224
225 bool wxDatePickerCtrl::Destroy()
226 {
227 if (m_cal)
228 m_cal->Destroy();
229 if (m_dlg)
230 m_dlg->Destroy();
231 if (m_txt)
232 m_txt->Destroy();
233 if (m_btn)
234 m_btn->Destroy();
235
236 m_dlg = NULL;
237 m_txt = NULL;
238 m_cal = NULL;
239 m_btn = NULL;
240
241 return wxControl::Destroy();
242 }
243
244 // ----------------------------------------------------------------------------
245 // overridden base class methods
246 // ----------------------------------------------------------------------------
247
248 void wxDatePickerCtrl::DoMoveWindow(int x, int y, int w, int h)
249 {
250 wxControl::DoMoveWindow(x, y, w, h);
251 wxSize bs=m_btn->GetBestSize();
252 int eh=m_txt->GetBestSize().y;
253
254 m_txt->SetSize(0, 0, w-bs.x-1, h > eh ? eh : h);
255 m_btn->SetSize(w - bs.x, 0, bs.x, h > bs.y ? bs.y : h);
256
257 if (m_dropped)
258 DropDown();
259 }
260
261 wxSize wxDatePickerCtrl::DoGetBestSize() const
262 {
263 int bh=m_btn->GetBestSize().y;
264 int eh=m_txt->GetBestSize().y;
265 return wxSize(DEFAULT_ITEM_WIDTH, bh > eh ? bh : eh);
266 }
267
268
269 bool wxDatePickerCtrl::Show(bool show)
270 {
271 if ( !wxControl::Show(show) )
272 {
273 return FALSE;
274 }
275
276 if (!show)
277 {
278 if (m_dlg)
279 {
280 m_dlg->Hide();
281 m_dropped = false;
282 }
283 }
284
285 return TRUE;
286 }
287
288
289 bool wxDatePickerCtrl::Enable(bool enable)
290 {
291 if ( !wxControl::Enable(enable) )
292 {
293 return FALSE;
294 }
295
296 if (!enable)
297 {
298 if (m_cal)
299 m_cal->Hide();
300 }
301 if (m_btn)
302 m_btn->Enable(enable);
303 return TRUE;
304 }
305
306 // ----------------------------------------------------------------------------
307 // wxDatePickerCtrl API
308 // ----------------------------------------------------------------------------
309
310 bool
311 wxDatePickerCtrl::SetDateRange(const wxDateTime& lowerdate,
312 const wxDateTime& upperdate)
313 {
314 return m_cal->SetDateRange(lowerdate, upperdate);
315 }
316
317 bool wxDatePickerCtrl::SetFormat(const wxChar *fmt)
318 {
319 wxDateTime dt;
320 dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
321 wxString str=dt.Format(fmt);
322 wxChar *p=(wxChar*)str.c_str();
323
324 m_format=wxEmptyString;
325
326 while (*p)
327 {
328 int n=wxAtoi(p);
329 if (n == dt.GetDay())
330 {
331 m_format.Append(wxT("%d"));
332 p += 2;
333 }
334 else if (n == (int)dt.GetMonth()+1)
335 {
336 m_format.Append(wxT("%m"));
337 p += 2;
338 }
339 else if (n == dt.GetYear())
340 {
341 m_format.Append(wxT("%Y"));
342 p += 4;
343 }
344 else
345 m_format.Append(*p++);
346 }
347
348 if (m_txt)
349 {
350 wxStringList valList;
351 wxChar c;
352 for (c='0'; c <= '9'; c++)
353 valList.Add(wxString(c, 1));
354 wxChar *p=(wxChar*)m_format.c_str();
355 while (*p)
356 {
357 if (*p == '%')
358 p += 2;
359 else
360 valList.Add(wxString(*p++, 1));
361 }
362 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
363 tv.SetIncludeList(valList);
364
365 m_txt->SetValidator(tv);
366 }
367 return true;
368 }
369
370
371 wxDateTime wxDatePickerCtrl::GetValue() const
372 {
373 wxDateTime dt;
374 wxString txt=m_txt->GetValue();
375
376 if (!txt.IsEmpty())
377 dt.ParseFormat(txt, m_format);
378
379 return dt;
380 }
381
382
383 void wxDatePickerCtrl::SetValue(const wxDateTime& date)
384 {
385 if (m_cal)
386 {
387 if (date.IsValid())
388 m_txt->SetValue(date.FormatDate());
389 else
390 m_txt->SetValue(wxEmptyString);
391 }
392 }
393
394
395 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
396 {
397 if (dt1)
398 *dt1 = m_cal->GetLowerDateLimit();
399 if (dt1)
400 *dt2 = m_cal->GetUpperDateLimit();
401 return true;
402 }
403
404
405 void wxDatePickerCtrl::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
406 {
407 m_cal->SetDateRange(dt1, dt2);
408 }
409
410 // ----------------------------------------------------------------------------
411 // event handlers
412 // ----------------------------------------------------------------------------
413
414 void wxDatePickerCtrl::DropDown(bool down)
415 {
416 if (m_dlg)
417 {
418 if (down)
419 {
420 if (m_txt->GetValue().IsEmpty())
421 m_cal->SetDate(wxDateTime::Today());
422 else
423 {
424 wxDateTime dt;
425 dt.ParseFormat(m_txt->GetValue(), m_format);
426 m_cal->SetDate(dt);
427 }
428 wxPoint pos=GetParent()->ClientToScreen(GetPosition());
429
430 m_dlg->Move(pos.x, pos.y + GetSize().y);
431 m_dlg->Show();
432 m_dropped = true;
433 }
434 else
435 {
436 if (m_dropped)
437 m_dlg->Hide();
438 m_dropped = false;
439 }
440 }
441 }
442
443
444 void wxDatePickerCtrl::OnChildSetFocus(wxChildFocusEvent &ev)
445 {
446 ev.Skip();
447 m_ignoreDrop = false;
448
449 wxWindow *w=(wxWindow*)ev.GetEventObject();
450 while (w)
451 {
452 if (w == m_dlg)
453 return;
454 w = w->GetParent();
455 }
456
457 if (m_dropped)
458 {
459 DropDown(false);
460 if (ev.GetEventObject() == m_btn)
461 m_ignoreDrop = true;
462 }
463 }
464
465
466 void wxDatePickerCtrl::OnClick(wxCommandEvent& event)
467 {
468 if (m_ignoreDrop)
469 {
470 m_ignoreDrop = false;
471 m_txt->SetFocus();
472 }
473 else
474 {
475 DropDown();
476 m_cal->SetFocus();
477 }
478 }
479
480
481 void wxDatePickerCtrl::OnSetFocus(wxFocusEvent &ev)
482 {
483 if (m_txt)
484 {
485 m_txt->SetFocus();
486 m_txt->SetSelection(0, 100);
487 }
488 }
489
490
491 void wxDatePickerCtrl::OnKillFocus(wxFocusEvent &ev)
492 {
493 ev.Skip();
494
495 wxDateTime dt;
496 dt.ParseFormat(m_txt->GetValue(), m_format);
497 if (!dt.IsValid())
498 m_txt->SetValue(wxEmptyString);
499 else
500 m_txt->SetValue(dt.Format(m_format));
501 }
502
503
504 void wxDatePickerCtrl::OnSelChange(wxCalendarEvent &ev)
505 {
506 if (m_cal)
507 {
508 m_txt->SetValue(m_cal->GetDate().FormatDate());
509 if (ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED)
510 {
511 DropDown(false);
512 m_txt->SetFocus();
513 }
514 }
515 ev.SetEventObject(this);
516 ev.SetId(GetId());
517 GetParent()->ProcessEvent(ev);
518 }
519
520
521 void wxDatePickerCtrl::OnText(wxCommandEvent &ev)
522 {
523 ev.SetEventObject(this);
524 ev.SetId(GetId());
525 GetParent()->ProcessEvent(ev);
526
527 // We'll create an additional event if the date is valid.
528 // If the date isn't valid, the user's probable in the middle of typing
529 wxString txt=m_txt->GetValue();
530 wxDateTime dt;
531 if (!txt.IsEmpty())
532 {
533 dt.ParseFormat(txt, m_format);
534 if (!dt.IsValid())
535 return;
536 }
537
538 wxCalendarEvent cev(m_cal, wxEVT_CALENDAR_SEL_CHANGED);
539 cev.SetEventObject(this);
540 cev.SetId(GetId());
541 cev.SetDate(dt);
542
543 GetParent()->ProcessEvent(cev);
544 }
545
546
547 void wxDatePickerCtrl::OnEditKey(wxKeyEvent & ev)
548 {
549 if (ev.GetKeyCode() == WXK_DOWN && !ev.HasModifiers())
550 DropDown();
551 else
552 ev.Skip();
553 }
554
555
556 void wxDatePickerCtrl::OnCalKey(wxKeyEvent & ev)
557 {
558 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
559 DropDown(false);
560 else
561 ev.Skip();
562 }
563