]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
reactivate commented out method
[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 "wx/gtk/private.h"
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 extern bool g_blockEventsOnDrag;
31
32 //-----------------------------------------------------------------------------
33 // "value_changed"
34 //-----------------------------------------------------------------------------
35
36 extern "C" {
37 static void
38 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
39 {
40 if (!win->m_hasVMT || g_blockEventsOnDrag)
41 return;
42
43 if (wxIsKindOf(win, wxSpinCtrl))
44 {
45 wxSpinEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
46 event.SetEventObject( win );
47 event.SetPosition(static_cast<wxSpinCtrl*>(win)->GetValue());
48 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
49 win->HandleWindowEvent( event );
50 }
51 else // wxIsKindOf(win, wxSpinCtrlDouble)
52 {
53 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, win->GetId());
54 event.SetEventObject( win );
55 event.SetValue(static_cast<wxSpinCtrlDouble*>(win)->GetValue());
56 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
57 win->HandleWindowEvent( event );
58 }
59 }
60 }
61
62 //-----------------------------------------------------------------------------
63 // "changed"
64 //-----------------------------------------------------------------------------
65
66 extern "C" {
67 static void
68 gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
69 {
70 if (!win->m_hasVMT)
71 return;
72
73 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
74 event.SetEventObject( win );
75 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
76 event.SetInt(win->GetValue());
77 win->HandleWindowEvent( event );
78 }
79 }
80
81 //-----------------------------------------------------------------------------
82 // wxSpinCtrlGTKBase
83 //-----------------------------------------------------------------------------
84
85 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase)
86
87 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
88 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
89 END_EVENT_TABLE()
90
91 bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
92 const wxString& value,
93 const wxPoint& pos, const wxSize& size,
94 long style,
95 double min, double max, double initial, double inc,
96 const wxString& name)
97 {
98 if (!PreCreation( parent, pos, size ) ||
99 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
100 {
101 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
102 return false;
103 }
104
105 m_widget = gtk_spin_button_new_with_range(min, max, inc);
106 g_object_ref(m_widget);
107
108 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
109
110 gfloat align;
111 if ( HasFlag(wxALIGN_RIGHT) )
112 align = 1.0;
113 else if ( HasFlag(wxALIGN_CENTRE) )
114 align = 0.5;
115 else
116 align = 0.0;
117
118 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
119
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
121 (int)(m_windowStyle & wxSP_WRAP) );
122
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);
125
126 m_parent->DoAddChild( this );
127
128 PostCreation(size);
129
130 if (!value.empty())
131 {
132 SetValue(value);
133 }
134
135 return true;
136 }
137
138 double wxSpinCtrlGTKBase::DoGetValue() const
139 {
140 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
141
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);
155 GtkAdjustment* adj =
156 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget));
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;
163
164 return value;
165 }
166
167 double wxSpinCtrlGTKBase::DoGetMin() const
168 {
169 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
170
171 double min = 0;
172 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
173 return min;
174 }
175
176 double wxSpinCtrlGTKBase::DoGetMax() const
177 {
178 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
179
180 double max = 0;
181 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
182 return max;
183 }
184
185 double wxSpinCtrlGTKBase::DoGetIncrement() const
186 {
187 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
188
189 double inc = 0;
190 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL);
191 return inc;
192 }
193
194 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
195 {
196 wxCHECK_MSG(m_widget, false, "invalid spin button");
197
198 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) );
199 }
200
201 void wxSpinCtrlGTKBase::SetValue( const wxString& value )
202 {
203 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
204
205 double n;
206 if ( wxSscanf(value, "%lg", &n) == 1 )
207 {
208 // a number - set it, let DoSetValue round for int value
209 DoSetValue(n);
210 return;
211 }
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();
217 }
218
219 void wxSpinCtrlGTKBase::DoSetValue( double value )
220 {
221 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
222
223 GtkDisableEvents();
224 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
225 GtkEnableEvents();
226 }
227
228 void 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
235 void wxSpinCtrlGTKBase::SetSelection(long from, long to)
236 {
237 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
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
248 void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
249 {
250 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
251
252 GtkDisableEvents();
253 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
254 GtkEnableEvents();
255 }
256
257 void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
258 {
259 wxCHECK_RET( m_widget, "invalid spin button" );
260
261 GtkDisableEvents();
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);
268 GtkEnableEvents();
269 }
270
271 void wxSpinCtrlGTKBase::GtkDisableEvents() const
272 {
273 g_signal_handlers_block_by_func( m_widget,
274 (gpointer)gtk_value_changed, (void*) this);
275
276 g_signal_handlers_block_by_func(m_widget,
277 (gpointer)gtk_changed, (void*) this);
278 }
279
280 void wxSpinCtrlGTKBase::GtkEnableEvents() const
281 {
282 g_signal_handlers_unblock_by_func(m_widget,
283 (gpointer)gtk_value_changed, (void*) this);
284
285 g_signal_handlers_unblock_by_func(m_widget,
286 (gpointer)gtk_changed, (void*) this);
287 }
288
289 void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
290 {
291 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
292
293 if (event.GetKeyCode() == WXK_RETURN)
294 {
295 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
296
297 if ( GTK_IS_WINDOW(top_frame->m_widget) )
298 {
299 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
300 if ( window )
301 {
302 GtkWidget* widgetDef = gtk_window_get_default_widget(window);
303
304 if ( widgetDef )
305 {
306 gtk_widget_activate(widgetDef);
307 return;
308 }
309 }
310 }
311 }
312
313 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
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 );
320 if (HandleWindowEvent(evt)) return;
321 }
322
323 event.Skip();
324 }
325
326 GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
327 {
328 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
329
330 windows.push_back(spinbutton->entry.text_area);
331 windows.push_back(spinbutton->panel);
332
333 return NULL;
334 }
335
336 wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
337 {
338 wxSize ret( wxControl::DoGetBestSize() );
339 wxSize best(95, ret.y); // FIXME: 95?
340 CacheBestSize(best);
341 return best;
342 }
343
344 // static
345 wxVisualAttributes
346 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
347 {
348 // TODO: overload to accept functions like gtk_spin_button_new?
349 // Until then use a similar type
350 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
351 }
352
353 //-----------------------------------------------------------------------------
354 // wxSpinCtrl
355 //-----------------------------------------------------------------------------
356
357 //-----------------------------------------------------------------------------
358 // wxSpinCtrlDouble
359 //-----------------------------------------------------------------------------
360
361 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
362
363 unsigned wxSpinCtrlDouble::GetDigits() const
364 {
365 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
366
367 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
368 }
369
370 void wxSpinCtrlDouble::SetDigits(unsigned digits)
371 {
372 wxCHECK_RET( m_widget, "invalid spin button" );
373
374 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
375 }
376
377 #endif // wxUSE_SPINCTRL