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 wxSize new_size
= size
;
82 if (!PreCreation( parent
, pos
, new_size
) ||
83 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
85 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
91 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
93 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
95 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
96 (int)(m_windowStyle
& wxSP_WRAP
) );
98 gtk_signal_connect( GTK_OBJECT (m_adjust
),
100 (GtkSignalFunc
) gtk_spinctrl_callback
,
103 m_parent
->DoAddChild( this );
107 SetBackgroundColour( parent
->GetBackgroundColour() );
116 int wxSpinCtrl::GetMin() const
118 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
120 return (int)ceil(m_adjust
->lower
);
123 int wxSpinCtrl::GetMax() const
125 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
127 return (int)ceil(m_adjust
->upper
);
130 int wxSpinCtrl::GetValue() const
132 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
134 return (int)ceil(m_adjust
->value
);
137 void wxSpinCtrl::SetValue( const wxString
& value
)
139 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
142 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
149 // invalid number - set text as is (wxMSW compatible)
150 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
154 void wxSpinCtrl::SetValue( int value
)
156 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
158 float fpos
= (float)value
;
160 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
162 m_adjust
->value
= fpos
;
164 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
167 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
169 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
171 float fmin
= (float)minVal
;
172 float fmax
= (float)maxVal
;
174 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
175 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
180 m_adjust
->lower
= fmin
;
181 m_adjust
->upper
= fmax
;
183 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
185 // these two calls are required due to some bug in GTK
190 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
192 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
194 if (event
.KeyCode() == WXK_RETURN
)
196 wxWindow
*top_frame
= m_parent
;
197 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
198 top_frame
= top_frame
->GetParent();
199 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
201 if (window
->default_widget
)
203 gtk_widget_activate (window
->default_widget
);
211 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
213 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
216 void wxSpinCtrl::ApplyWidgetStyle()
219 gtk_widget_set_style( m_widget
, m_widgetStyle
);