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 win
->m_pos
= int(gtk_spin_button_get_value(spinbutton
));
40 if (!win
->m_hasVMT
|| g_blockEventsOnDrag
|| win
->m_blockScrollEvent
)
43 wxCommandEvent
event( wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
44 event
.SetEventObject( win
);
46 // note that we don't use wxSpinCtrl::GetValue() here because it would
47 // adjust the value to fit into the control range and this means that we
48 // would never be able to enter an "invalid" value in the control, even
49 // temporarily - and trying to enter 10 into the control which accepts the
50 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
52 event
.SetInt(win
->m_pos
);
53 win
->GetEventHandler()->ProcessEvent( event
);
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
63 gtk_changed(GtkSpinButton
* spinbutton
, wxSpinCtrl
* win
)
65 if (!win
->m_hasVMT
|| win
->m_blockScrollEvent
)
68 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
69 event
.SetEventObject( win
);
70 event
.SetString( GTK_ENTRY(spinbutton
)->text
);
73 event
.SetInt(win
->m_pos
);
74 win
->GetEventHandler()->ProcessEvent( event
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
84 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
85 EVT_CHAR(wxSpinCtrl::OnChar
)
88 wxSpinCtrl::wxSpinCtrl()
93 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
94 const wxString
& value
,
95 const wxPoint
& pos
, const wxSize
& size
,
97 int min
, int max
, int initial
,
102 if (!PreCreation( parent
, pos
, size
) ||
103 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
105 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
109 m_widget
= gtk_spin_button_new_with_range(min
, max
, 1);
110 gtk_spin_button_set_value((GtkSpinButton
*)m_widget
, initial
);
111 m_pos
= int(gtk_spin_button_get_value((GtkSpinButton
*)m_widget
));
113 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
114 (int)(m_windowStyle
& wxSP_WRAP
) );
116 g_signal_connect(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
117 g_signal_connect(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
119 m_parent
->DoAddChild( this );
131 int wxSpinCtrl::GetMin() const
133 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
136 gtk_spin_button_get_range((GtkSpinButton
*)m_widget
, &min
, NULL
);
140 int wxSpinCtrl::GetMax() const
142 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
145 gtk_spin_button_get_range((GtkSpinButton
*)m_widget
, NULL
, &max
);
149 int wxSpinCtrl::GetValue() const
151 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
153 wx_const_cast(wxSpinCtrl
*, this)->BlockScrollEvent();
154 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
155 wx_const_cast(wxSpinCtrl
*, this)->UnblockScrollEvent();
160 void wxSpinCtrl::SetValue( const wxString
& value
)
162 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
165 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
172 // invalid number - set text as is (wxMSW compatible)
174 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
175 UnblockScrollEvent();
179 void wxSpinCtrl::SetValue( int value
)
181 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
184 gtk_spin_button_set_value((GtkSpinButton
*)m_widget
, value
);
185 UnblockScrollEvent();
188 void wxSpinCtrl::SetSelection(long from
, long to
)
190 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
192 if ( from
== -1 && to
== -1 )
198 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
201 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
203 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
206 gtk_spin_button_set_range((GtkSpinButton
*)m_widget
, minVal
, maxVal
);
207 UnblockScrollEvent();
210 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
212 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
214 if (event
.GetKeyCode() == WXK_RETURN
)
216 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
218 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
220 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
223 GtkWidget
*widgetDef
= window
->default_widget
;
227 gtk_widget_activate(widgetDef
);
234 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
236 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
237 evt
.SetEventObject(this);
238 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
239 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
240 evt
.SetString( val
);
241 if (GetEventHandler()->ProcessEvent(evt
)) return;
247 GdkWindow
*wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows
& windows
) const
249 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
251 windows
.push_back(spinbutton
->entry
.text_area
);
252 windows
.push_back(spinbutton
->panel
);
257 wxSize
wxSpinCtrl::DoGetBestSize() const
259 wxSize
ret( wxControl::DoGetBestSize() );
260 wxSize
best(95, ret
.y
); // FIXME: 95?
267 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
269 // TODO: overload to accept functions like gtk_spin_button_new?
270 // Until then use a similar type
271 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);