]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/timectrl.cpp
38218a6a62b99241031eb00ad2a6d340898e1db5
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
);
77 m_currentField
= Field_Hour
;
78 m_isFirstDigit
= true;
80 // We don't support arbitrary formats currently as this requires
81 // significantly more work both here and also in wxLocale::GetInfo().
83 // For now just use either "%H:%M:%S" or "%I:%M:%S %p". It would be
84 // nice to add support to "%k" and "%l" (hours with leading blanks
85 // instead of zeros) too as this is the most common unsupported case in
87 m_useAMPM
= wxLocale::GetInfo(wxLOCALE_TIME_FMT
).Contains("%p");
92 wxFocusEventHandler(wxTimePickerGenericImpl::OnTextSetFocus
),
99 wxKeyEventHandler(wxTimePickerGenericImpl::OnTextKeyDown
),
106 wxMouseEventHandler(wxTimePickerGenericImpl::OnTextClick
),
114 wxSpinEventHandler(wxTimePickerGenericImpl::OnArrowUp
),
121 wxSpinEventHandler(wxTimePickerGenericImpl::OnArrowDown
),
127 // Set the new value.
128 void SetValue(const wxDateTime
& time
)
130 m_time
= time
.IsValid() ? time
: wxDateTime::Now();
132 UpdateTextWithoutEvent();
136 // The text part of the control.
139 // The spin button used to change the text fields.
142 // The current time (date part is ignored).
146 // The logical fields of the text control (AM/PM one may not be present).
156 // Direction of change of time fields.
159 // Notice that the enum elements values matter.
164 // A range of character positions ("from" is inclusive, "to" -- exclusive).
171 // Event handlers for various events in our controls.
172 void OnTextSetFocus(wxFocusEvent
& event
)
174 HighlightCurrentField();
179 // Keyboard interface here is modelled over MSW native control and may need
180 // adjustments for other platforms.
181 void OnTextKeyDown(wxKeyEvent
& event
)
183 const int key
= event
.GetKeyCode();
188 ChangeCurrentFieldBy1(Dir_Down
);
192 ChangeCurrentFieldBy1(Dir_Up
);
196 CycleCurrentField(Dir_Down
);
200 CycleCurrentField(Dir_Up
);
204 ResetCurrentField(Dir_Down
);
208 ResetCurrentField(Dir_Up
);
221 // The digits work in all keys except AM/PM.
222 if ( m_currentField
!= Field_AMPM
)
224 AppendDigitToCurrentField(key
- '0');
230 // These keys only work to toggle AM/PM field.
231 if ( m_currentField
== Field_AMPM
)
233 unsigned hour
= m_time
.GetHour();
245 if ( hour
!= m_time
.GetHour() )
247 m_time
.SetHour(hour
);
253 // Do not skip the other events, just consume them to prevent the
254 // user from editing the text directly.
258 void OnTextClick(wxMouseEvent
& event
)
260 Field field
wxDUMMY_INITIALIZE(Field_Max
);
262 switch ( m_text
->HitTest(event
.GetPosition(), &pos
) )
264 case wxTE_HT_UNKNOWN
:
265 // Don't do anything, it's better than doing something wrong.
269 // Select the first field.
273 case wxTE_HT_ON_TEXT
:
274 // Find the field containing this position.
275 for ( field
= Field_Hour
; field
<= GetLastField(); )
277 const CharRange range
= GetFieldRange(field
);
279 // Normally the "to" end is exclusive but we want to give
280 // focus to some field when the user clicks between them so
281 // count it as part of the preceding field here.
282 if ( range
.from
<= pos
&& pos
<= range
.to
)
285 field
= static_cast<Field
>(field
+ 1);
290 // This shouldn't happen for single line control.
291 wxFAIL_MSG( "Unreachable" );
295 // Select the last field.
296 field
= GetLastField();
300 ChangeCurrentField(field
);
302 // As we don't skip the event, we also prevent the system from setting
303 // focus to this control as it does by default, so do it manually.
307 void OnArrowUp(wxSpinEvent
& WXUNUSED(event
))
309 ChangeCurrentFieldBy1(Dir_Up
);
314 void OnArrowDown(wxSpinEvent
& WXUNUSED(event
))
316 ChangeCurrentFieldBy1(Dir_Down
);
322 // Get the range of the given field in character positions ("from" is
323 // inclusive, "to" exclusive).
324 static CharRange
GetFieldRange(Field field
)
326 // Currently we can just hard code the ranges as they are the same for
327 // both supported formats, if we want to support arbitrary formats in
328 // the future, we'd need to determine them dynamically by examining the
330 static const CharRange ranges
[] =
338 wxCOMPILE_TIME_ASSERT( WXSIZEOF(ranges
) == Field_Max
,
339 FieldRangesMismatch
);
341 return ranges
[field
];
344 // Get the last field used depending on m_useAMPM.
345 Field
GetLastField() const
347 return m_useAMPM
? Field_AMPM
: Field_Sec
;
350 // Change the current field. For convenience, accept int field here as this
351 // allows us to use arithmetic operations in the caller.
352 void ChangeCurrentField(int field
)
354 if ( field
== m_currentField
)
357 wxCHECK_RET( field
<= GetLastField(), "Invalid field" );
359 m_currentField
= static_cast<Field
>(field
);
360 m_isFirstDigit
= true;
362 HighlightCurrentField();
365 // Go to the next (Dir_Up) or previous (Dir_Down) field, wrapping if
367 void CycleCurrentField(Direction dir
)
369 const unsigned numFields
= GetLastField() + 1;
371 ChangeCurrentField((m_currentField
+ numFields
+ dir
) % numFields
);
374 // Select the currently actively field.
375 void HighlightCurrentField()
377 const CharRange range
= GetFieldRange(m_currentField
);
379 m_text
->SetSelection(range
.from
, range
.to
);
382 // Decrement or increment the value of the current field (wrapping if
384 void ChangeCurrentFieldBy1(Direction dir
)
386 switch ( m_currentField
)
389 m_time
.SetHour((m_time
.GetHour() + 24 + dir
) % 24);
393 m_time
.SetMinute((m_time
.GetMinute() + 60 + dir
) % 60);
397 m_time
.SetSecond((m_time
.GetSecond() + 60 + dir
) % 60);
401 m_time
.SetHour((m_time
.GetHour() + 12) % 24);
405 wxFAIL_MSG( "Invalid field" );
411 // Set the current field to its minimal or maximal value.
412 void ResetCurrentField(Direction dir
)
414 switch ( m_currentField
)
418 // In 12-hour mode setting the hour to the minimal value
419 // also changes the suffix to AM and, correspondingly,
420 // setting it to the maximal one changes the suffix to PM.
421 // And, for consistency with the native MSW behaviour, we
422 // also do the same thing when changing AM/PM field itself,
423 // so change hours in any case.
424 m_time
.SetHour(dir
== Dir_Down
? 0 : 23);
428 m_time
.SetMinute(dir
== Dir_Down
? 0 : 59);
432 m_time
.SetSecond(dir
== Dir_Down
? 0 : 59);
436 wxFAIL_MSG( "Invalid field" );
442 // Append the given digit (from 0 to 9) to the current value of the current
444 void AppendDigitToCurrentField(int n
)
446 bool moveToNextField
= false;
448 if ( !m_isFirstDigit
)
450 // The first digit simply replaces the existing field contents,
451 // but the second one should be combined with the previous one,
452 // otherwise entering 2-digit numbers would be impossible.
453 int currentValue
wxDUMMY_INITIALIZE(0),
454 maxValue
wxDUMMY_INITIALIZE(0);
456 switch ( m_currentField
)
459 currentValue
= m_time
.GetHour();
464 currentValue
= m_time
.GetMinute();
469 currentValue
= m_time
.GetSecond();
475 wxFAIL_MSG( "Invalid field" );
478 // Check if the new value is acceptable. If not, we just handle
479 // this digit as if it were the first one.
480 int newValue
= currentValue
*10 + n
;
481 if ( newValue
< maxValue
)
485 // If we're not on the seconds field, advance to the next one.
486 // This makes it more convenient to enter times as you can just
487 // press all digits one after one without touching the cursor
488 // arrow keys at all.
490 // Notice that MSW native control doesn't do this but it seems
491 // so useful that we intentionally diverge from it here.
492 moveToNextField
= true;
494 // We entered both digits so the next one will be "first" again.
495 m_isFirstDigit
= true;
498 else // First digit entered.
500 // The next one won't be first any more.
501 m_isFirstDigit
= false;
504 switch ( m_currentField
)
520 wxFAIL_MSG( "Invalid field" );
523 if ( moveToNextField
&& m_currentField
< Field_Sec
)
524 CycleCurrentField(Dir_Up
);
529 // Update the text value to correspond to the current time. By default also
530 // generate an event but this can be avoided by calling the "WithoutEvent"
534 UpdateTextWithoutEvent();
536 wxWindow
* const ctrl
= m_text
->GetParent();
538 wxDateEvent
event(ctrl
, m_time
, wxEVT_TIME_CHANGED
);
539 ctrl
->HandleWindowEvent(event
);
542 void UpdateTextWithoutEvent()
544 m_text
->SetValue(m_time
.Format(m_useAMPM
? "%I:%M:%S %p" : "%H:%M:%S"));
546 HighlightCurrentField();
550 // The current field of the text control: this is the one affected by
551 // pressing arrow keys or spin button.
552 Field m_currentField
;
554 // Flag indicating whether we use AM/PM indicator or not.
557 // Flag indicating whether the next digit pressed by user will be the first
558 // digit of the current field or the second one. This is necessary because
559 // the first digit replaces the current field contents while the second one
560 // is appended to it (if possible, e.g. pressing '7' in a field already
561 // containing '8' will still replace it as "78" would be invalid).
564 wxDECLARE_NO_COPY_CLASS(wxTimePickerGenericImpl
);
567 // ============================================================================
568 // wxTimePickerCtrlGeneric implementation
569 // ============================================================================
571 // ----------------------------------------------------------------------------
572 // wxTimePickerCtrlGeneric creation
573 // ----------------------------------------------------------------------------
575 void wxTimePickerCtrlGeneric::Init()
581 wxTimePickerCtrlGeneric::Create(wxWindow
*parent
,
583 const wxDateTime
& date
,
587 const wxValidator
& validator
,
588 const wxString
& name
)
590 // The text control we use already has a border, so we don't need one
592 style
&= ~wxBORDER_MASK
;
593 style
|= wxBORDER_NONE
;
595 if ( !Base::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
598 m_impl
= new wxTimePickerGenericImpl(this);
599 m_impl
->SetValue(date
);
601 InvalidateBestSize();
602 SetInitialSize(size
);
607 wxTimePickerCtrlGeneric::~wxTimePickerCtrlGeneric()
612 wxWindowList
wxTimePickerCtrlGeneric::GetCompositeWindowParts() const
617 parts
.push_back(m_impl
->m_text
);
618 parts
.push_back(m_impl
->m_btn
);
623 // ----------------------------------------------------------------------------
624 // wxTimePickerCtrlGeneric value
625 // ----------------------------------------------------------------------------
627 void wxTimePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
629 wxCHECK_RET( m_impl
, "Must create first" );
631 m_impl
->SetValue(date
);
634 wxDateTime
wxTimePickerCtrlGeneric::GetValue() const
636 wxCHECK_MSG( m_impl
, wxDateTime(), "Must create first" );
638 return m_impl
->m_time
;
641 // ----------------------------------------------------------------------------
642 // wxTimePickerCtrlGeneric geometry
643 // ----------------------------------------------------------------------------
645 void wxTimePickerCtrlGeneric::DoMoveWindow(int x
, int y
, int width
, int height
)
647 Base::DoMoveWindow(x
, y
, width
, height
);
652 const int widthBtn
= m_impl
->m_btn
->GetSize().x
;
653 const int widthText
= width
- widthBtn
- HMARGIN_TEXT_SPIN
;
655 m_impl
->m_text
->SetSize(0, 0, widthText
, height
);
656 m_impl
->m_btn
->SetSize(widthText
+ HMARGIN_TEXT_SPIN
, 0, widthBtn
, height
);
659 wxSize
wxTimePickerCtrlGeneric::DoGetBestSize() const
662 return Base::DoGetBestSize();
664 wxSize size
= m_impl
->m_text
->GetBestSize();
665 size
.x
+= m_impl
->m_btn
->GetBestSize().x
+ HMARGIN_TEXT_SPIN
;
670 #endif // !wxHAS_NATIVE_TIMEPICKERCTRL || wxUSE_TIMEPICKCTRL_GENERIC
672 #endif // wxUSE_TIMEPICKCTRL