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