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