1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/spinctrl.cpp
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/spinctrl.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
25 #include "wx/gtk1/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 static const float sensitivity
= 0.02;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
49 wx_gtk_spin_input(GtkSpinButton
* spin
, gdouble
* val
, wxSpinCtrl
* win
)
51 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
52 // wxString function even if this requires an extra conversion.
54 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin
))));
57 if ( !text
.ToLong(&lval
, win
->GetBase()) )
66 wx_gtk_spin_output(GtkSpinButton
* spin
, wxSpinCtrl
* win
)
68 const gint val
= gtk_spin_button_get_value_as_int(spin
);
73 wxPrivate::wxSpinCtrlFormatAsHex(val
, win
->GetMax()).utf8_str()
79 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
81 if (g_isIdle
) wxapp_install_idle_handler();
83 if (!win
->m_hasVMT
) return;
84 if (g_blockEventsOnDrag
) return;
86 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
87 event
.SetEventObject( win
);
89 // note that we don't use wxSpinCtrl::GetValue() here because it would
90 // adjust the value to fit into the control range and this means that we
91 // would never be able to enter an "invalid" value in the control, even
92 // temporarily - and trying to enter 10 into the control which accepts the
93 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
95 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
96 win
->HandleWindowEvent( event
);
100 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
106 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
108 if (!win
->m_hasVMT
) return;
111 wxapp_install_idle_handler();
113 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
114 event
.SetEventObject( win
);
117 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
118 win
->HandleWindowEvent( event
);
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
127 EVT_CHAR(wxSpinCtrl::OnChar
)
130 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
131 const wxString
& value
,
132 const wxPoint
& pos
, const wxSize
& size
,
134 int min
, int max
, int initial
,
135 const wxString
& name
)
138 m_acceptsFocus
= true;
140 if (!PreCreation( parent
, pos
, size
) ||
141 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
143 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
149 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
151 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
153 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
154 (int)(m_windowStyle
& wxSP_WRAP
) );
158 m_parent
->DoAddChild( this );
167 void wxSpinCtrl::GtkDisableEvents()
169 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
170 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
173 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
174 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
178 void wxSpinCtrl::GtkEnableEvents()
180 gtk_signal_connect( GTK_OBJECT (m_adjust
),
182 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
185 gtk_signal_connect( GTK_OBJECT(m_widget
),
187 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
191 int wxSpinCtrl::GetMin() const
193 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
195 return (int)ceil(m_adjust
->lower
);
198 int wxSpinCtrl::GetMax() const
200 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
202 return (int)ceil(m_adjust
->upper
);
205 int wxSpinCtrl::GetValue() const
207 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
209 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
211 return (int)ceil(m_adjust
->value
);
214 void wxSpinCtrl::SetValue( const wxString
& value
)
216 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
219 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
226 // invalid number - set text as is (wxMSW compatible)
228 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
233 void wxSpinCtrl::SetValue( int value
)
235 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
237 float fpos
= (float)value
;
239 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
241 m_adjust
->value
= fpos
;
244 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
248 void wxSpinCtrl::SetSelection(long from
, long to
)
250 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
252 if ( from
== -1 && to
== -1 )
258 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
261 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
263 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
265 float fmin
= (float)minVal
;
266 float fmax
= (float)maxVal
;
268 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
269 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
274 m_adjust
->lower
= fmin
;
275 m_adjust
->upper
= fmax
;
277 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
279 // these two calls are required due to some bug in GTK
284 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
286 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
288 if (event
.GetKeyCode() == WXK_RETURN
)
290 wxWindow
*top_frame
= m_parent
;
291 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
292 top_frame
= top_frame
->GetParent();
294 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
296 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
299 GtkWidget
*widgetDef
= window
->default_widget
;
303 gtk_widget_activate(widgetDef
);
310 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
312 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
313 evt
.SetEventObject(this);
314 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
315 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
316 evt
.SetString( val
);
317 if (HandleWindowEvent(evt
)) return;
323 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
325 if (GTK_SPIN_BUTTON(m_widget
)->entry
.text_area
== window
) return true;
327 if (GTK_SPIN_BUTTON(m_widget
)->panel
== window
) return true;
332 wxSize
wxSpinCtrl::DoGetBestSize() const
334 wxSize
ret( wxControl::DoGetBestSize() );
335 wxSize
best(95, ret
.y
);
340 bool wxSpinCtrl::SetBase(int base
)
342 // Currently we only support base 10 and 16. We could add support for base
343 // 8 quite easily but wxMSW doesn't support it natively so don't bother
344 // with doing something wxGTK-specific here.
345 if ( base
!= 10 && base
!= 16 )
348 if ( base
== m_base
)
353 // We need to be able to enter letters for any base greater than 10.
354 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget
), m_base
<= 10 );
358 gtk_signal_connect( GTK_OBJECT(m_widget
), "input",
359 GTK_SIGNAL_FUNC(wx_gtk_spin_input
), this);
360 gtk_signal_connect( GTK_OBJECT(m_widget
), "output",
361 GTK_SIGNAL_FUNC(wx_gtk_spin_output
), this);
365 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget
),
366 GTK_SIGNAL_FUNC(wx_gtk_spin_input
),
368 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget
),
369 GTK_SIGNAL_FUNC(wx_gtk_spin_output
),
378 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
380 // TODO: overload to accept functions like gtk_spin_button_new?
381 // Until then use a similar type
382 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);