1 /////////////////////////////////////////////////////////////////////////////
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinctrl.h"
15 #include "wx/spinctrl.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 static const float sensitivity
= 0.02;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!win
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
53 event
.SetEventObject( win
);
55 // note that we don't use wxSpinCtrl::GetValue() here because it would
56 // adjust the value to fit into the control range and this means that we
57 // would never be able to enter an "invalid" value in the control, even
58 // temporarily - and trying to enter 10 into the control which accepts the
59 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
61 event
.SetInt( (int)ceil(win
->m_adjust
->value
) );
62 win
->GetEventHandler()->ProcessEvent( event
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
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
);
79 event
.SetInt( win
->GetValue() );
80 win
->GetEventHandler()->ProcessEvent( event
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
89 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
90 EVT_CHAR(wxSpinCtrl::OnChar
)
93 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
94 const wxString
& value
,
95 const wxPoint
& pos
, const wxSize
& size
,
97 int min
, int max
, int initial
,
101 m_acceptsFocus
= TRUE
;
103 if (!PreCreation( parent
, pos
, size
) ||
104 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
106 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
112 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
114 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
116 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
117 (int)(m_windowStyle
& wxSP_WRAP
) );
121 m_parent
->DoAddChild( this );
125 SetFont( parent
->GetFont() );
127 wxSize
size_best( DoGetBestSize() );
128 wxSize
new_size( size
);
129 if (new_size
.x
== -1)
130 new_size
.x
= size_best
.x
;
131 if (new_size
.y
== -1)
132 new_size
.y
= size_best
.y
;
133 if (new_size
.y
> size_best
.y
)
134 new_size
.y
= size_best
.y
;
135 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
136 SetSize( new_size
.x
, new_size
.y
);
138 SetBackgroundColour( parent
->GetBackgroundColour() );
147 void wxSpinCtrl::GtkDisableEvents()
149 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
150 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
153 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
154 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
158 void wxSpinCtrl::GtkEnableEvents()
160 gtk_signal_connect( GTK_OBJECT (m_adjust
),
162 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
165 gtk_signal_connect( GTK_OBJECT(m_widget
),
167 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
171 int wxSpinCtrl::GetMin() const
173 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
175 return (int)ceil(m_adjust
->lower
);
178 int wxSpinCtrl::GetMax() const
180 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
182 return (int)ceil(m_adjust
->upper
);
185 int wxSpinCtrl::GetValue() const
187 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
189 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
191 return (int)ceil(m_adjust
->value
);
194 void wxSpinCtrl::SetValue( const wxString
& value
)
196 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
199 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
206 // invalid number - set text as is (wxMSW compatible)
208 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
213 void wxSpinCtrl::SetValue( int value
)
215 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
217 float fpos
= (float)value
;
219 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
221 m_adjust
->value
= fpos
;
224 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
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
.KeyCode() == WXK_RETURN
)
257 wxWindow
*top_frame
= m_parent
;
258 while (top_frame
->GetParent() && !(top_frame
->GetParent()->IsTopLevel()))
259 top_frame
= top_frame
->GetParent();
260 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
262 if (window
->default_widget
)
264 gtk_widget_activate (window
->default_widget
);
272 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
274 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
277 void wxSpinCtrl::ApplyWidgetStyle()
280 gtk_widget_set_style( m_widget
, m_widgetStyle
);
283 wxSize
wxSpinCtrl::DoGetBestSize() const
285 wxSize
ret( wxControl::DoGetBestSize() );
286 return wxSize(95, ret
.y
);