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