]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
fixed yet another memory leak in LoadGnomeDataFromKeyFile() (coverity checker CID 51)
[wxWidgets.git] / src / gtk / slider.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: gtk/slider.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/slider.h"
dcf924a3
RR
14
15#if wxUSE_SLIDER
16
c801d85f 17#include "wx/utils.h"
463c4d71 18#include "wx/math.h"
9e691f46 19#include "wx/gtk/private.h"
83624f79 20
acfd422a
RR
21//-----------------------------------------------------------------------------
22// idle system
23//-----------------------------------------------------------------------------
24
25extern void wxapp_install_idle_handler();
26extern bool g_isIdle;
27
66bd6b93
RR
28//-----------------------------------------------------------------------------
29// data
30//-----------------------------------------------------------------------------
31
1e6feb95 32extern bool g_blockEventsOnDrag;
66bd6b93 33
2b024653
VZ
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{
46c48053
VZ
55 const int orient = win->HasFlag(wxSL_VERTICAL) ? wxVERTICAL
56 : wxHORIZONTAL;
2b024653 57
46c48053 58 const int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
2b024653 59
46c48053
VZ
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
8e3e14c4
VZ
71 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
72 {
46c48053
VZ
73 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
74 event.SetEventObject( win );
75 win->GetEventHandler()->ProcessEvent( event );
8e3e14c4
VZ
76 }
77
46c48053
VZ
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 );
2b024653 83}
57a1fd73 84
c801d85f 85//-----------------------------------------------------------------------------
97b3455a 86// "value_changed"
c801d85f
KB
87//-----------------------------------------------------------------------------
88
865bb325 89extern "C" {
9e691f46 90static void gtk_slider_callback( GtkAdjustment *adjust,
9e691f46 91 wxSlider *win )
80810ca3 92{
acfd422a
RR
93 if (g_isIdle) wxapp_install_idle_handler();
94
a2053b27 95 if (!win->m_hasVMT) return;
2d17d68f 96 if (g_blockEventsOnDrag) return;
e1811a01 97
2b024653
VZ
98 const double dvalue = adjust->value;
99 const double diff = dvalue - win->m_oldPos;
100 if ( AreSameAdjustValues(diff, 0) )
101 return;
80810ca3 102
2b024653 103 wxEventType evtType;
2b024653
VZ
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;
46c48053
VZ
119 else
120 evtType = wxEVT_NULL; // wxEVT_SCROLL_CHANGED will still be generated
2d17d68f 121
2b024653 122 ProcessScrollEvent(win, evtType, dvalue);
80810ca3 123
2b024653
VZ
124 win->m_oldPos = dvalue;
125}
80810ca3 126
2b024653
VZ
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;
80810ca3 133
2b024653
VZ
134 return FALSE;
135}
80810ca3 136
2b024653
VZ
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;
6de97a3b 148}
2b024653 149
865bb325 150}
c801d85f 151
97b3455a
RR
152//-----------------------------------------------------------------------------
153// wxSlider
154//-----------------------------------------------------------------------------
155
c801d85f
KB
156IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
157
debe6624
JS
158bool wxSlider::Create(wxWindow *parent, wxWindowID id,
159 int value, int minValue, int maxValue,
c801d85f 160 const wxPoint& pos, const wxSize& size,
6de97a3b 161 long style, const wxValidator& validator, const wxString& name )
c801d85f 162{
b292e2f5 163 m_acceptsFocus = TRUE;
2d17d68f 164 m_needParent = TRUE;
80810ca3 165
4dcaf11a
RR
166 if (!PreCreation( parent, pos, size ) ||
167 !CreateBase( parent, id, pos, size, style, validator, name ))
168 {
223d09f6 169 wxFAIL_MSG( wxT("wxSlider creation failed") );
ef7a25a7 170 return FALSE;
4dcaf11a 171 }
6de97a3b 172
2d17d68f 173 m_oldPos = 0.0;
c801d85f 174
2e563988 175 if (style & wxSL_VERTICAL)
2d17d68f 176 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
19da4326
RR
177 else
178 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
80810ca3 179
2e563988 180 if (style & wxSL_LABELS)
858b5bdd 181 {
2e563988 182 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
ef7a25a7 183 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 184
f03fc89f
VZ
185 /* labels need more space and too small window will
186 cause junk to appear on the dialog */
858b5bdd 187 if (style & wxSL_VERTICAL)
f03fc89f
VZ
188 {
189 wxSize sz( size );
190 if (sz.x < 35)
191 {
192 sz.x = 35;
193 SetSize( sz );
194 }
195 }
858b5bdd 196 else
f03fc89f
VZ
197 {
198 wxSize sz( size );
199 if (sz.y < 35)
200 {
201 sz.y = 35;
202 SetSize( sz );
203 }
204 }
858b5bdd 205 }
2e563988
RR
206 else
207 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 208
2d17d68f 209 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3 210
1e219378
KH
211 if (style & wxSL_INVERSE)
212 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
213
91b167dd 214 GtkEnableEvents();
9fa72bd2
MR
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);
91b167dd 221
2d17d68f
RR
222 SetRange( minValue, maxValue );
223 SetValue( value );
80810ca3 224
f03fc89f 225 m_parent->DoAddChild( this );
80810ca3 226
abdeb9e7 227 PostCreation(size);
80810ca3 228
2d17d68f 229 return TRUE;
6de97a3b 230}
c801d85f 231
1e1fafb9 232int wxSlider::GetValue() const
c801d85f 233{
2b024653 234 return AdjustValueToInt(m_adjust->value);
6de97a3b 235}
c801d85f 236
debe6624 237void wxSlider::SetValue( int value )
c801d85f 238{
2b024653 239 double fpos = (double)value;
2d17d68f 240 m_oldPos = fpos;
2b024653
VZ
241 if ( AreSameAdjustValues(fpos, m_adjust->value) )
242 return;
80810ca3 243
2d17d68f 244 m_adjust->value = fpos;
80810ca3 245
e1b93ccb 246 GtkDisableEvents();
2b024653 247
9fa72bd2 248 g_signal_emit_by_name (m_adjust, "value_changed");
2b024653 249
e1b93ccb 250 GtkEnableEvents();
6de97a3b 251}
c801d85f 252
debe6624 253void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 254{
2b024653
VZ
255 double fmin = (double)minValue;
256 double fmax = (double)maxValue;
80810ca3 257
2d17d68f
RR
258 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
259 (fabs(fmax-m_adjust->upper) < 0.2))
260 {
261 return;
262 }
80810ca3 263
2d17d68f
RR
264 m_adjust->lower = fmin;
265 m_adjust->upper = fmax;
4ee1741f
RR
266 m_adjust->step_increment = 1.0;
267 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 268
e1b93ccb 269 GtkDisableEvents();
2b024653 270
9fa72bd2 271 g_signal_emit_by_name (m_adjust, "changed");
2b024653 272
e1b93ccb 273 GtkEnableEvents();
6de97a3b 274}
c801d85f 275
1e1fafb9 276int wxSlider::GetMin() const
c801d85f 277{
b1a39f47 278 return (int)ceil(m_adjust->lower);
6de97a3b 279}
c801d85f 280
1e1fafb9 281int wxSlider::GetMax() const
c801d85f 282{
b1a39f47 283 return (int)ceil(m_adjust->upper);
6de97a3b 284}
c801d85f 285
debe6624 286void wxSlider::SetPageSize( int pageSize )
c801d85f 287{
2b024653 288 double fpage = (double)pageSize;
80810ca3 289
2d17d68f 290 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 291
2d17d68f 292 m_adjust->page_increment = fpage;
c801d85f 293
e1b93ccb 294 GtkDisableEvents();
2b024653 295
9fa72bd2 296 g_signal_emit_by_name (m_adjust, "changed");
2b024653 297
e1b93ccb 298 GtkEnableEvents();
6de97a3b 299}
c801d85f 300
1e1fafb9 301int wxSlider::GetPageSize() const
c801d85f 302{
b1a39f47 303 return (int)ceil(m_adjust->page_increment);
6de97a3b 304}
c801d85f 305
debe6624 306void wxSlider::SetThumbLength( int len )
c801d85f 307{
2b024653 308 double flen = (double)len;
80810ca3 309
2d17d68f 310 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 311
2d17d68f 312 m_adjust->page_size = flen;
c801d85f 313
e1b93ccb 314 GtkDisableEvents();
2b024653 315
9fa72bd2 316 g_signal_emit_by_name (m_adjust, "changed");
2b024653 317
e1b93ccb 318 GtkEnableEvents();
6de97a3b 319}
c801d85f 320
1e1fafb9 321int wxSlider::GetThumbLength() const
c801d85f 322{
b1a39f47 323 return (int)ceil(m_adjust->page_size);
6de97a3b 324}
c801d85f 325
debe6624 326void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 327{
6de97a3b 328}
c801d85f 329
1e1fafb9 330int wxSlider::GetLineSize() const
c801d85f 331{
2d17d68f 332 return 0;
6de97a3b 333}
c801d85f 334
b4071e91
RR
335bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
336{
2d17d68f 337 GtkRange *range = GTK_RANGE(m_widget);
2b5f62a0 338 return (range->event_window == window);
b4071e91
RR
339}
340
e1b93ccb
RR
341void wxSlider::GtkDisableEvents()
342{
9fa72bd2
MR
343 g_signal_handlers_disconnect_by_func (m_adjust,
344 (gpointer) gtk_slider_callback,
345 this);
e1b93ccb
RR
346}
347
348void wxSlider::GtkEnableEvents()
349{
9fa72bd2
MR
350 g_signal_connect (m_adjust, "value_changed",
351 G_CALLBACK (gtk_slider_callback), this);
e1b93ccb
RR
352}
353
9d522606
RD
354// static
355wxVisualAttributes
356wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
357{
358 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
359}
360
dcf924a3 361#endif