]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinctrl.cpp
Merge in from trunk r64802 - r68625
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
CommitLineData
738f9e5a 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk1/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
19 #include "wx/utils.h"
fec9cc08 20 #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
18680f86 21 #include "wx/math.h"
de6185e2 22#endif
aec0ed2e 23
3cbab641 24#include "wx/gtk1/private.h"
738f9e5a
RR
25
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
0c623889
RR
33static const float sensitivity = 0.02;
34
738f9e5a
RR
35//-----------------------------------------------------------------------------
36// data
37//-----------------------------------------------------------------------------
38
39extern bool g_blockEventsOnDrag;
40
738f9e5a
RR
41//-----------------------------------------------------------------------------
42// "value_changed"
43//-----------------------------------------------------------------------------
44
865bb325 45extern "C" {
738f9e5a
RR
46static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
47{
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (!win->m_hasVMT) return;
51 if (g_blockEventsOnDrag) return;
52
58c837a4 53 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
738f9e5a 54 event.SetEventObject( win );
4477936a
VZ
55
56 // note that we don't use wxSpinCtrl::GetValue() here because it would
57 // adjust the value to fit into the control range and this means that we
58 // would never be able to enter an "invalid" value in the control, even
59 // temporarily - and trying to enter 10 into the control which accepts the
60 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
61 // enter 1!) (VZ)
62 event.SetInt( (int)ceil(win->m_adjust->value) );
937013e0 63 win->HandleWindowEvent( event );
738f9e5a 64}
865bb325 65}
738f9e5a 66
0a07a7d8
RR
67//-----------------------------------------------------------------------------
68// "changed"
69//-----------------------------------------------------------------------------
70
865bb325 71extern "C" {
0a07a7d8
RR
72static void
73gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
74{
75 if (!win->m_hasVMT) return;
76
77 if (g_isIdle)
78 wxapp_install_idle_handler();
79
80 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
81 event.SetEventObject( win );
3d257b8d 82
ea46eba0
RR
83 // see above
84 event.SetInt( (int)ceil(win->m_adjust->value) );
937013e0 85 win->HandleWindowEvent( event );
0a07a7d8 86}
865bb325 87}
0a07a7d8 88
738f9e5a
RR
89//-----------------------------------------------------------------------------
90// wxSpinCtrl
91//-----------------------------------------------------------------------------
92
da048e3d
RR
93BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
94 EVT_CHAR(wxSpinCtrl::OnChar)
95END_EVENT_TABLE()
96
738f9e5a 97bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
98 const wxString& value,
99 const wxPoint& pos, const wxSize& size,
100 long style,
101 int min, int max, int initial,
102 const wxString& name)
738f9e5a 103{
8e13c1ec
WS
104 m_needParent = true;
105 m_acceptsFocus = true;
738f9e5a 106
0279e844
RR
107 if (!PreCreation( parent, pos, size ) ||
108 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
109 {
110 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
8e13c1ec 111 return false;
738f9e5a
RR
112 }
113
114 m_oldPos = initial;
115
116 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
117
118 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
3d257b8d 119
b02da6b1
VZ
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
121 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 122
07f5b19a 123 GtkEnableEvents();
3d257b8d 124
738f9e5a
RR
125 m_parent->DoAddChild( this );
126
abdeb9e7 127 PostCreation(size);
db434467 128
ce89fdd2
VZ
129 SetValue( value );
130
8e13c1ec 131 return true;
738f9e5a
RR
132}
133
07f5b19a
RR
134void wxSpinCtrl::GtkDisableEvents()
135{
136 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
137 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
138 (gpointer) this );
139
0a07a7d8
RR
140 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
141 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
142 (gpointer) this );
07f5b19a
RR
143}
144
145void wxSpinCtrl::GtkEnableEvents()
146{
147 gtk_signal_connect( GTK_OBJECT (m_adjust),
148 "value_changed",
149 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
150 (gpointer) this );
3d257b8d 151
0a07a7d8
RR
152 gtk_signal_connect( GTK_OBJECT(m_widget),
153 "changed",
154 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
155 (gpointer)this);
07f5b19a
RR
156}
157
738f9e5a
RR
158int wxSpinCtrl::GetMin() const
159{
160 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
161
162 return (int)ceil(m_adjust->lower);
163}
164
165int wxSpinCtrl::GetMax() const
166{
167 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
168
169 return (int)ceil(m_adjust->upper);
170}
171
172int wxSpinCtrl::GetValue() const
173{
174 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
175
33720b2d
RR
176 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
177
738f9e5a
RR
178 return (int)ceil(m_adjust->value);
179}
180
ce89fdd2
VZ
181void wxSpinCtrl::SetValue( const wxString& value )
182{
183 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
184
185 int n;
186 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
187 {
188 // a number - set it
189 SetValue(n);
190 }
191 else
192 {
193 // invalid number - set text as is (wxMSW compatible)
07f5b19a 194 GtkDisableEvents();
fab591c5 195 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
07f5b19a 196 GtkEnableEvents();
ce89fdd2
VZ
197 }
198}
199
738f9e5a
RR
200void wxSpinCtrl::SetValue( int value )
201{
202 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
203
204 float fpos = (float)value;
205 m_oldPos = fpos;
206 if (fabs(fpos-m_adjust->value) < sensitivity) return;
207
208 m_adjust->value = fpos;
209
07f5b19a 210 GtkDisableEvents();
738f9e5a 211 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
07f5b19a 212 GtkEnableEvents();
738f9e5a
RR
213}
214
f8f9ec55
VZ
215void wxSpinCtrl::SetSelection(long from, long to)
216{
77ffb593 217 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
218 // entire range
219 if ( from == -1 && to == -1 )
220 {
221 from = 0;
222 to = INT_MAX;
223 }
224
225 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
226}
227
738f9e5a
RR
228void wxSpinCtrl::SetRange(int minVal, int maxVal)
229{
230 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
231
232 float fmin = (float)minVal;
233 float fmax = (float)maxVal;
234
235 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
236 (fabs(fmax-m_adjust->upper) < sensitivity))
237 {
238 return;
239 }
240
241 m_adjust->lower = fmin;
242 m_adjust->upper = fmax;
243
244 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 245
738f9e5a
RR
246 // these two calls are required due to some bug in GTK
247 Refresh();
248 SetFocus();
249}
250
da048e3d
RR
251void wxSpinCtrl::OnChar( wxKeyEvent &event )
252{
253 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
254
12a3f227 255 if (event.GetKeyCode() == WXK_RETURN)
da048e3d
RR
256 {
257 wxWindow *top_frame = m_parent;
055e633d 258 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 259 top_frame = top_frame->GetParent();
0e0d8857 260
fa8a793a 261 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 262 {
fa8a793a
VZ
263 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
264 if ( window )
265 {
266 GtkWidget *widgetDef = window->default_widget;
267
055e633d 268 if ( widgetDef )
fa8a793a
VZ
269 {
270 gtk_widget_activate(widgetDef);
271 return;
272 }
273 }
9750fc42 274 }
da048e3d
RR
275 }
276
8e13c1ec 277 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2
RR
278 {
279 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
280 evt.SetEventObject(this);
281 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
282 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
283 evt.SetString( val );
937013e0 284 if (HandleWindowEvent(evt)) return;
4a11cca2
RR
285 }
286
da048e3d
RR
287 event.Skip();
288}
289
738f9e5a
RR
290bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
291{
8e13c1ec 292 if (GTK_SPIN_BUTTON(m_widget)->entry.text_area == window) return true;
3d257b8d 293
8e13c1ec 294 if (GTK_SPIN_BUTTON(m_widget)->panel == window) return true;
2b904684 295
8e13c1ec 296 return false;
738f9e5a
RR
297}
298
9d9b7755
VZ
299wxSize wxSpinCtrl::DoGetBestSize() const
300{
0279e844 301 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
302 wxSize best(95, ret.y);
303 CacheBestSize(best);
304 return best;
9d9b7755
VZ
305}
306
9d522606
RD
307// static
308wxVisualAttributes
309wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
310{
311 // TODO: overload to accept functions like gtk_spin_button_new?
312 // Until then use a similar type
313 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
314}
315
738f9e5a 316#endif
aec0ed2e 317 // wxUSE_SPINCTRL