]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
corrected eventobject setting for mouse events and capture
[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
VZ
133
134 gtk_signal_connect( GTK_OBJECT(m_adjust),
2d17d68f 135 "value_changed",
80810ca3 136 (GtkSignalFunc) gtk_slider_callback,
f03fc89f 137 (gpointer) this );
80810ca3 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
2d17d68f 169 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 170}
c801d85f 171
debe6624 172void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 173{
2d17d68f
RR
174 float fmin = (float)minValue;
175 float fmax = (float)maxValue;
80810ca3 176
2d17d68f
RR
177 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
178 (fabs(fmax-m_adjust->upper) < 0.2))
179 {
180 return;
181 }
80810ca3 182
2d17d68f
RR
183 m_adjust->lower = fmin;
184 m_adjust->upper = fmax;
4ee1741f
RR
185 m_adjust->step_increment = 1.0;
186 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 187
2d17d68f 188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 189}
c801d85f 190
1e1fafb9 191int wxSlider::GetMin() const
c801d85f 192{
b1a39f47 193 return (int)ceil(m_adjust->lower);
6de97a3b 194}
c801d85f 195
1e1fafb9 196int wxSlider::GetMax() const
c801d85f 197{
b1a39f47 198 return (int)ceil(m_adjust->upper);
6de97a3b 199}
c801d85f 200
debe6624 201void wxSlider::SetPageSize( int pageSize )
c801d85f 202{
2d17d68f 203 float fpage = (float)pageSize;
80810ca3 204
2d17d68f 205 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 206
2d17d68f 207 m_adjust->page_increment = fpage;
c801d85f 208
2d17d68f 209 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 210}
c801d85f 211
1e1fafb9 212int wxSlider::GetPageSize() const
c801d85f 213{
b1a39f47 214 return (int)ceil(m_adjust->page_increment);
6de97a3b 215}
c801d85f 216
debe6624 217void wxSlider::SetThumbLength( int len )
c801d85f 218{
2d17d68f 219 float flen = (float)len;
80810ca3 220
2d17d68f 221 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 222
2d17d68f 223 m_adjust->page_size = flen;
c801d85f 224
2d17d68f 225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 226}
c801d85f 227
1e1fafb9 228int wxSlider::GetThumbLength() const
c801d85f 229{
b1a39f47 230 return (int)ceil(m_adjust->page_size);
6de97a3b 231}
c801d85f 232
debe6624 233void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 234{
6de97a3b 235}
c801d85f 236
1e1fafb9 237int wxSlider::GetLineSize() const
c801d85f 238{
2d17d68f 239 return 0;
6de97a3b 240}
c801d85f 241
b4071e91
RR
242bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
243{
2d17d68f 244 GtkRange *range = GTK_RANGE(m_widget);
9e691f46
VZ
245 return ( (window == GTK_WIDGET(range)->window)
246#ifndef __WXGTK20__
247 || (window == range->trough)
248 || (window == range->slider)
249 || (window == range->step_forw)
250 || (window == range->step_back)
251#endif // GTK+ 1.x
252 );
b4071e91
RR
253}
254
58614078
RR
255void wxSlider::ApplyWidgetStyle()
256{
2d17d68f
RR
257 SetWidgetStyle();
258 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 259}
dcf924a3
RR
260
261#endif