Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / gtk / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinctrl.cpp
3 // Purpose: wxSpinCtrl
4 // Author: Robert
5 // Modified by:
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SPINCTRL
14
15 #include "wx/spinctrl.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/textctrl.h" // for wxEVT_TEXT
19 #include "wx/utils.h"
20 #include "wx/wxcrtvararg.h"
21 #endif
22
23 #include <gtk/gtk.h>
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/private/gtk2-compat.h"
26
27 //-----------------------------------------------------------------------------
28 // data
29 //-----------------------------------------------------------------------------
30
31 extern bool g_blockEventsOnDrag;
32
33 //-----------------------------------------------------------------------------
34 // "value_changed"
35 //-----------------------------------------------------------------------------
36
37 extern "C" {
38 static void
39 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
40 {
41 if (g_blockEventsOnDrag)
42 return;
43
44 if (wxIsKindOf(win, wxSpinCtrl))
45 {
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 );
51 }
52 else // wxIsKindOf(win, wxSpinCtrlDouble)
53 {
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 );
59 }
60 }
61 }
62
63 //-----------------------------------------------------------------------------
64 // "changed"
65 //-----------------------------------------------------------------------------
66
67 extern "C" {
68 static void
69 gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
70 {
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 );
76 }
77 }
78
79 //-----------------------------------------------------------------------------
80 // wxSpinCtrlGTKBase
81 //-----------------------------------------------------------------------------
82
83 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
84 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
85 END_EVENT_TABLE()
86
87 bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
88 const wxString& value,
89 const wxPoint& pos, const wxSize& size,
90 long style,
91 double min, double max, double initial, double inc,
92 const wxString& name)
93 {
94 if (!PreCreation( parent, pos, size ) ||
95 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
96 {
97 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
98 return false;
99 }
100
101 m_widget = gtk_spin_button_new_with_range(min, max, inc);
102 g_object_ref(m_widget);
103
104 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
105
106 gfloat align;
107 if ( HasFlag(wxALIGN_RIGHT) )
108 align = 1.0;
109 else if ( HasFlag(wxALIGN_CENTRE) )
110 align = 0.5;
111 else
112 align = 0.0;
113
114 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
115
116 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
117 (int)(m_windowStyle & wxSP_WRAP) );
118
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);
121
122 m_parent->DoAddChild( this );
123
124 PostCreation(size);
125
126 if (!value.empty())
127 {
128 SetValue(value);
129 }
130
131 return true;
132 }
133
134 double wxSpinCtrlGTKBase::DoGetValue() const
135 {
136 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
137
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;
144 if (sig_id == 0)
145 sig_id = g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON);
146 double value;
147 int handled = 0;
148 g_signal_emit(m_widget, sig_id, 0, &value, &handled);
149 if (!handled)
150 value = g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget)), NULL);
151 GtkAdjustment* adj =
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);
155 if (value < lower)
156 value = lower;
157 else if (value > upper)
158 value = upper;
159
160 return value;
161 }
162
163 double wxSpinCtrlGTKBase::DoGetMin() const
164 {
165 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
166
167 double min = 0;
168 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
169 return min;
170 }
171
172 double wxSpinCtrlGTKBase::DoGetMax() const
173 {
174 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
175
176 double max = 0;
177 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
178 return max;
179 }
180
181 double wxSpinCtrlGTKBase::DoGetIncrement() const
182 {
183 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
184
185 double inc = 0;
186 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL);
187 return inc;
188 }
189
190 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
191 {
192 wxCHECK_MSG(m_widget, false, "invalid spin button");
193
194 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ) != 0;
195 }
196
197 void wxSpinCtrlGTKBase::SetValue( const wxString& value )
198 {
199 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
200
201 double n;
202 if ( wxSscanf(value, "%lg", &n) == 1 )
203 {
204 // a number - set it, let DoSetValue round for int value
205 DoSetValue(n);
206 return;
207 }
208
209 // invalid number - set text as is (wxMSW compatible)
210 GtkDisableEvents();
211 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
212 GtkEnableEvents();
213 }
214
215 void wxSpinCtrlGTKBase::DoSetValue( double value )
216 {
217 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
218
219 GtkDisableEvents();
220 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
221 GtkEnableEvents();
222 }
223
224 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
225 {
226 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
227
228 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
229 }
230
231 void wxSpinCtrlGTKBase::SetSelection(long from, long to)
232 {
233 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
234 // entire range
235 if ( from == -1 && to == -1 )
236 {
237 from = 0;
238 to = INT_MAX;
239 }
240
241 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
242 }
243
244 void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
245 {
246 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
247
248 GtkDisableEvents();
249 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
250 GtkEnableEvents();
251 }
252
253 void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
254 {
255 wxCHECK_RET( m_widget, "invalid spin button" );
256
257 GtkDisableEvents();
258
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);
262
263 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, page);
264 GtkEnableEvents();
265 }
266
267 void wxSpinCtrlGTKBase::GtkDisableEvents() const
268 {
269 g_signal_handlers_block_by_func( m_widget,
270 (gpointer)gtk_value_changed, (void*) this);
271
272 g_signal_handlers_block_by_func(m_widget,
273 (gpointer)gtk_changed, (void*) this);
274 }
275
276 void wxSpinCtrlGTKBase::GtkEnableEvents() const
277 {
278 g_signal_handlers_unblock_by_func(m_widget,
279 (gpointer)gtk_value_changed, (void*) this);
280
281 g_signal_handlers_unblock_by_func(m_widget,
282 (gpointer)gtk_changed, (void*) this);
283 }
284
285 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
286 {
287 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
288
289 if (event.GetKeyCode() == WXK_RETURN)
290 {
291 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
292
293 if ( GTK_IS_WINDOW(top_frame->m_widget) )
294 {
295 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
296 if ( window )
297 {
298 GtkWidget* widgetDef = gtk_window_get_default_widget(window);
299
300 if ( widgetDef )
301 {
302 gtk_widget_activate(widgetDef);
303 return;
304 }
305 }
306 }
307 }
308
309 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
310 {
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;
317 }
318
319 event.Skip();
320 }
321
322 GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
323 {
324 #ifdef __WXGTK3__
325 // no access to internal GdkWindows
326 wxUnusedVar(windows);
327 #else
328 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
329
330 windows.push_back(spinbutton->entry.text_area);
331 windows.push_back(spinbutton->panel);
332 #endif
333
334 return NULL;
335 }
336
337 wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
338 {
339 return DoGetSizeFromTextSize(95); // TODO: 95 is completely arbitrary
340 }
341
342 wxSize wxSpinCtrlGTKBase::DoGetSizeFromTextSize(int xlen, int ylen) const
343 {
344 wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
345
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);
350
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.
353
354 // Returned height is OK
355 wxSize totalS = GTKGetPreferredSize(m_widget);
356
357 #if GTK_CHECK_VERSION(3,4,0)
358 // two buttons in horizontal
359 totalS.x = 46 + 15; // margins included
360 #else
361 // two small buttons in vertical
362 totalS.x = GetFont().GetPixelSize().y + 13; // margins included
363 #endif
364
365 wxSize tsize(xlen + totalS.x, totalS.y);
366
367 // Check if the user requested a non-standard height.
368 if ( ylen > 0 )
369 tsize.IncBy(0, ylen - GetCharHeight());
370
371 return tsize;
372 }
373
374 // static
375 wxVisualAttributes
376 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
377 {
378 return GetDefaultAttributesFromGTKWidget(gtk_spin_button_new_with_range(0, 100, 1), true);
379 }
380
381 //-----------------------------------------------------------------------------
382 // wxSpinCtrl
383 //-----------------------------------------------------------------------------
384
385 extern "C"
386 {
387
388 static gboolean
389 wx_gtk_spin_input(GtkSpinButton* spin, gdouble* val, wxSpinCtrl* win)
390 {
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.
393 const wxString
394 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin))));
395
396 long lval;
397 if ( !text.ToLong(&lval, win->GetBase()) )
398 return FALSE;
399
400 *val = lval;
401
402 return TRUE;
403 }
404
405 static gint
406 wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrl* win)
407 {
408 const gint val = gtk_spin_button_get_value_as_int(spin);
409
410 gtk_entry_set_text
411 (
412 GTK_ENTRY(spin),
413 wxPrivate::wxSpinCtrlFormatAsHex(val, win->GetMax()).utf8_str()
414 );
415
416 return TRUE;
417 }
418
419 } // extern "C"
420
421 bool wxSpinCtrl::SetBase(int base)
422 {
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 )
427 return false;
428
429 if ( base == m_base )
430 return true;
431
432 m_base = base;
433
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 );
436
437 if ( m_base != 10 )
438 {
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);
443 }
444 else
445 {
446 g_signal_handlers_disconnect_by_func(m_widget,
447 (gpointer)wx_gtk_spin_input,
448 this);
449 g_signal_handlers_disconnect_by_func(m_widget,
450 (gpointer)wx_gtk_spin_output,
451 this);
452 }
453
454 return true;
455 }
456
457 //-----------------------------------------------------------------------------
458 // wxSpinCtrlDouble
459 //-----------------------------------------------------------------------------
460
461 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
462
463 unsigned wxSpinCtrlDouble::GetDigits() const
464 {
465 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
466
467 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
468 }
469
470 void wxSpinCtrlDouble::SetDigits(unsigned digits)
471 {
472 wxCHECK_RET( m_widget, "invalid spin button" );
473
474 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
475 }
476
477 #endif // wxUSE_SPINCTRL