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
);
54 event
.SetInt( win
->GetValue() );
55 win
->GetEventHandler()->ProcessEvent( event
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
63 gtk_spinctrl_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
65 if (!win
->m_hasVMT
) return;
68 wxapp_install_idle_handler();
70 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
71 event
.SetEventObject( win
);
72 event
.SetInt( win
->GetValue() );
73 win
->GetEventHandler()->ProcessEvent( event
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
82 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
83 EVT_CHAR(wxSpinCtrl::OnChar
)
86 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
87 const wxString
& value
,
88 const wxPoint
& pos
, const wxSize
& size
,
90 int min
, int max
, int initial
,
94 m_acceptsFocus
= TRUE
;
96 if (!PreCreation( parent
, pos
, size
) ||
97 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
99 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
105 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
107 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
109 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
110 (int)(m_windowStyle
& wxSP_WRAP
) );
114 m_parent
->DoAddChild( this );
118 SetFont( parent
->GetFont() );
120 wxSize
size_best( DoGetBestSize() );
121 wxSize
new_size( size
);
122 if (new_size
.x
== -1)
123 new_size
.x
= size_best
.x
;
124 if (new_size
.y
== -1)
125 new_size
.y
= size_best
.y
;
126 if (new_size
.y
> size_best
.y
)
127 new_size
.y
= size_best
.y
;
128 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
129 SetSize( new_size
.x
, new_size
.y
);
131 SetBackgroundColour( parent
->GetBackgroundColour() );
140 void wxSpinCtrl::GtkDisableEvents()
142 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
143 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
146 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
147 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
151 void wxSpinCtrl::GtkEnableEvents()
153 gtk_signal_connect( GTK_OBJECT (m_adjust
),
155 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
158 gtk_signal_connect( GTK_OBJECT(m_widget
),
160 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback
),
164 int wxSpinCtrl::GetMin() const
166 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
168 return (int)ceil(m_adjust
->lower
);
171 int wxSpinCtrl::GetMax() const
173 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
175 return (int)ceil(m_adjust
->upper
);
178 int wxSpinCtrl::GetValue() const
180 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
182 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
184 return (int)ceil(m_adjust
->value
);
187 void wxSpinCtrl::SetValue( const wxString
& value
)
189 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
192 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
199 // invalid number - set text as is (wxMSW compatible)
201 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
206 void wxSpinCtrl::SetValue( int value
)
208 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
210 float fpos
= (float)value
;
212 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
214 m_adjust
->value
= fpos
;
217 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
221 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
223 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
225 float fmin
= (float)minVal
;
226 float fmax
= (float)maxVal
;
228 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
229 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
234 m_adjust
->lower
= fmin
;
235 m_adjust
->upper
= fmax
;
237 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
239 // these two calls are required due to some bug in GTK
244 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
246 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
248 if (event
.KeyCode() == WXK_RETURN
)
250 wxWindow
*top_frame
= m_parent
;
251 while (top_frame
->GetParent() && !(top_frame
->GetParent()->IsTopLevel()))
252 top_frame
= top_frame
->GetParent();
253 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
255 if (window
->default_widget
)
257 gtk_widget_activate (window
->default_widget
);
265 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
267 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
270 void wxSpinCtrl::ApplyWidgetStyle()
273 gtk_widget_set_style( m_widget
, m_widgetStyle
);
276 wxSize
wxSpinCtrl::DoGetBestSize() const
278 wxSize
ret( wxControl::DoGetBestSize() );
279 return wxSize(95, ret
.y
);