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