1 /////////////////////////////////////////////////////////////////////////////
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/spinctrl.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22 #include "wx/gtk/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 static const float sensitivity
= 0.02;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!win
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
52 event
.SetEventObject( win
);
54 // note that we don't use wxSpinCtrl::GetValue() here because it would
55 // adjust the value to fit into the control range and this means that we
56 // would never be able to enter an "invalid" value in the control, even
57 // temporarily - and trying to enter 10 into the control which accepts the
58 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
60 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
61 win
->GetEventHandler()->ProcessEvent( event
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
71 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
73 if (!win
->m_hasVMT
) return;
76 wxapp_install_idle_handler();
78 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
79 event
.SetEventObject( win
);
82 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
83 win
->GetEventHandler()->ProcessEvent( event
);
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
93 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
94 EVT_CHAR(wxSpinCtrl::OnChar
)
97 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
98 const wxString
& value
,
99 const wxPoint
& pos
, const wxSize
& size
,
101 int min
, int max
, int initial
,
102 const wxString
& name
)
105 m_acceptsFocus
= TRUE
;
107 if (!PreCreation( parent
, pos
, size
) ||
108 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
110 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
116 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
118 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
121 (int)(m_windowStyle
& wxSP_WRAP
) );
125 m_parent
->DoAddChild( this );
134 void wxSpinCtrl::GtkDisableEvents()
136 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
137 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
140 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
141 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
145 void wxSpinCtrl::GtkEnableEvents()
147 gtk_signal_connect( GTK_OBJECT (m_adjust
),
149 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
152 gtk_signal_connect( GTK_OBJECT(m_widget
),
154 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
158 int wxSpinCtrl::GetMin() const
160 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
162 return (int)ceil(m_adjust
->lower
);
165 int wxSpinCtrl::GetMax() const
167 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
169 return (int)ceil(m_adjust
->upper
);
172 int wxSpinCtrl::GetValue() const
174 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
176 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
178 return (int)ceil(m_adjust
->value
);
181 void wxSpinCtrl::SetValue( const wxString
& value
)
183 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
186 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
193 // invalid number - set text as is (wxMSW compatible)
195 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
200 void wxSpinCtrl::SetValue( int value
)
202 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
204 float fpos
= (float)value
;
206 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
208 m_adjust
->value
= fpos
;
211 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
215 void wxSpinCtrl::SetSelection(long from
, long to
)
217 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
219 if ( from
== -1 && to
== -1 )
225 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
228 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
230 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
232 float fmin
= (float)minVal
;
233 float fmax
= (float)maxVal
;
235 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
236 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
241 m_adjust
->lower
= fmin
;
242 m_adjust
->upper
= fmax
;
244 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
246 // these two calls are required due to some bug in GTK
251 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
253 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
255 if (event
.GetKeyCode() == WXK_RETURN
)
257 wxWindow
*top_frame
= m_parent
;
258 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
259 top_frame
= top_frame
->GetParent();
261 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
263 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
266 GtkWidget
*widgetDef
= window
->default_widget
;
270 gtk_widget_activate(widgetDef
);
277 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
279 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
280 evt
.SetEventObject(this);
281 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
282 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
283 evt
.SetString( val
);
284 if (GetEventHandler()->ProcessEvent(evt
)) return;
290 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
292 if (GTK_SPIN_BUTTON(m_widget
)->entry
.text_area
== window
) return TRUE
;
294 if (GTK_SPIN_BUTTON(m_widget
)->panel
== window
) return TRUE
;
299 wxSize
wxSpinCtrl::DoGetBestSize() const
301 wxSize
ret( wxControl::DoGetBestSize() );
302 wxSize
best(95, ret
.y
);
309 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
311 // TODO: overload to accept functions like gtk_spin_button_new?
312 // Until then use a similar type
313 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);