]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
added a check which should prevent the crash of bug 555111
[wxWidgets.git] / src / gtk / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
12 #pragma implementation "spinctrl.h"
13 #endif
14
15 #include "wx/spinctrl.h"
16
17 #if wxUSE_SPINCTRL
18
19 #include "wx/utils.h"
20
21 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
23 #include <math.h>
24
25 #include "wx/gtk/private.h"
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 static const float sensitivity = 0.02;
35
36 //-----------------------------------------------------------------------------
37 // data
38 //-----------------------------------------------------------------------------
39
40 extern bool g_blockEventsOnDrag;
41
42 //-----------------------------------------------------------------------------
43 // "value_changed"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
47 {
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (!win->m_hasVMT) return;
51 if (g_blockEventsOnDrag) return;
52
53 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
54 event.SetEventObject( win );
55
56 // note that we don't use wxSpinCtrl::GetValue() here because it would
57 // adjust the value to fit into the control range and this means that we
58 // would never be able to enter an "invalid" value in the control, even
59 // temporarily - and trying to enter 10 into the control which accepts the
60 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
61 // enter 1!) (VZ)
62 event.SetInt( (int)ceil(win->m_adjust->value) );
63 win->GetEventHandler()->ProcessEvent( event );
64 }
65
66 //-----------------------------------------------------------------------------
67 // "changed"
68 //-----------------------------------------------------------------------------
69
70 static void
71 gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
72 {
73 if (!win->m_hasVMT) return;
74
75 if (g_isIdle)
76 wxapp_install_idle_handler();
77
78 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
79 event.SetEventObject( win );
80 event.SetInt( win->GetValue() );
81 win->GetEventHandler()->ProcessEvent( event );
82 }
83
84 //-----------------------------------------------------------------------------
85 // wxSpinCtrl
86 //-----------------------------------------------------------------------------
87
88 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
89
90 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
91 EVT_CHAR(wxSpinCtrl::OnChar)
92 END_EVENT_TABLE()
93
94 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
95 const wxString& value,
96 const wxPoint& pos, const wxSize& size,
97 long style,
98 int min, int max, int initial,
99 const wxString& name)
100 {
101 m_needParent = TRUE;
102 m_acceptsFocus = TRUE;
103
104 if (!PreCreation( parent, pos, size ) ||
105 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
106 {
107 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
108 return FALSE;
109 }
110
111 m_oldPos = initial;
112
113 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
114
115 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
116
117 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
118 (int)(m_windowStyle & wxSP_WRAP) );
119
120 GtkEnableEvents();
121
122 m_parent->DoAddChild( this );
123
124 PostCreation();
125
126 SetFont( parent->GetFont() );
127
128 wxSize size_best( DoGetBestSize() );
129 wxSize new_size( size );
130 if (new_size.x == -1)
131 new_size.x = size_best.x;
132 if (new_size.y == -1)
133 new_size.y = size_best.y;
134 if (new_size.y > size_best.y)
135 new_size.y = size_best.y;
136 if ((new_size.x != size.x) || (new_size.y != size.y))
137 SetSize( new_size.x, new_size.y );
138
139 SetBackgroundColour( parent->GetBackgroundColour() );
140
141 SetValue( value );
142
143 Show( TRUE );
144
145 return TRUE;
146 }
147
148 void wxSpinCtrl::GtkDisableEvents()
149 {
150 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
151 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
152 (gpointer) this );
153
154 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
155 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
156 (gpointer) this );
157 }
158
159 void wxSpinCtrl::GtkEnableEvents()
160 {
161 gtk_signal_connect( GTK_OBJECT (m_adjust),
162 "value_changed",
163 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
164 (gpointer) this );
165
166 gtk_signal_connect( GTK_OBJECT(m_widget),
167 "changed",
168 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
169 (gpointer)this);
170 }
171
172 int wxSpinCtrl::GetMin() const
173 {
174 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
175
176 return (int)ceil(m_adjust->lower);
177 }
178
179 int wxSpinCtrl::GetMax() const
180 {
181 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
182
183 return (int)ceil(m_adjust->upper);
184 }
185
186 int wxSpinCtrl::GetValue() const
187 {
188 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
189
190 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
191
192 return (int)ceil(m_adjust->value);
193 }
194
195 void wxSpinCtrl::SetValue( const wxString& value )
196 {
197 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
198
199 int n;
200 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
201 {
202 // a number - set it
203 SetValue(n);
204 }
205 else
206 {
207 // invalid number - set text as is (wxMSW compatible)
208 GtkDisableEvents();
209 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
210 GtkEnableEvents();
211 }
212 }
213
214 void wxSpinCtrl::SetValue( int value )
215 {
216 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
217
218 float fpos = (float)value;
219 m_oldPos = fpos;
220 if (fabs(fpos-m_adjust->value) < sensitivity) return;
221
222 m_adjust->value = fpos;
223
224 GtkDisableEvents();
225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
226 GtkEnableEvents();
227 }
228
229 void wxSpinCtrl::SetRange(int minVal, int maxVal)
230 {
231 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
232
233 float fmin = (float)minVal;
234 float fmax = (float)maxVal;
235
236 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
237 (fabs(fmax-m_adjust->upper) < sensitivity))
238 {
239 return;
240 }
241
242 m_adjust->lower = fmin;
243 m_adjust->upper = fmax;
244
245 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
246
247 // these two calls are required due to some bug in GTK
248 Refresh();
249 SetFocus();
250 }
251
252 void wxSpinCtrl::OnChar( wxKeyEvent &event )
253 {
254 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
255
256 if (event.KeyCode() == WXK_RETURN)
257 {
258 wxWindow *top_frame = m_parent;
259 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
260 top_frame = top_frame->GetParent();
261
262 if ( GTK_IS_WINDOW(top_frame->m_widget) )
263 {
264 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
265 if ( window )
266 {
267 GtkWidget *widgetDef = window->default_widget;
268
269 if ( widgetDef && GTK_IS_WINDOW(widgetDef) )
270 {
271 gtk_widget_activate(widgetDef);
272 return;
273 }
274 }
275 }
276 }
277
278 event.Skip();
279 }
280
281 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
282 {
283 return GTK_SPIN_BUTTON(m_widget)->panel == window;
284 }
285
286 void wxSpinCtrl::ApplyWidgetStyle()
287 {
288 SetWidgetStyle();
289 gtk_widget_set_style( m_widget, m_widgetStyle );
290 }
291
292 wxSize wxSpinCtrl::DoGetBestSize() const
293 {
294 wxSize ret( wxControl::DoGetBestSize() );
295 return wxSize(95, ret.y);
296 }
297
298 #endif
299 // wxUSE_SPINCTRL