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