Use g_signal* instead of deprecated gtk_signal*. Use proper casts for the arguments.
[wxWidgets.git] / src / gtk / 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 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
64 {
65 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
66 event2.SetEventObject( win );
67 win->GetEventHandler()->ProcessEvent( event2 );
68 }
69
70 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
71 cevent.SetEventObject( win );
72 cevent.SetInt( value );
73 win->GetEventHandler()->ProcessEvent( cevent );
74 }
75
76 //-----------------------------------------------------------------------------
77 // "value_changed"
78 //-----------------------------------------------------------------------------
79
80 extern "C" {
81 static void gtk_slider_callback( GtkAdjustment *adjust,
82 SCROLLBAR_CBACK_ARG
83 wxSlider *win )
84 {
85 if (g_isIdle) wxapp_install_idle_handler();
86
87 if (!win->m_hasVMT) return;
88 if (g_blockEventsOnDrag) return;
89
90 const double dvalue = adjust->value;
91 const double diff = dvalue - win->m_oldPos;
92 if ( AreSameAdjustValues(diff, 0) )
93 return;
94
95 wxEventType evtType;
96 if ( win->m_isScrolling )
97 evtType = wxEVT_SCROLL_THUMBTRACK;
98 // it could seem that UP/DOWN are inversed but this is what wxMSW does
99 else if ( AreSameAdjustValues(diff, adjust->step_increment) )
100 evtType = wxEVT_SCROLL_LINEDOWN;
101 else if ( AreSameAdjustValues(diff, -adjust->step_increment) )
102 evtType = wxEVT_SCROLL_LINEUP;
103 else if ( AreSameAdjustValues(diff, adjust->page_increment) )
104 evtType = wxEVT_SCROLL_PAGEDOWN;
105 else if ( AreSameAdjustValues(diff, -adjust->page_increment) )
106 evtType = wxEVT_SCROLL_PAGEUP;
107 else if ( AreSameAdjustValues(adjust->value, adjust->lower) )
108 evtType = wxEVT_SCROLL_TOP;
109 else if ( AreSameAdjustValues(adjust->value, adjust->upper) )
110 evtType = wxEVT_SCROLL_BOTTOM;
111
112 ProcessScrollEvent(win, evtType, dvalue);
113
114 win->m_oldPos = dvalue;
115 }
116
117 static gint gtk_slider_button_press_callback( GtkWidget * /* widget */,
118 GdkEventButton * /* gdk_event */,
119 wxWindowGTK *win)
120 {
121 // indicate that the thumb is being dragged with the mouse
122 win->m_isScrolling = true;
123
124 return FALSE;
125 }
126
127 static gint gtk_slider_button_release_callback( GtkWidget *scale,
128 GdkEventButton * /* gdk_event */,
129 wxSlider *win)
130 {
131 // not scrolling any longer
132 win->m_isScrolling = false;
133
134 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE,
135 GTK_RANGE(scale)->adjustment->value);
136
137 return FALSE;
138 }
139
140 }
141
142 //-----------------------------------------------------------------------------
143 // wxSlider
144 //-----------------------------------------------------------------------------
145
146 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
147
148 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
149 int value, int minValue, int maxValue,
150 const wxPoint& pos, const wxSize& size,
151 long style, const wxValidator& validator, const wxString& name )
152 {
153 m_acceptsFocus = TRUE;
154 m_needParent = TRUE;
155
156 if (!PreCreation( parent, pos, size ) ||
157 !CreateBase( parent, id, pos, size, style, validator, name ))
158 {
159 wxFAIL_MSG( wxT("wxSlider creation failed") );
160 return FALSE;
161 }
162
163 m_oldPos = 0.0;
164
165 if (style & wxSL_VERTICAL)
166 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
167 else
168 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
169
170 if (style & wxSL_LABELS)
171 {
172 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
173 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
174
175 /* labels need more space and too small window will
176 cause junk to appear on the dialog */
177 if (style & wxSL_VERTICAL)
178 {
179 wxSize sz( size );
180 if (sz.x < 35)
181 {
182 sz.x = 35;
183 SetSize( sz );
184 }
185 }
186 else
187 {
188 wxSize sz( size );
189 if (sz.y < 35)
190 {
191 sz.y = 35;
192 SetSize( sz );
193 }
194 }
195 }
196 else
197 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
198
199 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
200
201 if (style & wxSL_INVERSE)
202 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
203
204 GtkEnableEvents();
205 g_signal_connect (m_widget, "button_press_event",
206 G_CALLBACK (gtk_slider_button_press_callback),
207 this);
208 g_signal_connect (m_widget, "button_release_event",
209 G_CALLBACK (gtk_slider_button_release_callback),
210 this);
211
212 SetRange( minValue, maxValue );
213 SetValue( value );
214
215 m_parent->DoAddChild( this );
216
217 PostCreation(size);
218
219 return TRUE;
220 }
221
222 int wxSlider::GetValue() const
223 {
224 return AdjustValueToInt(m_adjust->value);
225 }
226
227 void wxSlider::SetValue( int value )
228 {
229 double fpos = (double)value;
230 m_oldPos = fpos;
231 if ( AreSameAdjustValues(fpos, m_adjust->value) )
232 return;
233
234 m_adjust->value = fpos;
235
236 GtkDisableEvents();
237
238 g_signal_emit_by_name (m_adjust, "value_changed");
239
240 GtkEnableEvents();
241 }
242
243 void wxSlider::SetRange( int minValue, int maxValue )
244 {
245 double fmin = (double)minValue;
246 double fmax = (double)maxValue;
247
248 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
249 (fabs(fmax-m_adjust->upper) < 0.2))
250 {
251 return;
252 }
253
254 m_adjust->lower = fmin;
255 m_adjust->upper = fmax;
256 m_adjust->step_increment = 1.0;
257 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
258
259 GtkDisableEvents();
260
261 g_signal_emit_by_name (m_adjust, "changed");
262
263 GtkEnableEvents();
264 }
265
266 int wxSlider::GetMin() const
267 {
268 return (int)ceil(m_adjust->lower);
269 }
270
271 int wxSlider::GetMax() const
272 {
273 return (int)ceil(m_adjust->upper);
274 }
275
276 void wxSlider::SetPageSize( int pageSize )
277 {
278 double fpage = (double)pageSize;
279
280 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
281
282 m_adjust->page_increment = fpage;
283
284 GtkDisableEvents();
285
286 g_signal_emit_by_name (m_adjust, "changed");
287
288 GtkEnableEvents();
289 }
290
291 int wxSlider::GetPageSize() const
292 {
293 return (int)ceil(m_adjust->page_increment);
294 }
295
296 void wxSlider::SetThumbLength( int len )
297 {
298 double flen = (double)len;
299
300 if (fabs(flen-m_adjust->page_size) < 0.2) return;
301
302 m_adjust->page_size = flen;
303
304 GtkDisableEvents();
305
306 g_signal_emit_by_name (m_adjust, "changed");
307
308 GtkEnableEvents();
309 }
310
311 int wxSlider::GetThumbLength() const
312 {
313 return (int)ceil(m_adjust->page_size);
314 }
315
316 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
317 {
318 }
319
320 int wxSlider::GetLineSize() const
321 {
322 return 0;
323 }
324
325 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
326 {
327 GtkRange *range = GTK_RANGE(m_widget);
328 return (range->event_window == window);
329 }
330
331 void wxSlider::GtkDisableEvents()
332 {
333 g_signal_handlers_disconnect_by_func (m_adjust,
334 (gpointer) gtk_slider_callback,
335 this);
336 }
337
338 void wxSlider::GtkEnableEvents()
339 {
340 g_signal_connect (m_adjust, "value_changed",
341 G_CALLBACK (gtk_slider_callback), this);
342 }
343
344 // static
345 wxVisualAttributes
346 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
347 {
348 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
349 }
350
351 #endif