]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
don't use deprecated toolbar API
[wxWidgets.git] / src / gtk / spinctrl.cpp
CommitLineData
738f9e5a 1/////////////////////////////////////////////////////////////////////////////
8e13c1ec 2// Name: src/gtk/spinbutt.cpp
738f9e5a
RR
3// Purpose: wxSpinCtrl
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
65571936 8// Licence: wxWindows licence
738f9e5a
RR
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
0e0d8857 14#if wxUSE_SPINCTRL
738f9e5a 15
de6185e2
WS
16#include "wx/spinctrl.h"
17
18#ifndef WX_PRECOMP
7f81dfa1 19 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
de6185e2 20 #include "wx/utils.h"
0cb7e05c 21 #include "wx/wxcrtvararg.h"
de6185e2 22#endif
aec0ed2e 23
e1910715 24#include "wx/gtk/private.h"
738f9e5a 25
738f9e5a
RR
26//-----------------------------------------------------------------------------
27// data
28//-----------------------------------------------------------------------------
29
30extern bool g_blockEventsOnDrag;
31
738f9e5a
RR
32//-----------------------------------------------------------------------------
33// "value_changed"
34//-----------------------------------------------------------------------------
35
865bb325 36extern "C" {
7f81dfa1 37static void
8cd6a9ad 38gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
738f9e5a 39{
8cd6a9ad 40 win->m_value = gtk_spin_button_get_value(spinbutton);
00dc9772 41 if (!win->m_hasVMT || g_blockEventsOnDrag)
7f81dfa1 42 return;
738f9e5a 43
4477936a
VZ
44 // note that we don't use wxSpinCtrl::GetValue() here because it would
45 // adjust the value to fit into the control range and this means that we
46 // would never be able to enter an "invalid" value in the control, even
47 // temporarily - and trying to enter 10 into the control which accepts the
48 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
49 // enter 1!) (VZ)
8cd6a9ad
VZ
50
51 if (wxIsKindOf(win, wxSpinCtrl))
52 {
53 wxSpinEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
54 event.SetEventObject( win );
55 event.SetPosition((int)(win->m_value + 0.5)); // FIXME should be SetValue
56 event.SetString(GTK_ENTRY(spinbutton)->text);
57 win->HandleWindowEvent( event );
58 }
59 else // wxIsKindOf(win, wxSpinCtrlDouble)
60 {
61 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, win->GetId());
62 event.SetEventObject( win );
63 event.SetValue(win->m_value);
64 event.SetString(GTK_ENTRY(spinbutton)->text);
65 win->HandleWindowEvent( event );
66 }
738f9e5a 67}
865bb325 68}
738f9e5a 69
0a07a7d8
RR
70//-----------------------------------------------------------------------------
71// "changed"
72//-----------------------------------------------------------------------------
73
865bb325 74extern "C" {
0a07a7d8 75static void
7f81dfa1 76gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
0a07a7d8 77{
00dc9772 78 if (!win->m_hasVMT)
7f81dfa1
PC
79 return;
80
0a07a7d8
RR
81 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
82 event.SetEventObject( win );
cf5d5057 83 event.SetString( GTK_ENTRY(spinbutton)->text );
3d257b8d 84
ea46eba0 85 // see above
8cd6a9ad 86 event.SetInt((int)win->m_value);
937013e0 87 win->HandleWindowEvent( event );
0a07a7d8 88}
865bb325 89}
0a07a7d8 90
738f9e5a 91//-----------------------------------------------------------------------------
8cd6a9ad 92// wxSpinCtrlGTKBase
738f9e5a
RR
93//-----------------------------------------------------------------------------
94
8cd6a9ad 95IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase)
738f9e5a 96
8cd6a9ad
VZ
97BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
98 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
da048e3d
RR
99END_EVENT_TABLE()
100
8cd6a9ad 101bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
102 const wxString& value,
103 const wxPoint& pos, const wxSize& size,
104 long style,
8cd6a9ad 105 double min, double max, double initial, double inc,
ce89fdd2 106 const wxString& name)
738f9e5a 107{
0279e844
RR
108 if (!PreCreation( parent, pos, size ) ||
109 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a 110 {
8cd6a9ad 111 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
8e13c1ec 112 return false;
738f9e5a
RR
113 }
114
8cd6a9ad
VZ
115 m_widget = gtk_spin_button_new_with_range(min, max, inc);
116
4a783bb4 117 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
8cd6a9ad 118 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
3d257b8d 119
b02da6b1
VZ
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
121 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 122
0bf36922
RR
123 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
124 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
3d257b8d 125
738f9e5a
RR
126 m_parent->DoAddChild( this );
127
abdeb9e7 128 PostCreation(size);
db434467 129
7f81dfa1
PC
130 if (!value.empty())
131 {
132 SetValue(value);
133 }
ce89fdd2 134
8e13c1ec 135 return true;
738f9e5a
RR
136}
137
8cd6a9ad 138double wxSpinCtrlGTKBase::DoGetValue() const
738f9e5a
RR
139{
140 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
141
8cd6a9ad
VZ
142 GtkDisableEvents();
143 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
144 wx_const_cast(wxSpinCtrlGTKBase*, this)->m_value =
145 gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
146 GtkEnableEvents();
147
148 return m_value;
149}
150
151double wxSpinCtrlGTKBase::DoGetMin() const
152{
153 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
154
155 double min = 0;
4a783bb4 156 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
8cd6a9ad 157 return min;
738f9e5a
RR
158}
159
8cd6a9ad 160double wxSpinCtrlGTKBase::DoGetMax() const
738f9e5a
RR
161{
162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
163
8cd6a9ad 164 double max = 0;
4a783bb4 165 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
8cd6a9ad 166 return max;
738f9e5a
RR
167}
168
8cd6a9ad 169double wxSpinCtrlGTKBase::DoGetIncrement() const
738f9e5a
RR
170{
171 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
172
8cd6a9ad
VZ
173 double inc = 0;
174 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &inc);
175 return inc;
176}
177
178bool wxSpinCtrlGTKBase::GetSnapToTicks() const
179{
180 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
33720b2d 181
8cd6a9ad 182 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) );
738f9e5a
RR
183}
184
8cd6a9ad 185void wxSpinCtrlGTKBase::SetValue( const wxString& value )
ce89fdd2
VZ
186{
187 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
188
8cd6a9ad
VZ
189 double n;
190 if ( wxSscanf(value, "%lg", &n) == 1 )
ce89fdd2 191 {
8cd6a9ad
VZ
192 // a number - set it, let DoSetValue round for int value
193 DoSetValue(n);
194 return;
ce89fdd2 195 }
8cd6a9ad
VZ
196
197 // invalid number - set text as is (wxMSW compatible)
198 GtkDisableEvents();
199 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
200 GtkEnableEvents();
ce89fdd2
VZ
201}
202
8cd6a9ad 203void wxSpinCtrlGTKBase::DoSetValue( double value )
738f9e5a
RR
204{
205 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
206
8cd6a9ad
VZ
207 if (wxIsKindOf(this, wxSpinCtrl))
208 value = int(value + 0.5);
209
0bf36922 210 GtkDisableEvents();
4a783bb4 211 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
8cd6a9ad 212 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
0bf36922 213 GtkEnableEvents();
738f9e5a
RR
214}
215
8cd6a9ad
VZ
216void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
217{
218 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
219
220 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
221}
222
223void wxSpinCtrlGTKBase::SetSelection(long from, long to)
f8f9ec55 224{
77ffb593 225 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
226 // entire range
227 if ( from == -1 && to == -1 )
228 {
229 from = 0;
230 to = INT_MAX;
231 }
232
233 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
234}
235
8cd6a9ad 236void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
738f9e5a
RR
237{
238 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
239
0bf36922 240 GtkDisableEvents();
4a783bb4 241 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
8cd6a9ad 242 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
0bf36922
RR
243 GtkEnableEvents();
244}
245
8cd6a9ad
VZ
246void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
247{
248 wxCHECK_RET( m_widget, "invalid spin button" );
249
250 GtkDisableEvents();
251 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, 10*inc);
252 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
253 GtkEnableEvents();
254}
0bf36922 255
8cd6a9ad 256void wxSpinCtrlGTKBase::GtkDisableEvents() const
0bf36922
RR
257{
258 g_signal_handlers_block_by_func( m_widget,
259 (gpointer)gtk_value_changed, (void*) this);
00dc9772 260
0bf36922
RR
261 g_signal_handlers_block_by_func(m_widget,
262 (gpointer)gtk_changed, (void*) this);
263}
264
8cd6a9ad 265void wxSpinCtrlGTKBase::GtkEnableEvents() const
0bf36922
RR
266{
267 g_signal_handlers_unblock_by_func(m_widget,
268 (gpointer)gtk_value_changed, (void*) this);
00dc9772 269
0bf36922
RR
270 g_signal_handlers_unblock_by_func(m_widget,
271 (gpointer)gtk_changed, (void*) this);
738f9e5a
RR
272}
273
8cd6a9ad 274void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
da048e3d
RR
275{
276 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
277
12a3f227 278 if (event.GetKeyCode() == WXK_RETURN)
da048e3d 279 {
7f81dfa1 280 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
0e0d8857 281
fa8a793a 282 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 283 {
fa8a793a
VZ
284 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
285 if ( window )
286 {
287 GtkWidget *widgetDef = window->default_widget;
288
055e633d 289 if ( widgetDef )
fa8a793a
VZ
290 {
291 gtk_widget_activate(widgetDef);
292 return;
293 }
294 }
9750fc42 295 }
da048e3d
RR
296 }
297
8e13c1ec 298 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2
RR
299 {
300 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
301 evt.SetEventObject(this);
302 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
303 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
304 evt.SetString( val );
937013e0 305 if (HandleWindowEvent(evt)) return;
4a11cca2
RR
306 }
307
da048e3d
RR
308 event.Skip();
309}
310
8cd6a9ad 311GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
738f9e5a 312{
7f81dfa1 313 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
ef5c70f9
VZ
314
315 windows.push_back(spinbutton->entry.text_area);
316 windows.push_back(spinbutton->panel);
317
318 return NULL;
738f9e5a
RR
319}
320
8cd6a9ad 321wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
9d9b7755 322{
0279e844 323 wxSize ret( wxControl::DoGetBestSize() );
ef5c70f9 324 wxSize best(95, ret.y); // FIXME: 95?
9f884528
RD
325 CacheBestSize(best);
326 return best;
9d9b7755
VZ
327}
328
9d522606
RD
329// static
330wxVisualAttributes
8cd6a9ad 331wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
9d522606
RD
332{
333 // TODO: overload to accept functions like gtk_spin_button_new?
334 // Until then use a similar type
335 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
336}
337
8cd6a9ad
VZ
338//-----------------------------------------------------------------------------
339// wxSpinCtrl
340//-----------------------------------------------------------------------------
341
342IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGTKBase)
343
344//-----------------------------------------------------------------------------
345// wxSpinCtrlDouble
346//-----------------------------------------------------------------------------
347
348IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
349
350unsigned wxSpinCtrlDouble::GetDigits() const
351{
352 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
353
354 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
355}
356
357void wxSpinCtrlDouble::SetDigits(unsigned digits)
358{
359 wxCHECK_RET( m_widget, "invalid spin button" );
360
361 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
362}
363
364#endif // wxUSE_SPINCTRL