]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinctrl.cpp
Added various MACROS for converting strings
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
CommitLineData
738f9e5a
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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#ifdef __GNUG__
12#pragma implementation "spinctrl.h"
13#endif
14
15#include "wx/spinctrl.h"
16
0e0d8857 17#if wxUSE_SPINCTRL
738f9e5a
RR
18
19#include "wx/utils.h"
aec0ed2e 20
78bcfcfc
VZ
21#include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
22
738f9e5a
RR
23#include <math.h>
24
e1910715 25#include "wx/gtk/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
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) );
738f9e5a 63 win->GetEventHandler()->ProcessEvent( event );
738f9e5a
RR
64}
65
0a07a7d8
RR
66//-----------------------------------------------------------------------------
67// "changed"
68//-----------------------------------------------------------------------------
69
70static void
71gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
72{
73 if (!win->m_hasVMT) return;
74
75 if (g_isIdle)
76 wxapp_install_idle_handler();
77
78 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
79 event.SetEventObject( win );
ea46eba0
RR
80
81 // see above
82 event.SetInt( (int)ceil(win->m_adjust->value) );
0a07a7d8
RR
83 win->GetEventHandler()->ProcessEvent( event );
84}
85
738f9e5a
RR
86//-----------------------------------------------------------------------------
87// wxSpinCtrl
88//-----------------------------------------------------------------------------
89
90IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
91
da048e3d
RR
92BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
93 EVT_CHAR(wxSpinCtrl::OnChar)
94END_EVENT_TABLE()
95
738f9e5a 96bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
97 const wxString& value,
98 const wxPoint& pos, const wxSize& size,
99 long style,
100 int min, int max, int initial,
101 const wxString& name)
738f9e5a
RR
102{
103 m_needParent = TRUE;
354aa1e3 104 m_acceptsFocus = TRUE;
738f9e5a 105
0279e844
RR
106 if (!PreCreation( parent, pos, size ) ||
107 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
108 {
109 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
9750fc42 110 return FALSE;
738f9e5a
RR
111 }
112
113 m_oldPos = initial;
114
115 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
116
117 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
0279e844 118
b02da6b1
VZ
119 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
120 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 121
07f5b19a 122 GtkEnableEvents();
db434467 123
738f9e5a
RR
124 m_parent->DoAddChild( this );
125
126 PostCreation();
127
db434467
RR
128 SetFont( parent->GetFont() );
129
130 wxSize size_best( DoGetBestSize() );
131 wxSize new_size( size );
132 if (new_size.x == -1)
133 new_size.x = size_best.x;
134 if (new_size.y == -1)
135 new_size.y = size_best.y;
b0e282b3
RR
136 if (new_size.y > size_best.y)
137 new_size.y = size_best.y;
db434467
RR
138 if ((new_size.x != size.x) || (new_size.y != size.y))
139 SetSize( new_size.x, new_size.y );
140
738f9e5a
RR
141 SetBackgroundColour( parent->GetBackgroundColour() );
142
ce89fdd2
VZ
143 SetValue( value );
144
738f9e5a
RR
145 Show( TRUE );
146
147 return TRUE;
148}
149
07f5b19a
RR
150void wxSpinCtrl::GtkDisableEvents()
151{
152 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
153 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
154 (gpointer) this );
155
0a07a7d8
RR
156 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
157 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
158 (gpointer) this );
07f5b19a
RR
159}
160
161void wxSpinCtrl::GtkEnableEvents()
162{
163 gtk_signal_connect( GTK_OBJECT (m_adjust),
164 "value_changed",
165 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
166 (gpointer) this );
0a07a7d8
RR
167
168 gtk_signal_connect( GTK_OBJECT(m_widget),
169 "changed",
170 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
171 (gpointer)this);
07f5b19a
RR
172}
173
738f9e5a
RR
174int wxSpinCtrl::GetMin() const
175{
176 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
177
178 return (int)ceil(m_adjust->lower);
179}
180
181int wxSpinCtrl::GetMax() const
182{
183 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
184
185 return (int)ceil(m_adjust->upper);
186}
187
188int wxSpinCtrl::GetValue() const
189{
190 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
191
33720b2d
RR
192 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
193
738f9e5a
RR
194 return (int)ceil(m_adjust->value);
195}
196
ce89fdd2
VZ
197void wxSpinCtrl::SetValue( const wxString& value )
198{
199 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
200
201 int n;
202 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
203 {
204 // a number - set it
205 SetValue(n);
206 }
207 else
208 {
209 // invalid number - set text as is (wxMSW compatible)
07f5b19a 210 GtkDisableEvents();
7dd62924 211 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
07f5b19a 212 GtkEnableEvents();
ce89fdd2
VZ
213 }
214}
215
738f9e5a
RR
216void wxSpinCtrl::SetValue( int value )
217{
218 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
219
220 float fpos = (float)value;
221 m_oldPos = fpos;
222 if (fabs(fpos-m_adjust->value) < sensitivity) return;
223
224 m_adjust->value = fpos;
225
07f5b19a 226 GtkDisableEvents();
738f9e5a 227 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
07f5b19a 228 GtkEnableEvents();
738f9e5a
RR
229}
230
231void wxSpinCtrl::SetRange(int minVal, int maxVal)
232{
233 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
234
235 float fmin = (float)minVal;
236 float fmax = (float)maxVal;
237
238 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
239 (fabs(fmax-m_adjust->upper) < sensitivity))
240 {
241 return;
242 }
243
244 m_adjust->lower = fmin;
245 m_adjust->upper = fmax;
246
247 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 248
738f9e5a
RR
249 // these two calls are required due to some bug in GTK
250 Refresh();
251 SetFocus();
252}
253
da048e3d
RR
254void wxSpinCtrl::OnChar( wxKeyEvent &event )
255{
256 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
257
258 if (event.KeyCode() == WXK_RETURN)
259 {
260 wxWindow *top_frame = m_parent;
f6bcfd97 261 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
da048e3d 262 top_frame = top_frame->GetParent();
0e0d8857 263
fa8a793a 264 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 265 {
fa8a793a
VZ
266 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
267 if ( window )
268 {
269 GtkWidget *widgetDef = window->default_widget;
270
271 if ( widgetDef && GTK_IS_WINDOW(widgetDef) )
272 {
273 gtk_widget_activate(widgetDef);
274 return;
275 }
276 }
9750fc42 277 }
da048e3d
RR
278 }
279
280 event.Skip();
281}
282
738f9e5a
RR
283bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
284{
285 return GTK_SPIN_BUTTON(m_widget)->panel == window;
286}
287
288void wxSpinCtrl::ApplyWidgetStyle()
289{
290 SetWidgetStyle();
291 gtk_widget_set_style( m_widget, m_widgetStyle );
292}
293
9d9b7755
VZ
294wxSize wxSpinCtrl::DoGetBestSize() const
295{
0279e844
RR
296 wxSize ret( wxControl::DoGetBestSize() );
297 return wxSize(95, ret.y);
9d9b7755
VZ
298}
299
738f9e5a 300#endif
aec0ed2e 301 // wxUSE_SPINCTRL