Merge in from trunk r68684 - r69046
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/spinctrl.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 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_SPINCTRL
15
16 #include "wx/spinctrl.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/utils.h"
20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
21 #include "wx/math.h"
22 #include "wx/crt.h"
23 #endif
24
25 #include "wx/gtk1/private.h"
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 static const float sensitivity = 0.02;
35
36 //-----------------------------------------------------------------------------
37 // data
38 //-----------------------------------------------------------------------------
39
40 extern bool g_blockEventsOnDrag;
41
42 //-----------------------------------------------------------------------------
43 // "value_changed"
44 //-----------------------------------------------------------------------------
45
46 extern "C" {
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->HandleWindowEvent( event );
65 }
66 }
67
68 //-----------------------------------------------------------------------------
69 // "changed"
70 //-----------------------------------------------------------------------------
71
72 extern "C" {
73 static void
74 gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
75 {
76 if (!win->m_hasVMT) return;
77
78 if (g_isIdle)
79 wxapp_install_idle_handler();
80
81 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
82 event.SetEventObject( win );
83
84 // see above
85 event.SetInt( (int)ceil(win->m_adjust->value) );
86 win->HandleWindowEvent( event );
87 }
88 }
89
90 //-----------------------------------------------------------------------------
91 // wxSpinCtrl
92 //-----------------------------------------------------------------------------
93
94 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
95 EVT_CHAR(wxSpinCtrl::OnChar)
96 END_EVENT_TABLE()
97
98 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
99 const wxString& value,
100 const wxPoint& pos, const wxSize& size,
101 long style,
102 int min, int max, int initial,
103 const wxString& name)
104 {
105 m_needParent = true;
106 m_acceptsFocus = true;
107
108 if (!PreCreation( parent, pos, size ) ||
109 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
110 {
111 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
112 return false;
113 }
114
115 m_oldPos = initial;
116
117 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
118
119 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
120
121 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
122 (int)(m_windowStyle & wxSP_WRAP) );
123
124 GtkEnableEvents();
125
126 m_parent->DoAddChild( this );
127
128 PostCreation(size);
129
130 SetValue( value );
131
132 return true;
133 }
134
135 void wxSpinCtrl::GtkDisableEvents()
136 {
137 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
138 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
139 (gpointer) this );
140
141 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
142 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
143 (gpointer) this );
144 }
145
146 void wxSpinCtrl::GtkEnableEvents()
147 {
148 gtk_signal_connect( GTK_OBJECT (m_adjust),
149 "value_changed",
150 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
151 (gpointer) this );
152
153 gtk_signal_connect( GTK_OBJECT(m_widget),
154 "changed",
155 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
156 (gpointer)this);
157 }
158
159 int wxSpinCtrl::GetMin() const
160 {
161 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
162
163 return (int)ceil(m_adjust->lower);
164 }
165
166 int wxSpinCtrl::GetMax() const
167 {
168 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
169
170 return (int)ceil(m_adjust->upper);
171 }
172
173 int wxSpinCtrl::GetValue() const
174 {
175 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
176
177 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
178
179 return (int)ceil(m_adjust->value);
180 }
181
182 void wxSpinCtrl::SetValue( const wxString& value )
183 {
184 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
185
186 int n;
187 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
188 {
189 // a number - set it
190 SetValue(n);
191 }
192 else
193 {
194 // invalid number - set text as is (wxMSW compatible)
195 GtkDisableEvents();
196 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
197 GtkEnableEvents();
198 }
199 }
200
201 void wxSpinCtrl::SetValue( int value )
202 {
203 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
204
205 float fpos = (float)value;
206 m_oldPos = fpos;
207 if (fabs(fpos-m_adjust->value) < sensitivity) return;
208
209 m_adjust->value = fpos;
210
211 GtkDisableEvents();
212 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
213 GtkEnableEvents();
214 }
215
216 void wxSpinCtrl::SetSelection(long from, long to)
217 {
218 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
219 // entire range
220 if ( from == -1 && to == -1 )
221 {
222 from = 0;
223 to = INT_MAX;
224 }
225
226 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
227 }
228
229 void 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" );
246
247 // these two calls are required due to some bug in GTK
248 Refresh();
249 SetFocus();
250 }
251
252 void wxSpinCtrl::OnChar( wxKeyEvent &event )
253 {
254 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
255
256 if (event.GetKeyCode() == WXK_RETURN)
257 {
258 wxWindow *top_frame = m_parent;
259 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
260 top_frame = top_frame->GetParent();
261
262 if ( GTK_IS_WINDOW(top_frame->m_widget) )
263 {
264 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
265 if ( window )
266 {
267 GtkWidget *widgetDef = window->default_widget;
268
269 if ( widgetDef )
270 {
271 gtk_widget_activate(widgetDef);
272 return;
273 }
274 }
275 }
276 }
277
278 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
279 {
280 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
281 evt.SetEventObject(this);
282 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
283 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
284 evt.SetString( val );
285 if (HandleWindowEvent(evt)) return;
286 }
287
288 event.Skip();
289 }
290
291 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
292 {
293 if (GTK_SPIN_BUTTON(m_widget)->entry.text_area == window) return true;
294
295 if (GTK_SPIN_BUTTON(m_widget)->panel == window) return true;
296
297 return false;
298 }
299
300 wxSize wxSpinCtrl::DoGetBestSize() const
301 {
302 wxSize ret( wxControl::DoGetBestSize() );
303 wxSize best(95, ret.y);
304 CacheBestSize(best);
305 return best;
306 }
307
308 // static
309 wxVisualAttributes
310 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
311 {
312 // TODO: overload to accept functions like gtk_spin_button_new?
313 // Until then use a similar type
314 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
315 }
316
317 #endif
318 // wxUSE_SPINCTRL