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