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