Remove obsolete VisualAge-related files.
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/utils.h"
19 #include "wx/textctrl.h" // for wxEVT_TEXT
20 #include "wx/math.h"
21 #include "wx/crt.h"
22 #endif
23
24 #include "wx/gtk1/private.h"
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 static const float sensitivity = 0.02;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40
41 //-----------------------------------------------------------------------------
42 // "value_changed"
43 //-----------------------------------------------------------------------------
44
45 extern "C" {
46
47 static gboolean
48 wx_gtk_spin_input(GtkSpinButton* spin, gdouble* val, wxSpinCtrl* win)
49 {
50 // We might use g_ascii_strtoll() here but it's 2.12+ only, so use our own
51 // wxString function even if this requires an extra conversion.
52 const wxString
53 text(wxString::FromUTF8(gtk_entry_get_text(GTK_ENTRY(spin))));
54
55 long lval;
56 if ( !text.ToLong(&lval, win->GetBase()) )
57 return FALSE;
58
59 *val = lval;
60
61 return TRUE;
62 }
63
64 static gint
65 wx_gtk_spin_output(GtkSpinButton* spin, wxSpinCtrl* win)
66 {
67 const gint val = gtk_spin_button_get_value_as_int(spin);
68
69 gtk_entry_set_text
70 (
71 GTK_ENTRY(spin),
72 wxPrivate::wxSpinCtrlFormatAsHex(val, win->GetMax()).utf8_str()
73 );
74
75 return TRUE;
76 }
77
78 static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
79 {
80 if (g_isIdle) wxapp_install_idle_handler();
81
82 if (!win->m_hasVMT) return;
83 if (g_blockEventsOnDrag) return;
84
85 wxCommandEvent event( wxEVT_SPINCTRL, win->GetId());
86 event.SetEventObject( win );
87
88 // note that we don't use wxSpinCtrl::GetValue() here because it would
89 // adjust the value to fit into the control range and this means that we
90 // would never be able to enter an "invalid" value in the control, even
91 // temporarily - and trying to enter 10 into the control which accepts the
92 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
93 // enter 1!) (VZ)
94 event.SetInt( (int)ceil(win->m_adjust->value) );
95 win->HandleWindowEvent( event );
96 }
97 }
98
99 //-----------------------------------------------------------------------------
100 // "changed"
101 //-----------------------------------------------------------------------------
102
103 extern "C" {
104 static void
105 gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
106 {
107 if (!win->m_hasVMT) return;
108
109 if (g_isIdle)
110 wxapp_install_idle_handler();
111
112 wxCommandEvent event( wxEVT_TEXT, win->GetId() );
113 event.SetEventObject( win );
114
115 // see above
116 event.SetInt( (int)ceil(win->m_adjust->value) );
117 win->HandleWindowEvent( event );
118 }
119 }
120
121 //-----------------------------------------------------------------------------
122 // wxSpinCtrl
123 //-----------------------------------------------------------------------------
124
125 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
126 EVT_CHAR(wxSpinCtrl::OnChar)
127 END_EVENT_TABLE()
128
129 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
130 const wxString& value,
131 const wxPoint& pos, const wxSize& size,
132 long style,
133 int min, int max, int initial,
134 const wxString& name)
135 {
136 m_needParent = true;
137 m_acceptsFocus = true;
138
139 if (!PreCreation( parent, pos, size ) ||
140 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
141 {
142 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
143 return false;
144 }
145
146 m_oldPos = initial;
147
148 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
149
150 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
151
152 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
153 (int)(m_windowStyle & wxSP_WRAP) );
154
155 GtkEnableEvents();
156
157 m_parent->DoAddChild( this );
158
159 PostCreation(size);
160
161 SetValue( value );
162
163 return true;
164 }
165
166 void wxSpinCtrl::GtkDisableEvents()
167 {
168 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
169 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
170 (gpointer) this );
171
172 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
173 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
174 (gpointer) this );
175 }
176
177 void wxSpinCtrl::GtkEnableEvents()
178 {
179 gtk_signal_connect( GTK_OBJECT (m_adjust),
180 "value_changed",
181 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
182 (gpointer) this );
183
184 gtk_signal_connect( GTK_OBJECT(m_widget),
185 "changed",
186 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
187 (gpointer)this);
188 }
189
190 int wxSpinCtrl::GetMin() const
191 {
192 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
193
194 return (int)ceil(m_adjust->lower);
195 }
196
197 int wxSpinCtrl::GetMax() const
198 {
199 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
200
201 return (int)ceil(m_adjust->upper);
202 }
203
204 int wxSpinCtrl::GetValue() const
205 {
206 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
207
208 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
209
210 return (int)ceil(m_adjust->value);
211 }
212
213 void wxSpinCtrl::SetValue( const wxString& value )
214 {
215 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
216
217 int n;
218 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
219 {
220 // a number - set it
221 SetValue(n);
222 }
223 else
224 {
225 // invalid number - set text as is (wxMSW compatible)
226 GtkDisableEvents();
227 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
228 GtkEnableEvents();
229 }
230 }
231
232 void wxSpinCtrl::SetValue( int value )
233 {
234 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
235
236 float fpos = (float)value;
237 m_oldPos = fpos;
238 if (fabs(fpos-m_adjust->value) < sensitivity) return;
239
240 m_adjust->value = fpos;
241
242 GtkDisableEvents();
243 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
244 GtkEnableEvents();
245 }
246
247 void wxSpinCtrl::SetSelection(long from, long to)
248 {
249 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
250 // entire range
251 if ( from == -1 && to == -1 )
252 {
253 from = 0;
254 to = INT_MAX;
255 }
256
257 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
258 }
259
260 void wxSpinCtrl::SetRange(int minVal, int maxVal)
261 {
262 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
263
264 float fmin = (float)minVal;
265 float fmax = (float)maxVal;
266
267 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
268 (fabs(fmax-m_adjust->upper) < sensitivity))
269 {
270 return;
271 }
272
273 m_adjust->lower = fmin;
274 m_adjust->upper = fmax;
275
276 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
277
278 // these two calls are required due to some bug in GTK
279 Refresh();
280 SetFocus();
281 }
282
283 void wxSpinCtrl::OnChar( wxKeyEvent &event )
284 {
285 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
286
287 if (event.GetKeyCode() == WXK_RETURN)
288 {
289 wxWindow *top_frame = m_parent;
290 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
291 top_frame = top_frame->GetParent();
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_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 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
323 {
324 if (GTK_SPIN_BUTTON(m_widget)->entry.text_area == window) return true;
325
326 if (GTK_SPIN_BUTTON(m_widget)->panel == window) return true;
327
328 return false;
329 }
330
331 wxSize wxSpinCtrl::DoGetBestSize() const
332 {
333 wxSize ret( wxControl::DoGetBestSize() );
334 wxSize best(95, ret.y);
335 CacheBestSize(best);
336 return best;
337 }
338
339 bool wxSpinCtrl::SetBase(int base)
340 {
341 // Currently we only support base 10 and 16. We could add support for base
342 // 8 quite easily but wxMSW doesn't support it natively so don't bother
343 // with doing something wxGTK-specific here.
344 if ( base != 10 && base != 16 )
345 return false;
346
347 if ( base == m_base )
348 return true;
349
350 m_base = base;
351
352 // We need to be able to enter letters for any base greater than 10.
353 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON(m_widget), m_base <= 10 );
354
355 if ( m_base != 10 )
356 {
357 gtk_signal_connect( GTK_OBJECT(m_widget), "input",
358 GTK_SIGNAL_FUNC(wx_gtk_spin_input), this);
359 gtk_signal_connect( GTK_OBJECT(m_widget), "output",
360 GTK_SIGNAL_FUNC(wx_gtk_spin_output), this);
361 }
362 else
363 {
364 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget),
365 GTK_SIGNAL_FUNC(wx_gtk_spin_input),
366 this);
367 gtk_signal_disconnect_by_func(GTK_OBJECT(m_widget),
368 GTK_SIGNAL_FUNC(wx_gtk_spin_output),
369 this);
370 }
371
372 return true;
373 }
374
375 // static
376 wxVisualAttributes
377 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
378 {
379 // TODO: overload to accept functions like gtk_spin_button_new?
380 // Until then use a similar type
381 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
382 }
383
384 #endif
385 // wxUSE_SPINCTRL