]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/spinbutt.cpp
small optimization in wxConfigPathChanger: don't change the path if it is already...
[wxWidgets.git] / src / gtk / spinbutt.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: spinbutt.cpp
3// Purpose: wxSpinButton
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "spinbutt.h"
13 #pragma implementation "spinbutbase.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#include "wx/spinbutt.h"
20
21#if wxUSE_SPINBTN
22
23#include "wx/utils.h"
24
25#include <math.h>
26
27#include "wx/gtk/private.h"
28
29//-----------------------------------------------------------------------------
30// idle system
31//-----------------------------------------------------------------------------
32
33extern void wxapp_install_idle_handler();
34extern bool g_isIdle;
35
36//-----------------------------------------------------------------------------
37// data
38//-----------------------------------------------------------------------------
39
40extern bool g_blockEventsOnDrag;
41
42static const float sensitivity = 0.02;
43
44//-----------------------------------------------------------------------------
45// "value_changed"
46//-----------------------------------------------------------------------------
47
48static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
49{
50 if (g_isIdle) wxapp_install_idle_handler();
51
52 if (!win->m_hasVMT) return;
53 if (g_blockEventsOnDrag) return;
54
55 float diff = win->m_adjust->value - win->m_oldPos;
56 if (fabs(diff) < sensitivity) return;
57
58 wxEventType command = wxEVT_NULL;
59
60 float line_step = win->m_adjust->step_increment;
61
62 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
63 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
64 else command = wxEVT_SCROLL_THUMBTRACK;
65
66 int value = (int)ceil(win->m_adjust->value);
67
68 wxSpinEvent event( command, win->GetId());
69 event.SetPosition( value );
70 event.SetEventObject( win );
71
72 if ((win->GetEventHandler()->ProcessEvent( event )) &&
73 !event.IsAllowed() )
74 {
75 /* program has vetoed */
76 win->m_adjust->value = win->m_oldPos;
77
78 gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust),
79 (GtkSignalFunc) gtk_spinbutt_callback,
80 (gpointer) win );
81
82 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
83
84 gtk_signal_connect( GTK_OBJECT (win->m_adjust),
85 "value_changed",
86 (GtkSignalFunc) gtk_spinbutt_callback,
87 (gpointer) win );
88 return;
89 }
90
91 win->m_oldPos = win->m_adjust->value;
92
93 /* always send a thumbtrack event */
94 if (command != wxEVT_SCROLL_THUMBTRACK)
95 {
96 command = wxEVT_SCROLL_THUMBTRACK;
97 wxSpinEvent event2( command, win->GetId());
98 event2.SetPosition( value );
99 event2.SetEventObject( win );
100 win->GetEventHandler()->ProcessEvent( event2 );
101 }
102}
103
104//-----------------------------------------------------------------------------
105// wxSpinButton
106//-----------------------------------------------------------------------------
107
108IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
109IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
110
111BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
112 EVT_SIZE(wxSpinButton::OnSize)
113END_EVENT_TABLE()
114
115bool wxSpinButton::Create(wxWindow *parent,
116 wxWindowID id,
117 const wxPoint& pos,
118 const wxSize& size,
119 long style,
120 const wxString& name)
121{
122 m_needParent = TRUE;
123
124 wxSize new_size = size,
125 sizeBest = DoGetBestSize();
126 new_size.x = sizeBest.x; // override width always
127 if (new_size.y == -1)
128 new_size.y = sizeBest.y;
129
130 if (!PreCreation( parent, pos, new_size ) ||
131 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
132 {
133 wxFAIL_MSG( wxT("wxXX creation failed") );
134 return FALSE;
135 }
136
137 m_oldPos = 0.0;
138
139 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
140
141 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
142
143 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
144 (int)(m_windowStyle & wxSP_WRAP) );
145
146 gtk_signal_connect( GTK_OBJECT (m_adjust),
147 "value_changed",
148 (GtkSignalFunc) gtk_spinbutt_callback,
149 (gpointer) this );
150
151 m_parent->DoAddChild( this );
152
153 PostCreation(new_size);
154
155 return TRUE;
156}
157
158int wxSpinButton::GetMin() const
159{
160 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
161
162 return (int)ceil(m_adjust->lower);
163}
164
165int wxSpinButton::GetMax() const
166{
167 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
168
169 return (int)ceil(m_adjust->upper);
170}
171
172int wxSpinButton::GetValue() const
173{
174 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
175
176 return (int)ceil(m_adjust->value);
177}
178
179void wxSpinButton::SetValue( int value )
180{
181 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
182
183 float fpos = (float)value;
184 m_oldPos = fpos;
185 if (fabs(fpos-m_adjust->value) < sensitivity) return;
186
187 m_adjust->value = fpos;
188
189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
190}
191
192void wxSpinButton::SetRange(int minVal, int maxVal)
193{
194 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
195
196 float fmin = (float)minVal;
197 float fmax = (float)maxVal;
198
199 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
200 (fabs(fmax-m_adjust->upper) < sensitivity))
201 {
202 return;
203 }
204
205 m_adjust->lower = fmin;
206 m_adjust->upper = fmax;
207
208 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
209
210 // these two calls are required due to some bug in GTK
211 Refresh();
212 SetFocus();
213}
214
215void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
216{
217 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
218
219 m_width = DoGetBestSize().x;
220 gtk_widget_set_usize( m_widget, m_width, m_height );
221}
222
223bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
224{
225 return GTK_SPIN_BUTTON(m_widget)->panel == window;
226}
227
228wxSize wxSpinButton::DoGetBestSize() const
229{
230 wxSize best(15, 26); // FIXME
231 CacheBestSize(best);
232 return best;
233}
234
235// static
236wxVisualAttributes
237wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
238{
239 // TODO: overload to accept functions like gtk_spin_button_new?
240 // Until then use a similar type
241 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
242}
243
244#endif