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