]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/slider.cpp
cleanup - reformat
[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 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 #ifdef __WXGTK20__
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 = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
114 #endif
115
116 ProcessScrollEvent(win, evtType, dvalue);
117
118 win->m_oldPos = dvalue;
119 }
120
121 static gint gtk_slider_button_press_callback( GtkWidget * /* widget */,
122 GdkEventButton * /* gdk_event */,
123 wxWindowGTK *win)
124 {
125 // indicate that the thumb is being dragged with the mouse
126 win->m_isScrolling = true;
127
128 return FALSE;
129 }
130
131 static gint gtk_slider_button_release_callback( GtkWidget *scale,
132 GdkEventButton * /* gdk_event */,
133 wxSlider *win)
134 {
135 // not scrolling any longer
136 win->m_isScrolling = false;
137
138 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE,
139 GTK_RANGE(scale)->adjustment->value);
140
141 return FALSE;
142 }
143
144 }
145
146 //-----------------------------------------------------------------------------
147 // wxSlider
148 //-----------------------------------------------------------------------------
149
150 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
151
152 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
153 int value, int minValue, int maxValue,
154 const wxPoint& pos, const wxSize& size,
155 long style, const wxValidator& validator, const wxString& name )
156 {
157 m_acceptsFocus = TRUE;
158 m_needParent = TRUE;
159
160 if (!PreCreation( parent, pos, size ) ||
161 !CreateBase( parent, id, pos, size, style, validator, name ))
162 {
163 wxFAIL_MSG( wxT("wxSlider creation failed") );
164 return FALSE;
165 }
166
167 m_oldPos = 0.0;
168
169 if (style & wxSL_VERTICAL)
170 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
171 else
172 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
173
174 if (style & wxSL_LABELS)
175 {
176 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
177 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
178
179 /* labels need more space and too small window will
180 cause junk to appear on the dialog */
181 if (style & wxSL_VERTICAL)
182 {
183 wxSize sz( size );
184 if (sz.x < 35)
185 {
186 sz.x = 35;
187 SetSize( sz );
188 }
189 }
190 else
191 {
192 wxSize sz( size );
193 if (sz.y < 35)
194 {
195 sz.y = 35;
196 SetSize( sz );
197 }
198 }
199 }
200 else
201 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
202
203 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
204
205 #ifdef __WXGTK20__
206 if (style & wxSL_INVERSE)
207 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
208 #endif
209
210 GtkEnableEvents();
211 gtk_signal_connect( GTK_OBJECT(m_widget),
212 "button_press_event",
213 (GtkSignalFunc)gtk_slider_button_press_callback,
214 (gpointer) this );
215 gtk_signal_connect( GTK_OBJECT(m_widget),
216 "button_release_event",
217 (GtkSignalFunc)gtk_slider_button_release_callback,
218 (gpointer) this );
219
220 SetRange( minValue, maxValue );
221 SetValue( value );
222
223 m_parent->DoAddChild( this );
224
225 PostCreation(size);
226
227 return TRUE;
228 }
229
230 int wxSlider::GetValue() const
231 {
232 return AdjustValueToInt(m_adjust->value);
233 }
234
235 void wxSlider::SetValue( int value )
236 {
237 double fpos = (double)value;
238 m_oldPos = fpos;
239 if ( AreSameAdjustValues(fpos, m_adjust->value) )
240 return;
241
242 m_adjust->value = fpos;
243
244 GtkDisableEvents();
245
246 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
247
248 GtkEnableEvents();
249 }
250
251 void wxSlider::SetRange( int minValue, int maxValue )
252 {
253 double fmin = (double)minValue;
254 double fmax = (double)maxValue;
255
256 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
257 (fabs(fmax-m_adjust->upper) < 0.2))
258 {
259 return;
260 }
261
262 m_adjust->lower = fmin;
263 m_adjust->upper = fmax;
264 m_adjust->step_increment = 1.0;
265 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
266
267 GtkDisableEvents();
268
269 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
270
271 GtkEnableEvents();
272 }
273
274 int wxSlider::GetMin() const
275 {
276 return (int)ceil(m_adjust->lower);
277 }
278
279 int wxSlider::GetMax() const
280 {
281 return (int)ceil(m_adjust->upper);
282 }
283
284 void wxSlider::SetPageSize( int pageSize )
285 {
286 double fpage = (double)pageSize;
287
288 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
289
290 m_adjust->page_increment = fpage;
291
292 GtkDisableEvents();
293
294 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
295
296 GtkEnableEvents();
297 }
298
299 int wxSlider::GetPageSize() const
300 {
301 return (int)ceil(m_adjust->page_increment);
302 }
303
304 void wxSlider::SetThumbLength( int len )
305 {
306 double flen = (double)len;
307
308 if (fabs(flen-m_adjust->page_size) < 0.2) return;
309
310 m_adjust->page_size = flen;
311
312 GtkDisableEvents();
313
314 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
315
316 GtkEnableEvents();
317 }
318
319 int wxSlider::GetThumbLength() const
320 {
321 return (int)ceil(m_adjust->page_size);
322 }
323
324 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
325 {
326 }
327
328 int wxSlider::GetLineSize() const
329 {
330 return 0;
331 }
332
333 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
334 {
335 GtkRange *range = GTK_RANGE(m_widget);
336 #ifdef __WXGTK20__
337 return (range->event_window == window);
338 #else
339 return ( (window == GTK_WIDGET(range)->window)
340 || (window == range->trough)
341 || (window == range->slider)
342 || (window == range->step_forw)
343 || (window == range->step_back) );
344 #endif
345 }
346
347 void wxSlider::GtkDisableEvents()
348 {
349 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
350 GTK_SIGNAL_FUNC(gtk_slider_callback),
351 (gpointer) this );
352 }
353
354 void wxSlider::GtkEnableEvents()
355 {
356 gtk_signal_connect( GTK_OBJECT (m_adjust),
357 "value_changed",
358 GTK_SIGNAL_FUNC(gtk_slider_callback),
359 (gpointer) this );
360 }
361
362 // static
363 wxVisualAttributes
364 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
365 {
366 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
367 }
368
369 #endif