]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
The mmedia contrib requires sys/soundcard.h on unix, so disable it on unix
[wxWidgets.git] / src / gtk1 / 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 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
11#pragma implementation "slider.h"
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
c801d85f 17#include "wx/slider.h"
dcf924a3
RR
18
19#if wxUSE_SLIDER
20
c801d85f 21#include "wx/utils.h"
463c4d71 22#include "wx/math.h"
9e691f46 23#include "wx/gtk/private.h"
83624f79 24
acfd422a
RR
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
66bd6b93
RR
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
1e6feb95 36extern bool g_blockEventsOnDrag;
66bd6b93 37
57a1fd73
RR
38static const float sensitivity = 0.02;
39
c801d85f 40//-----------------------------------------------------------------------------
97b3455a 41// "value_changed"
c801d85f
KB
42//-----------------------------------------------------------------------------
43
865bb325 44extern "C" {
9e691f46
VZ
45static void gtk_slider_callback( GtkAdjustment *adjust,
46 SCROLLBAR_CBACK_ARG
47 wxSlider *win )
80810ca3 48{
acfd422a
RR
49 if (g_isIdle) wxapp_install_idle_handler();
50
a2053b27 51 if (!win->m_hasVMT) return;
2d17d68f 52 if (g_blockEventsOnDrag) return;
e1811a01
RR
53
54 float diff = adjust->value - win->m_oldPos;
57a1fd73 55 if (fabs(diff) < sensitivity) return;
80810ca3 56
e1811a01 57 win->m_oldPos = adjust->value;
2d17d68f 58
9e691f46 59 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
80810ca3 60
0a07a7d8 61 double dvalue = adjust->value;
83c22707 62 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
80810ca3 63
1e6feb95
VZ
64 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
65 : wxHORIZONTAL;
80810ca3 66
2d17d68f
RR
67 wxScrollEvent event( command, win->GetId(), value, orient );
68 event.SetEventObject( win );
828f655f 69 win->GetEventHandler()->ProcessEvent( event );
80810ca3 70
2d17d68f
RR
71 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
f6bcfd97 73 cevent.SetInt( value );
828f655f 74 win->GetEventHandler()->ProcessEvent( cevent );
6de97a3b 75}
865bb325 76}
c801d85f 77
97b3455a
RR
78//-----------------------------------------------------------------------------
79// wxSlider
80//-----------------------------------------------------------------------------
81
c801d85f
KB
82IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
83
debe6624
JS
84bool wxSlider::Create(wxWindow *parent, wxWindowID id,
85 int value, int minValue, int maxValue,
c801d85f 86 const wxPoint& pos, const wxSize& size,
6de97a3b 87 long style, const wxValidator& validator, const wxString& name )
c801d85f 88{
b292e2f5 89 m_acceptsFocus = TRUE;
2d17d68f 90 m_needParent = TRUE;
80810ca3 91
4dcaf11a
RR
92 if (!PreCreation( parent, pos, size ) ||
93 !CreateBase( parent, id, pos, size, style, validator, name ))
94 {
223d09f6 95 wxFAIL_MSG( wxT("wxSlider creation failed") );
ef7a25a7 96 return FALSE;
4dcaf11a 97 }
6de97a3b 98
2d17d68f 99 m_oldPos = 0.0;
c801d85f 100
2e563988 101 if (style & wxSL_VERTICAL)
2d17d68f 102 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
19da4326
RR
103 else
104 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
80810ca3 105
2e563988 106 if (style & wxSL_LABELS)
858b5bdd 107 {
2e563988 108 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
ef7a25a7 109 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 110
f03fc89f
VZ
111 /* labels need more space and too small window will
112 cause junk to appear on the dialog */
858b5bdd 113 if (style & wxSL_VERTICAL)
f03fc89f
VZ
114 {
115 wxSize sz( size );
116 if (sz.x < 35)
117 {
118 sz.x = 35;
119 SetSize( sz );
120 }
121 }
858b5bdd 122 else
f03fc89f
VZ
123 {
124 wxSize sz( size );
125 if (sz.y < 35)
126 {
127 sz.y = 35;
128 SetSize( sz );
129 }
130 }
858b5bdd 131 }
2e563988
RR
132 else
133 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 134
2d17d68f 135 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3 136
3baba4c6 137#ifdef __WXGTK20__
1e219378
KH
138 if (style & wxSL_INVERSE)
139 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
3baba4c6 140#endif
1e219378 141
91b167dd
JS
142 GtkEnableEvents();
143
2d17d68f
RR
144 SetRange( minValue, maxValue );
145 SetValue( value );
80810ca3 146
f03fc89f 147 m_parent->DoAddChild( this );
80810ca3 148
abdeb9e7 149 PostCreation(size);
80810ca3 150
2d17d68f 151 return TRUE;
6de97a3b 152}
c801d85f 153
1e1fafb9 154int wxSlider::GetValue() const
c801d85f 155{
ef7a25a7
VZ
156 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
157 // -0.9 is rounded to -1
158 double val = m_adjust->value;
83c22707 159 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 160}
c801d85f 161
debe6624 162void wxSlider::SetValue( int value )
c801d85f 163{
2d17d68f
RR
164 float fpos = (float)value;
165 m_oldPos = fpos;
166 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 167
2d17d68f 168 m_adjust->value = fpos;
80810ca3 169
e1b93ccb
RR
170 GtkDisableEvents();
171
2d17d68f 172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
e1b93ccb
RR
173
174 GtkEnableEvents();
6de97a3b 175}
c801d85f 176
debe6624 177void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 178{
2d17d68f
RR
179 float fmin = (float)minValue;
180 float fmax = (float)maxValue;
80810ca3 181
2d17d68f
RR
182 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
183 (fabs(fmax-m_adjust->upper) < 0.2))
184 {
185 return;
186 }
80810ca3 187
2d17d68f
RR
188 m_adjust->lower = fmin;
189 m_adjust->upper = fmax;
4ee1741f
RR
190 m_adjust->step_increment = 1.0;
191 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 192
e1b93ccb
RR
193 GtkDisableEvents();
194
2d17d68f 195 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
196
197 GtkEnableEvents();
6de97a3b 198}
c801d85f 199
1e1fafb9 200int wxSlider::GetMin() const
c801d85f 201{
b1a39f47 202 return (int)ceil(m_adjust->lower);
6de97a3b 203}
c801d85f 204
1e1fafb9 205int wxSlider::GetMax() const
c801d85f 206{
b1a39f47 207 return (int)ceil(m_adjust->upper);
6de97a3b 208}
c801d85f 209
debe6624 210void wxSlider::SetPageSize( int pageSize )
c801d85f 211{
2d17d68f 212 float fpage = (float)pageSize;
80810ca3 213
2d17d68f 214 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 215
2d17d68f 216 m_adjust->page_increment = fpage;
c801d85f 217
e1b93ccb
RR
218 GtkDisableEvents();
219
2d17d68f 220 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
221
222 GtkEnableEvents();
6de97a3b 223}
c801d85f 224
1e1fafb9 225int wxSlider::GetPageSize() const
c801d85f 226{
b1a39f47 227 return (int)ceil(m_adjust->page_increment);
6de97a3b 228}
c801d85f 229
debe6624 230void wxSlider::SetThumbLength( int len )
c801d85f 231{
2d17d68f 232 float flen = (float)len;
80810ca3 233
2d17d68f 234 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 235
2d17d68f 236 m_adjust->page_size = flen;
c801d85f 237
e1b93ccb
RR
238 GtkDisableEvents();
239
2d17d68f 240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
241
242 GtkEnableEvents();
6de97a3b 243}
c801d85f 244
1e1fafb9 245int wxSlider::GetThumbLength() const
c801d85f 246{
b1a39f47 247 return (int)ceil(m_adjust->page_size);
6de97a3b 248}
c801d85f 249
debe6624 250void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 251{
6de97a3b 252}
c801d85f 253
1e1fafb9 254int wxSlider::GetLineSize() const
c801d85f 255{
2d17d68f 256 return 0;
6de97a3b 257}
c801d85f 258
b4071e91
RR
259bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
260{
2d17d68f 261 GtkRange *range = GTK_RANGE(m_widget);
2b5f62a0
VZ
262#ifdef __WXGTK20__
263 return (range->event_window == window);
264#else
9e691f46 265 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
266 || (window == range->trough)
267 || (window == range->slider)
268 || (window == range->step_forw)
2b5f62a0
VZ
269 || (window == range->step_back) );
270#endif
b4071e91
RR
271}
272
e1b93ccb
RR
273void wxSlider::GtkDisableEvents()
274{
275 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
276 GTK_SIGNAL_FUNC(gtk_slider_callback),
277 (gpointer) this );
278}
279
280void wxSlider::GtkEnableEvents()
281{
282 gtk_signal_connect( GTK_OBJECT (m_adjust),
283 "value_changed",
284 GTK_SIGNAL_FUNC(gtk_slider_callback),
285 (gpointer) this );
286}
287
9d522606
RD
288// static
289wxVisualAttributes
290wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
291{
292 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
293}
294
dcf924a3 295#endif