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