]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinctrl.cpp
Fixed a bug so wxLC_VRULES works by itself.
[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 "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
23 #include <math.h>
24
25 #include <gdk/gdk.h>
26 #include <gtk/gtk.h>
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 static const float sensitivity = 0.02;
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern bool g_blockEventsOnDrag;
42
43 //-----------------------------------------------------------------------------
44 // "value_changed"
45 //-----------------------------------------------------------------------------
46
47 static 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
54 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
55 event.SetEventObject( win );
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) );
64 win->GetEventHandler()->ProcessEvent( event );
65 }
66
67 //-----------------------------------------------------------------------------
68 // "changed"
69 //-----------------------------------------------------------------------------
70
71 static void
72 gtk_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
85 //-----------------------------------------------------------------------------
86 // wxSpinCtrl
87 //-----------------------------------------------------------------------------
88
89 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
90
91 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
92 EVT_CHAR(wxSpinCtrl::OnChar)
93 END_EVENT_TABLE()
94
95 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
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)
101 {
102 m_needParent = TRUE;
103 m_acceptsFocus = TRUE;
104
105 if (!PreCreation( parent, pos, size ) ||
106 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
107 {
108 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
109 return FALSE;
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 );
117
118 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
119 (int)(m_windowStyle & wxSP_WRAP) );
120
121 GtkEnableEvents();
122
123 m_parent->DoAddChild( this );
124
125 PostCreation();
126
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;
135 if (new_size.y > size_best.y)
136 new_size.y = size_best.y;
137 if ((new_size.x != size.x) || (new_size.y != size.y))
138 SetSize( new_size.x, new_size.y );
139
140 SetBackgroundColour( parent->GetBackgroundColour() );
141
142 SetValue( value );
143
144 Show( TRUE );
145
146 return TRUE;
147 }
148
149 void wxSpinCtrl::GtkDisableEvents()
150 {
151 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
152 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
153 (gpointer) this );
154
155 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
156 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
157 (gpointer) this );
158 }
159
160 void wxSpinCtrl::GtkEnableEvents()
161 {
162 gtk_signal_connect( GTK_OBJECT (m_adjust),
163 "value_changed",
164 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
165 (gpointer) this );
166
167 gtk_signal_connect( GTK_OBJECT(m_widget),
168 "changed",
169 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
170 (gpointer)this);
171 }
172
173 int 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
180 int 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
187 int wxSpinCtrl::GetValue() const
188 {
189 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
190
191 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
192
193 return (int)ceil(m_adjust->value);
194 }
195
196 void 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)
209 GtkDisableEvents();
210 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
211 GtkEnableEvents();
212 }
213 }
214
215 void 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
225 GtkDisableEvents();
226 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
227 GtkEnableEvents();
228 }
229
230 void 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" );
247
248 // these two calls are required due to some bug in GTK
249 Refresh();
250 SetFocus();
251 }
252
253 void 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;
260 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
261 top_frame = top_frame->GetParent();
262 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
263
264 if (window->default_widget)
265 {
266 gtk_widget_activate (window->default_widget);
267 return;
268 }
269 }
270
271 event.Skip();
272 }
273
274 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
275 {
276 return GTK_SPIN_BUTTON(m_widget)->panel == window;
277 }
278
279 void wxSpinCtrl::ApplyWidgetStyle()
280 {
281 SetWidgetStyle();
282 gtk_widget_set_style( m_widget, m_widgetStyle );
283 }
284
285 wxSize wxSpinCtrl::DoGetBestSize() const
286 {
287 wxSize ret( wxControl::DoGetBestSize() );
288 return wxSize(95, ret.y);
289 }
290
291 #endif
292 // wxUSE_SPINCTRL