]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
Modified wxGetHomeDir to take environment variable HOME into account.
[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
1e6feb95 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"
57a1fd73 22
f5368809 23#include <math.h>
c801d85f 24
9e691f46 25#include "wx/gtk/private.h"
83624f79 26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
66bd6b93
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
1e6feb95 38extern bool g_blockEventsOnDrag;
66bd6b93 39
57a1fd73
RR
40static const float sensitivity = 0.02;
41
c801d85f 42//-----------------------------------------------------------------------------
97b3455a 43// "value_changed"
c801d85f
KB
44//-----------------------------------------------------------------------------
45
9e691f46
VZ
46static void gtk_slider_callback( GtkAdjustment *adjust,
47 SCROLLBAR_CBACK_ARG
48 wxSlider *win )
80810ca3 49{
acfd422a
RR
50 if (g_isIdle) wxapp_install_idle_handler();
51
a2053b27 52 if (!win->m_hasVMT) return;
2d17d68f 53 if (g_blockEventsOnDrag) return;
e1811a01
RR
54
55 float diff = adjust->value - win->m_oldPos;
57a1fd73 56 if (fabs(diff) < sensitivity) return;
80810ca3 57
e1811a01 58 win->m_oldPos = adjust->value;
2d17d68f 59
9e691f46 60 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
80810ca3 61
0a07a7d8 62 double dvalue = adjust->value;
83c22707 63 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
80810ca3 64
1e6feb95
VZ
65 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
66 : wxHORIZONTAL;
80810ca3 67
2d17d68f
RR
68 wxScrollEvent event( command, win->GetId(), value, orient );
69 event.SetEventObject( win );
828f655f 70 win->GetEventHandler()->ProcessEvent( event );
80810ca3 71
2d17d68f
RR
72 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
73 cevent.SetEventObject( win );
f6bcfd97 74 cevent.SetInt( value );
828f655f 75 win->GetEventHandler()->ProcessEvent( cevent );
6de97a3b 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
91b167dd
JS
137 GtkEnableEvents();
138
2d17d68f
RR
139 SetRange( minValue, maxValue );
140 SetValue( value );
80810ca3 141
f03fc89f 142 m_parent->DoAddChild( this );
80810ca3 143
2d17d68f 144 PostCreation();
80810ca3 145
2d17d68f 146 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 147
2d17d68f 148 Show( TRUE );
80810ca3 149
2d17d68f 150 return TRUE;
6de97a3b 151}
c801d85f 152
1e1fafb9 153int wxSlider::GetValue() const
c801d85f 154{
ef7a25a7
VZ
155 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
156 // -0.9 is rounded to -1
157 double val = m_adjust->value;
83c22707 158 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 159}
c801d85f 160
debe6624 161void wxSlider::SetValue( int value )
c801d85f 162{
2d17d68f
RR
163 float fpos = (float)value;
164 m_oldPos = fpos;
165 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 166
2d17d68f 167 m_adjust->value = fpos;
80810ca3 168
e1b93ccb
RR
169 GtkDisableEvents();
170
2d17d68f 171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
e1b93ccb
RR
172
173 GtkEnableEvents();
6de97a3b 174}
c801d85f 175
debe6624 176void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 177{
2d17d68f
RR
178 float fmin = (float)minValue;
179 float fmax = (float)maxValue;
80810ca3 180
2d17d68f
RR
181 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
182 (fabs(fmax-m_adjust->upper) < 0.2))
183 {
184 return;
185 }
80810ca3 186
2d17d68f
RR
187 m_adjust->lower = fmin;
188 m_adjust->upper = fmax;
4ee1741f
RR
189 m_adjust->step_increment = 1.0;
190 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 191
e1b93ccb
RR
192 GtkDisableEvents();
193
2d17d68f 194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
195
196 GtkEnableEvents();
6de97a3b 197}
c801d85f 198
1e1fafb9 199int wxSlider::GetMin() const
c801d85f 200{
b1a39f47 201 return (int)ceil(m_adjust->lower);
6de97a3b 202}
c801d85f 203
1e1fafb9 204int wxSlider::GetMax() const
c801d85f 205{
b1a39f47 206 return (int)ceil(m_adjust->upper);
6de97a3b 207}
c801d85f 208
debe6624 209void wxSlider::SetPageSize( int pageSize )
c801d85f 210{
2d17d68f 211 float fpage = (float)pageSize;
80810ca3 212
2d17d68f 213 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 214
2d17d68f 215 m_adjust->page_increment = fpage;
c801d85f 216
e1b93ccb
RR
217 GtkDisableEvents();
218
2d17d68f 219 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
220
221 GtkEnableEvents();
6de97a3b 222}
c801d85f 223
1e1fafb9 224int wxSlider::GetPageSize() const
c801d85f 225{
b1a39f47 226 return (int)ceil(m_adjust->page_increment);
6de97a3b 227}
c801d85f 228
debe6624 229void wxSlider::SetThumbLength( int len )
c801d85f 230{
2d17d68f 231 float flen = (float)len;
80810ca3 232
2d17d68f 233 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 234
2d17d68f 235 m_adjust->page_size = flen;
c801d85f 236
e1b93ccb
RR
237 GtkDisableEvents();
238
2d17d68f 239 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
240
241 GtkEnableEvents();
6de97a3b 242}
c801d85f 243
1e1fafb9 244int wxSlider::GetThumbLength() const
c801d85f 245{
b1a39f47 246 return (int)ceil(m_adjust->page_size);
6de97a3b 247}
c801d85f 248
debe6624 249void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 250{
6de97a3b 251}
c801d85f 252
1e1fafb9 253int wxSlider::GetLineSize() const
c801d85f 254{
2d17d68f 255 return 0;
6de97a3b 256}
c801d85f 257
b4071e91
RR
258bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
259{
2d17d68f 260 GtkRange *range = GTK_RANGE(m_widget);
2b5f62a0
VZ
261#ifdef __WXGTK20__
262 return (range->event_window == window);
263#else
9e691f46 264 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
265 || (window == range->trough)
266 || (window == range->slider)
267 || (window == range->step_forw)
2b5f62a0
VZ
268 || (window == range->step_back) );
269#endif
b4071e91
RR
270}
271
58614078
RR
272void wxSlider::ApplyWidgetStyle()
273{
2d17d68f
RR
274 SetWidgetStyle();
275 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 276}
dcf924a3 277
e1b93ccb
RR
278void wxSlider::GtkDisableEvents()
279{
280 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
281 GTK_SIGNAL_FUNC(gtk_slider_callback),
282 (gpointer) this );
283}
284
285void wxSlider::GtkEnableEvents()
286{
287 gtk_signal_connect( GTK_OBJECT (m_adjust),
288 "value_changed",
289 GTK_SIGNAL_FUNC(gtk_slider_callback),
290 (gpointer) this );
291}
292
dcf924a3 293#endif