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