]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
updated setup.h for OpenVMS
[wxWidgets.git] / src / gtk / spinctrl.cpp
CommitLineData
738f9e5a 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/spinctrl.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
9dc44eff 24#include <gtk/gtk.h>
e1910715 25#include "wx/gtk/private.h"
9dc44eff 26#include "wx/gtk/private/gtk2-compat.h"
738f9e5a 27
738f9e5a
RR
28//-----------------------------------------------------------------------------
29// data
30//-----------------------------------------------------------------------------
31
32extern bool g_blockEventsOnDrag;
33
738f9e5a
RR
34//-----------------------------------------------------------------------------
35// "value_changed"
36//-----------------------------------------------------------------------------
37
865bb325 38extern "C" {
7f81dfa1 39static void
8cd6a9ad 40gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
738f9e5a 41{
00dc9772 42 if (!win->m_hasVMT || g_blockEventsOnDrag)
7f81dfa1 43 return;
738f9e5a 44
8cd6a9ad
VZ
45 if (wxIsKindOf(win, wxSpinCtrl))
46 {
47 wxSpinEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
48 event.SetEventObject( win );
845a6bbf
PC
49 event.SetPosition(static_cast<wxSpinCtrl*>(win)->GetValue());
50 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
8cd6a9ad
VZ
51 win->HandleWindowEvent( event );
52 }
53 else // wxIsKindOf(win, wxSpinCtrlDouble)
54 {
55 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, win->GetId());
56 event.SetEventObject( win );
845a6bbf
PC
57 event.SetValue(static_cast<wxSpinCtrlDouble*>(win)->GetValue());
58 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
8cd6a9ad
VZ
59 win->HandleWindowEvent( event );
60 }
738f9e5a 61}
865bb325 62}
738f9e5a 63
0a07a7d8
RR
64//-----------------------------------------------------------------------------
65// "changed"
66//-----------------------------------------------------------------------------
67
865bb325 68extern "C" {
0a07a7d8 69static void
7f81dfa1 70gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
0a07a7d8 71{
00dc9772 72 if (!win->m_hasVMT)
7f81dfa1
PC
73 return;
74
0a07a7d8
RR
75 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
76 event.SetEventObject( win );
845a6bbf
PC
77 event.SetString(gtk_entry_get_text(GTK_ENTRY(spinbutton)));
78 event.SetInt(win->GetValue());
937013e0 79 win->HandleWindowEvent( event );
0a07a7d8 80}
865bb325 81}
0a07a7d8 82
738f9e5a 83//-----------------------------------------------------------------------------
8cd6a9ad 84// wxSpinCtrlGTKBase
738f9e5a
RR
85//-----------------------------------------------------------------------------
86
8cd6a9ad 87IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase)
738f9e5a 88
8cd6a9ad
VZ
89BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
90 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
da048e3d
RR
91END_EVENT_TABLE()
92
8cd6a9ad 93bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
94 const wxString& value,
95 const wxPoint& pos, const wxSize& size,
96 long style,
8cd6a9ad 97 double min, double max, double initial, double inc,
ce89fdd2 98 const wxString& name)
738f9e5a 99{
0279e844
RR
100 if (!PreCreation( parent, pos, size ) ||
101 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a 102 {
8cd6a9ad 103 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
8e13c1ec 104 return false;
738f9e5a
RR
105 }
106
8cd6a9ad 107 m_widget = gtk_spin_button_new_with_range(min, max, inc);
9ff9d30c 108 g_object_ref(m_widget);
8cd6a9ad 109
4a783bb4 110 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
3d257b8d 111
7e4952db 112 gfloat align;
f1ddb476 113 if ( HasFlag(wxALIGN_RIGHT) )
7e4952db 114 align = 1.0;
f1ddb476 115 else if ( HasFlag(wxALIGN_CENTRE) )
7e4952db
VZ
116 align = 0.5;
117 else
118 align = 0.0;
119
120 gtk_entry_set_alignment(GTK_ENTRY(m_widget), align);
121
b02da6b1
VZ
122 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
123 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 124
0bf36922
RR
125 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
126 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
3d257b8d 127
738f9e5a
RR
128 m_parent->DoAddChild( this );
129
abdeb9e7 130 PostCreation(size);
db434467 131
7f81dfa1
PC
132 if (!value.empty())
133 {
134 SetValue(value);
135 }
ce89fdd2 136
8e13c1ec 137 return true;
738f9e5a
RR
138}
139
8cd6a9ad 140double wxSpinCtrlGTKBase::DoGetValue() const
738f9e5a
RR
141{
142 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
143
845a6bbf
PC
144 // Get value directly from current control text, just as
145 // gtk_spin_button_update() would do. Calling gtk_spin_button_update() causes
146 // a redraw, which causes an idle event, so if GetValue() is called from
147 // a UI update handler, you get a never ending sequence of idle events. It
148 // also forces the text into valid range, which wxMSW GetValue() does not do.
149 static unsigned sig_id;
150 if (sig_id == 0)
151 sig_id = g_signal_lookup("input", GTK_TYPE_SPIN_BUTTON);
152 double value;
153 int handled = 0;
154 g_signal_emit(m_widget, sig_id, 0, &value, &handled);
155 if (!handled)
156 value = g_strtod(gtk_entry_get_text(GTK_ENTRY(m_widget)), NULL);
385e8575 157 GtkAdjustment* adj =
845a6bbf 158 gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(m_widget));
385e8575
PC
159 const double lower = gtk_adjustment_get_lower(adj);
160 const double upper = gtk_adjustment_get_upper(adj);
161 if (value < lower)
162 value = lower;
163 else if (value > upper)
164 value = upper;
845a6bbf
PC
165
166 return value;
8cd6a9ad
VZ
167}
168
169double wxSpinCtrlGTKBase::DoGetMin() const
170{
171 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
172
173 double min = 0;
4a783bb4 174 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
8cd6a9ad 175 return min;
738f9e5a
RR
176}
177
8cd6a9ad 178double wxSpinCtrlGTKBase::DoGetMax() const
738f9e5a
RR
179{
180 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
181
8cd6a9ad 182 double max = 0;
4a783bb4 183 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
8cd6a9ad 184 return max;
738f9e5a
RR
185}
186
8cd6a9ad 187double wxSpinCtrlGTKBase::DoGetIncrement() const
738f9e5a
RR
188{
189 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
190
8cd6a9ad 191 double inc = 0;
63fb6907 192 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), &inc, NULL);
8cd6a9ad
VZ
193 return inc;
194}
195
196bool wxSpinCtrlGTKBase::GetSnapToTicks() const
197{
845a6bbf 198 wxCHECK_MSG(m_widget, false, "invalid spin button");
33720b2d 199
d5027818 200 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ) != 0;
738f9e5a
RR
201}
202
8cd6a9ad 203void wxSpinCtrlGTKBase::SetValue( const wxString& value )
ce89fdd2
VZ
204{
205 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
206
8cd6a9ad
VZ
207 double n;
208 if ( wxSscanf(value, "%lg", &n) == 1 )
ce89fdd2 209 {
8cd6a9ad
VZ
210 // a number - set it, let DoSetValue round for int value
211 DoSetValue(n);
212 return;
ce89fdd2 213 }
8cd6a9ad
VZ
214
215 // invalid number - set text as is (wxMSW compatible)
216 GtkDisableEvents();
217 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
218 GtkEnableEvents();
ce89fdd2
VZ
219}
220
8cd6a9ad 221void wxSpinCtrlGTKBase::DoSetValue( double value )
738f9e5a
RR
222{
223 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
224
0bf36922 225 GtkDisableEvents();
4a783bb4 226 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
0bf36922 227 GtkEnableEvents();
738f9e5a
RR
228}
229
8cd6a9ad
VZ
230void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
231{
232 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
233
234 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
235}
236
237void wxSpinCtrlGTKBase::SetSelection(long from, long to)
f8f9ec55 238{
77ffb593 239 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
240 // entire range
241 if ( from == -1 && to == -1 )
242 {
243 from = 0;
244 to = INT_MAX;
245 }
246
247 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
248}
249
8cd6a9ad 250void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
738f9e5a
RR
251{
252 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
253
0bf36922 254 GtkDisableEvents();
4a783bb4 255 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
0bf36922
RR
256 GtkEnableEvents();
257}
258
8cd6a9ad
VZ
259void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
260{
261 wxCHECK_RET( m_widget, "invalid spin button" );
262
263 GtkDisableEvents();
c85a7996
VZ
264
265 // Preserve the old page value when changing just the increment.
266 double page = 10*inc;
267 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &page);
268
269 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, page);
8cd6a9ad
VZ
270 GtkEnableEvents();
271}
0bf36922 272
8cd6a9ad 273void wxSpinCtrlGTKBase::GtkDisableEvents() const
0bf36922
RR
274{
275 g_signal_handlers_block_by_func( m_widget,
276 (gpointer)gtk_value_changed, (void*) this);
00dc9772 277
0bf36922
RR
278 g_signal_handlers_block_by_func(m_widget,
279 (gpointer)gtk_changed, (void*) this);
280}
281
8cd6a9ad 282void wxSpinCtrlGTKBase::GtkEnableEvents() const
0bf36922
RR
283{
284 g_signal_handlers_unblock_by_func(m_widget,
285 (gpointer)gtk_value_changed, (void*) this);
00dc9772 286
0bf36922
RR
287 g_signal_handlers_unblock_by_func(m_widget,
288 (gpointer)gtk_changed, (void*) this);
738f9e5a
RR
289}
290
8cd6a9ad 291void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
da048e3d
RR
292{
293 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
294
12a3f227 295 if (event.GetKeyCode() == WXK_RETURN)
da048e3d 296 {
7f81dfa1 297 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
0e0d8857 298
fa8a793a 299 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 300 {
fa8a793a
VZ
301 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
302 if ( window )
303 {
385e8575 304 GtkWidget* widgetDef = gtk_window_get_default_widget(window);
fa8a793a 305
055e633d 306 if ( widgetDef )
fa8a793a
VZ
307 {
308 gtk_widget_activate(widgetDef);
309 return;
310 }
311 }
9750fc42 312 }
da048e3d
RR
313 }
314
8e13c1ec 315 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2
RR
316 {
317 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
318 evt.SetEventObject(this);
319 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
320 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
321 evt.SetString( val );
937013e0 322 if (HandleWindowEvent(evt)) return;
4a11cca2
RR
323 }
324
da048e3d
RR
325 event.Skip();
326}
327
8cd6a9ad 328GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
738f9e5a 329{
9dc44eff
PC
330#ifdef __WXGTK3__
331 // no access to internal GdkWindows
332 wxUnusedVar(windows);
333#else
7f81dfa1 334 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
ef5c70f9
VZ
335
336 windows.push_back(spinbutton->entry.text_area);
337 windows.push_back(spinbutton->panel);
9dc44eff 338#endif
ef5c70f9
VZ
339
340 return NULL;
738f9e5a
RR
341}
342
8cd6a9ad 343wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
9d9b7755 344{
0279e844 345 wxSize ret( wxControl::DoGetBestSize() );
ef5c70f9 346 wxSize best(95, ret.y); // FIXME: 95?
9f884528
RD
347 CacheBestSize(best);
348 return best;
9d9b7755
VZ
349}
350
9d522606
RD
351// static
352wxVisualAttributes
8cd6a9ad 353wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
9d522606
RD
354{
355 // TODO: overload to accept functions like gtk_spin_button_new?
356 // Until then use a similar type
357 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
358}
359
8cd6a9ad
VZ
360//-----------------------------------------------------------------------------
361// wxSpinCtrl
362//-----------------------------------------------------------------------------
363
8cd6a9ad
VZ
364//-----------------------------------------------------------------------------
365// wxSpinCtrlDouble
366//-----------------------------------------------------------------------------
367
368IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
369
370unsigned wxSpinCtrlDouble::GetDigits() const
371{
372 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
373
374 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
375}
376
377void wxSpinCtrlDouble::SetDigits(unsigned digits)
378{
379 wxCHECK_RET( m_widget, "invalid spin button" );
380
381 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
382}
383
384#endif // wxUSE_SPINCTRL