]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinctrl.cpp
Change required for new event types.
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
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
17 #if wxUSE_SPINCTRL
18
19 #include "wx/utils.h"
20
21 #include <math.h>
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 static const float sensitivity = 0.02;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40
41 //-----------------------------------------------------------------------------
42 // "value_changed"
43 //-----------------------------------------------------------------------------
44
45 static 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
52 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
53 event.SetEventObject( win );
54 event.SetInt( win->GetValue() );
55 win->GetEventHandler()->ProcessEvent( event );
56 }
57
58 //-----------------------------------------------------------------------------
59 // "changed"
60 //-----------------------------------------------------------------------------
61
62 static void
63 gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
64 {
65 if (!win->m_hasVMT) return;
66
67 if (g_isIdle)
68 wxapp_install_idle_handler();
69
70 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
71 event.SetEventObject( win );
72 event.SetInt( win->GetValue() );
73 win->GetEventHandler()->ProcessEvent( event );
74 }
75
76 //-----------------------------------------------------------------------------
77 // wxSpinCtrl
78 //-----------------------------------------------------------------------------
79
80 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
81
82 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
83 EVT_CHAR(wxSpinCtrl::OnChar)
84 END_EVENT_TABLE()
85
86 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
87 const wxString& value,
88 const wxPoint& pos, const wxSize& size,
89 long style,
90 int min, int max, int initial,
91 const wxString& name)
92 {
93 m_needParent = TRUE;
94 m_acceptsFocus = TRUE;
95
96 if (!PreCreation( parent, pos, size ) ||
97 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
98 {
99 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
100 return FALSE;
101 }
102
103 m_oldPos = initial;
104
105 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
106
107 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
108
109 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
110 (int)(m_windowStyle & wxSP_WRAP) );
111
112 GtkEnableEvents();
113
114 m_parent->DoAddChild( this );
115
116 PostCreation();
117
118 SetFont( parent->GetFont() );
119
120 wxSize size_best( DoGetBestSize() );
121 wxSize new_size( size );
122 if (new_size.x == -1)
123 new_size.x = size_best.x;
124 if (new_size.y == -1)
125 new_size.y = size_best.y;
126 if (new_size.y > size_best.y)
127 new_size.y = size_best.y;
128 if ((new_size.x != size.x) || (new_size.y != size.y))
129 SetSize( new_size.x, new_size.y );
130
131 SetBackgroundColour( parent->GetBackgroundColour() );
132
133 SetValue( value );
134
135 Show( TRUE );
136
137 return TRUE;
138 }
139
140 void wxSpinCtrl::GtkDisableEvents()
141 {
142 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
143 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
144 (gpointer) this );
145
146 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
147 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
148 (gpointer) this );
149 }
150
151 void wxSpinCtrl::GtkEnableEvents()
152 {
153 gtk_signal_connect( GTK_OBJECT (m_adjust),
154 "value_changed",
155 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
156 (gpointer) this );
157
158 gtk_signal_connect( GTK_OBJECT(m_widget),
159 "changed",
160 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
161 (gpointer)this);
162 }
163
164 int wxSpinCtrl::GetMin() const
165 {
166 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
167
168 return (int)ceil(m_adjust->lower);
169 }
170
171 int wxSpinCtrl::GetMax() const
172 {
173 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
174
175 return (int)ceil(m_adjust->upper);
176 }
177
178 int wxSpinCtrl::GetValue() const
179 {
180 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
181
182 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
183
184 return (int)ceil(m_adjust->value);
185 }
186
187 void wxSpinCtrl::SetValue( const wxString& value )
188 {
189 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
190
191 int n;
192 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
193 {
194 // a number - set it
195 SetValue(n);
196 }
197 else
198 {
199 // invalid number - set text as is (wxMSW compatible)
200 GtkDisableEvents();
201 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
202 GtkEnableEvents();
203 }
204 }
205
206 void wxSpinCtrl::SetValue( int value )
207 {
208 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
209
210 float fpos = (float)value;
211 m_oldPos = fpos;
212 if (fabs(fpos-m_adjust->value) < sensitivity) return;
213
214 m_adjust->value = fpos;
215
216 GtkDisableEvents();
217 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
218 GtkEnableEvents();
219 }
220
221 void 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
237 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
238
239 // these two calls are required due to some bug in GTK
240 Refresh();
241 SetFocus();
242 }
243
244 void wxSpinCtrl::OnChar( wxKeyEvent &event )
245 {
246 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
247
248 if (event.KeyCode() == WXK_RETURN)
249 {
250 wxWindow *top_frame = m_parent;
251 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
252 top_frame = top_frame->GetParent();
253 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
254
255 if (window->default_widget)
256 {
257 gtk_widget_activate (window->default_widget);
258 return;
259 }
260 }
261
262 event.Skip();
263 }
264
265 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
266 {
267 return GTK_SPIN_BUTTON(m_widget)->panel == window;
268 }
269
270 void wxSpinCtrl::ApplyWidgetStyle()
271 {
272 SetWidgetStyle();
273 gtk_widget_set_style( m_widget, m_widgetStyle );
274 }
275
276 wxSize wxSpinCtrl::DoGetBestSize() const
277 {
278 wxSize ret( wxControl::DoGetBestSize() );
279 return wxSize(95, ret.y);
280 }
281
282 #endif
283 // wxUSE_SPINCTRL