]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
Make wxWindow::HasScrollbar() respect wxScrolled::ShowScrollbars().
[wxWidgets.git] / src / gtk / spinctrl.cpp
CommitLineData
738f9e5a 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/spinctrl.cpp
738f9e5a
RR
3// Purpose: wxSpinCtrl
4// Author: Robert
5// Modified by:
738f9e5a 6// Copyright: (c) Robert Roebling
65571936 7// Licence: wxWindows licence
738f9e5a
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
0e0d8857 13#if wxUSE_SPINCTRL
738f9e5a 14
de6185e2
WS
15#include "wx/spinctrl.h"
16
17#ifndef WX_PRECOMP
ce7fe42e 18 #include "wx/textctrl.h" // for wxEVT_TEXT
de6185e2 19 #include "wx/utils.h"
0cb7e05c 20 #include "wx/wxcrtvararg.h"
de6185e2 21#endif
aec0ed2e 22
9dc44eff 23#include <gtk/gtk.h>
e1910715 24#include "wx/gtk/private.h"
9dc44eff 25#include "wx/gtk/private/gtk2-compat.h"
738f9e5a 26
738f9e5a
RR
27//-----------------------------------------------------------------------------
28// data
29//-----------------------------------------------------------------------------
30
31extern bool g_blockEventsOnDrag;
32
738f9e5a
RR
33//-----------------------------------------------------------------------------
34// "value_changed"
35//-----------------------------------------------------------------------------
36
865bb325 37extern "C" {
7f81dfa1 38static void
8cd6a9ad 39gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
738f9e5a 40{
8ab75332 41 if (g_blockEventsOnDrag)
7f81dfa1 42 return;
738f9e5a 43
8cd6a9ad
VZ
44 if (wxIsKindOf(win, wxSpinCtrl))
45 {
ce7fe42e 46 wxSpinEvent event(wxEVT_SPINCTRL, win->GetId());
8cd6a9ad 47 event.SetEventObject( win );
845a6bbf
PC
48 event.SetPosition(static_cast<wxSpinCtrl*>(win)->GetValue());
49 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
8cd6a9ad
VZ
50 win->HandleWindowEvent( event );
51 }
52 else // wxIsKindOf(win, wxSpinCtrlDouble)
53 {
ce7fe42e 54 wxSpinDoubleEvent event( wxEVT_SPINCTRLDOUBLE, win->GetId());
8cd6a9ad 55 event.SetEventObject( win );
845a6bbf
PC
56 event.SetValue(static_cast<wxSpinCtrlDouble*>(win)->GetValue());
57 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
8cd6a9ad
VZ
58 win->HandleWindowEvent( event );
59 }
738f9e5a 60}
865bb325 61}
738f9e5a 62
0a07a7d8
RR
63//-----------------------------------------------------------------------------
64// "changed"
65//-----------------------------------------------------------------------------
66
865bb325 67extern "C" {
0a07a7d8 68static void
7f81dfa1 69gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
0a07a7d8 70{
ce7fe42e 71 wxCommandEvent event( wxEVT_TEXT, win->GetId() );
0a07a7d8 72 event.SetEventObject( win );
845a6bbf
PC
73 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
74 event.SetInt(win->GetValue());
937013e0 75 win->HandleWindowEvent( event );
0a07a7d8 76}
865bb325 77}
0a07a7d8 78
dcbf35f8
VZ
79// ----------------------------------------------------------------------------
80// wxSpinCtrlEventDisabler: helper to temporarily disable GTK+ events
81// ----------------------------------------------------------------------------
82
83class wxSpinCtrlEventDisabler
84{
85public:
86 wxEXPLICIT wxSpinCtrlEventDisabler(wxSpinCtrlGTKBase* spin)
87 : m_spin(spin)
88 {
89 m_spin->GtkDisableEvents();
90 }
91
92 ~wxSpinCtrlEventDisabler()
93 {
94 m_spin->GtkEnableEvents();
95 }
96
97private:
98 wxSpinCtrlGTKBase* const m_spin;
99
100 wxDECLARE_NO_COPY_CLASS(wxSpinCtrlEventDisabler);
101};
102
738f9e5a 103//-----------------------------------------------------------------------------
8cd6a9ad 104// wxSpinCtrlGTKBase
738f9e5a
RR
105//-----------------------------------------------------------------------------
106
8cd6a9ad
VZ
107BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
108 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
da048e3d
RR
109END_EVENT_TABLE()
110
8cd6a9ad 111bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
112 const wxString& value,
113 const wxPoint& pos, const wxSize& size,
114 long style,
8cd6a9ad 115 double min, double max, double initial, double inc,
ce89fdd2 116 const wxString& name)
738f9e5a 117{
0279e844
RR
118 if (!PreCreation( parent, pos, size ) ||
119 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a 120 {
8cd6a9ad 121 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
8e13c1ec 122 return false;
738f9e5a
RR
123 }
124
8cd6a9ad 125 m_widget = gtk_spin_button_new_with_range(min, max, inc);
9ff9d30c 126 g_object_ref(m_widget);
8cd6a9ad 127
4a783bb4 128 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
3d257b8d 129
7e4952db 130 gfloat align;
f1ddb476 131 if ( HasFlag(wxALIGN_RIGHT) )
7e4952db 132 align = 1.0;
f1ddb476 133 else if ( HasFlag(wxALIGN_CENTRE) )
7e4952db
VZ
134 align = 0.5;
135 else
136 align = 0.0;
137
138 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
139
b02da6b1
VZ
140 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
141 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 142
0bf36922
RR
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);
3d257b8d 145
738f9e5a
RR
146 m_parent->DoAddChild( this );
147
abdeb9e7 148 PostCreation(size);
db434467 149
7f81dfa1
PC
150 if (!value.empty())
151 {
152 SetValue(value);
153 }
ce89fdd2 154
8e13c1ec 155 return true;
738f9e5a
RR
156}
157
8cd6a9ad 158double wxSpinCtrlGTKBase::DoGetValue() const
738f9e5a
RR
159{
160 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
161
845a6bbf
PC
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;
168 if (sig_id == 0)
169 sig_id = g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON);
170 double value;
171 int handled = 0;
172 g_signal_emit(m_widget, sig_id, 0, &value, &handled);
173 if (!handled)
174 value = g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget)), NULL);
385e8575 175 GtkAdjustment* adj =
845a6bbf 176 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget));
385e8575
PC
177 const double lower = gtk_adjustment_get_lower(adj);
178 const double upper = gtk_adjustment_get_upper(adj);
179 if (value < lower)
180 value = lower;
181 else if (value > upper)
182 value = upper;
845a6bbf
PC
183
184 return value;
8cd6a9ad
VZ
185}
186
187double wxSpinCtrlGTKBase::DoGetMin() const
188{
189 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
190
191 double min = 0;
4a783bb4 192 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
8cd6a9ad 193 return min;
738f9e5a
RR
194}
195
8cd6a9ad 196double wxSpinCtrlGTKBase::DoGetMax() const
738f9e5a
RR
197{
198 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
199
8cd6a9ad 200 double max = 0;
4a783bb4 201 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
8cd6a9ad 202 return max;
738f9e5a
RR
203}
204
8cd6a9ad 205double wxSpinCtrlGTKBase::DoGetIncrement() const
738f9e5a
RR
206{
207 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
208
8cd6a9ad 209 double inc = 0;
63fb6907 210 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL);
8cd6a9ad
VZ
211 return inc;
212}
213
214bool wxSpinCtrlGTKBase::GetSnapToTicks() const
215{
845a6bbf 216 wxCHECK_MSG(m_widget, false, "invalid spin button");
33720b2d 217
d5027818 218 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ) != 0;
738f9e5a
RR
219}
220
8cd6a9ad 221void wxSpinCtrlGTKBase::SetValue( const wxString& value )
ce89fdd2
VZ
222{
223 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
224
8cd6a9ad
VZ
225 double n;
226 if ( wxSscanf(value, "%lg", &n) == 1 )
ce89fdd2 227 {
8cd6a9ad
VZ
228 // a number - set it, let DoSetValue round for int value
229 DoSetValue(n);
230 return;
ce89fdd2 231 }
8cd6a9ad
VZ
232
233 // invalid number - set text as is (wxMSW compatible)
dcbf35f8 234 wxSpinCtrlEventDisabler disable(this);
8cd6a9ad 235 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
ce89fdd2
VZ
236}
237
8cd6a9ad 238void wxSpinCtrlGTKBase::DoSetValue( double value )
738f9e5a
RR
239{
240 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
241
dcbf35f8 242 wxSpinCtrlEventDisabler disable(this);
4a783bb4 243 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
738f9e5a
RR
244}
245
8cd6a9ad
VZ
246void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
247{
248 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
249
250 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
251}
252
253void wxSpinCtrlGTKBase::SetSelection(long from, long to)
f8f9ec55 254{
77ffb593 255 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
256 // entire range
257 if ( from == -1 && to == -1 )
258 {
259 from = 0;
260 to = INT_MAX;
261 }
262
263 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
264}
265
8cd6a9ad 266void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
738f9e5a
RR
267{
268 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
269
dcbf35f8 270 wxSpinCtrlEventDisabler disable(this);
4a783bb4 271 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
0bf36922
RR
272}
273
8cd6a9ad
VZ
274void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
275{
276 wxCHECK_RET( m_widget, "invalid spin button" );
277
dcbf35f8 278 wxSpinCtrlEventDisabler disable(this);
c85a7996
VZ
279
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);
283
284 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, page);
8cd6a9ad 285}
0bf36922 286
8cd6a9ad 287void wxSpinCtrlGTKBase::GtkDisableEvents() const
0bf36922
RR
288{
289 g_signal_handlers_block_by_func( m_widget,
290 (gpointer)gtk_value_changed, (void*) this);
00dc9772 291
0bf36922
RR
292 g_signal_handlers_block_by_func(m_widget,
293 (gpointer)gtk_changed, (void*) this);
294}
295
8cd6a9ad 296void wxSpinCtrlGTKBase::GtkEnableEvents() const
0bf36922
RR
297{
298 g_signal_handlers_unblock_by_func(m_widget,
299 (gpointer)gtk_value_changed, (void*) this);
00dc9772 300
0bf36922
RR
301 g_signal_handlers_unblock_by_func(m_widget,
302 (gpointer)gtk_changed, (void*) this);
738f9e5a
RR
303}
304
8cd6a9ad 305void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
da048e3d
RR
306{
307 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
308
12a3f227 309 if (event.GetKeyCode() == WXK_RETURN)
da048e3d 310 {
7f81dfa1 311 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
0e0d8857 312
fa8a793a 313 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 314 {
fa8a793a
VZ
315 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
316 if ( window )
317 {
385e8575 318 GtkWidget* widgetDef = gtk_window_get_default_widget(window);
fa8a793a 319
055e633d 320 if ( widgetDef )
fa8a793a
VZ
321 {
322 gtk_widget_activate(widgetDef);
323 return;
324 }
325 }
9750fc42 326 }
da048e3d
RR
327 }
328
8e13c1ec 329 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2 330 {
ce7fe42e 331 wxCommandEvent evt( wxEVT_TEXT_ENTER, m_windowId );
4a11cca2
RR
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 );
937013e0 336 if (HandleWindowEvent(evt)) return;
4a11cca2
RR
337 }
338
da048e3d
RR
339 event.Skip();
340}
341
8cd6a9ad 342GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
738f9e5a 343{
9dc44eff
PC
344#ifdef __WXGTK3__
345 // no access to internal GdkWindows
346 wxUnusedVar(windows);
347#else
7f81dfa1 348 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
ef5c70f9
VZ
349
350 windows.push_back(spinbutton->entry.text_area);
351 windows.push_back(spinbutton->panel);
9dc44eff 352#endif
ef5c70f9
VZ
353
354 return NULL;
738f9e5a
RR
355}
356
8cd6a9ad 357wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
9d9b7755 358{
40aa1a7e
VZ
359 return DoGetSizeFromTextSize(95); // TODO: 95 is completely arbitrary
360}
361
362wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const
363{
364 wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
365
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);
370
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.
373
374 // Returned height is OK
375 wxSize totalS = GTKGetPreferredSize(m_widget);
376
377#if GTK_CHECK_VERSION(3,4,0)
378 // two buttons in horizontal
379 totalS.x = 46 + 15; // margins included
380#else
381 // two small buttons in vertical
382 totalS.x = GetFont().GetPixelSize().y + 13; // margins included
383#endif
384
385 wxSize tsize(xlen + totalS.x, totalS.y);
386
387 // Check if the user requested a non-standard height.
388 if ( ylen > 0 )
389 tsize.IncBy(0, ylen - GetCharHeight());
390
391 return tsize;
9d9b7755
VZ
392}
393
9d522606
RD
394// static
395wxVisualAttributes
8cd6a9ad 396wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
9d522606 397{
7fff16b8 398 return GetDefaultAttributesFromGTKWidget(gtk_spin_button_new_with_range(0, 100, 1), true);
9d522606
RD
399}
400
8cd6a9ad
VZ
401//-----------------------------------------------------------------------------
402// wxSpinCtrl
403//-----------------------------------------------------------------------------
404
9e565667
VZ
405extern "C"
406{
407
408static gboolean
409wx_gtk_spin_input(GtkSpinButton* spin, gdouble* val, wxSpinCtrl* win)
410{
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.
413 const wxString
414 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin))));
415
416 long lval;
417 if ( !text.ToLong(&lval, win->GetBase()) )
418 return FALSE;
419
420 *val = lval;
421
422 return TRUE;
423}
424
425static gint
426wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrl* win)
427{
428 const gint val = gtk_spin_button_get_value_as_int(spin);
429
430 gtk_entry_set_text
431 (
432 GTK_ENTRY(spin),
433 wxPrivate::wxSpinCtrlFormatAsHex(val, win->GetMax()).utf8_str()
434 );
435
436 return TRUE;
437}
438
439} // extern "C"
440
441bool wxSpinCtrl::SetBase(int base)
442{
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 )
447 return false;
448
449 if ( base == m_base )
450 return true;
451
452 m_base = base;
453
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 );
456
457 if ( m_base != 10 )
458 {
84f623f8 459 g_signal_connect( m_widget, "input",
9e565667 460 G_CALLBACK(wx_gtk_spin_input), this);
84f623f8 461 g_signal_connect( m_widget, "output",
9e565667
VZ
462 G_CALLBACK(wx_gtk_spin_output), this);
463 }
464 else
465 {
84f623f8 466 g_signal_handlers_disconnect_by_func(m_widget,
9e565667
VZ
467 (gpointer)wx_gtk_spin_input,
468 this);
84f623f8 469 g_signal_handlers_disconnect_by_func(m_widget,
9e565667
VZ
470 (gpointer)wx_gtk_spin_output,
471 this);
472 }
473
474 return true;
475}
476
8cd6a9ad
VZ
477//-----------------------------------------------------------------------------
478// wxSpinCtrlDouble
479//-----------------------------------------------------------------------------
480
481IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
482
483unsigned wxSpinCtrlDouble::GetDigits() const
484{
485 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
486
487 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
488}
489
490void wxSpinCtrlDouble::SetDigits(unsigned digits)
491{
492 wxCHECK_RET( m_widget, "invalid spin button" );
493
dcbf35f8 494 wxSpinCtrlEventDisabler disable(this);
8cd6a9ad
VZ
495 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
496}
497
498#endif // wxUSE_SPINCTRL