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