1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinctrl.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"
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/private/gtk2-compat.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
40 gtk_value_changed(GtkSpinButton
* spinbutton
, wxSpinCtrlGTKBase
* win
)
42 if (g_blockEventsOnDrag
)
45 if (wxIsKindOf(win
, wxSpinCtrl
))
47 wxSpinEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, win
->GetId());
48 event
.SetEventObject( win
);
49 event
.SetPosition(static_cast<wxSpinCtrl
*>(win
)->GetValue());
50 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
51 win
->HandleWindowEvent( event
);
53 else // wxIsKindOf(win, wxSpinCtrlDouble)
55 wxSpinDoubleEvent
event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED
, win
->GetId());
56 event
.SetEventObject( win
);
57 event
.SetValue(static_cast<wxSpinCtrlDouble
*>(win
)->GetValue());
58 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
59 win
->HandleWindowEvent( event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
70 gtk_changed(GtkSpinButton
* spinbutton
, wxSpinCtrl
* win
)
72 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
73 event
.SetEventObject( win
);
74 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
75 event
.SetInt(win
->GetValue());
76 win
->HandleWindowEvent( event
);
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
85 EVT_CHAR(wxSpinCtrlGTKBase::OnChar
)
88 bool wxSpinCtrlGTKBase::Create(wxWindow
*parent
, wxWindowID id
,
89 const wxString
& value
,
90 const wxPoint
& pos
, const wxSize
& size
,
92 double min
, double max
, double initial
, double inc
,
95 if (!PreCreation( parent
, pos
, size
) ||
96 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
98 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
102 m_widget
= gtk_spin_button_new_with_range(min
, max
, inc
);
103 g_object_ref(m_widget
);
105 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), initial
);
108 if ( HasFlag(wxALIGN_RIGHT
) )
110 else if ( HasFlag(wxALIGN_CENTRE
) )
115 gtk_entry_set_alignment(GTK_ENTRY(m_widget
), align
);
117 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
118 (int)(m_windowStyle
& wxSP_WRAP
) );
120 g_signal_connect_after(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
121 g_signal_connect_after(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
123 m_parent
->DoAddChild( this );
135 double wxSpinCtrlGTKBase::DoGetValue() const
137 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
139 // Get value directly from current control text, just as
140 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
141 // a redraw, which causes an idle event, so if GetValue() is called from
142 // a UI update handler, you get a never ending sequence of idle events. It
143 // also forces the text into valid range, which wxMSW GetValue() does not do.
144 static unsigned sig_id
;
146 sig_id
= g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON
);
149 g_signal_emit(m_widget
, sig_id
, 0, &value
, &handled
);
151 value
= g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget
)), NULL
);
153 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget
));
154 const double lower
= gtk_adjustment_get_lower(adj
);
155 const double upper
= gtk_adjustment_get_upper(adj
);
158 else if (value
> upper
)
164 double wxSpinCtrlGTKBase::DoGetMin() const
166 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
169 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), &min
, NULL
);
173 double wxSpinCtrlGTKBase::DoGetMax() const
175 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
178 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), NULL
, &max
);
182 double wxSpinCtrlGTKBase::DoGetIncrement() const
184 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
187 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), &inc
, NULL
);
191 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
193 wxCHECK_MSG(m_widget
, false, "invalid spin button");
195 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
) ) != 0;
198 void wxSpinCtrlGTKBase::SetValue( const wxString
& value
)
200 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
203 if ( wxSscanf(value
, "%lg", &n
) == 1 )
205 // a number - set it, let DoSetValue round for int value
210 // invalid number - set text as is (wxMSW compatible)
212 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
216 void wxSpinCtrlGTKBase::DoSetValue( double value
)
218 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
221 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), value
);
225 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks
)
227 wxCHECK_RET( (m_widget
!= NULL
), "invalid spin button" );
229 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
), snap_to_ticks
);
232 void wxSpinCtrlGTKBase::SetSelection(long from
, long to
)
234 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
236 if ( from
== -1 && to
== -1 )
242 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
245 void wxSpinCtrlGTKBase::DoSetRange(double minVal
, double maxVal
)
247 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
250 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget
), minVal
, maxVal
);
254 void wxSpinCtrlGTKBase::DoSetIncrement(double inc
)
256 wxCHECK_RET( m_widget
, "invalid spin button" );
260 // Preserve the old page value when changing just the increment.
261 double page
= 10*inc
;
262 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), NULL
, &page
);
264 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget
), inc
, page
);
268 void wxSpinCtrlGTKBase::GtkDisableEvents() const
270 g_signal_handlers_block_by_func( m_widget
,
271 (gpointer
)gtk_value_changed
, (void*) this);
273 g_signal_handlers_block_by_func(m_widget
,
274 (gpointer
)gtk_changed
, (void*) this);
277 void wxSpinCtrlGTKBase::GtkEnableEvents() const
279 g_signal_handlers_unblock_by_func(m_widget
,
280 (gpointer
)gtk_value_changed
, (void*) this);
282 g_signal_handlers_unblock_by_func(m_widget
,
283 (gpointer
)gtk_changed
, (void*) this);
286 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent
&event
)
288 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
290 if (event
.GetKeyCode() == WXK_RETURN
)
292 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
294 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
296 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
299 GtkWidget
* widgetDef
= gtk_window_get_default_widget(window
);
303 gtk_widget_activate(widgetDef
);
310 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
312 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
313 evt
.SetEventObject(this);
314 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
315 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
316 evt
.SetString( val
);
317 if (HandleWindowEvent(evt
)) return;
323 GdkWindow
*wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows
& windows
) const
326 // no access to internal GdkWindows
327 wxUnusedVar(windows
);
329 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
331 windows
.push_back(spinbutton
->entry
.text_area
);
332 windows
.push_back(spinbutton
->panel
);
338 wxSize
wxSpinCtrlGTKBase::DoGetBestSize() const
340 wxSize
ret( wxControl::DoGetBestSize() );
341 wxSize
best(95, ret
.y
); // FIXME: 95?
348 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
350 // TODO: overload to accept functions like gtk_spin_button_new?
351 // Until then use a similar type
352 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
355 //-----------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------
363 wx_gtk_spin_input(GtkSpinButton
* spin
, gdouble
* val
, wxSpinCtrl
* win
)
365 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
366 // wxString function even if this requires an extra conversion.
368 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin
))));
371 if ( !text
.ToLong(&lval
, win
->GetBase()) )
380 wx_gtk_spin_output(GtkSpinButton
* spin
, wxSpinCtrl
* win
)
382 const gint val
= gtk_spin_button_get_value_as_int(spin
);
387 wxPrivate::wxSpinCtrlFormatAsHex(val
, win
->GetMax()).utf8_str()
395 bool wxSpinCtrl::SetBase(int base
)
397 // Currently we only support base 10 and 16. We could add support for base
398 // 8 quite easily but wxMSW doesn't support it natively so don't bother
399 // with doing something wxGTK-specific here.
400 if ( base
!= 10 && base
!= 16 )
403 if ( base
== m_base
)
408 // We need to be able to enter letters for any base greater than 10.
409 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget
), m_base
<= 10 );
413 g_signal_connect( m_widget
, "input",
414 G_CALLBACK(wx_gtk_spin_input
), this);
415 g_signal_connect( m_widget
, "output",
416 G_CALLBACK(wx_gtk_spin_output
), this);
420 g_signal_handlers_disconnect_by_func(m_widget
,
421 (gpointer
)wx_gtk_spin_input
,
423 g_signal_handlers_disconnect_by_func(m_widget
,
424 (gpointer
)wx_gtk_spin_output
,
431 //-----------------------------------------------------------------------------
433 //-----------------------------------------------------------------------------
435 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGTKBase
)
437 unsigned wxSpinCtrlDouble::GetDigits() const
439 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
441 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget
) );
444 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
446 wxCHECK_RET( m_widget
, "invalid spin button" );
448 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget
), digits
);
451 #endif // wxUSE_SPINCTRL