]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/spinctrl.cpp
Improve wxMenu docs
[wxWidgets.git] / src / gtk / spinctrl.cpp
... / ...
CommitLineData
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
30extern bool g_blockEventsOnDrag;
31
32//-----------------------------------------------------------------------------
33// "value_changed"
34//-----------------------------------------------------------------------------
35
36extern "C" {
37static void
38gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win)
39{
40 win->m_value = gtk_spin_button_get_value(spinbutton);
41 if (!win->m_hasVMT || g_blockEventsOnDrag)
42 return;
43
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)
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 }
67}
68}
69
70//-----------------------------------------------------------------------------
71// "changed"
72//-----------------------------------------------------------------------------
73
74extern "C" {
75static void
76gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
77{
78 if (!win->m_hasVMT)
79 return;
80
81 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
82 event.SetEventObject( win );
83 event.SetString( GTK_ENTRY(spinbutton)->text );
84
85 // see above
86 event.SetInt((int)win->m_value);
87 win->HandleWindowEvent( event );
88}
89}
90
91//-----------------------------------------------------------------------------
92// wxSpinCtrlGTKBase
93//-----------------------------------------------------------------------------
94
95IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase)
96
97BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase)
98 EVT_CHAR(wxSpinCtrlGTKBase::OnChar)
99END_EVENT_TABLE()
100
101bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id,
102 const wxString& value,
103 const wxPoint& pos, const wxSize& size,
104 long style,
105 double min, double max, double initial, double inc,
106 const wxString& name)
107{
108 if (!PreCreation( parent, pos, size ) ||
109 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
110 {
111 wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") );
112 return false;
113 }
114
115 m_widget = gtk_spin_button_new_with_range(min, max, inc);
116
117 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
118 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
119
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
121 (int)(m_windowStyle & wxSP_WRAP) );
122
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);
125
126 m_parent->DoAddChild( this );
127
128 PostCreation(size);
129
130 if (!value.empty())
131 {
132 SetValue(value);
133 }
134
135 return true;
136}
137
138double wxSpinCtrlGTKBase::DoGetValue() const
139{
140 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
141
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;
156 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
157 return min;
158}
159
160double wxSpinCtrlGTKBase::DoGetMax() const
161{
162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
163
164 double max = 0;
165 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
166 return max;
167}
168
169double wxSpinCtrlGTKBase::DoGetIncrement() const
170{
171 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
172
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" );
181
182 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) );
183}
184
185void wxSpinCtrlGTKBase::SetValue( const wxString& value )
186{
187 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
188
189 double n;
190 if ( wxSscanf(value, "%lg", &n) == 1 )
191 {
192 // a number - set it, let DoSetValue round for int value
193 DoSetValue(n);
194 return;
195 }
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();
201}
202
203void wxSpinCtrlGTKBase::DoSetValue( double value )
204{
205 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
206
207 if (wxIsKindOf(this, wxSpinCtrl))
208 value = int(value + 0.5);
209
210 GtkDisableEvents();
211 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
212 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
213 GtkEnableEvents();
214}
215
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)
224{
225 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
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
236void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
237{
238 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
239
240 GtkDisableEvents();
241 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
242 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
243 GtkEnableEvents();
244}
245
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}
255
256void wxSpinCtrlGTKBase::GtkDisableEvents() const
257{
258 g_signal_handlers_block_by_func( m_widget,
259 (gpointer)gtk_value_changed, (void*) this);
260
261 g_signal_handlers_block_by_func(m_widget,
262 (gpointer)gtk_changed, (void*) this);
263}
264
265void wxSpinCtrlGTKBase::GtkEnableEvents() const
266{
267 g_signal_handlers_unblock_by_func(m_widget,
268 (gpointer)gtk_value_changed, (void*) this);
269
270 g_signal_handlers_unblock_by_func(m_widget,
271 (gpointer)gtk_changed, (void*) this);
272}
273
274void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
275{
276 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
277
278 if (event.GetKeyCode() == WXK_RETURN)
279 {
280 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
281
282 if ( GTK_IS_WINDOW(top_frame->m_widget) )
283 {
284 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
285 if ( window )
286 {
287 GtkWidget *widgetDef = window->default_widget;
288
289 if ( widgetDef )
290 {
291 gtk_widget_activate(widgetDef);
292 return;
293 }
294 }
295 }
296 }
297
298 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
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 );
305 if (HandleWindowEvent(evt)) return;
306 }
307
308 event.Skip();
309}
310
311GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
312{
313 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
314
315 windows.push_back(spinbutton->entry.text_area);
316 windows.push_back(spinbutton->panel);
317
318 return NULL;
319}
320
321wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
322{
323 wxSize ret( wxControl::DoGetBestSize() );
324 wxSize best(95, ret.y); // FIXME: 95?
325 CacheBestSize(best);
326 return best;
327}
328
329// static
330wxVisualAttributes
331wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
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
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