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