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 (!win
->m_hasVMT
|| 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
)
75 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
76 event
.SetEventObject( win
);
77 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
78 event
.SetInt(win
->GetValue());
79 win
->HandleWindowEvent( event
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
88 EVT_CHAR(wxSpinCtrlGTKBase::OnChar
)
91 bool wxSpinCtrlGTKBase::Create(wxWindow
*parent
, wxWindowID id
,
92 const wxString
& value
,
93 const wxPoint
& pos
, const wxSize
& size
,
95 double min
, double max
, double initial
, double inc
,
98 if (!PreCreation( parent
, pos
, size
) ||
99 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
101 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
105 m_widget
= gtk_spin_button_new_with_range(min
, max
, inc
);
106 g_object_ref(m_widget
);
108 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), initial
);
111 if ( HasFlag(wxALIGN_RIGHT
) )
113 else if ( HasFlag(wxALIGN_CENTRE
) )
118 gtk_entry_set_alignment(GTK_ENTRY(m_widget
), align
);
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
121 (int)(m_windowStyle
& wxSP_WRAP
) );
123 g_signal_connect_after(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
124 g_signal_connect_after(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
126 m_parent
->DoAddChild( this );
138 double wxSpinCtrlGTKBase::DoGetValue() const
140 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
142 // Get value directly from current control text, just as
143 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
144 // a redraw, which causes an idle event, so if GetValue() is called from
145 // a UI update handler, you get a never ending sequence of idle events. It
146 // also forces the text into valid range, which wxMSW GetValue() does not do.
147 static unsigned sig_id
;
149 sig_id
= g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON
);
152 g_signal_emit(m_widget
, sig_id
, 0, &value
, &handled
);
154 value
= g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget
)), NULL
);
156 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget
));
157 const double lower
= gtk_adjustment_get_lower(adj
);
158 const double upper
= gtk_adjustment_get_upper(adj
);
161 else if (value
> upper
)
167 double wxSpinCtrlGTKBase::DoGetMin() const
169 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
172 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), &min
, NULL
);
176 double wxSpinCtrlGTKBase::DoGetMax() const
178 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
181 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), NULL
, &max
);
185 double wxSpinCtrlGTKBase::DoGetIncrement() const
187 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
190 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), &inc
, NULL
);
194 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
196 wxCHECK_MSG(m_widget
, false, "invalid spin button");
198 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
) ) != 0;
201 void wxSpinCtrlGTKBase::SetValue( const wxString
& value
)
203 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
206 if ( wxSscanf(value
, "%lg", &n
) == 1 )
208 // a number - set it, let DoSetValue round for int value
213 // invalid number - set text as is (wxMSW compatible)
215 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
219 void wxSpinCtrlGTKBase::DoSetValue( double value
)
221 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
224 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), value
);
228 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks
)
230 wxCHECK_RET( (m_widget
!= NULL
), "invalid spin button" );
232 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
), snap_to_ticks
);
235 void wxSpinCtrlGTKBase::SetSelection(long from
, long to
)
237 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
239 if ( from
== -1 && to
== -1 )
245 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
248 void wxSpinCtrlGTKBase::DoSetRange(double minVal
, double maxVal
)
250 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
253 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget
), minVal
, maxVal
);
257 void wxSpinCtrlGTKBase::DoSetIncrement(double inc
)
259 wxCHECK_RET( m_widget
, "invalid spin button" );
263 // Preserve the old page value when changing just the increment.
264 double page
= 10*inc
;
265 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), NULL
, &page
);
267 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget
), inc
, page
);
271 void wxSpinCtrlGTKBase::GtkDisableEvents() const
273 g_signal_handlers_block_by_func( m_widget
,
274 (gpointer
)gtk_value_changed
, (void*) this);
276 g_signal_handlers_block_by_func(m_widget
,
277 (gpointer
)gtk_changed
, (void*) this);
280 void wxSpinCtrlGTKBase::GtkEnableEvents() const
282 g_signal_handlers_unblock_by_func(m_widget
,
283 (gpointer
)gtk_value_changed
, (void*) this);
285 g_signal_handlers_unblock_by_func(m_widget
,
286 (gpointer
)gtk_changed
, (void*) this);
289 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent
&event
)
291 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
293 if (event
.GetKeyCode() == WXK_RETURN
)
295 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
297 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
299 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
302 GtkWidget
* widgetDef
= gtk_window_get_default_widget(window
);
306 gtk_widget_activate(widgetDef
);
313 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
315 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
316 evt
.SetEventObject(this);
317 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
318 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
319 evt
.SetString( val
);
320 if (HandleWindowEvent(evt
)) return;
326 GdkWindow
*wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows
& windows
) const
329 // no access to internal GdkWindows
330 wxUnusedVar(windows
);
332 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
334 windows
.push_back(spinbutton
->entry
.text_area
);
335 windows
.push_back(spinbutton
->panel
);
341 wxSize
wxSpinCtrlGTKBase::DoGetBestSize() const
343 wxSize
ret( wxControl::DoGetBestSize() );
344 wxSize
best(95, ret
.y
); // FIXME: 95?
351 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
353 // TODO: overload to accept functions like gtk_spin_button_new?
354 // Until then use a similar type
355 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
358 //-----------------------------------------------------------------------------
360 //-----------------------------------------------------------------------------
366 wx_gtk_spin_input(GtkSpinButton
* spin
, gdouble
* val
, wxSpinCtrl
* win
)
368 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
369 // wxString function even if this requires an extra conversion.
371 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin
))));
374 if ( !text
.ToLong(&lval
, win
->GetBase()) )
383 wx_gtk_spin_output(GtkSpinButton
* spin
, wxSpinCtrl
* win
)
385 const gint val
= gtk_spin_button_get_value_as_int(spin
);
390 wxPrivate::wxSpinCtrlFormatAsHex(val
, win
->GetMax()).utf8_str()
398 bool wxSpinCtrl::SetBase(int base
)
400 // Currently we only support base 10 and 16. We could add support for base
401 // 8 quite easily but wxMSW doesn't support it natively so don't bother
402 // with doing something wxGTK-specific here.
403 if ( base
!= 10 && base
!= 16 )
406 if ( base
== m_base
)
411 // We need to be able to enter letters for any base greater than 10.
412 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget
), m_base
<= 10 );
416 g_signal_connect( m_widget
, "input",
417 G_CALLBACK(wx_gtk_spin_input
), this);
418 g_signal_connect( m_widget
, "output",
419 G_CALLBACK(wx_gtk_spin_output
), this);
423 g_signal_handlers_disconnect_by_func(m_widget
,
424 (gpointer
)wx_gtk_spin_input
,
426 g_signal_handlers_disconnect_by_func(m_widget
,
427 (gpointer
)wx_gtk_spin_output
,
434 //-----------------------------------------------------------------------------
436 //-----------------------------------------------------------------------------
438 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGTKBase
)
440 unsigned wxSpinCtrlDouble::GetDigits() const
442 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
444 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget
) );
447 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
449 wxCHECK_RET( m_widget
, "invalid spin button" );
451 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget
), digits
);
454 #endif // wxUSE_SPINCTRL