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
);
82 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
83 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 );
128 SetFont( parent
->GetFont() );
130 wxSize
size_best( DoGetBestSize() );
131 wxSize
new_size( size
);
132 if (new_size
.x
== -1)
133 new_size
.x
= size_best
.x
;
134 if (new_size
.y
== -1)
135 new_size
.y
= size_best
.y
;
136 if (new_size
.y
> size_best
.y
)
137 new_size
.y
= size_best
.y
;
138 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
139 SetSize( new_size
.x
, new_size
.y
);
141 SetBackgroundColour( parent
->GetBackgroundColour() );
150 void wxSpinCtrl::GtkDisableEvents()
152 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
153 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
156 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
157 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
161 void wxSpinCtrl::GtkEnableEvents()
163 gtk_signal_connect( GTK_OBJECT (m_adjust
),
165 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
168 gtk_signal_connect( GTK_OBJECT(m_widget
),
170 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
174 int wxSpinCtrl::GetMin() const
176 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
178 return (int)ceil(m_adjust
->lower
);
181 int wxSpinCtrl::GetMax() const
183 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
185 return (int)ceil(m_adjust
->upper
);
188 int wxSpinCtrl::GetValue() const
190 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
192 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
194 return (int)ceil(m_adjust
->value
);
197 void wxSpinCtrl::SetValue( const wxString
& value
)
199 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
202 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
209 // invalid number - set text as is (wxMSW compatible)
211 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
216 void wxSpinCtrl::SetValue( int value
)
218 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
220 float fpos
= (float)value
;
222 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
224 m_adjust
->value
= fpos
;
227 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
231 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
233 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
235 float fmin
= (float)minVal
;
236 float fmax
= (float)maxVal
;
238 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
239 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
244 m_adjust
->lower
= fmin
;
245 m_adjust
->upper
= fmax
;
247 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
249 // these two calls are required due to some bug in GTK
254 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
256 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
258 if (event
.KeyCode() == WXK_RETURN
)
260 wxWindow
*top_frame
= m_parent
;
261 while (top_frame
->GetParent() && !(top_frame
->GetParent()->IsTopLevel()))
262 top_frame
= top_frame
->GetParent();
264 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
266 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
269 GtkWidget
*widgetDef
= window
->default_widget
;
271 if ( widgetDef
&& GTK_IS_WINDOW(widgetDef
) )
273 gtk_widget_activate(widgetDef
);
283 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
285 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
288 void wxSpinCtrl::ApplyWidgetStyle()
291 gtk_widget_set_style( m_widget
, m_widgetStyle
);
294 wxSize
wxSpinCtrl::DoGetBestSize() const
296 wxSize
ret( wxControl::DoGetBestSize() );
297 return wxSize(95, ret
.y
);