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 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
89 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
90 EVT_CHAR(wxSpinCtrlGTKBase::OnChar
)
93 bool wxSpinCtrlGTKBase::Create(wxWindow
*parent
, wxWindowID id
,
94 const wxString
& value
,
95 const wxPoint
& pos
, const wxSize
& size
,
97 double min
, double max
, double initial
, double inc
,
100 if (!PreCreation( parent
, pos
, size
) ||
101 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
103 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
107 m_widget
= gtk_spin_button_new_with_range(min
, max
, inc
);
108 g_object_ref(m_widget
);
110 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), initial
);
113 if ( HasFlag(wxALIGN_RIGHT
) )
115 else if ( HasFlag(wxALIGN_CENTRE
) )
120 gtk_entry_set_alignment(GTK_ENTRY(m_widget
), align
);
122 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
123 (int)(m_windowStyle
& wxSP_WRAP
) );
125 g_signal_connect_after(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
126 g_signal_connect_after(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
128 m_parent
->DoAddChild( this );
140 double wxSpinCtrlGTKBase::DoGetValue() const
142 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
144 // Get value directly from current control text, just as
145 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
146 // a redraw, which causes an idle event, so if GetValue() is called from
147 // a UI update handler, you get a never ending sequence of idle events. It
148 // also forces the text into valid range, which wxMSW GetValue() does not do.
149 static unsigned sig_id
;
151 sig_id
= g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON
);
154 g_signal_emit(m_widget
, sig_id
, 0, &value
, &handled
);
156 value
= g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget
)), NULL
);
158 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget
));
159 const double lower
= gtk_adjustment_get_lower(adj
);
160 const double upper
= gtk_adjustment_get_upper(adj
);
163 else if (value
> upper
)
169 double wxSpinCtrlGTKBase::DoGetMin() const
171 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
174 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), &min
, NULL
);
178 double wxSpinCtrlGTKBase::DoGetMax() const
180 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
183 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), NULL
, &max
);
187 double wxSpinCtrlGTKBase::DoGetIncrement() const
189 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
192 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), &inc
, NULL
);
196 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
198 wxCHECK_MSG(m_widget
, false, "invalid spin button");
200 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
) ) != 0;
203 void wxSpinCtrlGTKBase::SetValue( const wxString
& value
)
205 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
208 if ( wxSscanf(value
, "%lg", &n
) == 1 )
210 // a number - set it, let DoSetValue round for int value
215 // invalid number - set text as is (wxMSW compatible)
217 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
221 void wxSpinCtrlGTKBase::DoSetValue( double value
)
223 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
226 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), value
);
230 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks
)
232 wxCHECK_RET( (m_widget
!= NULL
), "invalid spin button" );
234 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
), snap_to_ticks
);
237 void wxSpinCtrlGTKBase::SetSelection(long from
, long to
)
239 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
241 if ( from
== -1 && to
== -1 )
247 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
250 void wxSpinCtrlGTKBase::DoSetRange(double minVal
, double maxVal
)
252 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
255 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget
), minVal
, maxVal
);
259 void wxSpinCtrlGTKBase::DoSetIncrement(double inc
)
261 wxCHECK_RET( m_widget
, "invalid spin button" );
265 // Preserve the old page value when changing just the increment.
266 double page
= 10*inc
;
267 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), NULL
, &page
);
269 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget
), inc
, page
);
273 void wxSpinCtrlGTKBase::GtkDisableEvents() const
275 g_signal_handlers_block_by_func( m_widget
,
276 (gpointer
)gtk_value_changed
, (void*) this);
278 g_signal_handlers_block_by_func(m_widget
,
279 (gpointer
)gtk_changed
, (void*) this);
282 void wxSpinCtrlGTKBase::GtkEnableEvents() const
284 g_signal_handlers_unblock_by_func(m_widget
,
285 (gpointer
)gtk_value_changed
, (void*) this);
287 g_signal_handlers_unblock_by_func(m_widget
,
288 (gpointer
)gtk_changed
, (void*) this);
291 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent
&event
)
293 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
295 if (event
.GetKeyCode() == WXK_RETURN
)
297 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
299 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
301 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
304 GtkWidget
* widgetDef
= gtk_window_get_default_widget(window
);
308 gtk_widget_activate(widgetDef
);
315 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
317 wxCommandEvent
evt( wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
318 evt
.SetEventObject(this);
319 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
320 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
321 evt
.SetString( val
);
322 if (HandleWindowEvent(evt
)) return;
328 GdkWindow
*wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows
& windows
) const
331 // no access to internal GdkWindows
332 wxUnusedVar(windows
);
334 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
336 windows
.push_back(spinbutton
->entry
.text_area
);
337 windows
.push_back(spinbutton
->panel
);
343 wxSize
wxSpinCtrlGTKBase::DoGetBestSize() const
345 wxSize
ret( wxControl::DoGetBestSize() );
346 wxSize
best(95, ret
.y
); // FIXME: 95?
353 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
355 // TODO: overload to accept functions like gtk_spin_button_new?
356 // Until then use a similar type
357 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);
360 //-----------------------------------------------------------------------------
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
366 //-----------------------------------------------------------------------------
368 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGTKBase
)
370 unsigned wxSpinCtrlDouble::GetDigits() const
372 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
374 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget
) );
377 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
379 wxCHECK_RET( m_widget
, "invalid spin button" );
381 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget
), digits
);
384 #endif // wxUSE_SPINCTRL