X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8cd6a9ad50c00a94e62558a3b55e814028d81100..7735b2ea691f41d7a7f007befbe969386873b86e:/src/generic/spinctlg.cpp diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 06eeb9354e..84cc154b40 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -52,15 +52,15 @@ static const wxCoord MARGIN = 2; #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble // ---------------------------------------------------------------------------- -// wxSpinCtrlText: text control used by spin control +// wxSpinCtrlTextGeneric: text control used by spin control // ---------------------------------------------------------------------------- -class wxSpinCtrlText : public wxTextCtrl +class wxSpinCtrlTextGeneric : public wxTextCtrl { public: - wxSpinCtrlText(wxSpinCtrlGenericBase *spin, const wxString& value) - : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, - wxDefaultSize, wxTE_NOHIDESEL|wxTE_PROCESS_ENTER) + wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0) + : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, wxDefaultSize, + ( style & wxALIGN_MASK ) | wxTE_NOHIDESEL | wxTE_PROCESS_ENTER) { m_spin = spin; @@ -68,7 +68,7 @@ public: SetSizeHints(wxDefaultCoord, wxDefaultCoord); } - virtual ~wxSpinCtrlText() + virtual ~wxSpinCtrlTextGeneric() { // MSW sends extra kill focus event on destroy if (m_spin) @@ -106,22 +106,22 @@ private: DECLARE_EVENT_TABLE() }; -BEGIN_EVENT_TABLE(wxSpinCtrlText, wxTextCtrl) - EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlText::OnTextEnter) +BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl) + EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEnter) - EVT_CHAR(wxSpinCtrlText::OnChar) + EVT_CHAR(wxSpinCtrlTextGeneric::OnChar) - EVT_KILL_FOCUS(wxSpinCtrlText::OnKillFocus) + EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus) END_EVENT_TABLE() // ---------------------------------------------------------------------------- -// wxSpinCtrlButton: spin button used by spin control +// wxSpinCtrlButtonGeneric: spin button used by spin control // ---------------------------------------------------------------------------- -class wxSpinCtrlButton : public wxSpinButton +class wxSpinCtrlButtonGeneric : public wxSpinButton { public: - wxSpinCtrlButton(wxSpinCtrlGenericBase *spin, int style) + wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style) : wxSpinButton(spin->GetParent(), wxID_ANY, wxDefaultPosition, wxDefaultSize, style | wxSP_VERTICAL) { @@ -145,9 +145,9 @@ private: DECLARE_EVENT_TABLE() }; -BEGIN_EVENT_TABLE(wxSpinCtrlButton, wxSpinButton) - EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButton::OnSpinButton) - EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButton::OnSpinButton) +BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton) + EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton) + EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton) END_EVENT_TABLE() // ============================================================================ @@ -186,7 +186,8 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent, // the text control borders (but we might want to use style border bits to // select the text control style) if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, - wxBORDER_NONE, wxDefaultValidator, name) ) + (style & ~wxBORDER_MASK) | wxBORDER_NONE, + wxDefaultValidator, name) ) { return false; } @@ -196,8 +197,8 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent, m_max = max; m_increment = increment; - m_textCtrl = new wxSpinCtrlText(this, value); - m_spinButton = new wxSpinCtrlButton(this, style); + m_textCtrl = new wxSpinCtrlTextGeneric(this, value, style); + m_spinButton = new wxSpinCtrlButtonGeneric(this, style); m_spin_value = m_spinButton->GetValue(); @@ -239,10 +240,10 @@ wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase() if (m_textCtrl) { - // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlText - wxDynamicCast(m_textCtrl, wxSpinCtrlText)->m_spin = NULL; + // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric + wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL; - wxSpinCtrlText *text = (wxSpinCtrlText*)m_textCtrl; + wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl; m_textCtrl = NULL; delete text; } @@ -306,7 +307,7 @@ bool wxSpinCtrlGenericBase::Show(bool show) return true; } -bool wxSpinCtrlGenericBase::Reparent(wxWindow *newParent) +bool wxSpinCtrlGenericBase::Reparent(wxWindowBase *newParent) { if ( m_spinButton ) { @@ -337,11 +338,7 @@ void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event) if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0))) step *= abs(spin_value - m_spin_value); - double value = m_value + step*m_increment; - - // we can always reach the ends using the spinbutton - if (value < m_min) value = m_min; - if (value > m_max) value = m_max; + double value = AdjustToFitInRange(m_value + step*m_increment); // Ignore the edges when it wraps since the up/down event may be opposite // They are in GTK and Mac @@ -353,7 +350,7 @@ void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event) m_spin_value = spin_value; - if (InRange(value) && DoSetValue(value)) + if ( DoSetValue(value) ) DoSendEvent(); } @@ -366,6 +363,12 @@ void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent& event) void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event) { + if ( !HasFlag(wxSP_ARROW_KEYS) ) + { + event.Skip(); + return; + } + double value = m_value; switch ( event.GetKeyCode() ) { @@ -390,6 +393,8 @@ void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event) return; } + value = AdjustToFitInRange(value); + if ( m_textCtrl && m_textCtrl->IsModified() ) SyncSpinToText(); @@ -432,7 +437,7 @@ void wxSpinCtrlGenericBase::SyncSpinToText() void wxSpinCtrlGenericBase::SetValue(const wxString& text) { - wxCHECK_RET( m_textCtrl, _T("invalid call to wxSpinCtrl::SetValue") ); + wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") ); double val; if ( text.ToDouble(&val) && InRange(val) ) @@ -448,7 +453,7 @@ void wxSpinCtrlGenericBase::SetValue(const wxString& text) bool wxSpinCtrlGenericBase::DoSetValue(double val) { - wxCHECK_MSG( m_textCtrl, false, _T("invalid call to wxSpinCtrl::SetValue") ); + wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") ); if (!InRange(val)) return false; @@ -480,6 +485,16 @@ bool wxSpinCtrlGenericBase::DoSetValue(double val) return false; } +double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const +{ + if (value < m_min) + value = HasFlag(wxSP_WRAP) ? m_max : m_min; + if (value > m_max) + value = HasFlag(wxSP_WRAP) ? m_min : m_max; + + return value; +} + void wxSpinCtrlGenericBase::DoSetRange(double min, double max) { m_min = min; @@ -499,7 +514,7 @@ void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks) void wxSpinCtrlGenericBase::SetSelection(long from, long to) { - wxCHECK_RET( m_textCtrl, _T("invalid call to wxSpinCtrl::SetSelection") ); + wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") ); m_textCtrl->SetSelection(from, to); }