]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/spinctrl.cpp
refactor wxDocChildFrame and wxDocMDIChildFrame to use wxDocChildFrameAny intead...
[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( wxRound(win->m_value) ); // 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 g_object_ref(m_widget);
117
118 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial);
119 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
120
121 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
122 (int)(m_windowStyle & wxSP_WRAP) );
123
124 g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
125 g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this);
126
127 m_parent->DoAddChild( this );
128
129 PostCreation(size);
130
131 if (!value.empty())
132 {
133 SetValue(value);
134 }
135
136 return true;
137}
138
139double wxSpinCtrlGTKBase::DoGetValue() const
140{
141 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
142
143 GtkDisableEvents();
144 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
145 const_cast<wxSpinCtrlGTKBase*>(this)->m_value =
146 gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
147 GtkEnableEvents();
148
149 return m_value;
150}
151
152double wxSpinCtrlGTKBase::DoGetMin() const
153{
154 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
155
156 double min = 0;
157 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL);
158 return min;
159}
160
161double wxSpinCtrlGTKBase::DoGetMax() const
162{
163 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
164
165 double max = 0;
166 gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max);
167 return max;
168}
169
170double wxSpinCtrlGTKBase::DoGetIncrement() const
171{
172 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
173
174 double inc = 0;
175 gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &inc);
176 return inc;
177}
178
179bool wxSpinCtrlGTKBase::GetSnapToTicks() const
180{
181 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
182
183 return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) );
184}
185
186void wxSpinCtrlGTKBase::SetValue( const wxString& value )
187{
188 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
189
190 double n;
191 if ( wxSscanf(value, "%lg", &n) == 1 )
192 {
193 // a number - set it, let DoSetValue round for int value
194 DoSetValue(n);
195 return;
196 }
197
198 // invalid number - set text as is (wxMSW compatible)
199 GtkDisableEvents();
200 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
201 GtkEnableEvents();
202}
203
204void wxSpinCtrlGTKBase::DoSetValue( double value )
205{
206 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
207
208 if (wxIsKindOf(this, wxSpinCtrl))
209 value = wxRound( value );
210
211 GtkDisableEvents();
212 gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value);
213 m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget));
214 GtkEnableEvents();
215}
216
217void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks)
218{
219 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
220
221 gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks);
222}
223
224void wxSpinCtrlGTKBase::SetSelection(long from, long to)
225{
226 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
227 // entire range
228 if ( from == -1 && to == -1 )
229 {
230 from = 0;
231 to = INT_MAX;
232 }
233
234 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
235}
236
237void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal)
238{
239 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
240
241 GtkDisableEvents();
242 gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal);
243 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
244 GtkEnableEvents();
245}
246
247void wxSpinCtrlGTKBase::DoSetIncrement(double inc)
248{
249 wxCHECK_RET( m_widget, "invalid spin button" );
250
251 GtkDisableEvents();
252 gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, 10*inc);
253 m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget));
254 GtkEnableEvents();
255}
256
257void wxSpinCtrlGTKBase::GtkDisableEvents() const
258{
259 g_signal_handlers_block_by_func( m_widget,
260 (gpointer)gtk_value_changed, (void*) this);
261
262 g_signal_handlers_block_by_func(m_widget,
263 (gpointer)gtk_changed, (void*) this);
264}
265
266void wxSpinCtrlGTKBase::GtkEnableEvents() const
267{
268 g_signal_handlers_unblock_by_func(m_widget,
269 (gpointer)gtk_value_changed, (void*) this);
270
271 g_signal_handlers_unblock_by_func(m_widget,
272 (gpointer)gtk_changed, (void*) this);
273}
274
275void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event )
276{
277 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
278
279 if (event.GetKeyCode() == WXK_RETURN)
280 {
281 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
282
283 if ( GTK_IS_WINDOW(top_frame->m_widget) )
284 {
285 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
286 if ( window )
287 {
288 GtkWidget *widgetDef = window->default_widget;
289
290 if ( widgetDef )
291 {
292 gtk_widget_activate(widgetDef);
293 return;
294 }
295 }
296 }
297 }
298
299 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
300 {
301 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
302 evt.SetEventObject(this);
303 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
304 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
305 evt.SetString( val );
306 if (HandleWindowEvent(evt)) return;
307 }
308
309 event.Skip();
310}
311
312GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const
313{
314 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
315
316 windows.push_back(spinbutton->entry.text_area);
317 windows.push_back(spinbutton->panel);
318
319 return NULL;
320}
321
322wxSize wxSpinCtrlGTKBase::DoGetBestSize() const
323{
324 wxSize ret( wxControl::DoGetBestSize() );
325 wxSize best(95, ret.y); // FIXME: 95?
326 CacheBestSize(best);
327 return best;
328}
329
330// static
331wxVisualAttributes
332wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
333{
334 // TODO: overload to accept functions like gtk_spin_button_new?
335 // Until then use a similar type
336 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
337}
338
339//-----------------------------------------------------------------------------
340// wxSpinCtrl
341//-----------------------------------------------------------------------------
342
343IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGTKBase)
344
345//-----------------------------------------------------------------------------
346// wxSpinCtrlDouble
347//-----------------------------------------------------------------------------
348
349IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase)
350
351unsigned wxSpinCtrlDouble::GetDigits() const
352{
353 wxCHECK_MSG( m_widget, 0, "invalid spin button" );
354
355 return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) );
356}
357
358void wxSpinCtrlDouble::SetDigits(unsigned digits)
359{
360 wxCHECK_RET( m_widget, "invalid spin button" );
361
362 gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits);
363}
364
365#endif // wxUSE_SPINCTRL