Nuke GTK1 from src/gtk
[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 gtk_signal_connect( GTK_OBJECT(m_widget),
206 "button_press_event",
207 (GtkSignalFunc)gtk_slider_button_press_callback,
208 (gpointer) this );
209 gtk_signal_connect( GTK_OBJECT(m_widget),
210 "button_release_event",
211 (GtkSignalFunc)gtk_slider_button_release_callback,
212 (gpointer) this );
213
214 SetRange( minValue, maxValue );
215 SetValue( value );
216
217 m_parent->DoAddChild( this );
218
219 PostCreation(size);
220
221 return TRUE;
222 }
223
224 int wxSlider::GetValue() const
225 {
226 return AdjustValueToInt(m_adjust->value);
227 }
228
229 void wxSlider::SetValue( int value )
230 {
231 double fpos = (double)value;
232 m_oldPos = fpos;
233 if ( AreSameAdjustValues(fpos, m_adjust->value) )
234 return;
235
236 m_adjust->value = fpos;
237
238 GtkDisableEvents();
239
240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
241
242 GtkEnableEvents();
243 }
244
245 void wxSlider::SetRange( int minValue, int maxValue )
246 {
247 double fmin = (double)minValue;
248 double fmax = (double)maxValue;
249
250 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
251 (fabs(fmax-m_adjust->upper) < 0.2))
252 {
253 return;
254 }
255
256 m_adjust->lower = fmin;
257 m_adjust->upper = fmax;
258 m_adjust->step_increment = 1.0;
259 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
260
261 GtkDisableEvents();
262
263 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
264
265 GtkEnableEvents();
266 }
267
268 int wxSlider::GetMin() const
269 {
270 return (int)ceil(m_adjust->lower);
271 }
272
273 int wxSlider::GetMax() const
274 {
275 return (int)ceil(m_adjust->upper);
276 }
277
278 void wxSlider::SetPageSize( int pageSize )
279 {
280 double fpage = (double)pageSize;
281
282 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
283
284 m_adjust->page_increment = fpage;
285
286 GtkDisableEvents();
287
288 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
289
290 GtkEnableEvents();
291 }
292
293 int wxSlider::GetPageSize() const
294 {
295 return (int)ceil(m_adjust->page_increment);
296 }
297
298 void wxSlider::SetThumbLength( int len )
299 {
300 double flen = (double)len;
301
302 if (fabs(flen-m_adjust->page_size) < 0.2) return;
303
304 m_adjust->page_size = flen;
305
306 GtkDisableEvents();
307
308 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
309
310 GtkEnableEvents();
311 }
312
313 int wxSlider::GetThumbLength() const
314 {
315 return (int)ceil(m_adjust->page_size);
316 }
317
318 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
319 {
320 }
321
322 int wxSlider::GetLineSize() const
323 {
324 return 0;
325 }
326
327 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
328 {
329 GtkRange *range = GTK_RANGE(m_widget);
330 return (range->event_window == window);
331 }
332
333 void wxSlider::GtkDisableEvents()
334 {
335 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
336 GTK_SIGNAL_FUNC(gtk_slider_callback),
337 (gpointer) this );
338 }
339
340 void wxSlider::GtkEnableEvents()
341 {
342 gtk_signal_connect( GTK_OBJECT (m_adjust),
343 "value_changed",
344 GTK_SIGNAL_FUNC(gtk_slider_callback),
345 (gpointer) this );
346 }
347
348 // static
349 wxVisualAttributes
350 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
351 {
352 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
353 }
354
355 #endif