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 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!win
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
51 event
.SetEventObject( win
);
52 event
.SetInt( win
->GetValue() );
53 win
->GetEventHandler()->ProcessEvent( event
);
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
62 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
63 EVT_CHAR(wxSpinCtrl::OnChar
)
66 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
67 const wxString
& value
,
68 const wxPoint
& pos
, const wxSize
& size
,
70 int min
, int max
, int initial
,
74 m_acceptsFocus
= TRUE
;
76 wxSize new_size
= size
;
80 if (!PreCreation( parent
, pos
, new_size
) ||
81 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
83 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
89 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
91 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
93 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
94 (int)(m_windowStyle
& wxSP_WRAP
) );
96 gtk_signal_connect( GTK_OBJECT (m_adjust
),
98 (GtkSignalFunc
) gtk_spinctrl_callback
,
101 m_parent
->DoAddChild( this );
105 SetBackgroundColour( parent
->GetBackgroundColour() );
114 int wxSpinCtrl::GetMin() const
116 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
118 return (int)ceil(m_adjust
->lower
);
121 int wxSpinCtrl::GetMax() const
123 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
125 return (int)ceil(m_adjust
->upper
);
128 int wxSpinCtrl::GetValue() const
130 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
132 return (int)ceil(m_adjust
->value
);
135 void wxSpinCtrl::SetValue( const wxString
& value
)
137 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
140 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
147 // invalid number - set text as is (wxMSW compatible)
148 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
152 void wxSpinCtrl::SetValue( int value
)
154 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
156 float fpos
= (float)value
;
158 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
160 m_adjust
->value
= fpos
;
162 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
165 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
167 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
169 float fmin
= (float)minVal
;
170 float fmax
= (float)maxVal
;
172 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
173 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
178 m_adjust
->lower
= fmin
;
179 m_adjust
->upper
= fmax
;
181 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
183 // these two calls are required due to some bug in GTK
188 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
190 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
192 if (event
.KeyCode() == WXK_RETURN
)
194 wxWindow
*top_frame
= m_parent
;
195 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
196 top_frame
= top_frame
->GetParent();
197 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
199 if (window
->default_widget
)
201 gtk_widget_activate (window
->default_widget
);
209 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
211 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
214 void wxSpinCtrl::ApplyWidgetStyle()
217 gtk_widget_set_style( m_widget
, m_widgetStyle
);