]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinctrl.cpp
Add a public wxModalDialogHook class for intercepting modal dialogs.
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
CommitLineData
738f9e5a 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk1/spinctrl.cpp
738f9e5a
RR
3// Purpose: wxSpinCtrl
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
65571936 8// Licence: wxWindows licence
738f9e5a
RR
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
0e0d8857 14#if wxUSE_SPINCTRL
738f9e5a 15
de6185e2
WS
16#include "wx/spinctrl.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/utils.h"
ce7fe42e 20 #include "wx/textctrl.h" // for wxEVT_TEXT
18680f86 21 #include "wx/math.h"
f06efcfd 22 #include "wx/crt.h"
de6185e2 23#endif
aec0ed2e 24
3cbab641 25#include "wx/gtk1/private.h"
738f9e5a
RR
26
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
0c623889
RR
34static const float sensitivity = 0.02;
35
738f9e5a
RR
36//-----------------------------------------------------------------------------
37// data
38//-----------------------------------------------------------------------------
39
40extern bool g_blockEventsOnDrag;
41
738f9e5a
RR
42//-----------------------------------------------------------------------------
43// "value_changed"
44//-----------------------------------------------------------------------------
45
865bb325 46extern "C" {
bab5e0d0
JJ
47
48static gboolean
49wx_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
65static gint
66wx_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
738f9e5a
RR
79static 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
ce7fe42e 86 wxCommandEvent event( wxEVT_SPINCTRL, win->GetId());
738f9e5a 87 event.SetEventObject( win );
4477936a
VZ
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) );
937013e0 96 win->HandleWindowEvent( event );
738f9e5a 97}
865bb325 98}
738f9e5a 99
0a07a7d8
RR
100//-----------------------------------------------------------------------------
101// "changed"
102//-----------------------------------------------------------------------------
103
865bb325 104extern "C" {
0a07a7d8
RR
105static void
106gtk_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
ce7fe42e 113 wxCommandEvent event( wxEVT_TEXT, win->GetId() );
0a07a7d8 114 event.SetEventObject( win );
3d257b8d 115
ea46eba0
RR
116 // see above
117 event.SetInt( (int)ceil(win->m_adjust->value) );
937013e0 118 win->HandleWindowEvent( event );
0a07a7d8 119}
865bb325 120}
0a07a7d8 121
738f9e5a
RR
122//-----------------------------------------------------------------------------
123// wxSpinCtrl
124//-----------------------------------------------------------------------------
125
da048e3d
RR
126BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
127 EVT_CHAR(wxSpinCtrl::OnChar)
128END_EVENT_TABLE()
129
738f9e5a 130bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
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)
738f9e5a 136{
8e13c1ec
WS
137 m_needParent = true;
138 m_acceptsFocus = true;
738f9e5a 139
0279e844
RR
140 if (!PreCreation( parent, pos, size ) ||
141 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
142 {
143 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
8e13c1ec 144 return false;
738f9e5a
RR
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 );
3d257b8d 152
b02da6b1
VZ
153 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
154 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 155
07f5b19a 156 GtkEnableEvents();
3d257b8d 157
738f9e5a
RR
158 m_parent->DoAddChild( this );
159
abdeb9e7 160 PostCreation(size);
db434467 161
ce89fdd2
VZ
162 SetValue( value );
163
8e13c1ec 164 return true;
738f9e5a
RR
165}
166
07f5b19a
RR
167void wxSpinCtrl::GtkDisableEvents()
168{
169 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
170 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
171 (gpointer) this );
172
0a07a7d8
RR
173 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
174 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
175 (gpointer) this );
07f5b19a
RR
176}
177
178void wxSpinCtrl::GtkEnableEvents()
179{
180 gtk_signal_connect( GTK_OBJECT (m_adjust),
181 "value_changed",
182 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
183 (gpointer) this );
3d257b8d 184
0a07a7d8
RR
185 gtk_signal_connect( GTK_OBJECT(m_widget),
186 "changed",
187 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
188 (gpointer)this);
07f5b19a
RR
189}
190
738f9e5a
RR
191int 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
198int 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
205int wxSpinCtrl::GetValue() const
206{
207 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
208
33720b2d
RR
209 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
210
738f9e5a
RR
211 return (int)ceil(m_adjust->value);
212}
213
ce89fdd2
VZ
214void 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)
07f5b19a 227 GtkDisableEvents();
fab591c5 228 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
07f5b19a 229 GtkEnableEvents();
ce89fdd2
VZ
230 }
231}
232
738f9e5a
RR
233void 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
07f5b19a 243 GtkDisableEvents();
738f9e5a 244 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
07f5b19a 245 GtkEnableEvents();
738f9e5a
RR
246}
247
f8f9ec55
VZ
248void wxSpinCtrl::SetSelection(long from, long to)
249{
77ffb593 250 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
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
738f9e5a
RR
261void 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" );
0e0d8857 278
738f9e5a
RR
279 // these two calls are required due to some bug in GTK
280 Refresh();
281 SetFocus();
282}
283
da048e3d
RR
284void wxSpinCtrl::OnChar( wxKeyEvent &event )
285{
286 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
287
12a3f227 288 if (event.GetKeyCode() == WXK_RETURN)
da048e3d
RR
289 {
290 wxWindow *top_frame = m_parent;
055e633d 291 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 292 top_frame = top_frame->GetParent();
0e0d8857 293
fa8a793a 294 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 295 {
fa8a793a
VZ
296 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
297 if ( window )
298 {
299 GtkWidget *widgetDef = window->default_widget;
300
055e633d 301 if ( widgetDef )
fa8a793a
VZ
302 {
303 gtk_widget_activate(widgetDef);
304 return;
305 }
306 }
9750fc42 307 }
da048e3d
RR
308 }
309
8e13c1ec 310 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2 311 {
ce7fe42e 312 wxCommandEvent evt( wxEVT_TEXT_ENTER, m_windowId );
4a11cca2
RR
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 );
937013e0 317 if (HandleWindowEvent(evt)) return;
4a11cca2
RR
318 }
319
da048e3d
RR
320 event.Skip();
321}
322
738f9e5a
RR
323bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
324{
8e13c1ec 325 if (GTK_SPIN_BUTTON(m_widget)->entry.text_area == window) return true;
3d257b8d 326
8e13c1ec 327 if (GTK_SPIN_BUTTON(m_widget)->panel == window) return true;
2b904684 328
8e13c1ec 329 return false;
738f9e5a
RR
330}
331
9d9b7755
VZ
332wxSize wxSpinCtrl::DoGetBestSize() const
333{
0279e844 334 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
335 wxSize best(95, ret.y);
336 CacheBestSize(best);
337 return best;
9d9b7755
VZ
338}
339
bab5e0d0
JJ
340bool 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
9d522606
RD
376// static
377wxVisualAttributes
378wxSpinCtrl::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
738f9e5a 385#endif
aec0ed2e 386 // wxUSE_SPINCTRL