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