determine the slider event type more precisely (i.e. generate LINE/PAGE UP/DOWN and...
[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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/slider.h"
14
15 #if wxUSE_SLIDER
16
17 #include "wx/utils.h"
18 #include "wx/math.h"
19 #include "wx/gtk/private.h"
20
21 //-----------------------------------------------------------------------------
22 // idle system
23 //-----------------------------------------------------------------------------
24
25 extern void wxapp_install_idle_handler();
26 extern bool g_isIdle;
27
28 //-----------------------------------------------------------------------------
29 // data
30 //-----------------------------------------------------------------------------
31
32 extern bool g_blockEventsOnDrag;
33
34 // ----------------------------------------------------------------------------
35 // helper functions
36 // ----------------------------------------------------------------------------
37
38 // compare 2 adjustment values up to some (hardcoded) precision
39 static inline bool AreSameAdjustValues(double x, double y)
40 {
41 return fabs(x - y) < 0.02;
42 }
43
44 static inline int AdjustValueToInt(double x)
45 {
46 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
47 // -0.9 is rounded to -1
48 return (int)(x < 0 ? x - 0.5 : x + 0.5);
49 }
50
51 // process a scroll event
52 static void
53 ProcessScrollEvent(wxSlider *win, wxEventType evtType, double dvalue)
54 {
55 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
56 : wxHORIZONTAL;
57
58 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
59 wxScrollEvent event( evtType, win->GetId(), value, orient );
60 event.SetEventObject( win );
61 win->GetEventHandler()->ProcessEvent( event );
62
63 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
64 cevent.SetEventObject( win );
65 cevent.SetInt( value );
66 win->GetEventHandler()->ProcessEvent( cevent );
67 }
68
69 //-----------------------------------------------------------------------------
70 // "value_changed"
71 //-----------------------------------------------------------------------------
72
73 extern "C" {
74 static void gtk_slider_callback( GtkAdjustment *adjust,
75 SCROLLBAR_CBACK_ARG
76 wxSlider *win )
77 {
78 if (g_isIdle) wxapp_install_idle_handler();
79
80 if (!win->m_hasVMT) return;
81 if (g_blockEventsOnDrag) return;
82
83 const double dvalue = adjust->value;
84 const double diff = dvalue - win->m_oldPos;
85 if ( AreSameAdjustValues(diff, 0) )
86 return;
87
88 wxEventType evtType;
89 #ifdef __WXGTK20__
90 if ( win->m_isScrolling )
91 evtType = wxEVT_SCROLL_THUMBTRACK;
92 // it could seem that UP/DOWN are inversed but this is what wxMSW does
93 else if ( AreSameAdjustValues(diff, adjust->step_increment) )
94 evtType = wxEVT_SCROLL_LINEDOWN;
95 else if ( AreSameAdjustValues(diff, -adjust->step_increment) )
96 evtType = wxEVT_SCROLL_LINEUP;
97 else if ( AreSameAdjustValues(diff, adjust->page_increment) )
98 evtType = wxEVT_SCROLL_PAGEDOWN;
99 else if ( AreSameAdjustValues(diff, -adjust->page_increment) )
100 evtType = wxEVT_SCROLL_PAGEUP;
101 else if ( AreSameAdjustValues(adjust->value, adjust->lower) )
102 evtType = wxEVT_SCROLL_TOP;
103 else if ( AreSameAdjustValues(adjust->value, adjust->upper) )
104 evtType = wxEVT_SCROLL_BOTTOM;
105 #else
106 evtType = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
107 #endif
108
109 ProcessScrollEvent(win, evtType, dvalue);
110
111 win->m_oldPos = dvalue;
112 }
113
114 static gint gtk_slider_button_press_callback( GtkWidget * /* widget */,
115 GdkEventButton * /* gdk_event */,
116 wxWindowGTK *win)
117 {
118 // indicate that the thumb is being dragged with the mouse
119 win->m_isScrolling = true;
120
121 return FALSE;
122 }
123
124 static gint gtk_slider_button_release_callback( GtkWidget *scale,
125 GdkEventButton * /* gdk_event */,
126 wxSlider *win)
127 {
128 // not scrolling any longer
129 win->m_isScrolling = false;
130
131 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE,
132 GTK_RANGE(scale)->adjustment->value);
133
134 return FALSE;
135 }
136
137 }
138
139 //-----------------------------------------------------------------------------
140 // wxSlider
141 //-----------------------------------------------------------------------------
142
143 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
144
145 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
146 int value, int minValue, int maxValue,
147 const wxPoint& pos, const wxSize& size,
148 long style, const wxValidator& validator, const wxString& name )
149 {
150 m_acceptsFocus = TRUE;
151 m_needParent = TRUE;
152
153 if (!PreCreation( parent, pos, size ) ||
154 !CreateBase( parent, id, pos, size, style, validator, name ))
155 {
156 wxFAIL_MSG( wxT("wxSlider creation failed") );
157 return FALSE;
158 }
159
160 m_oldPos = 0.0;
161
162 if (style & wxSL_VERTICAL)
163 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
164 else
165 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
166
167 if (style & wxSL_LABELS)
168 {
169 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
170 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
171
172 /* labels need more space and too small window will
173 cause junk to appear on the dialog */
174 if (style & wxSL_VERTICAL)
175 {
176 wxSize sz( size );
177 if (sz.x < 35)
178 {
179 sz.x = 35;
180 SetSize( sz );
181 }
182 }
183 else
184 {
185 wxSize sz( size );
186 if (sz.y < 35)
187 {
188 sz.y = 35;
189 SetSize( sz );
190 }
191 }
192 }
193 else
194 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
195
196 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
197
198 #ifdef __WXGTK20__
199 if (style & wxSL_INVERSE)
200 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
201 #endif
202
203 GtkEnableEvents();
204 gtk_signal_connect( GTK_OBJECT(m_widget),
205 "button_press_event",
206 (GtkSignalFunc)gtk_slider_button_press_callback,
207 (gpointer) this );
208 gtk_signal_connect( GTK_OBJECT(m_widget),
209 "button_release_event",
210 (GtkSignalFunc)gtk_slider_button_release_callback,
211 (gpointer) this );
212
213 SetRange( minValue, maxValue );
214 SetValue( value );
215
216 m_parent->DoAddChild( this );
217
218 PostCreation(size);
219
220 return TRUE;
221 }
222
223 int wxSlider::GetValue() const
224 {
225 return AdjustValueToInt(m_adjust->value);
226 }
227
228 void wxSlider::SetValue( int value )
229 {
230 double fpos = (double)value;
231 m_oldPos = fpos;
232 if ( AreSameAdjustValues(fpos, m_adjust->value) )
233 return;
234
235 m_adjust->value = fpos;
236
237 GtkDisableEvents();
238
239 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
240
241 GtkEnableEvents();
242 }
243
244 void wxSlider::SetRange( int minValue, int maxValue )
245 {
246 double fmin = (double)minValue;
247 double fmax = (double)maxValue;
248
249 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
250 (fabs(fmax-m_adjust->upper) < 0.2))
251 {
252 return;
253 }
254
255 m_adjust->lower = fmin;
256 m_adjust->upper = fmax;
257 m_adjust->step_increment = 1.0;
258 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
259
260 GtkDisableEvents();
261
262 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
263
264 GtkEnableEvents();
265 }
266
267 int wxSlider::GetMin() const
268 {
269 return (int)ceil(m_adjust->lower);
270 }
271
272 int wxSlider::GetMax() const
273 {
274 return (int)ceil(m_adjust->upper);
275 }
276
277 void wxSlider::SetPageSize( int pageSize )
278 {
279 double fpage = (double)pageSize;
280
281 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
282
283 m_adjust->page_increment = fpage;
284
285 GtkDisableEvents();
286
287 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
288
289 GtkEnableEvents();
290 }
291
292 int wxSlider::GetPageSize() const
293 {
294 return (int)ceil(m_adjust->page_increment);
295 }
296
297 void wxSlider::SetThumbLength( int len )
298 {
299 double flen = (double)len;
300
301 if (fabs(flen-m_adjust->page_size) < 0.2) return;
302
303 m_adjust->page_size = flen;
304
305 GtkDisableEvents();
306
307 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
308
309 GtkEnableEvents();
310 }
311
312 int wxSlider::GetThumbLength() const
313 {
314 return (int)ceil(m_adjust->page_size);
315 }
316
317 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
318 {
319 }
320
321 int wxSlider::GetLineSize() const
322 {
323 return 0;
324 }
325
326 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
327 {
328 GtkRange *range = GTK_RANGE(m_widget);
329 #ifdef __WXGTK20__
330 return (range->event_window == window);
331 #else
332 return ( (window == GTK_WIDGET(range)->window)
333 || (window == range->trough)
334 || (window == range->slider)
335 || (window == range->step_forw)
336 || (window == range->step_back) );
337 #endif
338 }
339
340 void wxSlider::GtkDisableEvents()
341 {
342 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
343 GTK_SIGNAL_FUNC(gtk_slider_callback),
344 (gpointer) this );
345 }
346
347 void wxSlider::GtkEnableEvents()
348 {
349 gtk_signal_connect( GTK_OBJECT (m_adjust),
350 "value_changed",
351 GTK_SIGNAL_FUNC(gtk_slider_callback),
352 (gpointer) this );
353 }
354
355 // static
356 wxVisualAttributes
357 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
358 {
359 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
360 }
361
362 #endif