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 //-----------------------------------------------------------------------------
47 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!win
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
55 event
.SetEventObject( win
);
57 // note that we don't use wxSpinCtrl::GetValue() here because it would
58 // adjust the value to fit into the control range and this means that we
59 // would never be able to enter an "invalid" value in the control, even
60 // temporarily - and trying to enter 10 into the control which accepts the
61 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
63 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
64 win
->HandleWindowEvent( event
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
74 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
76 if (!win
->m_hasVMT
) return;
79 wxapp_install_idle_handler();
81 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
82 event
.SetEventObject( win
);
85 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
86 win
->HandleWindowEvent( event
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
95 EVT_CHAR(wxSpinCtrl::OnChar
)
98 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
99 const wxString
& value
,
100 const wxPoint
& pos
, const wxSize
& size
,
102 int min
, int max
, int initial
,
103 const wxString
& name
)
106 m_acceptsFocus
= true;
108 if (!PreCreation( parent
, pos
, size
) ||
109 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
111 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
117 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
119 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
121 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
122 (int)(m_windowStyle
& wxSP_WRAP
) );
126 m_parent
->DoAddChild( this );
135 void wxSpinCtrl::GtkDisableEvents()
137 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
138 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
141 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
142 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
146 void wxSpinCtrl::GtkEnableEvents()
148 gtk_signal_connect( GTK_OBJECT (m_adjust
),
150 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
153 gtk_signal_connect( GTK_OBJECT(m_widget
),
155 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
159 int wxSpinCtrl::GetMin() const
161 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
163 return (int)ceil(m_adjust
->lower
);
166 int wxSpinCtrl::GetMax() const
168 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
170 return (int)ceil(m_adjust
->upper
);
173 int wxSpinCtrl::GetValue() const
175 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
177 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
179 return (int)ceil(m_adjust
->value
);
182 void wxSpinCtrl::SetValue( const wxString
& value
)
184 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
187 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
194 // invalid number - set text as is (wxMSW compatible)
196 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
201 void wxSpinCtrl::SetValue( int value
)
203 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
205 float fpos
= (float)value
;
207 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
209 m_adjust
->value
= fpos
;
212 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
216 void wxSpinCtrl::SetSelection(long from
, long to
)
218 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
220 if ( from
== -1 && to
== -1 )
226 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
229 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
231 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
233 float fmin
= (float)minVal
;
234 float fmax
= (float)maxVal
;
236 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
237 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
242 m_adjust
->lower
= fmin
;
243 m_adjust
->upper
= fmax
;
245 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
247 // these two calls are required due to some bug in GTK
252 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
254 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
256 if (event
.GetKeyCode() == WXK_RETURN
)
258 wxWindow
*top_frame
= m_parent
;
259 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
260 top_frame
= top_frame
->GetParent();
262 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
264 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
267 GtkWidget
*widgetDef
= window
->default_widget
;
271 gtk_widget_activate(widgetDef
);
278 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
280 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
281 evt
.SetEventObject(this);
282 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
283 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
284 evt
.SetString( val
);
285 if (HandleWindowEvent(evt
)) return;
291 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
293 if (GTK_SPIN_BUTTON(m_widget
)->entry
.text_area
== window
) return true;
295 if (GTK_SPIN_BUTTON(m_widget
)->panel
== window
) return true;
300 wxSize
wxSpinCtrl::DoGetBestSize() const
302 wxSize
ret( wxControl::DoGetBestSize() );
303 wxSize
best(95, ret
.y
);
310 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
312 // TODO: overload to accept functions like gtk_spin_button_new?
313 // Until then use a similar type
314 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);