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