]>
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
);
305 void OnArrowUp(wxSpinEvent
& WXUNUSED(event
))
307 ChangeCurrentFieldBy1(Dir_Up
);
310 void OnArrowDown(wxSpinEvent
& WXUNUSED(event
))
312 ChangeCurrentFieldBy1(Dir_Down
);
316 // Get the range of the given field in character positions ("from" is
317 // inclusive, "to" exclusive).
318 static CharRange
GetFieldRange(Field field
)
320 // Currently we can just hard code the ranges as they are the same for
321 // both supported formats, if we want to support arbitrary formats in
322 // the future, we'd need to determine them dynamically by examining the
324 static const CharRange ranges
[] =
332 wxCOMPILE_TIME_ASSERT( WXSIZEOF(ranges
) == Field_Max
,
333 FieldRangesMismatch
);
335 return ranges
[field
];
338 // Get the last field used depending on m_useAMPM.
339 Field
GetLastField() const
341 return m_useAMPM
? Field_AMPM
: Field_Sec
;
344 // Change the current field. For convenience, accept int field here as this
345 // allows us to use arithmetic operations in the caller.
346 void ChangeCurrentField(int field
)
348 if ( field
== m_currentField
)
351 wxCHECK_RET( field
<= GetLastField(), "Invalid field" );
353 m_currentField
= static_cast<Field
>(field
);
354 m_isFirstDigit
= true;
356 HighlightCurrentField();
359 // Go to the next (Dir_Up) or previous (Dir_Down) field, wrapping if
361 void CycleCurrentField(Direction dir
)
363 const unsigned numFields
= GetLastField() + 1;
365 ChangeCurrentField((m_currentField
+ numFields
+ dir
) % numFields
);
368 // Select the currently actively field.
369 void HighlightCurrentField()
373 const CharRange range
= GetFieldRange(m_currentField
);
375 m_text
->SetSelection(range
.from
, range
.to
);
378 // Decrement or increment the value of the current field (wrapping if
380 void ChangeCurrentFieldBy1(Direction dir
)
382 switch ( m_currentField
)
385 m_time
.SetHour((m_time
.GetHour() + 24 + dir
) % 24);
389 m_time
.SetMinute((m_time
.GetMinute() + 60 + dir
) % 60);
393 m_time
.SetSecond((m_time
.GetSecond() + 60 + dir
) % 60);
397 m_time
.SetHour((m_time
.GetHour() + 12) % 24);
401 wxFAIL_MSG( "Invalid field" );
407 // Set the current field to its minimal or maximal value.
408 void ResetCurrentField(Direction dir
)
410 switch ( m_currentField
)
414 // In 12-hour mode setting the hour to the minimal value
415 // also changes the suffix to AM and, correspondingly,
416 // setting it to the maximal one changes the suffix to PM.
417 // And, for consistency with the native MSW behaviour, we
418 // also do the same thing when changing AM/PM field itself,
419 // so change hours in any case.
420 m_time
.SetHour(dir
== Dir_Down
? 0 : 23);
424 m_time
.SetMinute(dir
== Dir_Down
? 0 : 59);
428 m_time
.SetSecond(dir
== Dir_Down
? 0 : 59);
432 wxFAIL_MSG( "Invalid field" );
438 // Append the given digit (from 0 to 9) to the current value of the current
440 void AppendDigitToCurrentField(int n
)
442 bool moveToNextField
= false;
444 if ( !m_isFirstDigit
)
446 // The first digit simply replaces the existing field contents,
447 // but the second one should be combined with the previous one,
448 // otherwise entering 2-digit numbers would be impossible.
449 int currentValue
wxDUMMY_INITIALIZE(0),
450 maxValue
wxDUMMY_INITIALIZE(0);
452 switch ( m_currentField
)
455 currentValue
= m_time
.GetHour();
460 currentValue
= m_time
.GetMinute();
465 currentValue
= m_time
.GetSecond();
471 wxFAIL_MSG( "Invalid field" );
474 // Check if the new value is acceptable. If not, we just handle
475 // this digit as if it were the first one.
476 int newValue
= currentValue
*10 + n
;
477 if ( newValue
< maxValue
)
481 // If we're not on the seconds field, advance to the next one.
482 // This makes it more convenient to enter times as you can just
483 // press all digits one after one without touching the cursor
484 // arrow keys at all.
486 // Notice that MSW native control doesn't do this but it seems
487 // so useful that we intentionally diverge from it here.
488 moveToNextField
= true;
490 // We entered both digits so the next one will be "first" again.
491 m_isFirstDigit
= true;
494 else // First digit entered.
496 // The next one won't be first any more.
497 m_isFirstDigit
= false;
500 switch ( m_currentField
)
516 wxFAIL_MSG( "Invalid field" );
519 if ( moveToNextField
&& m_currentField
< Field_Sec
)
520 CycleCurrentField(Dir_Up
);
525 // Update the text value to correspond to the current time. By default also
526 // generate an event but this can be avoided by calling the "WithoutEvent"
530 UpdateTextWithoutEvent();
532 wxWindow
* const ctrl
= m_text
->GetParent();
534 wxDateEvent
event(ctrl
, m_time
, wxEVT_TIME_CHANGED
);
535 ctrl
->HandleWindowEvent(event
);
538 void UpdateTextWithoutEvent()
540 m_text
->SetValue(m_time
.Format(m_useAMPM
? "%I:%M:%S %p" : "%H:%M:%S"));
542 HighlightCurrentField();
546 // The current field of the text control: this is the one affected by
547 // pressing arrow keys or spin button.
548 Field m_currentField
;
550 // Flag indicating whether we use AM/PM indicator or not.
553 // Flag indicating whether the next digit pressed by user will be the first
554 // digit of the current field or the second one. This is necessary because
555 // the first digit replaces the current field contents while the second one
556 // is appended to it (if possible, e.g. pressing '7' in a field already
557 // containing '8' will still replace it as "78" would be invalid).
560 wxDECLARE_NO_COPY_CLASS(wxTimePickerGenericImpl
);
563 // ============================================================================
564 // wxTimePickerCtrlGeneric implementation
565 // ============================================================================
567 // ----------------------------------------------------------------------------
568 // wxTimePickerCtrlGeneric creation
569 // ----------------------------------------------------------------------------
571 void wxTimePickerCtrlGeneric::Init()
577 wxTimePickerCtrlGeneric::Create(wxWindow
*parent
,
579 const wxDateTime
& date
,
583 const wxValidator
& validator
,
584 const wxString
& name
)
586 // The text control we use already has a border, so we don't need one
588 style
&= ~wxBORDER_MASK
;
589 style
|= wxBORDER_NONE
;
591 if ( !Base::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
594 m_impl
= new wxTimePickerGenericImpl(this);
595 m_impl
->SetValue(date
);
597 InvalidateBestSize();
598 SetInitialSize(size
);
603 wxTimePickerCtrlGeneric::~wxTimePickerCtrlGeneric()
608 wxWindowList
wxTimePickerCtrlGeneric::GetCompositeWindowParts() const
613 parts
.push_back(m_impl
->m_text
);
614 parts
.push_back(m_impl
->m_btn
);
619 // ----------------------------------------------------------------------------
620 // wxTimePickerCtrlGeneric value
621 // ----------------------------------------------------------------------------
623 void wxTimePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
625 wxCHECK_RET( m_impl
, "Must create first" );
627 m_impl
->SetValue(date
);
630 wxDateTime
wxTimePickerCtrlGeneric::GetValue() const
632 wxCHECK_MSG( m_impl
, wxDateTime(), "Must create first" );
634 return m_impl
->m_time
;
637 // ----------------------------------------------------------------------------
638 // wxTimePickerCtrlGeneric geometry
639 // ----------------------------------------------------------------------------
641 void wxTimePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int width
, int height
)
643 Base::DoMoveWindow(x
, y
, width
, height
);
648 const int widthBtn
= m_impl
->m_btn
->GetSize().x
;
649 const int widthText
= width
- widthBtn
- HMARGIN_TEXT_SPIN
;
651 m_impl
->m_text
->SetSize(0, 0, widthText
, height
);
652 m_impl
->m_btn
->SetSize(widthText
+ HMARGIN_TEXT_SPIN
, 0, widthBtn
, height
);
655 wxSize
wxTimePickerCtrlGeneric::DoGetBestSize() const
658 return Base::DoGetBestSize();
660 wxSize size
= m_impl
->m_text
->GetBestSize();
661 size
.x
+= m_impl
->m_btn
->GetBestSize().x
+ HMARGIN_TEXT_SPIN
;
666 #endif // !wxHAS_NATIVE_TIMEPICKERCTRL || wxUSE_TIMEPICKCTRL_GENERIC
668 #endif // wxUSE_TIMEPICKCTRL