1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinbutt.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"
22 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static const float sensitivity
= 0.02;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
43 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!win
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
51 event
.SetEventObject( win
);
53 // note that we don't use wxSpinCtrl::GetValue() here because it would
54 // adjust the value to fit into the control range and this means that we
55 // would never be able to enter an "invalid" value in the control, even
56 // temporarily - and trying to enter 10 into the control which accepts the
57 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
59 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
60 win
->GetEventHandler()->ProcessEvent( event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
70 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
72 if (!win
->m_hasVMT
) return;
75 wxapp_install_idle_handler();
77 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
78 event
.SetEventObject( win
);
81 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
82 win
->GetEventHandler()->ProcessEvent( event
);
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
92 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
93 EVT_CHAR(wxSpinCtrl::OnChar
)
96 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
97 const wxString
& value
,
98 const wxPoint
& pos
, const wxSize
& size
,
100 int min
, int max
, int initial
,
101 const wxString
& name
)
104 m_acceptsFocus
= true;
106 if (!PreCreation( parent
, pos
, size
) ||
107 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
109 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
115 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
117 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
119 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
120 (int)(m_windowStyle
& wxSP_WRAP
) );
124 m_parent
->DoAddChild( this );
133 void wxSpinCtrl::GtkDisableEvents()
135 g_signal_handlers_disconnect_by_func (m_adjust
,
136 (gpointer
) gtk_spinctrl_callback
,
138 g_signal_handlers_disconnect_by_func (m_widget
,
139 (gpointer
) gtk_spinctrl_text_changed_callback
,
143 void wxSpinCtrl::GtkEnableEvents()
145 g_signal_connect (m_adjust
, "value_changed",
146 G_CALLBACK (gtk_spinctrl_callback
),
148 g_signal_connect (m_widget
, "changed",
149 G_CALLBACK (gtk_spinctrl_text_changed_callback
),
153 int wxSpinCtrl::GetMin() const
155 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
157 return (int)ceil(m_adjust
->lower
);
160 int wxSpinCtrl::GetMax() const
162 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
164 return (int)ceil(m_adjust
->upper
);
167 int wxSpinCtrl::GetValue() const
169 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
171 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
173 return (int)ceil(m_adjust
->value
);
176 void wxSpinCtrl::SetValue( const wxString
& value
)
178 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
181 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
188 // invalid number - set text as is (wxMSW compatible)
190 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
195 void wxSpinCtrl::SetValue( int value
)
197 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
199 float fpos
= (float)value
;
201 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
203 m_adjust
->value
= fpos
;
206 g_signal_emit_by_name (m_adjust
, "value_changed");
210 void wxSpinCtrl::SetSelection(long from
, long to
)
212 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
214 if ( from
== -1 && to
== -1 )
220 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
223 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
225 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
227 float fmin
= (float)minVal
;
228 float fmax
= (float)maxVal
;
230 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
231 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
236 m_adjust
->lower
= fmin
;
237 m_adjust
->upper
= fmax
;
239 g_signal_emit_by_name (m_adjust
, "changed");
241 // these two calls are required due to some bug in GTK
246 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
248 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
250 if (event
.GetKeyCode() == WXK_RETURN
)
252 wxWindow
*top_frame
= m_parent
;
253 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
254 top_frame
= top_frame
->GetParent();
256 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
258 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
261 GtkWidget
*widgetDef
= window
->default_widget
;
265 gtk_widget_activate(widgetDef
);
272 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
274 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
275 evt
.SetEventObject(this);
276 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
277 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
278 evt
.SetString( val
);
279 if (GetEventHandler()->ProcessEvent(evt
)) return;
285 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
287 if (GTK_SPIN_BUTTON(m_widget
)->entry
.text_area
== window
) return true;
289 if (GTK_SPIN_BUTTON(m_widget
)->panel
== window
) return true;
294 wxSize
wxSpinCtrl::DoGetBestSize() const
296 wxSize
ret( wxControl::DoGetBestSize() );
297 wxSize
best(95, ret
.y
);
304 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
306 // TODO: overload to accept functions like gtk_spin_button_new?
307 // Until then use a similar type
308 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);