]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
updated setup.h for OpenVMS
[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_COMMAND_TEXT_UPDATED
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 (!win->m_hasVMT || g_blockEventsOnDrag)
43 return;
44
45 if (wxIsKindOf(win, wxSpinCtrl))
46 {
47 wxSpinEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, 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_COMMAND_SPINCTRLDOUBLE_UPDATED, 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 if (!win->m_hasVMT)
73 return;
74
75 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
76 event.SetEventObject( win );
77 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
78 event.SetInt(win->GetValue());
79 win->HandleWindowEvent( event );
80 }
81 }
82
83 //-----------------------------------------------------------------------------
84 // wxSpinCtrlGTKBase
85 //-----------------------------------------------------------------------------
86
87 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase)
88
89 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
90 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
91 END_EVENT_TABLE()
92
93 bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
94 const wxString& value,
95 const wxPoint& pos, const wxSize& size,
96 long style,
97 double min, double max, double initial, double inc,
98 const wxString& name)
99 {
100 if (!PreCreation( parent, pos, size ) ||
101 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
102 {
103 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
104 return false;
105 }
106
107 m_widget = gtk_spin_button_new_with_range(min, max, inc);
108 g_object_ref(m_widget);
109
110 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
111
112 gfloat align;
113 if ( HasFlag(wxALIGN_RIGHT) )
114 align = 1.0;
115 else if ( HasFlag(wxALIGN_CENTRE) )
116 align = 0.5;
117 else
118 align = 0.0;
119
120 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
121
122 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
123 (int)(m_windowStyle & wxSP_WRAP) );
124
125 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
126 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
127
128 m_parent->DoAddChild( this );
129
130 PostCreation(size);
131
132 if (!value.empty())
133 {
134 SetValue(value);
135 }
136
137 return true;
138 }
139
140 double wxSpinCtrlGTKBase::DoGetValue() const
141 {
142 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
143
144 // Get value directly from current control text, just as
145 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
146 // a redraw, which causes an idle event, so if GetValue() is called from
147 // a UI update handler, you get a never ending sequence of idle events. It
148 // also forces the text into valid range, which wxMSW GetValue() does not do.
149 static unsigned sig_id;
150 if (sig_id == 0)
151 sig_id = g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON);
152 double value;
153 int handled = 0;
154 g_signal_emit(m_widget, sig_id, 0, &value, &handled);
155 if (!handled)
156 value = g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget)), NULL);
157 GtkAdjustment* adj =
158 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget));
159 const double lower = gtk_adjustment_get_lower(adj);
160 const double upper = gtk_adjustment_get_upper(adj);
161 if (value < lower)
162 value = lower;
163 else if (value > upper)
164 value = upper;
165
166 return value;
167 }
168
169 double wxSpinCtrlGTKBase::DoGetMin() const
170 {
171 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
172
173 double min = 0;
174 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
175 return min;
176 }
177
178 double wxSpinCtrlGTKBase::DoGetMax() const
179 {
180 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
181
182 double max = 0;
183 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
184 return max;
185 }
186
187 double wxSpinCtrlGTKBase::DoGetIncrement() const
188 {
189 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
190
191 double inc = 0;
192 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL);
193 return inc;
194 }
195
196 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
197 {
198 wxCHECK_MSG(m_widget, false, "invalid spin button");
199
200 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ) != 0;
201 }
202
203 void wxSpinCtrlGTKBase::SetValue( const wxString& value )
204 {
205 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
206
207 double n;
208 if ( wxSscanf(value, "%lg", &n) == 1 )
209 {
210 // a number - set it, let DoSetValue round for int value
211 DoSetValue(n);
212 return;
213 }
214
215 // invalid number - set text as is (wxMSW compatible)
216 GtkDisableEvents();
217 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
218 GtkEnableEvents();
219 }
220
221 void wxSpinCtrlGTKBase::DoSetValue( double value )
222 {
223 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
224
225 GtkDisableEvents();
226 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
227 GtkEnableEvents();
228 }
229
230 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
231 {
232 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
233
234 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
235 }
236
237 void wxSpinCtrlGTKBase::SetSelection(long from, long to)
238 {
239 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
240 // entire range
241 if ( from == -1 && to == -1 )
242 {
243 from = 0;
244 to = INT_MAX;
245 }
246
247 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
248 }
249
250 void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
251 {
252 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
253
254 GtkDisableEvents();
255 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
256 GtkEnableEvents();
257 }
258
259 void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
260 {
261 wxCHECK_RET( m_widget, "invalid spin button" );
262
263 GtkDisableEvents();
264
265 // Preserve the old page value when changing just the increment.
266 double page = 10*inc;
267 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &page);
268
269 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, page);
270 GtkEnableEvents();
271 }
272
273 void wxSpinCtrlGTKBase::GtkDisableEvents() const
274 {
275 g_signal_handlers_block_by_func( m_widget,
276 (gpointer)gtk_value_changed, (void*) this);
277
278 g_signal_handlers_block_by_func(m_widget,
279 (gpointer)gtk_changed, (void*) this);
280 }
281
282 void wxSpinCtrlGTKBase::GtkEnableEvents() const
283 {
284 g_signal_handlers_unblock_by_func(m_widget,
285 (gpointer)gtk_value_changed, (void*) this);
286
287 g_signal_handlers_unblock_by_func(m_widget,
288 (gpointer)gtk_changed, (void*) this);
289 }
290
291 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
292 {
293 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
294
295 if (event.GetKeyCode() == WXK_RETURN)
296 {
297 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
298
299 if ( GTK_IS_WINDOW(top_frame->m_widget) )
300 {
301 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
302 if ( window )
303 {
304 GtkWidget* widgetDef = gtk_window_get_default_widget(window);
305
306 if ( widgetDef )
307 {
308 gtk_widget_activate(widgetDef);
309 return;
310 }
311 }
312 }
313 }
314
315 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
316 {
317 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
318 evt.SetEventObject(this);
319 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
320 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
321 evt.SetString( val );
322 if (HandleWindowEvent(evt)) return;
323 }
324
325 event.Skip();
326 }
327
328 GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
329 {
330 #ifdef __WXGTK3__
331 // no access to internal GdkWindows
332 wxUnusedVar(windows);
333 #else
334 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
335
336 windows.push_back(spinbutton->entry.text_area);
337 windows.push_back(spinbutton->panel);
338 #endif
339
340 return NULL;
341 }
342
343 wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
344 {
345 wxSize ret( wxControl::DoGetBestSize() );
346 wxSize best(95, ret.y); // FIXME: 95?
347 CacheBestSize(best);
348 return best;
349 }
350
351 // static
352 wxVisualAttributes
353 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
354 {
355 // TODO: overload to accept functions like gtk_spin_button_new?
356 // Until then use a similar type
357 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
358 }
359
360 //-----------------------------------------------------------------------------
361 // wxSpinCtrl
362 //-----------------------------------------------------------------------------
363
364 //-----------------------------------------------------------------------------
365 // wxSpinCtrlDouble
366 //-----------------------------------------------------------------------------
367
368 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
369
370 unsigned wxSpinCtrlDouble::GetDigits() const
371 {
372 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
373
374 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
375 }
376
377 void wxSpinCtrlDouble::SetDigits(unsigned digits)
378 {
379 wxCHECK_RET( m_widget, "invalid spin button" );
380
381 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
382 }
383
384 #endif // wxUSE_SPINCTRL