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