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