]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
work around VC6 bug
[wxWidgets.git] / src / gtk / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinbutt.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 win->m_value = gtk_spin_button_get_value(spinbutton);
41 if (!win->m_hasVMT || g_blockEventsOnDrag)
42 return;
43
44 // note that we don't use wxSpinCtrl::GetValue() here because it would
45 // adjust the value to fit into the control range and this means that we
46 // would never be able to enter an "invalid" value in the control, even
47 // temporarily - and trying to enter 10 into the control which accepts the
48 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
49 // enter 1!) (VZ)
50
51 if (wxIsKindOf(win, wxSpinCtrl))
52 {
53 wxSpinEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
54 event.SetEventObject( win );
55 event.SetPosition( wxRound(win->m_value) ); // FIXME should be SetValue
56 event.SetString(GTK_ENTRY(spinbutton)->text);
57 win->HandleWindowEvent( event );
58 }
59 else // wxIsKindOf(win, wxSpinCtrlDouble)
60 {
61 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, win->GetId());
62 event.SetEventObject( win );
63 event.SetValue(win->m_value);
64 event.SetString(GTK_ENTRY(spinbutton)->text);
65 win->HandleWindowEvent( event );
66 }
67 }
68 }
69
70 //-----------------------------------------------------------------------------
71 // "changed"
72 //-----------------------------------------------------------------------------
73
74 extern "C" {
75 static void
76 gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
77 {
78 if (!win->m_hasVMT)
79 return;
80
81 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
82 event.SetEventObject( win );
83 event.SetString( GTK_ENTRY(spinbutton)->text );
84
85 // see above
86 event.SetInt((int)win->m_value);
87 win->HandleWindowEvent( event );
88 }
89 }
90
91 //-----------------------------------------------------------------------------
92 // wxSpinCtrlGTKBase
93 //-----------------------------------------------------------------------------
94
95 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase)
96
97 BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
98 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
99 END_EVENT_TABLE()
100
101 bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
102 const wxString& value,
103 const wxPoint& pos, const wxSize& size,
104 long style,
105 double min, double max, double initial, double inc,
106 const wxString& name)
107 {
108 if (!PreCreation( parent, pos, size ) ||
109 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
110 {
111 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
112 return false;
113 }
114
115 m_widget = gtk_spin_button_new_with_range(min, max, inc);
116 g_object_ref(m_widget);
117
118 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
119 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
120
121 gfloat align;
122 if ( HasFlag(wxALIGN_RIGHT) )
123 align = 1.0;
124 else if ( HasFlag(wxALIGN_CENTRE) )
125 align = 0.5;
126 else
127 align = 0.0;
128
129 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
130
131 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
132 (int)(m_windowStyle & wxSP_WRAP) );
133
134 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
135 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
136
137 m_parent->DoAddChild( this );
138
139 PostCreation(size);
140
141 if (!value.empty())
142 {
143 SetValue(value);
144 }
145
146 return true;
147 }
148
149 double wxSpinCtrlGTKBase::DoGetValue() const
150 {
151 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
152
153 GtkDisableEvents();
154 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
155 const_cast<wxSpinCtrlGTKBase*>(this)->m_value =
156 gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
157 GtkEnableEvents();
158
159 return m_value;
160 }
161
162 double wxSpinCtrlGTKBase::DoGetMin() const
163 {
164 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
165
166 double min = 0;
167 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
168 return min;
169 }
170
171 double wxSpinCtrlGTKBase::DoGetMax() const
172 {
173 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
174
175 double max = 0;
176 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
177 return max;
178 }
179
180 double wxSpinCtrlGTKBase::DoGetIncrement() const
181 {
182 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
183
184 double inc = 0;
185 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &inc);
186 return inc;
187 }
188
189 bool wxSpinCtrlGTKBase::GetSnapToTicks() const
190 {
191 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
192
193 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) );
194 }
195
196 void wxSpinCtrlGTKBase::SetValue( const wxString& value )
197 {
198 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
199
200 double n;
201 if ( wxSscanf(value, "%lg", &n) == 1 )
202 {
203 // a number - set it, let DoSetValue round for int value
204 DoSetValue(n);
205 return;
206 }
207
208 // invalid number - set text as is (wxMSW compatible)
209 GtkDisableEvents();
210 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
211 GtkEnableEvents();
212 }
213
214 void wxSpinCtrlGTKBase::DoSetValue( double value )
215 {
216 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
217
218 if (wxIsKindOf(this, wxSpinCtrl))
219 value = wxRound( value );
220
221 GtkDisableEvents();
222 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
223 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
224 GtkEnableEvents();
225 }
226
227 void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
228 {
229 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
230
231 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
232 }
233
234 void wxSpinCtrlGTKBase::SetSelection(long from, long to)
235 {
236 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
237 // entire range
238 if ( from == -1 && to == -1 )
239 {
240 from = 0;
241 to = INT_MAX;
242 }
243
244 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
245 }
246
247 void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
248 {
249 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
250
251 GtkDisableEvents();
252 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
253 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
254 GtkEnableEvents();
255 }
256
257 void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
258 {
259 wxCHECK_RET( m_widget, "invalid spin button" );
260
261 GtkDisableEvents();
262 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, 10*inc);
263 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
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 = window->default_widget;
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_COMMAND_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 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
325
326 windows.push_back(spinbutton->entry.text_area);
327 windows.push_back(spinbutton->panel);
328
329 return NULL;
330 }
331
332 wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
333 {
334 wxSize ret( wxControl::DoGetBestSize() );
335 wxSize best(95, ret.y); // FIXME: 95?
336 CacheBestSize(best);
337 return best;
338 }
339
340 // static
341 wxVisualAttributes
342 wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
343 {
344 // TODO: overload to accept functions like gtk_spin_button_new?
345 // Until then use a similar type
346 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
347 }
348
349 //-----------------------------------------------------------------------------
350 // wxSpinCtrl
351 //-----------------------------------------------------------------------------
352
353 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGTKBase)
354
355 //-----------------------------------------------------------------------------
356 // wxSpinCtrlDouble
357 //-----------------------------------------------------------------------------
358
359 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
360
361 unsigned wxSpinCtrlDouble::GetDigits() const
362 {
363 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
364
365 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
366 }
367
368 void wxSpinCtrlDouble::SetDigits(unsigned digits)
369 {
370 wxCHECK_RET( m_widget, "invalid spin button" );
371
372 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
373 }
374
375 #endif // wxUSE_SPINCTRL