]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/timectrlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/timectrl.cpp
3 // Purpose: Generic implementation of wxTimePickerCtrl.
4 // Author: Paul Breen, Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #if wxUSE_TIMEPICKCTRL
28 #include "wx/textctrl.h"
31 #include "wx/timectrl.h"
33 // This class is only compiled if there is no native version or if we
34 // explicitly want to use both the native and generic one (this is useful for
35 // testing but not much otherwise and so by default we don't use the generic
36 // implementation if a native one is available).
37 #if !defined(wxHAS_NATIVE_TIMEPICKERCTRL) || wxUSE_TIMEPICKCTRL_GENERIC
39 #include "wx/generic/timectrl.h"
41 #include "wx/dateevt.h"
42 #include "wx/spinbutt.h"
44 #ifndef wxHAS_NATIVE_TIMEPICKERCTRL
45 IMPLEMENT_DYNAMIC_CLASS(wxTimePickerCtrl
, wxControl
)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
54 // Horizontal margin between the text and spin control.
58 // ----------------------------------------------------------------------------
59 // wxTimePickerGenericImpl: used to implement wxTimePickerCtrlGeneric
60 // ----------------------------------------------------------------------------
62 class wxTimePickerGenericImpl
: public wxEvtHandler
65 wxTimePickerGenericImpl(wxTimePickerCtrlGeneric
* ctrl
)
67 m_text
= new wxTextCtrl(ctrl
, wxID_ANY
, wxString());
69 // As this text can't be edited, don't use the standard cursor for it
70 // to avoid misleading the user. Ideally we'd also hide the caret but
71 // this is not currently supported by wxTextCtrl.
72 m_text
->SetCursor(wxCURSOR_ARROW
);
74 m_btn
= new wxSpinButton(ctrl
, wxID_ANY
,
75 wxDefaultPosition
, wxDefaultSize
,
76 wxSP_VERTICAL
| wxSP_WRAP
);
78 m_currentField
= Field_Hour
;
79 m_isFirstDigit
= true;
81 // We don't support arbitrary formats currently as this requires
82 // significantly more work both here and also in wxLocale::GetInfo().
84 // For now just use either "%H:%M:%S" or "%I:%M:%S %p". It would be
85 // nice to add support to "%k" and "%l" (hours with leading blanks
86 // instead of zeros) too as this is the most common unsupported case in
88 m_useAMPM
= wxLocale::GetInfo(wxLOCALE_TIME_FMT
).Contains("%p");
93 wxFocusEventHandler(wxTimePickerGenericImpl::OnTextSetFocus
),
100 wxKeyEventHandler(wxTimePickerGenericImpl::OnTextKeyDown
),
107 wxMouseEventHandler(wxTimePickerGenericImpl::OnTextClick
),
115 wxSpinEventHandler(wxTimePickerGenericImpl::OnArrowUp
),
122 wxSpinEventHandler(wxTimePickerGenericImpl::OnArrowDown
),
128 // Set the new value.
129 void SetValue(const wxDateTime
& time
)
131 m_time
= time
.IsValid() ? time
: wxDateTime::Now();
133 // Ensure that the date part doesn't correspond to a DST change date as
134 // time is discontinuous then resulting in many problems, e.g. it's
135 // impossible to even enter 2:00:00 at the beginning of summer time
136 // date as this time doesn't exist. By using Jan 1, on which nobody
137 // changes DST, we avoid all such problems.
138 wxDateTime::Tm tm
= m_time
.GetTm();
141 tm
.mon
= wxDateTime::Jan
;
144 UpdateTextWithoutEvent();
148 // The text part of the control.
151 // The spin button used to change the text fields.
154 // The current time (date part is ignored).
158 // The logical fields of the text control (AM/PM one may not be present).
168 // Direction of change of time fields.
171 // Notice that the enum elements values matter.
176 // A range of character positions ("from" is inclusive, "to" -- exclusive).
183 // Event handlers for various events in our controls.
184 void OnTextSetFocus(wxFocusEvent
& event
)
186 HighlightCurrentField();
191 // Keyboard interface here is modelled over MSW native control and may need
192 // adjustments for other platforms.
193 void OnTextKeyDown(wxKeyEvent
& event
)
195 const int key
= event
.GetKeyCode();
200 ChangeCurrentFieldBy1(Dir_Down
);
204 ChangeCurrentFieldBy1(Dir_Up
);
208 CycleCurrentField(Dir_Down
);
212 CycleCurrentField(Dir_Up
);
216 ResetCurrentField(Dir_Down
);
220 ResetCurrentField(Dir_Up
);
233 // The digits work in all keys except AM/PM.
234 if ( m_currentField
!= Field_AMPM
)
236 AppendDigitToCurrentField(key
- '0');
242 // These keys only work to toggle AM/PM field.
243 if ( m_currentField
== Field_AMPM
)
245 unsigned hour
= m_time
.GetHour();
257 if ( hour
!= m_time
.GetHour() )
259 m_time
.SetHour(hour
);
265 // Do not skip the other events, just consume them to prevent the
266 // user from editing the text directly.
270 void OnTextClick(wxMouseEvent
& event
)
272 Field field
= Field_Max
; // Initialize just to suppress warnings.
274 switch ( m_text
->HitTest(event
.GetPosition(), &pos
) )
276 case wxTE_HT_UNKNOWN
:
277 // Don't do anything, it's better than doing something wrong.
281 // Select the first field.
285 case wxTE_HT_ON_TEXT
:
286 // Find the field containing this position.
287 for ( field
= Field_Hour
; field
<= GetLastField(); )
289 const CharRange range
= GetFieldRange(field
);
291 // Normally the "to" end is exclusive but we want to give
292 // focus to some field when the user clicks between them so
293 // count it as part of the preceding field here.
294 if ( range
.from
<= pos
&& pos
<= range
.to
)
297 field
= static_cast<Field
>(field
+ 1);
302 // This shouldn't happen for single line control.
303 wxFAIL_MSG( "Unreachable" );
307 // Select the last field.
308 field
= GetLastField();
312 ChangeCurrentField(field
);
315 void OnArrowUp(wxSpinEvent
& WXUNUSED(event
))
317 ChangeCurrentFieldBy1(Dir_Up
);
320 void OnArrowDown(wxSpinEvent
& WXUNUSED(event
))
322 ChangeCurrentFieldBy1(Dir_Down
);
326 // Get the range of the given field in character positions ("from" is
327 // inclusive, "to" exclusive).
328 static CharRange
GetFieldRange(Field field
)
330 // Currently we can just hard code the ranges as they are the same for
331 // both supported formats, if we want to support arbitrary formats in
332 // the future, we'd need to determine them dynamically by examining the
334 static const CharRange ranges
[] =
342 wxCOMPILE_TIME_ASSERT( WXSIZEOF(ranges
) == Field_Max
,
343 FieldRangesMismatch
);
345 return ranges
[field
];
348 // Get the last field used depending on m_useAMPM.
349 Field
GetLastField() const
351 return m_useAMPM
? Field_AMPM
: Field_Sec
;
354 // Change the current field. For convenience, accept int field here as this
355 // allows us to use arithmetic operations in the caller.
356 void ChangeCurrentField(int field
)
358 if ( field
== m_currentField
)
361 wxCHECK_RET( field
<= GetLastField(), "Invalid field" );
363 m_currentField
= static_cast<Field
>(field
);
364 m_isFirstDigit
= true;
366 HighlightCurrentField();
369 // Go to the next (Dir_Up) or previous (Dir_Down) field, wrapping if
371 void CycleCurrentField(Direction dir
)
373 const unsigned numFields
= GetLastField() + 1;
375 ChangeCurrentField((m_currentField
+ numFields
+ dir
) % numFields
);
378 // Select the currently actively field.
379 void HighlightCurrentField()
383 const CharRange range
= GetFieldRange(m_currentField
);
385 m_text
->SetSelection(range
.from
, range
.to
);
388 // Decrement or increment the value of the current field (wrapping if
390 void ChangeCurrentFieldBy1(Direction dir
)
392 switch ( m_currentField
)
395 m_time
.SetHour((m_time
.GetHour() + 24 + dir
) % 24);
399 m_time
.SetMinute((m_time
.GetMinute() + 60 + dir
) % 60);
403 m_time
.SetSecond((m_time
.GetSecond() + 60 + dir
) % 60);
407 m_time
.SetHour((m_time
.GetHour() + 12) % 24);
411 wxFAIL_MSG( "Invalid field" );
417 // Set the current field to its minimal or maximal value.
418 void ResetCurrentField(Direction dir
)
420 switch ( m_currentField
)
424 // In 12-hour mode setting the hour to the minimal value
425 // also changes the suffix to AM and, correspondingly,
426 // setting it to the maximal one changes the suffix to PM.
427 // And, for consistency with the native MSW behaviour, we
428 // also do the same thing when changing AM/PM field itself,
429 // so change hours in any case.
430 m_time
.SetHour(dir
== Dir_Down
? 0 : 23);
434 m_time
.SetMinute(dir
== Dir_Down
? 0 : 59);
438 m_time
.SetSecond(dir
== Dir_Down
? 0 : 59);
442 wxFAIL_MSG( "Invalid field" );
448 // Append the given digit (from 0 to 9) to the current value of the current
450 void AppendDigitToCurrentField(int n
)
452 bool moveToNextField
= false;
454 if ( !m_isFirstDigit
)
456 // The first digit simply replaces the existing field contents,
457 // but the second one should be combined with the previous one,
458 // otherwise entering 2-digit numbers would be impossible.
459 int currentValue
= 0,
462 switch ( m_currentField
)
465 currentValue
= m_time
.GetHour();
470 currentValue
= m_time
.GetMinute();
475 currentValue
= m_time
.GetSecond();
481 wxFAIL_MSG( "Invalid field" );
485 // Check if the new value is acceptable. If not, we just handle
486 // this digit as if it were the first one.
487 int newValue
= currentValue
*10 + n
;
488 if ( newValue
< maxValue
)
492 // If we're not on the seconds field, advance to the next one.
493 // This makes it more convenient to enter times as you can just
494 // press all digits one after one without touching the cursor
495 // arrow keys at all.
497 // Notice that MSW native control doesn't do this but it seems
498 // so useful that we intentionally diverge from it here.
499 moveToNextField
= true;
501 // We entered both digits so the next one will be "first" again.
502 m_isFirstDigit
= true;
505 else // First digit entered.
507 // The next one won't be first any more.
508 m_isFirstDigit
= false;
511 switch ( m_currentField
)
527 wxFAIL_MSG( "Invalid field" );
530 if ( moveToNextField
&& m_currentField
< Field_Sec
)
531 CycleCurrentField(Dir_Up
);
536 // Update the text value to correspond to the current time. By default also
537 // generate an event but this can be avoided by calling the "WithoutEvent"
541 UpdateTextWithoutEvent();
543 wxWindow
* const ctrl
= m_text
->GetParent();
545 wxDateEvent
event(ctrl
, m_time
, wxEVT_TIME_CHANGED
);
546 ctrl
->HandleWindowEvent(event
);
549 void UpdateTextWithoutEvent()
551 m_text
->SetValue(m_time
.Format(m_useAMPM
? "%I:%M:%S %p" : "%H:%M:%S"));
553 HighlightCurrentField();
557 // The current field of the text control: this is the one affected by
558 // pressing arrow keys or spin button.
559 Field m_currentField
;
561 // Flag indicating whether we use AM/PM indicator or not.
564 // Flag indicating whether the next digit pressed by user will be the first
565 // digit of the current field or the second one. This is necessary because
566 // the first digit replaces the current field contents while the second one
567 // is appended to it (if possible, e.g. pressing '7' in a field already
568 // containing '8' will still replace it as "78" would be invalid).
571 wxDECLARE_NO_COPY_CLASS(wxTimePickerGenericImpl
);
574 // ============================================================================
575 // wxTimePickerCtrlGeneric implementation
576 // ============================================================================
578 // ----------------------------------------------------------------------------
579 // wxTimePickerCtrlGeneric creation
580 // ----------------------------------------------------------------------------
582 void wxTimePickerCtrlGeneric::Init()
588 wxTimePickerCtrlGeneric::Create(wxWindow
*parent
,
590 const wxDateTime
& date
,
594 const wxValidator
& validator
,
595 const wxString
& name
)
597 // The text control we use already has a border, so we don't need one
599 style
&= ~wxBORDER_MASK
;
600 style
|= wxBORDER_NONE
;
602 if ( !Base::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
605 m_impl
= new wxTimePickerGenericImpl(this);
606 m_impl
->SetValue(date
);
608 InvalidateBestSize();
609 SetInitialSize(size
);
614 wxTimePickerCtrlGeneric::~wxTimePickerCtrlGeneric()
619 wxWindowList
wxTimePickerCtrlGeneric::GetCompositeWindowParts() const
624 parts
.push_back(m_impl
->m_text
);
625 parts
.push_back(m_impl
->m_btn
);
630 // ----------------------------------------------------------------------------
631 // wxTimePickerCtrlGeneric value
632 // ----------------------------------------------------------------------------
634 void wxTimePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
636 wxCHECK_RET( m_impl
, "Must create first" );
638 m_impl
->SetValue(date
);
641 wxDateTime
wxTimePickerCtrlGeneric::GetValue() const
643 wxCHECK_MSG( m_impl
, wxDateTime(), "Must create first" );
645 return m_impl
->m_time
;
648 // ----------------------------------------------------------------------------
649 // wxTimePickerCtrlGeneric geometry
650 // ----------------------------------------------------------------------------
652 void wxTimePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int width
, int height
)
654 Base::DoMoveWindow(x
, y
, width
, height
);
659 const int widthBtn
= m_impl
->m_btn
->GetSize().x
;
660 const int widthText
= width
- widthBtn
- HMARGIN_TEXT_SPIN
;
662 m_impl
->m_text
->SetSize(0, 0, widthText
, height
);
663 m_impl
->m_btn
->SetSize(widthText
+ HMARGIN_TEXT_SPIN
, 0, widthBtn
, height
);
666 wxSize
wxTimePickerCtrlGeneric::DoGetBestSize() const
669 return Base::DoGetBestSize();
671 wxSize size
= m_impl
->m_text
->GetBestSize();
672 size
.x
+= m_impl
->m_btn
->GetBestSize().x
+ HMARGIN_TEXT_SPIN
;
677 #endif // !wxHAS_NATIVE_TIMEPICKERCTRL || wxUSE_TIMEPICKCTRL_GENERIC
679 #endif // wxUSE_TIMEPICKCTRL