Always handle "input" and "output" signals in wxGTK wxSpinCtrl
No real changes yet, just refactor the code to always connect these signals handlers and not just when using non-decimal base. Add wxSpinCtrl::GTKInput() and GTKOutput() virtual methods to handle these signals appropriately depending on the type of the control. This will allow further customizing conversion to/from string in the upcoming commit and will also make it possible to handle UI locale different from the C locale in the future.
This commit is contained in:
parent
8194f05553
commit
3ac3763705
@ -58,6 +58,19 @@ public:
|
|||||||
// implementation
|
// implementation
|
||||||
void OnChar( wxKeyEvent &event );
|
void OnChar( wxKeyEvent &event );
|
||||||
|
|
||||||
|
|
||||||
|
// These values map to the possible return values of "input" GTK signal but
|
||||||
|
// are more readable and type-safe.
|
||||||
|
enum GTKInputResult
|
||||||
|
{
|
||||||
|
GTKInput_Error = -1,
|
||||||
|
GTKInput_Default,
|
||||||
|
GTKInput_Converted
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual GTKInputResult GTKInput(double* value) const = 0;
|
||||||
|
virtual bool GTKOutput(wxString* text) const = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
double DoGetValue() const;
|
double DoGetValue() const;
|
||||||
double DoGetMin() const;
|
double DoGetMin() const;
|
||||||
@ -138,6 +151,9 @@ public:
|
|||||||
virtual int GetBase() const wxOVERRIDE { return m_base; }
|
virtual int GetBase() const wxOVERRIDE { return m_base; }
|
||||||
virtual bool SetBase(int base) wxOVERRIDE;
|
virtual bool SetBase(int base) wxOVERRIDE;
|
||||||
|
|
||||||
|
virtual GTKInputResult GTKInput(double* value) const wxOVERRIDE;
|
||||||
|
virtual bool GTKOutput(wxString* text) const wxOVERRIDE;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void GtkSetEntryWidth() wxOVERRIDE;
|
virtual void GtkSetEntryWidth() wxOVERRIDE;
|
||||||
|
|
||||||
@ -206,6 +222,9 @@ public:
|
|||||||
virtual int GetBase() const wxOVERRIDE { return 10; }
|
virtual int GetBase() const wxOVERRIDE { return 10; }
|
||||||
virtual bool SetBase(int WXUNUSED(base)) wxOVERRIDE { return false; }
|
virtual bool SetBase(int WXUNUSED(base)) wxOVERRIDE { return false; }
|
||||||
|
|
||||||
|
virtual GTKInputResult GTKInput(double* value) const wxOVERRIDE;
|
||||||
|
virtual bool GTKOutput(wxString* text) const wxOVERRIDE;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void GtkSetEntryWidth() wxOVERRIDE;
|
virtual void GtkSetEntryWidth() wxOVERRIDE;
|
||||||
|
|
||||||
|
@ -77,6 +77,47 @@ gtk_changed(GtkSpinButton*, wxSpinCtrl* win)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// "input" and "output"
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
|
||||||
|
static gint
|
||||||
|
wx_gtk_spin_input(GtkSpinButton*, gdouble* val, wxSpinCtrlGTKBase* win)
|
||||||
|
{
|
||||||
|
switch ( win->GTKInput(val) )
|
||||||
|
{
|
||||||
|
case wxSpinCtrl::GTKInput_Error:
|
||||||
|
return GTK_INPUT_ERROR;
|
||||||
|
|
||||||
|
case wxSpinCtrl::GTKInput_Default:
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
case wxSpinCtrl::GTKInput_Converted:
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxFAIL_MSG("unreachable");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gint
|
||||||
|
wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrlGTKBase* win)
|
||||||
|
{
|
||||||
|
wxString text;
|
||||||
|
if ( !win->GTKOutput(&text) )
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
if ( text != win->GetTextValue() )
|
||||||
|
gtk_entry_set_text(GTK_ENTRY(spin), text.utf8_str());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// wxSpinCtrlEventDisabler: helper to temporarily disable GTK+ events
|
// wxSpinCtrlEventDisabler: helper to temporarily disable GTK+ events
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@ -146,6 +187,9 @@ bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
|
|||||||
g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
|
g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
|
||||||
g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
|
g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
|
||||||
|
|
||||||
|
g_signal_connect(m_widget, "input", G_CALLBACK(wx_gtk_spin_input), this);
|
||||||
|
g_signal_connect(m_widget, "output", G_CALLBACK(wx_gtk_spin_output), this);
|
||||||
|
|
||||||
m_parent->DoAddChild( this );
|
m_parent->DoAddChild( this );
|
||||||
|
|
||||||
PostCreation(size);
|
PostCreation(size);
|
||||||
@ -399,42 +443,6 @@ wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
|
|||||||
// wxSpinCtrl
|
// wxSpinCtrl
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
wx_gtk_spin_input(GtkSpinButton* spin, gdouble* val, wxSpinCtrl* win)
|
|
||||||
{
|
|
||||||
// We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
|
|
||||||
// wxString function even if this requires an extra conversion.
|
|
||||||
const wxString
|
|
||||||
text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin))));
|
|
||||||
|
|
||||||
long lval;
|
|
||||||
if ( !text.ToLong(&lval, win->GetBase()) )
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
*val = lval;
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gint
|
|
||||||
wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrl* win)
|
|
||||||
{
|
|
||||||
const gint val = gtk_spin_button_get_value_as_int(spin);
|
|
||||||
|
|
||||||
gtk_entry_set_text
|
|
||||||
(
|
|
||||||
GTK_ENTRY(spin),
|
|
||||||
wxSpinCtrlImpl::FormatAsHex(val, win->GetMax()).utf8_str()
|
|
||||||
);
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // extern "C"
|
|
||||||
|
|
||||||
void wxSpinCtrl::GtkSetEntryWidth()
|
void wxSpinCtrl::GtkSetEntryWidth()
|
||||||
{
|
{
|
||||||
const int minVal = static_cast<int>(DoGetMin());
|
const int minVal = static_cast<int>(DoGetMin());
|
||||||
@ -467,23 +475,6 @@ bool wxSpinCtrl::SetBase(int base)
|
|||||||
// We need to be able to enter letters for any base greater than 10.
|
// We need to be able to enter letters for any base greater than 10.
|
||||||
gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget), m_base <= 10 );
|
gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget), m_base <= 10 );
|
||||||
|
|
||||||
if ( m_base != 10 )
|
|
||||||
{
|
|
||||||
g_signal_connect( m_widget, "input",
|
|
||||||
G_CALLBACK(wx_gtk_spin_input), this);
|
|
||||||
g_signal_connect( m_widget, "output",
|
|
||||||
G_CALLBACK(wx_gtk_spin_output), this);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g_signal_handlers_disconnect_by_func(m_widget,
|
|
||||||
(gpointer)wx_gtk_spin_input,
|
|
||||||
this);
|
|
||||||
g_signal_handlers_disconnect_by_func(m_widget,
|
|
||||||
(gpointer)wx_gtk_spin_output,
|
|
||||||
this);
|
|
||||||
}
|
|
||||||
|
|
||||||
InvalidateBestSize();
|
InvalidateBestSize();
|
||||||
|
|
||||||
GtkSetEntryWidth();
|
GtkSetEntryWidth();
|
||||||
@ -494,6 +485,44 @@ bool wxSpinCtrl::SetBase(int base)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxSpinCtrl::GTKInputResult
|
||||||
|
wxSpinCtrl::GTKInput(double* value) const
|
||||||
|
{
|
||||||
|
// Don't override the default logic unless really needed.
|
||||||
|
if ( GetBase() == 10 )
|
||||||
|
return GTKInput_Default;
|
||||||
|
|
||||||
|
long lval;
|
||||||
|
if ( !GetTextValue().ToLong(&lval, GetBase()) )
|
||||||
|
return GTKInput_Error;
|
||||||
|
|
||||||
|
*value = lval;
|
||||||
|
|
||||||
|
return GTKInput_Converted;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxSpinCtrl::GTKOutput(wxString* text) const
|
||||||
|
{
|
||||||
|
switch ( GetBase() )
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
wxFAIL_MSG("unsupported base");
|
||||||
|
wxFALLTHROUGH;
|
||||||
|
|
||||||
|
case 10:
|
||||||
|
// Don't override the default output format unless really needed.
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case 16:
|
||||||
|
const gint val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(m_widget));
|
||||||
|
|
||||||
|
*text = wxSpinCtrlImpl::FormatAsHex(val, GetMax());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// wxSpinCtrlDouble
|
// wxSpinCtrlDouble
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -528,4 +557,15 @@ void wxSpinCtrlDouble::SetDigits(unsigned digits)
|
|||||||
GtkSetEntryWidth();
|
GtkSetEntryWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxSpinCtrl::GTKInputResult
|
||||||
|
wxSpinCtrlDouble::GTKInput(double* WXUNUSED(value)) const
|
||||||
|
{
|
||||||
|
return GTKInput_Default;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxSpinCtrlDouble::GTKOutput(wxString* WXUNUSED(text)) const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // wxUSE_SPINCTRL
|
#endif // wxUSE_SPINCTRL
|
||||||
|
Loading…
Reference in New Issue
Block a user