[ 1793012 ] wxSpinCtrl::SetValue() doesn't update internal counter
[wxWidgets.git] / src / gtk / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 // 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/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
20 #include "wx/utils.h"
21 #include "wx/wxcrtvararg.h"
22 #endif
23
24 #include "wx/gtk/private.h"
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 extern bool g_blockEventsOnDrag;
31
32 //-----------------------------------------------------------------------------
33 // "value_changed"
34 //-----------------------------------------------------------------------------
35
36 extern "C" {
37 static void
38 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
39 {
40 win->m_pos = int(gtk_spin_button_get_value(spinbutton));
41 if (!win->m_hasVMT || g_blockEventsOnDrag || win->m_blockScrollEvent)
42 return;
43
44 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
45 event.SetEventObject( win );
46
47 // note that we don't use wxSpinCtrl::GetValue() here because it would
48 // adjust the value to fit into the control range and this means that we
49 // would never be able to enter an "invalid" value in the control, even
50 // temporarily - and trying to enter 10 into the control which accepts the
51 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
52 // enter 1!) (VZ)
53 event.SetInt(win->m_pos);
54 win->GetEventHandler()->ProcessEvent( event );
55 }
56 }
57
58 //-----------------------------------------------------------------------------
59 // "changed"
60 //-----------------------------------------------------------------------------
61
62 extern "C" {
63 static void
64 gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
65 {
66 if (!win->m_hasVMT || win->m_blockScrollEvent)
67 return;
68
69 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
70 event.SetEventObject( win );
71 event.SetString( GTK_ENTRY(spinbutton)->text );
72
73 // see above
74 event.SetInt(win->m_pos);
75 win->GetEventHandler()->ProcessEvent( event );
76 }
77 }
78
79 //-----------------------------------------------------------------------------
80 // wxSpinCtrl
81 //-----------------------------------------------------------------------------
82
83 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
84
85 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
86 EVT_CHAR(wxSpinCtrl::OnChar)
87 END_EVENT_TABLE()
88
89 wxSpinCtrl::wxSpinCtrl()
90 {
91 m_pos = 0;
92 }
93
94 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
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)
100 {
101 if (!PreCreation( parent, pos, size ) ||
102 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
103 {
104 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
105 return false;
106 }
107
108 m_widget = gtk_spin_button_new_with_range(min, max, 1);
109 gtk_spin_button_set_value((GtkSpinButton*)m_widget, initial);
110 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
111
112 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
113 (int)(m_windowStyle & wxSP_WRAP) );
114
115 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
116 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
117
118 m_parent->DoAddChild( this );
119
120 PostCreation(size);
121
122 if (!value.empty())
123 {
124 SetValue(value);
125 }
126
127 return true;
128 }
129
130 int wxSpinCtrl::GetMin() const
131 {
132 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
133
134 double min;
135 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
136 return int(min);
137 }
138
139 int wxSpinCtrl::GetMax() const
140 {
141 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
142
143 double max;
144 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
145 return int(max);
146 }
147
148 int wxSpinCtrl::GetValue() const
149 {
150 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
151
152 GtkDisableEvents();
153 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
154 GtkEnableEvents();
155
156 return m_pos;
157 }
158
159 void wxSpinCtrl::SetValue( const wxString& value )
160 {
161 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
162
163 int n;
164 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
165 {
166 // a number - set it
167 SetValue(n);
168 }
169 else
170 {
171 // invalid number - set text as is (wxMSW compatible)
172 GtkDisableEvents();
173 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
174 GtkEnableEvents();
175 }
176 }
177
178 void wxSpinCtrl::SetValue( int value )
179 {
180 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
181
182 GtkDisableEvents();
183 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
184 m_pos = value;
185 GtkEnableEvents();
186 }
187
188 void wxSpinCtrl::SetSelection(long from, long to)
189 {
190 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
191 // entire range
192 if ( from == -1 && to == -1 )
193 {
194 from = 0;
195 to = INT_MAX;
196 }
197
198 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
199 }
200
201 void wxSpinCtrl::SetRange(int minVal, int maxVal)
202 {
203 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
204
205 GtkDisableEvents();
206 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
207 GtkEnableEvents();
208 }
209
210
211 void wxSpinCtrl::GtkDisableEvents() const
212 {
213 g_signal_handlers_block_by_func( m_widget,
214 (gpointer)gtk_value_changed, (void*) this);
215
216 g_signal_handlers_block_by_func(m_widget,
217 (gpointer)gtk_changed, (void*) this);
218 }
219
220 void wxSpinCtrl::GtkEnableEvents() const
221 {
222 g_signal_handlers_unblock_by_func(m_widget,
223 (gpointer)gtk_value_changed, (void*) this);
224
225 g_signal_handlers_unblock_by_func(m_widget,
226 (gpointer)gtk_changed, (void*) this);
227 }
228
229 void wxSpinCtrl::OnChar( wxKeyEvent &event )
230 {
231 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
232
233 if (event.GetKeyCode() == WXK_RETURN)
234 {
235 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
236
237 if ( GTK_IS_WINDOW(top_frame->m_widget) )
238 {
239 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
240 if ( window )
241 {
242 GtkWidget *widgetDef = window->default_widget;
243
244 if ( widgetDef )
245 {
246 gtk_widget_activate(widgetDef);
247 return;
248 }
249 }
250 }
251 }
252
253 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
254 {
255 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
256 evt.SetEventObject(this);
257 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
258 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
259 evt.SetString( val );
260 if (GetEventHandler()->ProcessEvent(evt)) return;
261 }
262
263 event.Skip();
264 }
265
266 GdkWindow *wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const
267 {
268 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
269
270 windows.push_back(spinbutton->entry.text_area);
271 windows.push_back(spinbutton->panel);
272
273 return NULL;
274 }
275
276 wxSize wxSpinCtrl::DoGetBestSize() const
277 {
278 wxSize ret( wxControl::DoGetBestSize() );
279 wxSize best(95, ret.y); // FIXME: 95?
280 CacheBestSize(best);
281 return best;
282 }
283
284 // static
285 wxVisualAttributes
286 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
287 {
288 // TODO: overload to accept functions like gtk_spin_button_new?
289 // Until then use a similar type
290 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
291 }
292
293 #endif
294 // wxUSE_SPINCTRL