Provide shorter synonyms for wxEVT_XXX constants.
[wxWidgets.git] / src / gtk / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinctrl.cpp
3 // Purpose: wxSpinCtrl
4 // Author: Robert
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_SPINCTRL
15
16 #include "wx/spinctrl.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/textctrl.h" // for wxEVT_TEXT
20 #include "wx/utils.h"
21 #include "wx/wxcrtvararg.h"
22 #endif
23
24 #include <gtk/gtk.h>
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/private/gtk2-compat.h"
27
28 //-----------------------------------------------------------------------------
29 // data
30 //-----------------------------------------------------------------------------
31
32 extern bool g_blockEventsOnDrag;
33
34 //-----------------------------------------------------------------------------
35 // "value_changed"
36 //-----------------------------------------------------------------------------
37
38 extern "C" {
39 static void
40 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
41 {
42 if (g_blockEventsOnDrag)
43 return;
44
45 if (wxIsKindOf(win, wxSpinCtrl))
46 {
47 wxSpinEvent event(wxEVT_SPINCTRL, 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 );
52 }
53 else // wxIsKindOf(win, wxSpinCtrlDouble)
54 {
55 wxSpinDoubleEvent event( wxEVT_SPINCTRLDOUBLE, 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 );
60 }
61 }
62 }
63
64 //-----------------------------------------------------------------------------
65 // "changed"
66 //-----------------------------------------------------------------------------
67
68 extern "C" {
69 static void
70 gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
71 {
72 wxCommandEvent event( wxEVT_TEXT, win->GetId() );
73 event.SetEventObject( win );
74 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
75 event.SetInt(win->GetValue());
76 win->HandleWindowEvent( event );
77 }
78 }
79
80 //-----------------------------------------------------------------------------
81 // wxSpinCtrlGTKBase
82 //-----------------------------------------------------------------------------
83
84 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
85 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
86 END_EVENT_TABLE()
87
88 bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
89 const wxString& value,
90 const wxPoint& pos, const wxSize& size,
91 long style,
92 double min, double max, double initial, double inc,
93 const wxString& name)
94 {
95 if (!PreCreation( parent, pos, size ) ||
96 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
97 {
98 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
99 return false;
100 }
101
102 m_widget = gtk_spin_button_new_with_range(min, max, inc);
103 g_object_ref(m_widget);
104
105 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
106
107 gfloat align;
108 if ( HasFlag(wxALIGN_RIGHT) )
109 align = 1.0;
110 else if ( HasFlag(wxALIGN_CENTRE) )
111 align = 0.5;
112 else
113 align = 0.0;
114
115 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
116
117 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
118 (int)(m_windowStyle & wxSP_WRAP) );
119
120 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
121 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
122
123 m_parent->DoAddChild( this );
124
125 PostCreation(size);
126
127 if (!value.empty())
128 {
129 SetValue(value);
130 }
131
132 return true;
133 }
134
135 double wxSpinCtrlGTKBase::DoGetValue() const
136 {
137 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
138
139 // Get value directly from current control text, just as
140 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
141 // a redraw, which causes an idle event, so if GetValue() is called from
142 // a UI update handler, you get a never ending sequence of idle events. It
143 // also forces the text into valid range, which wxMSW GetValue() does not do.
144 static unsigned sig_id;
145 if (sig_id == 0)
146 sig_id = g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON);
147 double value;
148 int handled = 0;
149 g_signal_emit(m_widget, sig_id, 0, &value, &handled);
150 if (!handled)
151 value = g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget)), NULL);
152 GtkAdjustment* adj =
153 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget));
154 const double lower = gtk_adjustment_get_lower(adj);
155 const double upper = gtk_adjustment_get_upper(adj);
156 if (value < lower)
157 value = lower;
158 else if (value > upper)
159 value = upper;
160
161 return value;
162 }
163
164 double wxSpinCtrlGTKBase::DoGetMin() const
165 {
166 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
167
168 double min = 0;
169 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
170 return min;
171 }
172
173 double wxSpinCtrlGTKBase::DoGetMax() const
174 {
175 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
176
177 double max = 0;
178 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
179 return max;
180 }
181
182 double wxSpinCtrlGTKBase::DoGetIncrement() const
183 {
184 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
185
186 double inc = 0;
187 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL);
188 return inc;
189 }
190
191 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
192 {
193 wxCHECK_MSG(m_widget, false, "invalid spin button");
194
195 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ) != 0;
196 }
197
198 void wxSpinCtrlGTKBase::SetValue( const wxString& value )
199 {
200 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
201
202 double n;
203 if ( wxSscanf(value, "%lg", &n) == 1 )
204 {
205 // a number - set it, let DoSetValue round for int value
206 DoSetValue(n);
207 return;
208 }
209
210 // invalid number - set text as is (wxMSW compatible)
211 GtkDisableEvents();
212 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
213 GtkEnableEvents();
214 }
215
216 void wxSpinCtrlGTKBase::DoSetValue( double value )
217 {
218 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
219
220 GtkDisableEvents();
221 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
222 GtkEnableEvents();
223 }
224
225 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
226 {
227 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
228
229 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
230 }
231
232 void wxSpinCtrlGTKBase::SetSelection(long from, long to)
233 {
234 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
235 // entire range
236 if ( from == -1 && to == -1 )
237 {
238 from = 0;
239 to = INT_MAX;
240 }
241
242 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
243 }
244
245 void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
246 {
247 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
248
249 GtkDisableEvents();
250 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
251 GtkEnableEvents();
252 }
253
254 void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
255 {
256 wxCHECK_RET( m_widget, "invalid spin button" );
257
258 GtkDisableEvents();
259
260 // Preserve the old page value when changing just the increment.
261 double page = 10*inc;
262 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &page);
263
264 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, page);
265 GtkEnableEvents();
266 }
267
268 void wxSpinCtrlGTKBase::GtkDisableEvents() const
269 {
270 g_signal_handlers_block_by_func( m_widget,
271 (gpointer)gtk_value_changed, (void*) this);
272
273 g_signal_handlers_block_by_func(m_widget,
274 (gpointer)gtk_changed, (void*) this);
275 }
276
277 void wxSpinCtrlGTKBase::GtkEnableEvents() const
278 {
279 g_signal_handlers_unblock_by_func(m_widget,
280 (gpointer)gtk_value_changed, (void*) this);
281
282 g_signal_handlers_unblock_by_func(m_widget,
283 (gpointer)gtk_changed, (void*) this);
284 }
285
286 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
287 {
288 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
289
290 if (event.GetKeyCode() == WXK_RETURN)
291 {
292 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
293
294 if ( GTK_IS_WINDOW(top_frame->m_widget) )
295 {
296 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
297 if ( window )
298 {
299 GtkWidget* widgetDef = gtk_window_get_default_widget(window);
300
301 if ( widgetDef )
302 {
303 gtk_widget_activate(widgetDef);
304 return;
305 }
306 }
307 }
308 }
309
310 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
311 {
312 wxCommandEvent evt( wxEVT_TEXT_ENTER, m_windowId );
313 evt.SetEventObject(this);
314 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
315 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
316 evt.SetString( val );
317 if (HandleWindowEvent(evt)) return;
318 }
319
320 event.Skip();
321 }
322
323 GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
324 {
325 #ifdef __WXGTK3__
326 // no access to internal GdkWindows
327 wxUnusedVar(windows);
328 #else
329 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
330
331 windows.push_back(spinbutton->entry.text_area);
332 windows.push_back(spinbutton->panel);
333 #endif
334
335 return NULL;
336 }
337
338 wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
339 {
340 return DoGetSizeFromTextSize(95); // TODO: 95 is completely arbitrary
341 }
342
343 wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const
344 {
345 wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
346
347 // Set an as small as possible size for the control, so preferred sizes
348 // return "natural" sizes, not taking into account the previous ones (which
349 // seems to be GTK+3 behaviour)
350 gtk_widget_set_size_request(m_widget, 0, 0);
351
352 // Both Gtk+2 and Gtk+3 use current value/range to measure control's width.
353 // So, we can't ask Gtk+ for its width. Instead, we used hardcoded values.
354
355 // Returned height is OK
356 wxSize totalS = GTKGetPreferredSize(m_widget);
357
358 #if GTK_CHECK_VERSION(3,4,0)
359 // two buttons in horizontal
360 totalS.x = 46 + 15; // margins included
361 #else
362 // two small buttons in vertical
363 totalS.x = GetFont().GetPixelSize().y + 13; // margins included
364 #endif
365
366 wxSize tsize(xlen + totalS.x, totalS.y);
367
368 // Check if the user requested a non-standard height.
369 if ( ylen > 0 )
370 tsize.IncBy(0, ylen - GetCharHeight());
371
372 return tsize;
373 }
374
375 // static
376 wxVisualAttributes
377 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
378 {
379 return GetDefaultAttributesFromGTKWidget(gtk_spin_button_new_with_range(0, 100, 1), true);
380 }
381
382 //-----------------------------------------------------------------------------
383 // wxSpinCtrl
384 //-----------------------------------------------------------------------------
385
386 extern "C"
387 {
388
389 static gboolean
390 wx_gtk_spin_input(GtkSpinButton* spin, gdouble* val, wxSpinCtrl* win)
391 {
392 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
393 // wxString function even if this requires an extra conversion.
394 const wxString
395 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin))));
396
397 long lval;
398 if ( !text.ToLong(&lval, win->GetBase()) )
399 return FALSE;
400
401 *val = lval;
402
403 return TRUE;
404 }
405
406 static gint
407 wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrl* win)
408 {
409 const gint val = gtk_spin_button_get_value_as_int(spin);
410
411 gtk_entry_set_text
412 (
413 GTK_ENTRY(spin),
414 wxPrivate::wxSpinCtrlFormatAsHex(val, win->GetMax()).utf8_str()
415 );
416
417 return TRUE;
418 }
419
420 } // extern "C"
421
422 bool wxSpinCtrl::SetBase(int base)
423 {
424 // Currently we only support base 10 and 16. We could add support for base
425 // 8 quite easily but wxMSW doesn't support it natively so don't bother
426 // with doing something wxGTK-specific here.
427 if ( base != 10 && base != 16 )
428 return false;
429
430 if ( base == m_base )
431 return true;
432
433 m_base = base;
434
435 // We need to be able to enter letters for any base greater than 10.
436 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget), m_base <= 10 );
437
438 if ( m_base != 10 )
439 {
440 g_signal_connect( m_widget, "input",
441 G_CALLBACK(wx_gtk_spin_input), this);
442 g_signal_connect( m_widget, "output",
443 G_CALLBACK(wx_gtk_spin_output), this);
444 }
445 else
446 {
447 g_signal_handlers_disconnect_by_func(m_widget,
448 (gpointer)wx_gtk_spin_input,
449 this);
450 g_signal_handlers_disconnect_by_func(m_widget,
451 (gpointer)wx_gtk_spin_output,
452 this);
453 }
454
455 return true;
456 }
457
458 //-----------------------------------------------------------------------------
459 // wxSpinCtrlDouble
460 //-----------------------------------------------------------------------------
461
462 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
463
464 unsigned wxSpinCtrlDouble::GetDigits() const
465 {
466 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
467
468 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
469 }
470
471 void wxSpinCtrlDouble::SetDigits(unsigned digits)
472 {
473 wxCHECK_RET( m_widget, "invalid spin button" );
474
475 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
476 }
477
478 #endif // wxUSE_SPINCTRL