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