]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinctrl.cpp
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/spinctrl.h"
18 #include "wx/textctrl.h" // for wxEVT_TEXT
20 #include "wx/wxcrtvararg.h"
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/private/gtk2-compat.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern bool g_blockEventsOnDrag
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 gtk_value_changed(GtkSpinButton
* spinbutton
, wxSpinCtrlGTKBase
* win
)
41 if (g_blockEventsOnDrag
)
44 if (wxIsKindOf(win
, wxSpinCtrl
))
46 wxSpinEvent
event(wxEVT_SPINCTRL
, win
->GetId());
47 event
.SetEventObject( win
);
48 event
.SetPosition(static_cast<wxSpinCtrl
*>(win
)->GetValue());
49 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
50 win
->HandleWindowEvent( event
);
52 else // wxIsKindOf(win, wxSpinCtrlDouble)
54 wxSpinDoubleEvent
event( wxEVT_SPINCTRLDOUBLE
, win
->GetId());
55 event
.SetEventObject( win
);
56 event
.SetValue(static_cast<wxSpinCtrlDouble
*>(win
)->GetValue());
57 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
58 win
->HandleWindowEvent( event
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
69 gtk_changed(GtkSpinButton
* spinbutton
, wxSpinCtrl
* win
)
71 wxCommandEvent
event( wxEVT_TEXT
, win
->GetId() );
72 event
.SetEventObject( win
);
73 event
.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton
)));
74 event
.SetInt(win
->GetValue());
75 win
->HandleWindowEvent( event
);
79 // ----------------------------------------------------------------------------
80 // wxSpinCtrlEventDisabler: helper to temporarily disable GTK+ events
81 // ----------------------------------------------------------------------------
83 class wxSpinCtrlEventDisabler
86 wxEXPLICIT
wxSpinCtrlEventDisabler(wxSpinCtrlGTKBase
* spin
)
89 m_spin
->GtkDisableEvents();
92 ~wxSpinCtrlEventDisabler()
94 m_spin
->GtkEnableEvents();
98 wxSpinCtrlGTKBase
* const m_spin
;
100 wxDECLARE_NO_COPY_CLASS(wxSpinCtrlEventDisabler
);
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
107 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
108 EVT_CHAR(wxSpinCtrlGTKBase::OnChar
)
111 bool wxSpinCtrlGTKBase::Create(wxWindow
*parent
, wxWindowID id
,
112 const wxString
& value
,
113 const wxPoint
& pos
, const wxSize
& size
,
115 double min
, double max
, double initial
, double inc
,
116 const wxString
& name
)
118 if (!PreCreation( parent
, pos
, size
) ||
119 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
121 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
125 m_widget
= gtk_spin_button_new_with_range(min
, max
, inc
);
126 g_object_ref(m_widget
);
128 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), initial
);
131 if ( HasFlag(wxALIGN_RIGHT
) )
133 else if ( HasFlag(wxALIGN_CENTRE
) )
138 gtk_entry_set_alignment(GTK_ENTRY(m_widget
), align
);
140 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
141 (int)(m_windowStyle
& wxSP_WRAP
) );
143 g_signal_connect_after(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
144 g_signal_connect_after(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
146 m_parent
->DoAddChild( this );
158 double wxSpinCtrlGTKBase::DoGetValue() const
160 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
162 // Get value directly from current control text, just as
163 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
164 // a redraw, which causes an idle event, so if GetValue() is called from
165 // a UI update handler, you get a never ending sequence of idle events. It
166 // also forces the text into valid range, which wxMSW GetValue() does not do.
167 static unsigned sig_id
;
169 sig_id
= g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON
);
172 g_signal_emit(m_widget
, sig_id
, 0, &value
, &handled
);
174 value
= g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget
)), NULL
);
176 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget
));
177 const double lower
= gtk_adjustment_get_lower(adj
);
178 const double upper
= gtk_adjustment_get_upper(adj
);
181 else if (value
> upper
)
187 double wxSpinCtrlGTKBase::DoGetMin() const
189 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
192 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), &min
, NULL
);
196 double wxSpinCtrlGTKBase::DoGetMax() const
198 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
201 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), NULL
, &max
);
205 double wxSpinCtrlGTKBase::DoGetIncrement() const
207 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
210 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), &inc
, NULL
);
214 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
216 wxCHECK_MSG(m_widget
, false, "invalid spin button");
218 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
) ) != 0;
221 void wxSpinCtrlGTKBase::SetValue( const wxString
& value
)
223 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
226 if ( wxSscanf(value
, "%lg", &n
) == 1 )
228 // a number - set it, let DoSetValue round for int value
233 // invalid number - set text as is (wxMSW compatible)
234 wxSpinCtrlEventDisabler
disable(this);
235 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
238 void wxSpinCtrlGTKBase::DoSetValue( double value
)
240 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
242 wxSpinCtrlEventDisabler
disable(this);
243 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), value
);
246 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks
)
248 wxCHECK_RET( (m_widget
!= NULL
), "invalid spin button" );
250 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
), snap_to_ticks
);
253 void wxSpinCtrlGTKBase::SetSelection(long from
, long to
)
255 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
257 if ( from
== -1 && to
== -1 )
263 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
266 void wxSpinCtrlGTKBase::DoSetRange(double minVal
, double maxVal
)
268 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
270 wxSpinCtrlEventDisabler
disable(this);
271 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget
), minVal
, maxVal
);
274 void wxSpinCtrlGTKBase::DoSetIncrement(double inc
)
276 wxCHECK_RET( m_widget
, "invalid spin button" );
278 wxSpinCtrlEventDisabler
disable(this);
280 // Preserve the old page value when changing just the increment.
281 double page
= 10*inc
;
282 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), NULL
, &page
);
284 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget
), inc
, page
);
287 void wxSpinCtrlGTKBase::GtkDisableEvents() const
289 g_signal_handlers_block_by_func( m_widget
,
290 (gpointer
)gtk_value_changed
, (void*) this);
292 g_signal_handlers_block_by_func(m_widget
,
293 (gpointer
)gtk_changed
, (void*) this);
296 void wxSpinCtrlGTKBase::GtkEnableEvents() const
298 g_signal_handlers_unblock_by_func(m_widget
,
299 (gpointer
)gtk_value_changed
, (void*) this);
301 g_signal_handlers_unblock_by_func(m_widget
,
302 (gpointer
)gtk_changed
, (void*) this);
305 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent
&event
)
307 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
309 if (event
.GetKeyCode() == WXK_RETURN
)
311 wxWindow
*top_frame
= wxGetTopLevelParent(m_parent
);
313 if ( GTK_IS_WINDOW(top_frame
->m_widget
) )
315 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
318 GtkWidget
* widgetDef
= gtk_window_get_default_widget(window
);
322 gtk_widget_activate(widgetDef
);
329 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
331 wxCommandEvent
evt( wxEVT_TEXT_ENTER
, m_windowId
);
332 evt
.SetEventObject(this);
333 GtkSpinButton
*gsb
= GTK_SPIN_BUTTON(m_widget
);
334 wxString val
= wxGTK_CONV_BACK( gtk_entry_get_text( &gsb
->entry
) );
335 evt
.SetString( val
);
336 if (HandleWindowEvent(evt
)) return;
342 GdkWindow
*wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows
& windows
) const
345 // no access to internal GdkWindows
346 wxUnusedVar(windows
);
348 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
350 windows
.push_back(spinbutton
->entry
.text_area
);
351 windows
.push_back(spinbutton
->panel
);
357 wxSize
wxSpinCtrlGTKBase::DoGetBestSize() const
359 return DoGetSizeFromTextSize(95); // TODO: 95 is completely arbitrary
362 wxSize
wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen
, int ylen
) const
364 wxASSERT_MSG( m_widget
, wxS("GetSizeFromTextSize called before creation") );
366 // Set an as small as possible size for the control, so preferred sizes
367 // return "natural" sizes, not taking into account the previous ones (which
368 // seems to be GTK+3 behaviour)
369 gtk_widget_set_size_request(m_widget
, 0, 0);
371 // Both Gtk+2 and Gtk+3 use current value/range to measure control's width.
372 // So, we can't ask Gtk+ for its width. Instead, we used hardcoded values.
374 // Returned height is OK
375 wxSize totalS
= GTKGetPreferredSize(m_widget
);
377 #if GTK_CHECK_VERSION(3,4,0)
378 // two buttons in horizontal
379 totalS
.x
= 46 + 15; // margins included
381 // two small buttons in vertical
382 totalS
.x
= GetFont().GetPixelSize().y
+ 13; // margins included
385 wxSize
tsize(xlen
+ totalS
.x
, totalS
.y
);
387 // Check if the user requested a non-standard height.
389 tsize
.IncBy(0, ylen
- GetCharHeight());
396 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
398 return GetDefaultAttributesFromGTKWidget(gtk_spin_button_new_with_range(0, 100, 1), true);
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
409 wx_gtk_spin_input(GtkSpinButton
* spin
, gdouble
* val
, wxSpinCtrl
* win
)
411 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
412 // wxString function even if this requires an extra conversion.
414 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin
))));
417 if ( !text
.ToLong(&lval
, win
->GetBase()) )
426 wx_gtk_spin_output(GtkSpinButton
* spin
, wxSpinCtrl
* win
)
428 const gint val
= gtk_spin_button_get_value_as_int(spin
);
433 wxPrivate::wxSpinCtrlFormatAsHex(val
, win
->GetMax()).utf8_str()
441 bool wxSpinCtrl::SetBase(int base
)
443 // Currently we only support base 10 and 16. We could add support for base
444 // 8 quite easily but wxMSW doesn't support it natively so don't bother
445 // with doing something wxGTK-specific here.
446 if ( base
!= 10 && base
!= 16 )
449 if ( base
== m_base
)
454 // We need to be able to enter letters for any base greater than 10.
455 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget
), m_base
<= 10 );
459 g_signal_connect( m_widget
, "input",
460 G_CALLBACK(wx_gtk_spin_input
), this);
461 g_signal_connect( m_widget
, "output",
462 G_CALLBACK(wx_gtk_spin_output
), this);
466 g_signal_handlers_disconnect_by_func(m_widget
,
467 (gpointer
)wx_gtk_spin_input
,
469 g_signal_handlers_disconnect_by_func(m_widget
,
470 (gpointer
)wx_gtk_spin_output
,
477 //-----------------------------------------------------------------------------
479 //-----------------------------------------------------------------------------
481 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGTKBase
)
483 unsigned wxSpinCtrlDouble::GetDigits() const
485 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
487 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget
) );
490 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
492 wxCHECK_RET( m_widget
, "invalid spin button" );
494 wxSpinCtrlEventDisabler
disable(this);
495 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget
), digits
);
498 #endif // wxUSE_SPINCTRL