]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
588e5ce4d754b75a7012932012f4aa824491c4a9
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 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase
, wxSpinCtrlBase
)
84 EVT_CHAR(wxSpinCtrlGTKBase::OnChar
)
87 bool wxSpinCtrlGTKBase::Create(wxWindow
*parent
, wxWindowID id
,
88 const wxString
& value
,
89 const wxPoint
& pos
, const wxSize
& size
,
91 double min
, double max
, double initial
, double inc
,
94 if (!PreCreation( parent
, pos
, size
) ||
95 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
97 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
101 m_widget
= gtk_spin_button_new_with_range(min
, max
, inc
);
102 g_object_ref(m_widget
);
104 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), initial
);
107 if ( HasFlag(wxALIGN_RIGHT
) )
109 else if ( HasFlag(wxALIGN_CENTRE
) )
114 gtk_entry_set_alignment(GTK_ENTRY(m_widget
), align
);
116 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
117 (int)(m_windowStyle
& wxSP_WRAP
) );
119 g_signal_connect_after(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
120 g_signal_connect_after(m_widget
, "changed", G_CALLBACK(gtk_changed
), this);
122 m_parent
->DoAddChild( this );
134 double wxSpinCtrlGTKBase::DoGetValue() const
136 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
138 // Get value directly from current control text, just as
139 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
140 // a redraw, which causes an idle event, so if GetValue() is called from
141 // a UI update handler, you get a never ending sequence of idle events. It
142 // also forces the text into valid range, which wxMSW GetValue() does not do.
143 static unsigned sig_id
;
145 sig_id
= g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON
);
148 g_signal_emit(m_widget
, sig_id
, 0, &value
, &handled
);
150 value
= g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget
)), NULL
);
152 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget
));
153 const double lower
= gtk_adjustment_get_lower(adj
);
154 const double upper
= gtk_adjustment_get_upper(adj
);
157 else if (value
> upper
)
163 double wxSpinCtrlGTKBase::DoGetMin() const
165 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
168 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), &min
, NULL
);
172 double wxSpinCtrlGTKBase::DoGetMax() const
174 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
177 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget
), NULL
, &max
);
181 double wxSpinCtrlGTKBase::DoGetIncrement() const
183 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
186 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), &inc
, NULL
);
190 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
192 wxCHECK_MSG(m_widget
, false, "invalid spin button");
194 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
) ) != 0;
197 void wxSpinCtrlGTKBase::SetValue( const wxString
& value
)
199 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
202 if ( wxSscanf(value
, "%lg", &n
) == 1 )
204 // a number - set it, let DoSetValue round for int value
209 // invalid number - set text as is (wxMSW compatible)
211 gtk_entry_set_text( GTK_ENTRY(m_widget
), wxGTK_CONV( value
) );
215 void wxSpinCtrlGTKBase::DoSetValue( double value
)
217 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
220 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget
), value
);
224 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks
)
226 wxCHECK_RET( (m_widget
!= NULL
), "invalid spin button" );
228 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget
), snap_to_ticks
);
231 void wxSpinCtrlGTKBase::SetSelection(long from
, long to
)
233 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
235 if ( from
== -1 && to
== -1 )
241 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
244 void wxSpinCtrlGTKBase::DoSetRange(double minVal
, double maxVal
)
246 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
249 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget
), minVal
, maxVal
);
253 void wxSpinCtrlGTKBase::DoSetIncrement(double inc
)
255 wxCHECK_RET( m_widget
, "invalid spin button" );
259 // Preserve the old page value when changing just the increment.
260 double page
= 10*inc
;
261 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget
), NULL
, &page
);
263 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget
), inc
, page
);
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
= gtk_window_get_default_widget(window
);
302 gtk_widget_activate(widgetDef
);
309 if ((event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
311 wxCommandEvent
evt( wxEVT_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
325 // no access to internal GdkWindows
326 wxUnusedVar(windows
);
328 GtkSpinButton
* spinbutton
= GTK_SPIN_BUTTON(m_widget
);
330 windows
.push_back(spinbutton
->entry
.text_area
);
331 windows
.push_back(spinbutton
->panel
);
337 wxSize
wxSpinCtrlGTKBase::DoGetBestSize() const
339 return DoGetSizeFromTextSize(95); // TODO: 95 is completely arbitrary
342 wxSize
wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen
, int ylen
) const
344 wxASSERT_MSG( m_widget
, wxS("GetSizeFromTextSize called before creation") );
346 // Set an as small as possible size for the control, so preferred sizes
347 // return "natural" sizes, not taking into account the previous ones (which
348 // seems to be GTK+3 behaviour)
349 gtk_widget_set_size_request(m_widget
, 0, 0);
351 // Both Gtk+2 and Gtk+3 use current value/range to measure control's width.
352 // So, we can't ask Gtk+ for its width. Instead, we used hardcoded values.
354 // Returned height is OK
355 wxSize totalS
= GTKGetPreferredSize(m_widget
);
357 #if GTK_CHECK_VERSION(3,4,0)
358 // two buttons in horizontal
359 totalS
.x
= 46 + 15; // margins included
361 // two small buttons in vertical
362 totalS
.x
= GetFont().GetPixelSize().y
+ 13; // margins included
365 wxSize
tsize(xlen
+ totalS
.x
, totalS
.y
);
367 // Check if the user requested a non-standard height.
369 tsize
.IncBy(0, ylen
- GetCharHeight());
376 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
378 return GetDefaultAttributesFromGTKWidget(gtk_spin_button_new_with_range(0, 100, 1), true);
381 //-----------------------------------------------------------------------------
383 //-----------------------------------------------------------------------------
389 wx_gtk_spin_input(GtkSpinButton
* spin
, gdouble
* val
, wxSpinCtrl
* win
)
391 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
392 // wxString function even if this requires an extra conversion.
394 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin
))));
397 if ( !text
.ToLong(&lval
, win
->GetBase()) )
406 wx_gtk_spin_output(GtkSpinButton
* spin
, wxSpinCtrl
* win
)
408 const gint val
= gtk_spin_button_get_value_as_int(spin
);
413 wxPrivate::wxSpinCtrlFormatAsHex(val
, win
->GetMax()).utf8_str()
421 bool wxSpinCtrl::SetBase(int base
)
423 // Currently we only support base 10 and 16. We could add support for base
424 // 8 quite easily but wxMSW doesn't support it natively so don't bother
425 // with doing something wxGTK-specific here.
426 if ( base
!= 10 && base
!= 16 )
429 if ( base
== m_base
)
434 // We need to be able to enter letters for any base greater than 10.
435 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget
), m_base
<= 10 );
439 g_signal_connect( m_widget
, "input",
440 G_CALLBACK(wx_gtk_spin_input
), this);
441 g_signal_connect( m_widget
, "output",
442 G_CALLBACK(wx_gtk_spin_output
), this);
446 g_signal_handlers_disconnect_by_func(m_widget
,
447 (gpointer
)wx_gtk_spin_input
,
449 g_signal_handlers_disconnect_by_func(m_widget
,
450 (gpointer
)wx_gtk_spin_output
,
457 //-----------------------------------------------------------------------------
459 //-----------------------------------------------------------------------------
461 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble
, wxSpinCtrlGTKBase
)
463 unsigned wxSpinCtrlDouble::GetDigits() const
465 wxCHECK_MSG( m_widget
, 0, "invalid spin button" );
467 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget
) );
470 void wxSpinCtrlDouble::SetDigits(unsigned digits
)
472 wxCHECK_RET( m_widget
, "invalid spin button" );
475 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget
), digits
);
479 #endif // wxUSE_SPINCTRL