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