1 /////////////////////////////////////////////////////////////////////////////
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "spinctrl.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
18 #include "wx/spinctrl.h"
24 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
26 #include "wx/gtk/private.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 static const float sensitivity
= 0.02;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
48 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (!win
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
56 event
.SetEventObject( win
);
58 // note that we don't use wxSpinCtrl::GetValue() here because it would
59 // adjust the value to fit into the control range and this means that we
60 // would never be able to enter an "invalid" value in the control, even
61 // temporarily - and trying to enter 10 into the control which accepts the
62 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
64 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
65 win
->GetEventHandler()->ProcessEvent( event
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
75 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
77 if (!win
->m_hasVMT
) return;
80 wxapp_install_idle_handler();
82 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
83 event
.SetEventObject( win
);
86 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
87 win
->GetEventHandler()->ProcessEvent( event
);
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
97 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
98 EVT_CHAR(wxSpinCtrl::OnChar
)
101 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
102 const wxString
& value
,
103 const wxPoint
& pos
, const wxSize
& size
,
105 int min
, int max
, int initial
,
106 const wxString
& name
)
109 m_acceptsFocus
= TRUE
;
111 if (!PreCreation( parent
, pos
, size
) ||
112 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
114 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
120 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
122 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
124 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
125 (int)(m_windowStyle
& wxSP_WRAP
) );
129 m_parent
->DoAddChild( this );
138 void wxSpinCtrl::GtkDisableEvents()
140 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
141 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
144 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
145 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
149 void wxSpinCtrl::GtkEnableEvents()
151 gtk_signal_connect( GTK_OBJECT (m_adjust
),
153 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
156 gtk_signal_connect( GTK_OBJECT(m_widget
),
158 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
162 int wxSpinCtrl::GetMin() const
164 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
166 return (int)ceil(m_adjust
->lower
);
169 int wxSpinCtrl::GetMax() const
171 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
173 return (int)ceil(m_adjust
->upper
);
176 int wxSpinCtrl::GetValue() const
178 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
180 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
182 return (int)ceil(m_adjust
->value
);
185 void wxSpinCtrl::SetValue( const wxString
& value
)
187 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
190 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
197 // invalid number - set text as is (wxMSW compatible)
199 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
204 void wxSpinCtrl::SetValue( int value
)
206 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
208 float fpos
= (float)value
;
210 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
212 m_adjust
->value
= fpos
;
215 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
219 void wxSpinCtrl::SetSelection(long from
, long to
)
221 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
223 if ( from
== -1 && to
== -1 )
229 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
232 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
234 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
236 float fmin
= (float)minVal
;
237 float fmax
= (float)maxVal
;
239 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
240 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
245 m_adjust
->lower
= fmin
;
246 m_adjust
->upper
= fmax
;
248 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
250 // these two calls are required due to some bug in GTK
255 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
257 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
259 if (event
.GetKeyCode() == WXK_RETURN
)
261 wxWindow
*top_frame
= m_parent
;
262 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
263 top_frame
= top_frame
->GetParent();
265 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
267 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
270 GtkWidget
*widgetDef
= window
->default_widget
;
274 gtk_widget_activate(widgetDef
);
281 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
283 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
284 evt
.SetEventObject(this);
285 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
286 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
287 evt
.SetString( val
);
288 if (GetEventHandler()->ProcessEvent(evt
)) return;
294 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
296 if (GTK_SPIN_BUTTON(m_widget
)->entry
.text_area
== window
) return TRUE
;
298 if (GTK_SPIN_BUTTON(m_widget
)->panel
== window
) return TRUE
;
303 wxSize
wxSpinCtrl::DoGetBestSize() const
305 wxSize
ret( wxControl::DoGetBestSize() );
306 wxSize
best(95, ret
.y
);
313 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
315 // TODO: overload to accept functions like gtk_spin_button_new?
316 // Until then use a similar type
317 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);