]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/slider.cpp
warning fixes unsigned->signed
[wxWidgets.git] / src / gtk1 / slider.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/slider.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "slider.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/slider.h"
18
19 #if wxUSE_SLIDER
20
21 #include "wx/utils.h"
22
23 #include <math.h>
24
25 #include "wx/gtk/private.h"
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 //-----------------------------------------------------------------------------
35 // data
36 //-----------------------------------------------------------------------------
37
38 extern bool g_blockEventsOnDrag;
39
40 static const float sensitivity = 0.02;
41
42 //-----------------------------------------------------------------------------
43 // "value_changed"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_slider_callback( GtkAdjustment *adjust,
47 SCROLLBAR_CBACK_ARG
48 wxSlider *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 = adjust->value - win->m_oldPos;
56 if (fabs(diff) < sensitivity) return;
57
58 win->m_oldPos = adjust->value;
59
60 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
61
62 double dvalue = adjust->value;
63 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
64
65 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
66 : wxHORIZONTAL;
67
68 wxScrollEvent event( command, win->GetId(), value, orient );
69 event.SetEventObject( win );
70 win->GetEventHandler()->ProcessEvent( event );
71
72 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
73 cevent.SetEventObject( win );
74 cevent.SetInt( value );
75 win->GetEventHandler()->ProcessEvent( cevent );
76 }
77
78 //-----------------------------------------------------------------------------
79 // wxSlider
80 //-----------------------------------------------------------------------------
81
82 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
83
84 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
85 int value, int minValue, int maxValue,
86 const wxPoint& pos, const wxSize& size,
87 long style, const wxValidator& validator, const wxString& name )
88 {
89 m_acceptsFocus = TRUE;
90 m_needParent = TRUE;
91
92 if (!PreCreation( parent, pos, size ) ||
93 !CreateBase( parent, id, pos, size, style, validator, name ))
94 {
95 wxFAIL_MSG( wxT("wxSlider creation failed") );
96 return FALSE;
97 }
98
99 m_oldPos = 0.0;
100
101 if (style & wxSL_VERTICAL)
102 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
103 else
104 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
105
106 if (style & wxSL_LABELS)
107 {
108 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
109 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
110
111 /* labels need more space and too small window will
112 cause junk to appear on the dialog */
113 if (style & wxSL_VERTICAL)
114 {
115 wxSize sz( size );
116 if (sz.x < 35)
117 {
118 sz.x = 35;
119 SetSize( sz );
120 }
121 }
122 else
123 {
124 wxSize sz( size );
125 if (sz.y < 35)
126 {
127 sz.y = 35;
128 SetSize( sz );
129 }
130 }
131 }
132 else
133 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
134
135 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
136
137 GtkEnableEvents();
138
139 SetRange( minValue, maxValue );
140 SetValue( value );
141
142 m_parent->DoAddChild( this );
143
144 PostCreation(size);
145
146 return TRUE;
147 }
148
149 int wxSlider::GetValue() const
150 {
151 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
152 // -0.9 is rounded to -1
153 double val = m_adjust->value;
154 return (int)(val < 0 ? val - 0.5 : val + 0.5);
155 }
156
157 void wxSlider::SetValue( int value )
158 {
159 float fpos = (float)value;
160 m_oldPos = fpos;
161 if (fabs(fpos-m_adjust->value) < 0.2) return;
162
163 m_adjust->value = fpos;
164
165 GtkDisableEvents();
166
167 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
168
169 GtkEnableEvents();
170 }
171
172 void wxSlider::SetRange( int minValue, int maxValue )
173 {
174 float fmin = (float)minValue;
175 float fmax = (float)maxValue;
176
177 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
178 (fabs(fmax-m_adjust->upper) < 0.2))
179 {
180 return;
181 }
182
183 m_adjust->lower = fmin;
184 m_adjust->upper = fmax;
185 m_adjust->step_increment = 1.0;
186 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
187
188 GtkDisableEvents();
189
190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
191
192 GtkEnableEvents();
193 }
194
195 int wxSlider::GetMin() const
196 {
197 return (int)ceil(m_adjust->lower);
198 }
199
200 int wxSlider::GetMax() const
201 {
202 return (int)ceil(m_adjust->upper);
203 }
204
205 void wxSlider::SetPageSize( int pageSize )
206 {
207 float fpage = (float)pageSize;
208
209 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
210
211 m_adjust->page_increment = fpage;
212
213 GtkDisableEvents();
214
215 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
216
217 GtkEnableEvents();
218 }
219
220 int wxSlider::GetPageSize() const
221 {
222 return (int)ceil(m_adjust->page_increment);
223 }
224
225 void wxSlider::SetThumbLength( int len )
226 {
227 float flen = (float)len;
228
229 if (fabs(flen-m_adjust->page_size) < 0.2) return;
230
231 m_adjust->page_size = flen;
232
233 GtkDisableEvents();
234
235 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
236
237 GtkEnableEvents();
238 }
239
240 int wxSlider::GetThumbLength() const
241 {
242 return (int)ceil(m_adjust->page_size);
243 }
244
245 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
246 {
247 }
248
249 int wxSlider::GetLineSize() const
250 {
251 return 0;
252 }
253
254 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
255 {
256 GtkRange *range = GTK_RANGE(m_widget);
257 #ifdef __WXGTK20__
258 return (range->event_window == window);
259 #else
260 return ( (window == GTK_WIDGET(range)->window)
261 || (window == range->trough)
262 || (window == range->slider)
263 || (window == range->step_forw)
264 || (window == range->step_back) );
265 #endif
266 }
267
268 void wxSlider::GtkDisableEvents()
269 {
270 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
271 GTK_SIGNAL_FUNC(gtk_slider_callback),
272 (gpointer) this );
273 }
274
275 void wxSlider::GtkEnableEvents()
276 {
277 gtk_signal_connect( GTK_OBJECT (m_adjust),
278 "value_changed",
279 GTK_SIGNAL_FUNC(gtk_slider_callback),
280 (gpointer) this );
281 }
282
283 // static
284 wxVisualAttributes
285 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
286 {
287 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
288 }
289
290 #endif