]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinctrl.cpp
Corrected assertion
[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
e1910715 25#include "wx/gtk/private.h"
738f9e5a
RR
26
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
0c623889
RR
34static const float sensitivity = 0.02;
35
738f9e5a
RR
36//-----------------------------------------------------------------------------
37// data
38//-----------------------------------------------------------------------------
39
40extern bool g_blockEventsOnDrag;
41
738f9e5a
RR
42//-----------------------------------------------------------------------------
43// "value_changed"
44//-----------------------------------------------------------------------------
45
46static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
47{
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (!win->m_hasVMT) return;
51 if (g_blockEventsOnDrag) return;
52
58c837a4 53 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
738f9e5a 54 event.SetEventObject( win );
4477936a
VZ
55
56 // note that we don't use wxSpinCtrl::GetValue() here because it would
57 // adjust the value to fit into the control range and this means that we
58 // would never be able to enter an "invalid" value in the control, even
59 // temporarily - and trying to enter 10 into the control which accepts the
60 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
61 // enter 1!) (VZ)
62 event.SetInt( (int)ceil(win->m_adjust->value) );
738f9e5a 63 win->GetEventHandler()->ProcessEvent( event );
738f9e5a
RR
64}
65
0a07a7d8
RR
66//-----------------------------------------------------------------------------
67// "changed"
68//-----------------------------------------------------------------------------
69
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 );
80 event.SetInt( win->GetValue() );
81 win->GetEventHandler()->ProcessEvent( event );
82}
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 );
0279e844 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();
db434467 121
738f9e5a
RR
122 m_parent->DoAddChild( this );
123
124 PostCreation();
125
db434467
RR
126 SetFont( parent->GetFont() );
127
128 wxSize size_best( DoGetBestSize() );
129 wxSize new_size( size );
130 if (new_size.x == -1)
131 new_size.x = size_best.x;
132 if (new_size.y == -1)
133 new_size.y = size_best.y;
b0e282b3
RR
134 if (new_size.y > size_best.y)
135 new_size.y = size_best.y;
db434467
RR
136 if ((new_size.x != size.x) || (new_size.y != size.y))
137 SetSize( new_size.x, new_size.y );
138
738f9e5a
RR
139 SetBackgroundColour( parent->GetBackgroundColour() );
140
ce89fdd2
VZ
141 SetValue( value );
142
738f9e5a
RR
143 Show( TRUE );
144
145 return TRUE;
146}
147
07f5b19a
RR
148void wxSpinCtrl::GtkDisableEvents()
149{
150 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
151 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
152 (gpointer) this );
153
0a07a7d8
RR
154 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
155 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
156 (gpointer) this );
07f5b19a
RR
157}
158
159void wxSpinCtrl::GtkEnableEvents()
160{
161 gtk_signal_connect( GTK_OBJECT (m_adjust),
162 "value_changed",
163 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
164 (gpointer) this );
0a07a7d8
RR
165
166 gtk_signal_connect( GTK_OBJECT(m_widget),
167 "changed",
168 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
169 (gpointer)this);
07f5b19a
RR
170}
171
738f9e5a
RR
172int wxSpinCtrl::GetMin() const
173{
174 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
175
176 return (int)ceil(m_adjust->lower);
177}
178
179int wxSpinCtrl::GetMax() const
180{
181 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
182
183 return (int)ceil(m_adjust->upper);
184}
185
186int wxSpinCtrl::GetValue() const
187{
188 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
189
33720b2d
RR
190 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
191
738f9e5a
RR
192 return (int)ceil(m_adjust->value);
193}
194
ce89fdd2
VZ
195void wxSpinCtrl::SetValue( const wxString& value )
196{
197 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
198
199 int n;
200 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
201 {
202 // a number - set it
203 SetValue(n);
204 }
205 else
206 {
207 // invalid number - set text as is (wxMSW compatible)
07f5b19a 208 GtkDisableEvents();
7dd62924 209 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
07f5b19a 210 GtkEnableEvents();
ce89fdd2
VZ
211 }
212}
213
738f9e5a
RR
214void wxSpinCtrl::SetValue( int value )
215{
216 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
217
218 float fpos = (float)value;
219 m_oldPos = fpos;
220 if (fabs(fpos-m_adjust->value) < sensitivity) return;
221
222 m_adjust->value = fpos;
223
07f5b19a 224 GtkDisableEvents();
738f9e5a 225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
07f5b19a 226 GtkEnableEvents();
738f9e5a
RR
227}
228
229void wxSpinCtrl::SetRange(int minVal, int maxVal)
230{
231 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
232
233 float fmin = (float)minVal;
234 float fmax = (float)maxVal;
235
236 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
237 (fabs(fmax-m_adjust->upper) < sensitivity))
238 {
239 return;
240 }
241
242 m_adjust->lower = fmin;
243 m_adjust->upper = fmax;
244
245 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 246
738f9e5a
RR
247 // these two calls are required due to some bug in GTK
248 Refresh();
249 SetFocus();
250}
251
da048e3d
RR
252void wxSpinCtrl::OnChar( wxKeyEvent &event )
253{
254 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
255
256 if (event.KeyCode() == WXK_RETURN)
257 {
258 wxWindow *top_frame = m_parent;
f6bcfd97 259 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
da048e3d 260 top_frame = top_frame->GetParent();
0e0d8857 261
fa8a793a 262 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 263 {
fa8a793a
VZ
264 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
265 if ( window )
266 {
267 GtkWidget *widgetDef = window->default_widget;
268
269 if ( widgetDef && GTK_IS_WINDOW(widgetDef) )
270 {
271 gtk_widget_activate(widgetDef);
272 return;
273 }
274 }
9750fc42 275 }
da048e3d
RR
276 }
277
278 event.Skip();
279}
280
738f9e5a
RR
281bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
282{
283 return GTK_SPIN_BUTTON(m_widget)->panel == window;
284}
285
286void wxSpinCtrl::ApplyWidgetStyle()
287{
288 SetWidgetStyle();
289 gtk_widget_set_style( m_widget, m_widgetStyle );
290}
291
9d9b7755
VZ
292wxSize wxSpinCtrl::DoGetBestSize() const
293{
0279e844
RR
294 wxSize ret( wxControl::DoGetBestSize() );
295 return wxSize(95, ret.y);
9d9b7755
VZ
296}
297
738f9e5a 298#endif
aec0ed2e 299 // wxUSE_SPINCTRL