]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinctrl.cpp
change generic ints to enums in wxDC (closes #9959)
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
CommitLineData
738f9e5a 1/////////////////////////////////////////////////////////////////////////////
8e13c1ec 2// Name: src/gtk1/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
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
93IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
94
da048e3d
RR
95BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
96 EVT_CHAR(wxSpinCtrl::OnChar)
97END_EVENT_TABLE()
98
738f9e5a 99bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
100 const wxString& value,
101 const wxPoint& pos, const wxSize& size,
102 long style,
103 int min, int max, int initial,
104 const wxString& name)
738f9e5a 105{
8e13c1ec
WS
106 m_needParent = true;
107 m_acceptsFocus = true;
738f9e5a 108
0279e844
RR
109 if (!PreCreation( parent, pos, size ) ||
110 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
111 {
112 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
8e13c1ec 113 return false;
738f9e5a
RR
114 }
115
116 m_oldPos = initial;
117
118 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
119
120 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
3d257b8d 121
b02da6b1
VZ
122 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
123 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 124
07f5b19a 125 GtkEnableEvents();
3d257b8d 126
738f9e5a
RR
127 m_parent->DoAddChild( this );
128
abdeb9e7 129 PostCreation(size);
db434467 130
ce89fdd2
VZ
131 SetValue( value );
132
8e13c1ec 133 return true;
738f9e5a
RR
134}
135
07f5b19a
RR
136void wxSpinCtrl::GtkDisableEvents()
137{
138 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
139 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
140 (gpointer) this );
141
0a07a7d8
RR
142 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
143 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
144 (gpointer) this );
07f5b19a
RR
145}
146
147void wxSpinCtrl::GtkEnableEvents()
148{
149 gtk_signal_connect( GTK_OBJECT (m_adjust),
150 "value_changed",
151 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
152 (gpointer) this );
3d257b8d 153
0a07a7d8
RR
154 gtk_signal_connect( GTK_OBJECT(m_widget),
155 "changed",
156 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
157 (gpointer)this);
07f5b19a
RR
158}
159
738f9e5a
RR
160int wxSpinCtrl::GetMin() const
161{
162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
163
164 return (int)ceil(m_adjust->lower);
165}
166
167int wxSpinCtrl::GetMax() const
168{
169 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
170
171 return (int)ceil(m_adjust->upper);
172}
173
174int wxSpinCtrl::GetValue() const
175{
176 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
177
33720b2d
RR
178 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
179
738f9e5a
RR
180 return (int)ceil(m_adjust->value);
181}
182
ce89fdd2
VZ
183void wxSpinCtrl::SetValue( const wxString& value )
184{
185 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
186
187 int n;
188 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
189 {
190 // a number - set it
191 SetValue(n);
192 }
193 else
194 {
195 // invalid number - set text as is (wxMSW compatible)
07f5b19a 196 GtkDisableEvents();
fab591c5 197 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
07f5b19a 198 GtkEnableEvents();
ce89fdd2
VZ
199 }
200}
201
738f9e5a
RR
202void wxSpinCtrl::SetValue( int value )
203{
204 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
205
206 float fpos = (float)value;
207 m_oldPos = fpos;
208 if (fabs(fpos-m_adjust->value) < sensitivity) return;
209
210 m_adjust->value = fpos;
211
07f5b19a 212 GtkDisableEvents();
738f9e5a 213 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
07f5b19a 214 GtkEnableEvents();
738f9e5a
RR
215}
216
f8f9ec55
VZ
217void wxSpinCtrl::SetSelection(long from, long to)
218{
77ffb593 219 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
220 // entire range
221 if ( from == -1 && to == -1 )
222 {
223 from = 0;
224 to = INT_MAX;
225 }
226
227 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
228}
229
738f9e5a
RR
230void wxSpinCtrl::SetRange(int minVal, int maxVal)
231{
232 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
233
234 float fmin = (float)minVal;
235 float fmax = (float)maxVal;
236
237 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
238 (fabs(fmax-m_adjust->upper) < sensitivity))
239 {
240 return;
241 }
242
243 m_adjust->lower = fmin;
244 m_adjust->upper = fmax;
245
246 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 247
738f9e5a
RR
248 // these two calls are required due to some bug in GTK
249 Refresh();
250 SetFocus();
251}
252
da048e3d
RR
253void wxSpinCtrl::OnChar( wxKeyEvent &event )
254{
255 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
256
12a3f227 257 if (event.GetKeyCode() == WXK_RETURN)
da048e3d
RR
258 {
259 wxWindow *top_frame = m_parent;
055e633d 260 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
da048e3d 261 top_frame = top_frame->GetParent();
0e0d8857 262
fa8a793a 263 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 264 {
fa8a793a
VZ
265 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
266 if ( window )
267 {
268 GtkWidget *widgetDef = window->default_widget;
269
055e633d 270 if ( widgetDef )
fa8a793a
VZ
271 {
272 gtk_widget_activate(widgetDef);
273 return;
274 }
275 }
9750fc42 276 }
da048e3d
RR
277 }
278
8e13c1ec 279 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2
RR
280 {
281 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
282 evt.SetEventObject(this);
283 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
284 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
285 evt.SetString( val );
937013e0 286 if (HandleWindowEvent(evt)) return;
4a11cca2
RR
287 }
288
da048e3d
RR
289 event.Skip();
290}
291
738f9e5a
RR
292bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
293{
8e13c1ec 294 if (GTK_SPIN_BUTTON(m_widget)->entry.text_area == window) return true;
3d257b8d 295
8e13c1ec 296 if (GTK_SPIN_BUTTON(m_widget)->panel == window) return true;
2b904684 297
8e13c1ec 298 return false;
738f9e5a
RR
299}
300
9d9b7755
VZ
301wxSize wxSpinCtrl::DoGetBestSize() const
302{
0279e844 303 wxSize ret( wxControl::DoGetBestSize() );
9f884528
RD
304 wxSize best(95, ret.y);
305 CacheBestSize(best);
306 return best;
9d9b7755
VZ
307}
308
9d522606
RD
309// static
310wxVisualAttributes
311wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
312{
313 // TODO: overload to accept functions like gtk_spin_button_new?
314 // Until then use a similar type
315 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
316}
317
738f9e5a 318#endif
aec0ed2e 319 // wxUSE_SPINCTRL