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 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
64 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
65 EVT_CHAR(wxSpinCtrl::OnChar
)
68 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
69 const wxString
& value
,
70 const wxPoint
& pos
, const wxSize
& size
,
72 int min
, int max
, int initial
,
76 m_acceptsFocus
= TRUE
;
78 if (!PreCreation( parent
, pos
, size
) ||
79 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
81 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
87 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
89 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
91 wxSize new_size
= size
,
92 sizeBest
= DoGetBestSize();
94 new_size
.x
= sizeBest
.x
;
96 new_size
.y
= sizeBest
.y
;
98 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
99 SetSize( new_size
.x
, new_size
.y
);
101 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
102 (int)(m_windowStyle
& wxSP_WRAP
) );
105 m_parent
->DoAddChild( this );
109 SetBackgroundColour( parent
->GetBackgroundColour() );
118 void wxSpinCtrl::GtkDisableEvents()
120 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
121 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
126 void wxSpinCtrl::GtkEnableEvents()
128 gtk_signal_connect( GTK_OBJECT (m_adjust
),
130 GTK_SIGNAL_FUNC(gtk_spinctrl_callback
),
134 int wxSpinCtrl::GetMin() const
136 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
138 return (int)ceil(m_adjust
->lower
);
141 int wxSpinCtrl::GetMax() const
143 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
145 return (int)ceil(m_adjust
->upper
);
148 int wxSpinCtrl::GetValue() const
150 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
152 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
154 return (int)ceil(m_adjust
->value
);
157 void wxSpinCtrl::SetValue( const wxString
& value
)
159 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
162 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
169 // invalid number - set text as is (wxMSW compatible)
171 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
176 void wxSpinCtrl::SetValue( int value
)
178 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
180 float fpos
= (float)value
;
182 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
184 m_adjust
->value
= fpos
;
187 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
191 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
193 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
195 float fmin
= (float)minVal
;
196 float fmax
= (float)maxVal
;
198 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
199 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
204 m_adjust
->lower
= fmin
;
205 m_adjust
->upper
= fmax
;
207 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
209 // these two calls are required due to some bug in GTK
214 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
216 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
218 if (event
.KeyCode() == WXK_RETURN
)
220 wxWindow
*top_frame
= m_parent
;
221 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
222 top_frame
= top_frame
->GetParent();
223 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
225 if (window
->default_widget
)
227 gtk_widget_activate (window
->default_widget
);
235 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
237 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
240 void wxSpinCtrl::ApplyWidgetStyle()
243 gtk_widget_set_style( m_widget
, m_widgetStyle
);
246 wxSize
wxSpinCtrl::DoGetBestSize() const
248 wxSize
ret( wxControl::DoGetBestSize() );
249 return wxSize(95, ret
.y
);