1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinbutt.cpp
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/spinctrl.h"
19 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern bool g_blockEventsOnDrag
;
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
37 gtk_value_changed(GtkSpinButton
* spinbutton
, wxSpinCtrl
* win
)
39 if (g_isIdle
) wxapp_install_idle_handler();
41 win
->m_pos
= int(gtk_spin_button_get_value(spinbutton
));
42 if (!win
->m_hasVMT
|| g_blockEventsOnDrag
|| win
->m_blockScrollEvent
)
45 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
46 event
.SetEventObject( win
);
48 // note that we don't use wxSpinCtrl::GetValue() here because it would
49 // adjust the value to fit into the control range and this means that we
50 // would never be able to enter an "invalid" value in the control, even
51 // temporarily - and trying to enter 10 into the control which accepts the
52 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
54 event
.SetInt(win
->m_pos
);
55 win
->GetEventHandler()->ProcessEvent( event
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
65 gtk_changed(GtkSpinButton
* spinbutton
, wxSpinCtrl
* win
)
68 wxapp_install_idle_handler();
70 if (!win
->m_hasVMT
|| win
->m_blockScrollEvent
)
73 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
74 event
.SetEventObject( win
);
77 event
.SetInt(win
->m_pos
);
78 win
->GetEventHandler()->ProcessEvent( event
);
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
88 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
89 EVT_CHAR(wxSpinCtrl::OnChar
)
92 wxSpinCtrl::wxSpinCtrl()
97 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
98 const wxString
& value
,
99 const wxPoint
& pos
, const wxSize
& size
,
101 int min
, int max
, int initial
,
102 const wxString
& name
)
106 if (!PreCreation( parent
, pos
, size
) ||
107 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
109 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
113 m_widget
= gtk_spin_button_new_with_range(min
, max
, 1);
114 gtk_spin_button_set_value((GtkSpinButton
*)m_widget
, initial
);
115 m_pos
= int(gtk_spin_button_get_value((GtkSpinButton
*)m_widget
));
117 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
118 (int)(m_windowStyle
& wxSP_WRAP
) );
120 g_signal_connect(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
121 g_signal_connect(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
123 m_parent
->DoAddChild( this );
135 int wxSpinCtrl::GetMin() const
137 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
140 gtk_spin_button_get_range((GtkSpinButton
*)m_widget
, &min
, NULL
);
144 int wxSpinCtrl::GetMax() const
146 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
149 gtk_spin_button_get_range((GtkSpinButton
*)m_widget
, NULL
, &max
);
153 int wxSpinCtrl::GetValue() const
155 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
157 wx_const_cast(wxSpinCtrl
*, this)->BlockScrollEvent();
158 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
159 wx_const_cast(wxSpinCtrl
*, this)->UnblockScrollEvent();
164 void wxSpinCtrl::SetValue( const wxString
& value
)
166 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
169 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
176 // invalid number - set text as is (wxMSW compatible)
178 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
179 UnblockScrollEvent();
183 void wxSpinCtrl::SetValue( int value
)
185 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
188 gtk_spin_button_set_value((GtkSpinButton
*)m_widget
, value
);
189 UnblockScrollEvent();
192 void wxSpinCtrl::SetSelection(long from
, long to
)
194 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
196 if ( from
== -1 && to
== -1 )
202 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
205 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
207 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
210 gtk_spin_button_set_range((GtkSpinButton
*)m_widget
, minVal
, maxVal
);
211 UnblockScrollEvent();
214 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
216 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
218 if (event
.GetKeyCode() == WXK_RETURN
)
220 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
222 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
224 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
227 GtkWidget
*widgetDef
= window
->default_widget
;
231 gtk_widget_activate(widgetDef
);
238 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
240 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
241 evt
.SetEventObject(this);
242 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
243 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
244 evt
.SetString( val
);
245 if (GetEventHandler()->ProcessEvent(evt
)) return;
251 GdkWindow
*wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
253 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
255 windows
.push_back(spinbutton
->entry
.text_area
);
256 windows
.push_back(spinbutton
->panel
);
261 wxSize
wxSpinCtrl::DoGetBestSize() const
263 wxSize
ret( wxControl::DoGetBestSize() );
264 wxSize
best(95, ret
.y
); // FIXME: 95?
271 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
273 // TODO: overload to accept functions like gtk_spin_button_new?
274 // Until then use a similar type
275 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);