]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/timectrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/timectrl.cpp
3 // Purpose: Generic implementation of wxTimePickerCtrl.
4 // Author: Paul Breen, Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_TIMEPICKCTRL
29 #include "wx/textctrl.h"
32 #include "wx/timectrl.h"
34 // This class is only compiled if there is no native version or if we
35 // explicitly want to use both the native and generic one (this is useful for
36 // testing but not much otherwise and so by default we don't use the generic
37 // implementation if a native one is available).
38 #if !defined(wxHAS_NATIVE_TIMEPICKERCTRL) || wxUSE_TIMEPICKCTRL_GENERIC
40 #include "wx/generic/timectrl.h"
42 #include "wx/dateevt.h"
43 #include "wx/spinbutt.h"
45 #ifndef wxHAS_NATIVE_TIMEPICKERCTRL
46 IMPLEMENT_DYNAMIC_CLASS(wxTimePickerCtrl
, wxControl
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
55 // Horizontal margin between the text and spin control.
59 // ----------------------------------------------------------------------------
60 // wxTimePickerGenericImpl: used to implement wxTimePickerCtrlGeneric
61 // ----------------------------------------------------------------------------
63 class wxTimePickerGenericImpl
: public wxEvtHandler
66 wxTimePickerGenericImpl(wxTimePickerCtrlGeneric
* ctrl
)
68 m_text
= new wxTextCtrl(ctrl
, wxID_ANY
, wxString());
70 // As this text can't be edited, don't use the standard cursor for it
71 // to avoid misleading the user. Ideally we'd also hide the caret but
72 // this is not currently supported by wxTextCtrl.
73 m_text
->SetCursor(wxCURSOR_ARROW
);
75 m_btn
= new wxSpinButton(ctrl
, wxID_ANY
,
76 wxDefaultPosition
, wxDefaultSize
,
77 wxSP_VERTICAL
| wxSP_WRAP
);
79 m_currentField
= Field_Hour
;
80 m_isFirstDigit
= true;
82 // We don't support arbitrary formats currently as this requires
83 // significantly more work both here and also in wxLocale::GetInfo().
85 // For now just use either "%H:%M:%S" or "%I:%M:%S %p". It would be
86 // nice to add support to "%k" and "%l" (hours with leading blanks
87 // instead of zeros) too as this is the most common unsupported case in
89 m_useAMPM
= wxLocale::GetInfo(wxLOCALE_TIME_FMT
).Contains("%p");
94 wxFocusEventHandler(wxTimePickerGenericImpl::OnTextSetFocus
),
101 wxKeyEventHandler(wxTimePickerGenericImpl::OnTextKeyDown
),
108 wxMouseEventHandler(wxTimePickerGenericImpl::OnTextClick
),
116 wxSpinEventHandler(wxTimePickerGenericImpl::OnArrowUp
),
123 wxSpinEventHandler(wxTimePickerGenericImpl::OnArrowDown
),
129 // Set the new value.
130 void SetValue(const wxDateTime
& time
)
132 m_time
= time
.IsValid() ? time
: wxDateTime::Now();
134 UpdateTextWithoutEvent();
138 // The text part of the control.
141 // The spin button used to change the text fields.
144 // The current time (date part is ignored).
148 // The logical fields of the text control (AM/PM one may not be present).
158 // Direction of change of time fields.
161 // Notice that the enum elements values matter.
166 // A range of character positions ("from" is inclusive, "to" -- exclusive).
173 // Event handlers for various events in our controls.
174 void OnTextSetFocus(wxFocusEvent
& event
)
176 HighlightCurrentField();
181 // Keyboard interface here is modelled over MSW native control and may need
182 // adjustments for other platforms.
183 void OnTextKeyDown(wxKeyEvent
& event
)
185 const int key
= event
.GetKeyCode();
190 ChangeCurrentFieldBy1(Dir_Down
);
194 ChangeCurrentFieldBy1(Dir_Up
);
198 CycleCurrentField(Dir_Down
);
202 CycleCurrentField(Dir_Up
);
206 ResetCurrentField(Dir_Down
);
210 ResetCurrentField(Dir_Up
);
223 // The digits work in all keys except AM/PM.
224 if ( m_currentField
!= Field_AMPM
)
226 AppendDigitToCurrentField(key
- '0');
232 // These keys only work to toggle AM/PM field.
233 if ( m_currentField
== Field_AMPM
)
235 unsigned hour
= m_time
.GetHour();
247 if ( hour
!= m_time
.GetHour() )
249 m_time
.SetHour(hour
);
255 // Do not skip the other events, just consume them to prevent the
256 // user from editing the text directly.
260 void OnTextClick(wxMouseEvent
& event
)
262 Field field
wxDUMMY_INITIALIZE(Field_Max
);
264 switch ( m_text
->HitTest(event
.GetPosition(), &pos
) )
266 case wxTE_HT_UNKNOWN
:
267 // Don't do anything, it's better than doing something wrong.
271 // Select the first field.
275 case wxTE_HT_ON_TEXT
:
276 // Find the field containing this position.
277 for ( field
= Field_Hour
; field
<= GetLastField(); )
279 const CharRange range
= GetFieldRange(field
);
281 // Normally the "to" end is exclusive but we want to give
282 // focus to some field when the user clicks between them so
283 // count it as part of the preceding field here.
284 if ( range
.from
<= pos
&& pos
<= range
.to
)
287 field
= static_cast<Field
>(field
+ 1);
292 // This shouldn't happen for single line control.
293 wxFAIL_MSG( "Unreachable" );
297 // Select the last field.
298 field
= GetLastField();
302 ChangeCurrentField(field
);
304 // As we don't skip the event, we also prevent the system from setting
305 // focus to this control as it does by default, so do it manually.
309 void OnArrowUp(wxSpinEvent
& WXUNUSED(event
))
311 ChangeCurrentFieldBy1(Dir_Up
);
316 void OnArrowDown(wxSpinEvent
& WXUNUSED(event
))
318 ChangeCurrentFieldBy1(Dir_Down
);
324 // Get the range of the given field in character positions ("from" is
325 // inclusive, "to" exclusive).
326 static CharRange
GetFieldRange(Field field
)
328 // Currently we can just hard code the ranges as they are the same for
329 // both supported formats, if we want to support arbitrary formats in
330 // the future, we'd need to determine them dynamically by examining the
332 static const CharRange ranges
[] =
340 wxCOMPILE_TIME_ASSERT( WXSIZEOF(ranges
) == Field_Max
,
341 FieldRangesMismatch
);
343 return ranges
[field
];
346 // Get the last field used depending on m_useAMPM.
347 Field
GetLastField() const
349 return m_useAMPM
? Field_AMPM
: Field_Sec
;
352 // Change the current field. For convenience, accept int field here as this
353 // allows us to use arithmetic operations in the caller.
354 void ChangeCurrentField(int field
)
356 if ( field
== m_currentField
)
359 wxCHECK_RET( field
<= GetLastField(), "Invalid field" );
361 m_currentField
= static_cast<Field
>(field
);
362 m_isFirstDigit
= true;
364 HighlightCurrentField();
367 // Go to the next (Dir_Up) or previous (Dir_Down) field, wrapping if
369 void CycleCurrentField(Direction dir
)
371 const unsigned numFields
= GetLastField() + 1;
373 ChangeCurrentField((m_currentField
+ numFields
+ dir
) % numFields
);
376 // Select the currently actively field.
377 void HighlightCurrentField()
379 const CharRange range
= GetFieldRange(m_currentField
);
381 m_text
->SetSelection(range
.from
, range
.to
);
384 // Decrement or increment the value of the current field (wrapping if
386 void ChangeCurrentFieldBy1(Direction dir
)
388 switch ( m_currentField
)
391 m_time
.SetHour((m_time
.GetHour() + 24 + dir
) % 24);
395 m_time
.SetMinute((m_time
.GetMinute() + 60 + dir
) % 60);
399 m_time
.SetSecond((m_time
.GetSecond() + 60 + dir
) % 60);
403 m_time
.SetHour((m_time
.GetHour() + 12) % 24);
407 wxFAIL_MSG( "Invalid field" );
413 // Set the current field to its minimal or maximal value.
414 void ResetCurrentField(Direction dir
)
416 switch ( m_currentField
)
420 // In 12-hour mode setting the hour to the minimal value
421 // also changes the suffix to AM and, correspondingly,
422 // setting it to the maximal one changes the suffix to PM.
423 // And, for consistency with the native MSW behaviour, we
424 // also do the same thing when changing AM/PM field itself,
425 // so change hours in any case.
426 m_time
.SetHour(dir
== Dir_Down
? 0 : 23);
430 m_time
.SetMinute(dir
== Dir_Down
? 0 : 59);
434 m_time
.SetSecond(dir
== Dir_Down
? 0 : 59);
438 wxFAIL_MSG( "Invalid field" );
444 // Append the given digit (from 0 to 9) to the current value of the current
446 void AppendDigitToCurrentField(int n
)
448 bool moveToNextField
= false;
450 if ( !m_isFirstDigit
)
452 // The first digit simply replaces the existing field contents,
453 // but the second one should be combined with the previous one,
454 // otherwise entering 2-digit numbers would be impossible.
455 int currentValue
wxDUMMY_INITIALIZE(0),
456 maxValue
wxDUMMY_INITIALIZE(0);
458 switch ( m_currentField
)
461 currentValue
= m_time
.GetHour();
466 currentValue
= m_time
.GetMinute();
471 currentValue
= m_time
.GetSecond();
477 wxFAIL_MSG( "Invalid field" );
480 // Check if the new value is acceptable. If not, we just handle
481 // this digit as if it were the first one.
482 int newValue
= currentValue
*10 + n
;
483 if ( newValue
< maxValue
)
487 // If we're not on the seconds field, advance to the next one.
488 // This makes it more convenient to enter times as you can just
489 // press all digits one after one without touching the cursor
490 // arrow keys at all.
492 // Notice that MSW native control doesn't do this but it seems
493 // so useful that we intentionally diverge from it here.
494 moveToNextField
= true;
496 // We entered both digits so the next one will be "first" again.
497 m_isFirstDigit
= true;
500 else // First digit entered.
502 // The next one won't be first any more.
503 m_isFirstDigit
= false;
506 switch ( m_currentField
)
522 wxFAIL_MSG( "Invalid field" );
525 if ( moveToNextField
&& m_currentField
< Field_Sec
)
526 CycleCurrentField(Dir_Up
);
531 // Update the text value to correspond to the current time. By default also
532 // generate an event but this can be avoided by calling the "WithoutEvent"
536 UpdateTextWithoutEvent();
538 wxWindow
* const ctrl
= m_text
->GetParent();
540 wxDateEvent
event(ctrl
, m_time
, wxEVT_TIME_CHANGED
);
541 ctrl
->HandleWindowEvent(event
);
544 void UpdateTextWithoutEvent()
546 m_text
->SetValue(m_time
.Format(m_useAMPM
? "%I:%M:%S %p" : "%H:%M:%S"));
548 HighlightCurrentField();
552 // The current field of the text control: this is the one affected by
553 // pressing arrow keys or spin button.
554 Field m_currentField
;
556 // Flag indicating whether we use AM/PM indicator or not.
559 // Flag indicating whether the next digit pressed by user will be the first
560 // digit of the current field or the second one. This is necessary because
561 // the first digit replaces the current field contents while the second one
562 // is appended to it (if possible, e.g. pressing '7' in a field already
563 // containing '8' will still replace it as "78" would be invalid).
566 wxDECLARE_NO_COPY_CLASS(wxTimePickerGenericImpl
);
569 // ============================================================================
570 // wxTimePickerCtrlGeneric implementation
571 // ============================================================================
573 // ----------------------------------------------------------------------------
574 // wxTimePickerCtrlGeneric creation
575 // ----------------------------------------------------------------------------
577 void wxTimePickerCtrlGeneric::Init()
583 wxTimePickerCtrlGeneric::Create(wxWindow
*parent
,
585 const wxDateTime
& date
,
589 const wxValidator
& validator
,
590 const wxString
& name
)
592 // The text control we use already has a border, so we don't need one
594 style
&= ~wxBORDER_MASK
;
595 style
|= wxBORDER_NONE
;
597 if ( !Base::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
600 m_impl
= new wxTimePickerGenericImpl(this);
601 m_impl
->SetValue(date
);
603 InvalidateBestSize();
604 SetInitialSize(size
);
609 wxTimePickerCtrlGeneric::~wxTimePickerCtrlGeneric()
614 wxWindowList
wxTimePickerCtrlGeneric::GetCompositeWindowParts() const
619 parts
.push_back(m_impl
->m_text
);
620 parts
.push_back(m_impl
->m_btn
);
625 // ----------------------------------------------------------------------------
626 // wxTimePickerCtrlGeneric value
627 // ----------------------------------------------------------------------------
629 void wxTimePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
631 wxCHECK_RET( m_impl
, "Must create first" );
633 m_impl
->SetValue(date
);
636 wxDateTime
wxTimePickerCtrlGeneric::GetValue() const
638 wxCHECK_MSG( m_impl
, wxDateTime(), "Must create first" );
640 return m_impl
->m_time
;
643 // ----------------------------------------------------------------------------
644 // wxTimePickerCtrlGeneric geometry
645 // ----------------------------------------------------------------------------
647 void wxTimePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int width
, int height
)
649 Base::DoMoveWindow(x
, y
, width
, height
);
654 const int widthBtn
= m_impl
->m_btn
->GetSize().x
;
655 const int widthText
= width
- widthBtn
- HMARGIN_TEXT_SPIN
;
657 m_impl
->m_text
->SetSize(0, 0, widthText
, height
);
658 m_impl
->m_btn
->SetSize(widthText
+ HMARGIN_TEXT_SPIN
, 0, widthBtn
, height
);
661 wxSize
wxTimePickerCtrlGeneric::DoGetBestSize() const
664 return Base::DoGetBestSize();
666 wxSize size
= m_impl
->m_text
->GetBestSize();
667 size
.x
+= m_impl
->m_btn
->GetBestSize().x
+ HMARGIN_TEXT_SPIN
;
672 #endif // !wxHAS_NATIVE_TIMEPICKERCTRL || wxUSE_TIMEPICKCTRL_GENERIC
674 #endif // wxUSE_TIMEPICKCTRL