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