1 /////////////////////////////////////////////////////////////////////////////
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinctrl.h"
15 #include "wx/spinctrl.h"
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
25 #include "wx/gtk/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 //-----------------------------------------------------------------------------
46 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!win
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
54 event
.SetEventObject( win
);
56 // note that we don't use wxSpinCtrl::GetValue() here because it would
57 // adjust the value to fit into the control range and this means that we
58 // would never be able to enter an "invalid" value in the control, even
59 // temporarily - and trying to enter 10 into the control which accepts the
60 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
62 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
63 win
->GetEventHandler()->ProcessEvent( event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
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
);
80 event
.SetInt( win
->GetValue() );
81 win
->GetEventHandler()->ProcessEvent( event
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
90 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
91 EVT_CHAR(wxSpinCtrl::OnChar
)
94 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
95 const wxString
& value
,
96 const wxPoint
& pos
, const wxSize
& size
,
98 int min
, int max
, int initial
,
102 m_acceptsFocus
= TRUE
;
104 if (!PreCreation( parent
, pos
, size
) ||
105 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
107 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
113 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
115 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
117 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
118 (int)(m_windowStyle
& wxSP_WRAP
) );
122 m_parent
->DoAddChild( this );
126 SetFont( parent
->GetFont() );
128 wxSize
size_best( DoGetBestSize() );
129 wxSize
new_size( size
);
130 if (new_size
.x
== -1)
131 new_size
.x
= size_best
.x
;
132 if (new_size
.y
== -1)
133 new_size
.y
= size_best
.y
;
134 if (new_size
.y
> size_best
.y
)
135 new_size
.y
= size_best
.y
;
136 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
137 SetSize( new_size
.x
, new_size
.y
);
139 SetBackgroundColour( parent
->GetBackgroundColour() );
148 void wxSpinCtrl::GtkDisableEvents()
150 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
151 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
154 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
155 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
159 void wxSpinCtrl::GtkEnableEvents()
161 gtk_signal_connect( GTK_OBJECT (m_adjust
),
163 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
166 gtk_signal_connect( GTK_OBJECT(m_widget
),
168 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
172 int wxSpinCtrl::GetMin() const
174 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
176 return (int)ceil(m_adjust
->lower
);
179 int wxSpinCtrl::GetMax() const
181 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
183 return (int)ceil(m_adjust
->upper
);
186 int wxSpinCtrl::GetValue() const
188 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
190 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
192 return (int)ceil(m_adjust
->value
);
195 void wxSpinCtrl::SetValue( const wxString
& value
)
197 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
200 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
207 // invalid number - set text as is (wxMSW compatible)
209 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
214 void wxSpinCtrl::SetValue( int value
)
216 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
218 float fpos
= (float)value
;
220 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
222 m_adjust
->value
= fpos
;
225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
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
.KeyCode() == WXK_RETURN
)
258 wxWindow
*top_frame
= m_parent
;
259 while (top_frame
->GetParent() && !(top_frame
->GetParent()->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
;
269 if ( widgetDef
&& GTK_IS_WINDOW(widgetDef
) )
271 gtk_widget_activate(widgetDef
);
281 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
283 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
286 void wxSpinCtrl::ApplyWidgetStyle()
289 gtk_widget_set_style( m_widget
, m_widgetStyle
);
292 wxSize
wxSpinCtrl::DoGetBestSize() const
294 wxSize
ret( wxControl::DoGetBestSize() );
295 return wxSize(95, ret
.y
);