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