]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinctrl.cpp
wxInputConsumer
[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
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "spinctrl.h"
13#endif
14
15#include "wx/spinctrl.h"
16
0e0d8857 17#if wxUSE_SPINCTRL
738f9e5a
RR
18
19#include "wx/utils.h"
aec0ed2e 20
78bcfcfc
VZ
21#include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
738f9e5a
RR
23#include <math.h>
24
aec0ed2e
RR
25#include <gdk/gdk.h>
26#include <gtk/gtk.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
47static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
48{
49 if (g_isIdle) wxapp_install_idle_handler();
50
51 if (!win->m_hasVMT) return;
52 if (g_blockEventsOnDrag) return;
53
58c837a4 54 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
738f9e5a 55 event.SetEventObject( win );
4477936a
VZ
56
57 // note that we don't use wxSpinCtrl::GetValue() here because it would
58 // adjust the value to fit into the control range and this means that we
59 // would never be able to enter an "invalid" value in the control, even
60 // temporarily - and trying to enter 10 into the control which accepts the
61 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
62 // enter 1!) (VZ)
63 event.SetInt( (int)ceil(win->m_adjust->value) );
738f9e5a 64 win->GetEventHandler()->ProcessEvent( event );
738f9e5a
RR
65}
66
0a07a7d8
RR
67//-----------------------------------------------------------------------------
68// "changed"
69//-----------------------------------------------------------------------------
70
71static void
72gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
73{
74 if (!win->m_hasVMT) return;
75
76 if (g_isIdle)
77 wxapp_install_idle_handler();
78
79 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
80 event.SetEventObject( win );
81 event.SetInt( win->GetValue() );
82 win->GetEventHandler()->ProcessEvent( event );
83}
84
738f9e5a
RR
85//-----------------------------------------------------------------------------
86// wxSpinCtrl
87//-----------------------------------------------------------------------------
88
89IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
90
da048e3d
RR
91BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
92 EVT_CHAR(wxSpinCtrl::OnChar)
93END_EVENT_TABLE()
94
738f9e5a 95bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
96 const wxString& value,
97 const wxPoint& pos, const wxSize& size,
98 long style,
99 int min, int max, int initial,
100 const wxString& name)
738f9e5a
RR
101{
102 m_needParent = TRUE;
354aa1e3 103 m_acceptsFocus = TRUE;
738f9e5a 104
0279e844
RR
105 if (!PreCreation( parent, pos, size ) ||
106 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
107 {
108 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
9750fc42 109 return FALSE;
738f9e5a
RR
110 }
111
112 m_oldPos = initial;
113
114 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
115
116 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
0279e844 117
b02da6b1
VZ
118 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
119 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 120
07f5b19a 121 GtkEnableEvents();
db434467 122
738f9e5a
RR
123 m_parent->DoAddChild( this );
124
125 PostCreation();
126
db434467
RR
127 SetFont( parent->GetFont() );
128
129 wxSize size_best( DoGetBestSize() );
130 wxSize new_size( size );
131 if (new_size.x == -1)
132 new_size.x = size_best.x;
133 if (new_size.y == -1)
134 new_size.y = size_best.y;
b0e282b3
RR
135 if (new_size.y > size_best.y)
136 new_size.y = size_best.y;
db434467
RR
137 if ((new_size.x != size.x) || (new_size.y != size.y))
138 SetSize( new_size.x, new_size.y );
139
738f9e5a
RR
140 SetBackgroundColour( parent->GetBackgroundColour() );
141
ce89fdd2
VZ
142 SetValue( value );
143
738f9e5a
RR
144 Show( TRUE );
145
146 return TRUE;
147}
148
07f5b19a
RR
149void wxSpinCtrl::GtkDisableEvents()
150{
151 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
152 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
153 (gpointer) this );
154
0a07a7d8
RR
155 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
156 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
157 (gpointer) this );
07f5b19a
RR
158}
159
160void wxSpinCtrl::GtkEnableEvents()
161{
162 gtk_signal_connect( GTK_OBJECT (m_adjust),
163 "value_changed",
164 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
165 (gpointer) this );
0a07a7d8
RR
166
167 gtk_signal_connect( GTK_OBJECT(m_widget),
168 "changed",
169 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
170 (gpointer)this);
07f5b19a
RR
171}
172
738f9e5a
RR
173int wxSpinCtrl::GetMin() const
174{
175 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
176
177 return (int)ceil(m_adjust->lower);
178}
179
180int wxSpinCtrl::GetMax() const
181{
182 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
183
184 return (int)ceil(m_adjust->upper);
185}
186
187int wxSpinCtrl::GetValue() const
188{
189 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
190
33720b2d
RR
191 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
192
738f9e5a
RR
193 return (int)ceil(m_adjust->value);
194}
195
ce89fdd2
VZ
196void wxSpinCtrl::SetValue( const wxString& value )
197{
198 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
199
200 int n;
201 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
202 {
203 // a number - set it
204 SetValue(n);
205 }
206 else
207 {
208 // invalid number - set text as is (wxMSW compatible)
07f5b19a 209 GtkDisableEvents();
7dd62924 210 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
07f5b19a 211 GtkEnableEvents();
ce89fdd2
VZ
212 }
213}
214
738f9e5a
RR
215void wxSpinCtrl::SetValue( int value )
216{
217 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
218
219 float fpos = (float)value;
220 m_oldPos = fpos;
221 if (fabs(fpos-m_adjust->value) < sensitivity) return;
222
223 m_adjust->value = fpos;
224
07f5b19a 225 GtkDisableEvents();
738f9e5a 226 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
07f5b19a 227 GtkEnableEvents();
738f9e5a
RR
228}
229
230void wxSpinCtrl::SetRange(int minVal, int maxVal)
231{
232 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
233
234 float fmin = (float)minVal;
235 float fmax = (float)maxVal;
236
237 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
238 (fabs(fmax-m_adjust->upper) < sensitivity))
239 {
240 return;
241 }
242
243 m_adjust->lower = fmin;
244 m_adjust->upper = fmax;
245
246 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 247
738f9e5a
RR
248 // these two calls are required due to some bug in GTK
249 Refresh();
250 SetFocus();
251}
252
da048e3d
RR
253void wxSpinCtrl::OnChar( wxKeyEvent &event )
254{
255 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
256
257 if (event.KeyCode() == WXK_RETURN)
258 {
259 wxWindow *top_frame = m_parent;
f6bcfd97 260 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
da048e3d 261 top_frame = top_frame->GetParent();
9750fc42 262 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
0e0d8857 263
9750fc42 264 if (window->default_widget)
da048e3d
RR
265 {
266 gtk_widget_activate (window->default_widget);
9750fc42
VZ
267 return;
268 }
da048e3d
RR
269 }
270
271 event.Skip();
272}
273
738f9e5a
RR
274bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
275{
276 return GTK_SPIN_BUTTON(m_widget)->panel == window;
277}
278
279void wxSpinCtrl::ApplyWidgetStyle()
280{
281 SetWidgetStyle();
282 gtk_widget_set_style( m_widget, m_widgetStyle );
283}
284
9d9b7755
VZ
285wxSize wxSpinCtrl::DoGetBestSize() const
286{
0279e844
RR
287 wxSize ret( wxControl::DoGetBestSize() );
288 return wxSize(95, ret.y);
9d9b7755
VZ
289}
290
738f9e5a 291#endif
aec0ed2e 292 // wxUSE_SPINCTRL