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