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
21 #include "wx/wxcrtvararg.h"
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern bool g_blockEventsOnDrag
;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
38 gtk_value_changed(GtkSpinButton
* spinbutton
, wxSpinCtrlGTKBase
* win
)
40 win
->m_value
= gtk_spin_button_get_value(spinbutton
);
41 if (!win
->m_hasVMT
|| g_blockEventsOnDrag
)
44 // note that we don't use wxSpinCtrl::GetValue() here because it would
45 // adjust the value to fit into the control range and this means that we
46 // would never be able to enter an "invalid" value in the control, even
47 // temporarily - and trying to enter 10 into the control which accepts the
48 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
51 if (wxIsKindOf(win
, wxSpinCtrl
))
53 wxSpinEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
54 event
.SetEventObject( win
);
55 event
.SetPosition( wxRound(win
->m_value
) ); // FIXME should be SetValue
56 event
.SetString(GTK_ENTRY(spinbutton
)->text
);
57 win
->HandleWindowEvent( event
);
59 else // wxIsKindOf(win, wxSpinCtrlDouble)
61 wxSpinDoubleEvent
event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED
, win
->GetId());
62 event
.SetEventObject( win
);
63 event
.SetValue(win
->m_value
);
64 event
.SetString(GTK_ENTRY(spinbutton
)->text
);
65 win
->HandleWindowEvent( event
);
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
76 gtk_changed(GtkSpinButton
* spinbutton
, wxSpinCtrl
* win
)
81 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
82 event
.SetEventObject( win
);
83 event
.SetString( GTK_ENTRY(spinbutton
)->text
);
86 event
.SetInt((int)win
->m_value
);
87 win
->HandleWindowEvent( event
);
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
97 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
98 EVT_CHAR(wxSpinCtrlGTKBase::OnChar
)
101 bool wxSpinCtrlGTKBase::Create(wxWindow
*parent
, wxWindowID id
,
102 const wxString
& value
,
103 const wxPoint
& pos
, const wxSize
& size
,
105 double min
, double max
, double initial
, double inc
,
106 const wxString
& name
)
108 if (!PreCreation( parent
, pos
, size
) ||
109 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
111 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
115 m_widget
= gtk_spin_button_new_with_range(min
, max
, inc
);
116 g_object_ref(m_widget
);
118 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), initial
);
119 m_value
= gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget
));
122 if ( HasFlag(wxALIGN_RIGHT
) )
124 else if ( HasFlag(wxALIGN_CENTRE
) )
129 gtk_entry_set_alignment(GTK_ENTRY(m_widget
), align
);
131 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
132 (int)(m_windowStyle
& wxSP_WRAP
) );
134 g_signal_connect_after(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
135 g_signal_connect_after(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
137 m_parent
->DoAddChild( this );
149 double wxSpinCtrlGTKBase::DoGetValue() const
151 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
154 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget
) );
155 const_cast<wxSpinCtrlGTKBase
*>(this)->m_value
=
156 gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget
));
162 double wxSpinCtrlGTKBase::DoGetMin() const
164 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
167 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), &min
, NULL
);
171 double wxSpinCtrlGTKBase::DoGetMax() const
173 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
176 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), NULL
, &max
);
180 double wxSpinCtrlGTKBase::DoGetIncrement() const
182 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
185 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), NULL
, &inc
);
189 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
191 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
193 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
) );
196 void wxSpinCtrlGTKBase::SetValue( const wxString
& value
)
198 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
201 if ( wxSscanf(value
, "%lg", &n
) == 1 )
203 // a number - set it, let DoSetValue round for int value
208 // invalid number - set text as is (wxMSW compatible)
210 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
214 void wxSpinCtrlGTKBase::DoSetValue( double value
)
216 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
218 if (wxIsKindOf(this, wxSpinCtrl
))
219 value
= wxRound( value
);
222 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), value
);
223 m_value
= gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget
));
227 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks
)
229 wxCHECK_RET( (m_widget
!= NULL
), "invalid spin button" );
231 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
), snap_to_ticks
);
234 void wxSpinCtrlGTKBase::SetSelection(long from
, long to
)
236 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
238 if ( from
== -1 && to
== -1 )
244 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
247 void wxSpinCtrlGTKBase::DoSetRange(double minVal
, double maxVal
)
249 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
252 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget
), minVal
, maxVal
);
253 m_value
= gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget
));
257 void wxSpinCtrlGTKBase::DoSetIncrement(double inc
)
259 wxCHECK_RET( m_widget
, "invalid spin button" );
262 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget
), inc
, 10*inc
);
263 m_value
= gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget
));
267 void wxSpinCtrlGTKBase::GtkDisableEvents() const
269 g_signal_handlers_block_by_func( m_widget
,
270 (gpointer
)gtk_value_changed
, (void*) this);
272 g_signal_handlers_block_by_func(m_widget
,
273 (gpointer
)gtk_changed
, (void*) this);
276 void wxSpinCtrlGTKBase::GtkEnableEvents() const
278 g_signal_handlers_unblock_by_func(m_widget
,
279 (gpointer
)gtk_value_changed
, (void*) this);
281 g_signal_handlers_unblock_by_func(m_widget
,
282 (gpointer
)gtk_changed
, (void*) this);
285 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent
&event
)
287 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
289 if (event
.GetKeyCode() == WXK_RETURN
)
291 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
293 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
295 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
298 GtkWidget
*widgetDef
= window
->default_widget
;
302 gtk_widget_activate(widgetDef
);
309 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
311 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
312 evt
.SetEventObject(this);
313 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
314 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
315 evt
.SetString( val
);
316 if (HandleWindowEvent(evt
)) return;
322 GdkWindow
*wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows
& windows
) const
324 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
326 windows
.push_back(spinbutton
->entry
.text_area
);
327 windows
.push_back(spinbutton
->panel
);
332 wxSize
wxSpinCtrlGTKBase::DoGetBestSize() const
334 wxSize
ret( wxControl::DoGetBestSize() );
335 wxSize
best(95, ret
.y
); // FIXME: 95?
342 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
344 // TODO: overload to accept functions like gtk_spin_button_new?
345 // Until then use a similar type
346 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
349 //-----------------------------------------------------------------------------
351 //-----------------------------------------------------------------------------
353 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxSpinCtrlGTKBase
)
355 //-----------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------
359 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGTKBase
)
361 unsigned wxSpinCtrlDouble::GetDigits() const
363 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
365 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget
) );
368 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
370 wxCHECK_RET( m_widget
, "invalid spin button" );
372 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget
), digits
);
375 #endif // wxUSE_SPINCTRL