]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/spinctrl.cpp
More small fixes
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
... / ...
CommitLineData
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
17#if wxUSE_SPINCTRL
18
19#include "wx/utils.h"
20
21#include <math.h>
22
23#include <gdk/gdk.h>
24#include <gtk/gtk.h>
25
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
33static const float sensitivity = 0.02;
34
35//-----------------------------------------------------------------------------
36// data
37//-----------------------------------------------------------------------------
38
39extern bool g_blockEventsOnDrag;
40
41//-----------------------------------------------------------------------------
42// "value_changed"
43//-----------------------------------------------------------------------------
44
45static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
46{
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
52 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
53 event.SetEventObject( win );
54
55 // note that we don't use wxSpinCtrl::GetValue() here because it would
56 // adjust the value to fit into the control range and this means that we
57 // would never be able to enter an "invalid" value in the control, even
58 // temporarily - and trying to enter 10 into the control which accepts the
59 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
60 // enter 1!) (VZ)
61 event.SetInt( (int)ceil(win->m_adjust->value) );
62 win->GetEventHandler()->ProcessEvent( event );
63}
64
65//-----------------------------------------------------------------------------
66// "changed"
67//-----------------------------------------------------------------------------
68
69static void
70gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
71{
72 if (!win->m_hasVMT) return;
73
74 if (g_isIdle)
75 wxapp_install_idle_handler();
76
77 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
78 event.SetEventObject( win );
79 event.SetInt( win->GetValue() );
80 win->GetEventHandler()->ProcessEvent( event );
81}
82
83//-----------------------------------------------------------------------------
84// wxSpinCtrl
85//-----------------------------------------------------------------------------
86
87IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
88
89BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
90 EVT_CHAR(wxSpinCtrl::OnChar)
91END_EVENT_TABLE()
92
93bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
94 const wxString& value,
95 const wxPoint& pos, const wxSize& size,
96 long style,
97 int min, int max, int initial,
98 const wxString& name)
99{
100 m_needParent = TRUE;
101 m_acceptsFocus = TRUE;
102
103 if (!PreCreation( parent, pos, size ) ||
104 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
105 {
106 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
107 return FALSE;
108 }
109
110 m_oldPos = initial;
111
112 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
113
114 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
115
116 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
117 (int)(m_windowStyle & wxSP_WRAP) );
118
119 GtkEnableEvents();
120
121 m_parent->DoAddChild( this );
122
123 PostCreation();
124
125 SetFont( parent->GetFont() );
126
127 wxSize size_best( DoGetBestSize() );
128 wxSize new_size( size );
129 if (new_size.x == -1)
130 new_size.x = size_best.x;
131 if (new_size.y == -1)
132 new_size.y = size_best.y;
133 if (new_size.y > size_best.y)
134 new_size.y = size_best.y;
135 if ((new_size.x != size.x) || (new_size.y != size.y))
136 SetSize( new_size.x, new_size.y );
137
138 SetBackgroundColour( parent->GetBackgroundColour() );
139
140 SetValue( value );
141
142 Show( TRUE );
143
144 return TRUE;
145}
146
147void wxSpinCtrl::GtkDisableEvents()
148{
149 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
150 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
151 (gpointer) this );
152
153 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
154 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
155 (gpointer) this );
156}
157
158void wxSpinCtrl::GtkEnableEvents()
159{
160 gtk_signal_connect( GTK_OBJECT (m_adjust),
161 "value_changed",
162 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
163 (gpointer) this );
164
165 gtk_signal_connect( GTK_OBJECT(m_widget),
166 "changed",
167 GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback),
168 (gpointer)this);
169}
170
171int wxSpinCtrl::GetMin() const
172{
173 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
174
175 return (int)ceil(m_adjust->lower);
176}
177
178int wxSpinCtrl::GetMax() const
179{
180 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
181
182 return (int)ceil(m_adjust->upper);
183}
184
185int wxSpinCtrl::GetValue() const
186{
187 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
188
189 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
190
191 return (int)ceil(m_adjust->value);
192}
193
194void wxSpinCtrl::SetValue( const wxString& value )
195{
196 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
197
198 int n;
199 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
200 {
201 // a number - set it
202 SetValue(n);
203 }
204 else
205 {
206 // invalid number - set text as is (wxMSW compatible)
207 GtkDisableEvents();
208 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
209 GtkEnableEvents();
210 }
211}
212
213void wxSpinCtrl::SetValue( int value )
214{
215 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
216
217 float fpos = (float)value;
218 m_oldPos = fpos;
219 if (fabs(fpos-m_adjust->value) < sensitivity) return;
220
221 m_adjust->value = fpos;
222
223 GtkDisableEvents();
224 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
225 GtkEnableEvents();
226}
227
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" );
245
246 // these two calls are required due to some bug in GTK
247 Refresh();
248 SetFocus();
249}
250
251void wxSpinCtrl::OnChar( wxKeyEvent &event )
252{
253 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
254
255 if (event.KeyCode() == WXK_RETURN)
256 {
257 wxWindow *top_frame = m_parent;
258 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
259 top_frame = top_frame->GetParent();
260 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
261
262 if (window->default_widget)
263 {
264 gtk_widget_activate (window->default_widget);
265 return;
266 }
267 }
268
269 event.Skip();
270}
271
272bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
273{
274 return GTK_SPIN_BUTTON(m_widget)->panel == window;
275}
276
277void wxSpinCtrl::ApplyWidgetStyle()
278{
279 SetWidgetStyle();
280 gtk_widget_set_style( m_widget, m_widgetStyle );
281}
282
283wxSize wxSpinCtrl::DoGetBestSize() const
284{
285 wxSize ret( wxControl::DoGetBestSize() );
286 return wxSize(95, ret.y);
287}
288
289#endif
290 // wxUSE_SPINCTRL