]>
Commit | Line | Data |
---|---|---|
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 | ||
32 | extern bool g_blockEventsOnDrag; | |
33 | ||
738f9e5a RR |
34 | //----------------------------------------------------------------------------- |
35 | // "value_changed" | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
865bb325 | 38 | extern "C" { |
7f81dfa1 | 39 | static void |
8cd6a9ad | 40 | gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win) |
738f9e5a | 41 | { |
8ab75332 | 42 | if (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 | 68 | extern "C" { |
0a07a7d8 | 69 | static void |
7f81dfa1 | 70 | gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win) |
0a07a7d8 | 71 | { |
0a07a7d8 RR |
72 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); |
73 | event.SetEventObject( win ); | |
845a6bbf PC |
74 | event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton))); |
75 | event.SetInt(win->GetValue()); | |
937013e0 | 76 | win->HandleWindowEvent( event ); |
0a07a7d8 | 77 | } |
865bb325 | 78 | } |
0a07a7d8 | 79 | |
738f9e5a | 80 | //----------------------------------------------------------------------------- |
8cd6a9ad | 81 | // wxSpinCtrlGTKBase |
738f9e5a RR |
82 | //----------------------------------------------------------------------------- |
83 | ||
8cd6a9ad VZ |
84 | BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase) |
85 | EVT_CHAR(wxSpinCtrlGTKBase::OnChar) | |
da048e3d RR |
86 | END_EVENT_TABLE() |
87 | ||
8cd6a9ad | 88 | bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
89 | const wxString& value, |
90 | const wxPoint& pos, const wxSize& size, | |
91 | long style, | |
8cd6a9ad | 92 | double min, double max, double initial, double inc, |
ce89fdd2 | 93 | const wxString& name) |
738f9e5a | 94 | { |
0279e844 RR |
95 | if (!PreCreation( parent, pos, size ) || |
96 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
738f9e5a | 97 | { |
8cd6a9ad | 98 | wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") ); |
8e13c1ec | 99 | return false; |
738f9e5a RR |
100 | } |
101 | ||
8cd6a9ad | 102 | m_widget = gtk_spin_button_new_with_range(min, max, inc); |
9ff9d30c | 103 | g_object_ref(m_widget); |
8cd6a9ad | 104 | |
4a783bb4 | 105 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial); |
3d257b8d | 106 | |
7e4952db | 107 | gfloat align; |
f1ddb476 | 108 | if ( HasFlag(wxALIGN_RIGHT) ) |
7e4952db | 109 | align = 1.0; |
f1ddb476 | 110 | else if ( HasFlag(wxALIGN_CENTRE) ) |
7e4952db VZ |
111 | align = 0.5; |
112 | else | |
113 | align = 0.0; | |
114 | ||
115 | gtk_entry_set_alignment(GTK_ENTRY(m_widget), align); | |
116 | ||
b02da6b1 VZ |
117 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
118 | (int)(m_windowStyle & wxSP_WRAP) ); | |
738f9e5a | 119 | |
0bf36922 RR |
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); | |
3d257b8d | 122 | |
738f9e5a RR |
123 | m_parent->DoAddChild( this ); |
124 | ||
abdeb9e7 | 125 | PostCreation(size); |
db434467 | 126 | |
7f81dfa1 PC |
127 | if (!value.empty()) |
128 | { | |
129 | SetValue(value); | |
130 | } | |
ce89fdd2 | 131 | |
8e13c1ec | 132 | return true; |
738f9e5a RR |
133 | } |
134 | ||
8cd6a9ad | 135 | double wxSpinCtrlGTKBase::DoGetValue() const |
738f9e5a RR |
136 | { |
137 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
138 | ||
845a6bbf PC |
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); | |
385e8575 | 152 | GtkAdjustment* adj = |
845a6bbf | 153 | gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget)); |
385e8575 PC |
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; | |
845a6bbf PC |
160 | |
161 | return value; | |
8cd6a9ad VZ |
162 | } |
163 | ||
164 | double wxSpinCtrlGTKBase::DoGetMin() const | |
165 | { | |
166 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
167 | ||
168 | double min = 0; | |
4a783bb4 | 169 | gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL); |
8cd6a9ad | 170 | return min; |
738f9e5a RR |
171 | } |
172 | ||
8cd6a9ad | 173 | double wxSpinCtrlGTKBase::DoGetMax() const |
738f9e5a RR |
174 | { |
175 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
176 | ||
8cd6a9ad | 177 | double max = 0; |
4a783bb4 | 178 | gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max); |
8cd6a9ad | 179 | return max; |
738f9e5a RR |
180 | } |
181 | ||
8cd6a9ad | 182 | double wxSpinCtrlGTKBase::DoGetIncrement() const |
738f9e5a RR |
183 | { |
184 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
185 | ||
8cd6a9ad | 186 | double inc = 0; |
63fb6907 | 187 | gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL); |
8cd6a9ad VZ |
188 | return inc; |
189 | } | |
190 | ||
191 | bool wxSpinCtrlGTKBase::GetSnapToTicks() const | |
192 | { | |
845a6bbf | 193 | wxCHECK_MSG(m_widget, false, "invalid spin button"); |
33720b2d | 194 | |
d5027818 | 195 | return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ) != 0; |
738f9e5a RR |
196 | } |
197 | ||
8cd6a9ad | 198 | void wxSpinCtrlGTKBase::SetValue( const wxString& value ) |
ce89fdd2 VZ |
199 | { |
200 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
201 | ||
8cd6a9ad VZ |
202 | double n; |
203 | if ( wxSscanf(value, "%lg", &n) == 1 ) | |
ce89fdd2 | 204 | { |
8cd6a9ad VZ |
205 | // a number - set it, let DoSetValue round for int value |
206 | DoSetValue(n); | |
207 | return; | |
ce89fdd2 | 208 | } |
8cd6a9ad VZ |
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(); | |
ce89fdd2 VZ |
214 | } |
215 | ||
8cd6a9ad | 216 | void wxSpinCtrlGTKBase::DoSetValue( double value ) |
738f9e5a RR |
217 | { |
218 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
219 | ||
0bf36922 | 220 | GtkDisableEvents(); |
4a783bb4 | 221 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value); |
0bf36922 | 222 | GtkEnableEvents(); |
738f9e5a RR |
223 | } |
224 | ||
8cd6a9ad VZ |
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) | |
f8f9ec55 | 233 | { |
77ffb593 | 234 | // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the |
f8f9ec55 VZ |
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 | ||
8cd6a9ad | 245 | void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal) |
738f9e5a RR |
246 | { |
247 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
248 | ||
0bf36922 | 249 | GtkDisableEvents(); |
4a783bb4 | 250 | gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal); |
0bf36922 RR |
251 | GtkEnableEvents(); |
252 | } | |
253 | ||
8cd6a9ad VZ |
254 | void wxSpinCtrlGTKBase::DoSetIncrement(double inc) |
255 | { | |
256 | wxCHECK_RET( m_widget, "invalid spin button" ); | |
257 | ||
258 | GtkDisableEvents(); | |
c85a7996 VZ |
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); | |
8cd6a9ad VZ |
265 | GtkEnableEvents(); |
266 | } | |
0bf36922 | 267 | |
8cd6a9ad | 268 | void wxSpinCtrlGTKBase::GtkDisableEvents() const |
0bf36922 RR |
269 | { |
270 | g_signal_handlers_block_by_func( m_widget, | |
271 | (gpointer)gtk_value_changed, (void*) this); | |
00dc9772 | 272 | |
0bf36922 RR |
273 | g_signal_handlers_block_by_func(m_widget, |
274 | (gpointer)gtk_changed, (void*) this); | |
275 | } | |
276 | ||
8cd6a9ad | 277 | void wxSpinCtrlGTKBase::GtkEnableEvents() const |
0bf36922 RR |
278 | { |
279 | g_signal_handlers_unblock_by_func(m_widget, | |
280 | (gpointer)gtk_value_changed, (void*) this); | |
00dc9772 | 281 | |
0bf36922 RR |
282 | g_signal_handlers_unblock_by_func(m_widget, |
283 | (gpointer)gtk_changed, (void*) this); | |
738f9e5a RR |
284 | } |
285 | ||
8cd6a9ad | 286 | void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event ) |
da048e3d RR |
287 | { |
288 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
289 | ||
12a3f227 | 290 | if (event.GetKeyCode() == WXK_RETURN) |
da048e3d | 291 | { |
7f81dfa1 | 292 | wxWindow *top_frame = wxGetTopLevelParent(m_parent); |
0e0d8857 | 293 | |
fa8a793a | 294 | if ( GTK_IS_WINDOW(top_frame->m_widget) ) |
da048e3d | 295 | { |
fa8a793a VZ |
296 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
297 | if ( window ) | |
298 | { | |
385e8575 | 299 | GtkWidget* widgetDef = gtk_window_get_default_widget(window); |
fa8a793a | 300 | |
055e633d | 301 | if ( widgetDef ) |
fa8a793a VZ |
302 | { |
303 | gtk_widget_activate(widgetDef); | |
304 | return; | |
305 | } | |
306 | } | |
9750fc42 | 307 | } |
da048e3d RR |
308 | } |
309 | ||
8e13c1ec | 310 | if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER)) |
4a11cca2 RR |
311 | { |
312 | wxCommandEvent evt( wxEVT_COMMAND_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 ); | |
937013e0 | 317 | if (HandleWindowEvent(evt)) return; |
4a11cca2 RR |
318 | } |
319 | ||
da048e3d RR |
320 | event.Skip(); |
321 | } | |
322 | ||
8cd6a9ad | 323 | GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const |
738f9e5a | 324 | { |
9dc44eff PC |
325 | #ifdef __WXGTK3__ |
326 | // no access to internal GdkWindows | |
327 | wxUnusedVar(windows); | |
328 | #else | |
7f81dfa1 | 329 | GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget); |
ef5c70f9 VZ |
330 | |
331 | windows.push_back(spinbutton->entry.text_area); | |
332 | windows.push_back(spinbutton->panel); | |
9dc44eff | 333 | #endif |
ef5c70f9 VZ |
334 | |
335 | return NULL; | |
738f9e5a RR |
336 | } |
337 | ||
8cd6a9ad | 338 | wxSize wxSpinCtrlGTKBase::DoGetBestSize() const |
9d9b7755 | 339 | { |
0279e844 | 340 | wxSize ret( wxControl::DoGetBestSize() ); |
ef5c70f9 | 341 | wxSize best(95, ret.y); // FIXME: 95? |
9f884528 RD |
342 | CacheBestSize(best); |
343 | return best; | |
9d9b7755 VZ |
344 | } |
345 | ||
9d522606 RD |
346 | // static |
347 | wxVisualAttributes | |
8cd6a9ad | 348 | wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
9d522606 RD |
349 | { |
350 | // TODO: overload to accept functions like gtk_spin_button_new? | |
351 | // Until then use a similar type | |
352 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
353 | } | |
354 | ||
8cd6a9ad VZ |
355 | //----------------------------------------------------------------------------- |
356 | // wxSpinCtrl | |
357 | //----------------------------------------------------------------------------- | |
358 | ||
9e565667 VZ |
359 | extern "C" |
360 | { | |
361 | ||
362 | static gboolean | |
363 | wx_gtk_spin_input(GtkSpinButton* spin, gdouble* val, wxSpinCtrl* win) | |
364 | { | |
365 | // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own | |
366 | // wxString function even if this requires an extra conversion. | |
367 | const wxString | |
368 | text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin)))); | |
369 | ||
370 | long lval; | |
371 | if ( !text.ToLong(&lval, win->GetBase()) ) | |
372 | return FALSE; | |
373 | ||
374 | *val = lval; | |
375 | ||
376 | return TRUE; | |
377 | } | |
378 | ||
379 | static gint | |
380 | wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrl* win) | |
381 | { | |
382 | const gint val = gtk_spin_button_get_value_as_int(spin); | |
383 | ||
384 | gtk_entry_set_text | |
385 | ( | |
386 | GTK_ENTRY(spin), | |
387 | wxPrivate::wxSpinCtrlFormatAsHex(val, win->GetMax()).utf8_str() | |
388 | ); | |
389 | ||
390 | return TRUE; | |
391 | } | |
392 | ||
393 | } // extern "C" | |
394 | ||
395 | bool wxSpinCtrl::SetBase(int base) | |
396 | { | |
397 | // Currently we only support base 10 and 16. We could add support for base | |
398 | // 8 quite easily but wxMSW doesn't support it natively so don't bother | |
399 | // with doing something wxGTK-specific here. | |
400 | if ( base != 10 && base != 16 ) | |
401 | return false; | |
402 | ||
403 | if ( base == m_base ) | |
404 | return true; | |
405 | ||
406 | m_base = base; | |
407 | ||
408 | // We need to be able to enter letters for any base greater than 10. | |
409 | gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget), m_base <= 10 ); | |
410 | ||
411 | if ( m_base != 10 ) | |
412 | { | |
84f623f8 | 413 | g_signal_connect( m_widget, "input", |
9e565667 | 414 | G_CALLBACK(wx_gtk_spin_input), this); |
84f623f8 | 415 | g_signal_connect( m_widget, "output", |
9e565667 VZ |
416 | G_CALLBACK(wx_gtk_spin_output), this); |
417 | } | |
418 | else | |
419 | { | |
84f623f8 | 420 | g_signal_handlers_disconnect_by_func(m_widget, |
9e565667 VZ |
421 | (gpointer)wx_gtk_spin_input, |
422 | this); | |
84f623f8 | 423 | g_signal_handlers_disconnect_by_func(m_widget, |
9e565667 VZ |
424 | (gpointer)wx_gtk_spin_output, |
425 | this); | |
426 | } | |
427 | ||
428 | return true; | |
429 | } | |
430 | ||
8cd6a9ad VZ |
431 | //----------------------------------------------------------------------------- |
432 | // wxSpinCtrlDouble | |
433 | //----------------------------------------------------------------------------- | |
434 | ||
435 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase) | |
436 | ||
437 | unsigned wxSpinCtrlDouble::GetDigits() const | |
438 | { | |
439 | wxCHECK_MSG( m_widget, 0, "invalid spin button" ); | |
440 | ||
441 | return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) ); | |
442 | } | |
443 | ||
444 | void wxSpinCtrlDouble::SetDigits(unsigned digits) | |
445 | { | |
446 | wxCHECK_RET( m_widget, "invalid spin button" ); | |
447 | ||
448 | gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits); | |
449 | } | |
450 | ||
451 | #endif // wxUSE_SPINCTRL |