1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/spinctrl.cpp
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/spinctrl.h"
19 #include "wx/textctrl.h" // for wxEVT_TEXT
24 #include "wx/gtk1/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 static const float sensitivity
= 0.02;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
48 wx_gtk_spin_input(GtkSpinButton
* spin
, gdouble
* val
, wxSpinCtrl
* win
)
50 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
51 // wxString function even if this requires an extra conversion.
53 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin
))));
56 if ( !text
.ToLong(&lval
, win
->GetBase()) )
65 wx_gtk_spin_output(GtkSpinButton
* spin
, wxSpinCtrl
* win
)
67 const gint val
= gtk_spin_button_get_value_as_int(spin
);
72 wxPrivate::wxSpinCtrlFormatAsHex(val
, win
->GetMax()).utf8_str()
78 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
80 if (g_isIdle
) wxapp_install_idle_handler();
82 if (!win
->m_hasVMT
) return;
83 if (g_blockEventsOnDrag
) return;
85 wxCommandEvent
event( wxEVT_SPINCTRL
, win
->GetId());
86 event
.SetEventObject( win
);
88 // note that we don't use wxSpinCtrl::GetValue() here because it would
89 // adjust the value to fit into the control range and this means that we
90 // would never be able to enter an "invalid" value in the control, even
91 // temporarily - and trying to enter 10 into the control which accepts the
92 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
94 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
95 win
->HandleWindowEvent( event
);
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
105 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
107 if (!win
->m_hasVMT
) return;
110 wxapp_install_idle_handler();
112 wxCommandEvent
event( wxEVT_TEXT
, win
->GetId() );
113 event
.SetEventObject( win
);
116 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
117 win
->HandleWindowEvent( event
);
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
125 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
126 EVT_CHAR(wxSpinCtrl::OnChar
)
129 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
130 const wxString
& value
,
131 const wxPoint
& pos
, const wxSize
& size
,
133 int min
, int max
, int initial
,
134 const wxString
& name
)
137 m_acceptsFocus
= true;
139 if (!PreCreation( parent
, pos
, size
) ||
140 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
142 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
148 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
150 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
152 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
153 (int)(m_windowStyle
& wxSP_WRAP
) );
157 m_parent
->DoAddChild( this );
166 void wxSpinCtrl::GtkDisableEvents()
168 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
169 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
172 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
173 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
177 void wxSpinCtrl::GtkEnableEvents()
179 gtk_signal_connect( GTK_OBJECT (m_adjust
),
181 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
184 gtk_signal_connect( GTK_OBJECT(m_widget
),
186 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
190 int wxSpinCtrl::GetMin() const
192 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
194 return (int)ceil(m_adjust
->lower
);
197 int wxSpinCtrl::GetMax() const
199 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
201 return (int)ceil(m_adjust
->upper
);
204 int wxSpinCtrl::GetValue() const
206 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
208 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
210 return (int)ceil(m_adjust
->value
);
213 void wxSpinCtrl::SetValue( const wxString
& value
)
215 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
218 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
225 // invalid number - set text as is (wxMSW compatible)
227 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
232 void wxSpinCtrl::SetValue( int value
)
234 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
236 float fpos
= (float)value
;
238 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
240 m_adjust
->value
= fpos
;
243 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
247 void wxSpinCtrl::SetSelection(long from
, long to
)
249 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
251 if ( from
== -1 && to
== -1 )
257 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
260 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
262 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
264 float fmin
= (float)minVal
;
265 float fmax
= (float)maxVal
;
267 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
268 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
273 m_adjust
->lower
= fmin
;
274 m_adjust
->upper
= fmax
;
276 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
278 // these two calls are required due to some bug in GTK
283 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
285 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
287 if (event
.GetKeyCode() == WXK_RETURN
)
289 wxWindow
*top_frame
= m_parent
;
290 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
291 top_frame
= top_frame
->GetParent();
293 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
295 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
298 GtkWidget
*widgetDef
= window
->default_widget
;
302 gtk_widget_activate(widgetDef
);
309 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
311 wxCommandEvent
evt( wxEVT_TEXT_ENTER
, m_windowId
);
312 evt
.SetEventObject(this);
313 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
314 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
315 evt
.SetString( val
);
316 if (HandleWindowEvent(evt
)) return;
322 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
324 if (GTK_SPIN_BUTTON(m_widget
)->entry
.text_area
== window
) return true;
326 if (GTK_SPIN_BUTTON(m_widget
)->panel
== window
) return true;
331 wxSize
wxSpinCtrl::DoGetBestSize() const
333 wxSize
ret( wxControl::DoGetBestSize() );
334 wxSize
best(95, ret
.y
);
339 bool wxSpinCtrl::SetBase(int base
)
341 // Currently we only support base 10 and 16. We could add support for base
342 // 8 quite easily but wxMSW doesn't support it natively so don't bother
343 // with doing something wxGTK-specific here.
344 if ( base
!= 10 && base
!= 16 )
347 if ( base
== m_base
)
352 // We need to be able to enter letters for any base greater than 10.
353 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget
), m_base
<= 10 );
357 gtk_signal_connect( GTK_OBJECT(m_widget
), "input",
358 GTK_SIGNAL_FUNC(wx_gtk_spin_input
), this);
359 gtk_signal_connect( GTK_OBJECT(m_widget
), "output",
360 GTK_SIGNAL_FUNC(wx_gtk_spin_output
), this);
364 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget
),
365 GTK_SIGNAL_FUNC(wx_gtk_spin_input
),
367 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget
),
368 GTK_SIGNAL_FUNC(wx_gtk_spin_output
),
377 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
379 // TODO: overload to accept functions like gtk_spin_button_new?
380 // Until then use a similar type
381 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);