]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
Improve validation of wxCheckBox styles.
[wxWidgets.git] / src / gtk1 / slider.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
de6185e2 2// Name: src/gtk1/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
de6185e2
WS
13#if wxUSE_SLIDER
14
c801d85f 15#include "wx/slider.h"
dcf924a3 16
de6185e2
WS
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
18680f86 19 #include "wx/math.h"
de6185e2 20#endif
dcf924a3 21
3cbab641 22#include "wx/gtk1/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
2b024653
VZ
37// ----------------------------------------------------------------------------
38// helper functions
39// ----------------------------------------------------------------------------
40
41// compare 2 adjustment values up to some (hardcoded) precision
42static inline bool AreSameAdjustValues(double x, double y)
43{
44 return fabs(x - y) < 0.02;
45}
46
2b024653
VZ
47// process a scroll event
48static void
49ProcessScrollEvent(wxSlider *win, wxEventType evtType, double dvalue)
50{
51 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
52 : wxHORIZONTAL;
53
54 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
55 wxScrollEvent event( evtType, win->GetId(), value, orient );
56 event.SetEventObject( win );
937013e0 57 win->HandleWindowEvent( event );
2b024653 58
8e3e14c4
VZ
59 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
60 {
61 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
62 event2.SetEventObject( win );
937013e0 63 win->HandleWindowEvent( event2 );
8e3e14c4
VZ
64 }
65
2b024653
VZ
66 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
67 cevent.SetEventObject( win );
68 cevent.SetInt( value );
937013e0 69 win->HandleWindowEvent( cevent );
2b024653 70}
57a1fd73 71
c801d85f 72//-----------------------------------------------------------------------------
97b3455a 73// "value_changed"
c801d85f
KB
74//-----------------------------------------------------------------------------
75
865bb325 76extern "C" {
9e691f46
VZ
77static void gtk_slider_callback( GtkAdjustment *adjust,
78 SCROLLBAR_CBACK_ARG
79 wxSlider *win )
80810ca3 80{
acfd422a
RR
81 if (g_isIdle) wxapp_install_idle_handler();
82
a2053b27 83 if (!win->m_hasVMT) return;
2d17d68f 84 if (g_blockEventsOnDrag) return;
e1811a01 85
2b024653
VZ
86 const double dvalue = adjust->value;
87 const double diff = dvalue - win->m_oldPos;
88 if ( AreSameAdjustValues(diff, 0) )
89 return;
80810ca3 90
2b024653 91 wxEventType evtType;
2b024653 92 evtType = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
2d17d68f 93
2b024653 94 ProcessScrollEvent(win, evtType, dvalue);
80810ca3 95
2b024653
VZ
96 win->m_oldPos = dvalue;
97}
80810ca3 98
2b024653
VZ
99static gint gtk_slider_button_press_callback( GtkWidget * /* widget */,
100 GdkEventButton * /* gdk_event */,
101 wxWindowGTK *win)
102{
103 // indicate that the thumb is being dragged with the mouse
104 win->m_isScrolling = true;
80810ca3 105
2b024653
VZ
106 return FALSE;
107}
80810ca3 108
2b024653
VZ
109static gint gtk_slider_button_release_callback( GtkWidget *scale,
110 GdkEventButton * /* gdk_event */,
111 wxSlider *win)
112{
113 // not scrolling any longer
114 win->m_isScrolling = false;
115
116 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE,
117 GTK_RANGE(scale)->adjustment->value);
118
119 return FALSE;
6de97a3b 120}
2b024653 121
865bb325 122}
c801d85f 123
97b3455a
RR
124//-----------------------------------------------------------------------------
125// wxSlider
126//-----------------------------------------------------------------------------
127
c801d85f
KB
128IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
129
debe6624
JS
130bool wxSlider::Create(wxWindow *parent, wxWindowID id,
131 int value, int minValue, int maxValue,
c801d85f 132 const wxPoint& pos, const wxSize& size,
6de97a3b 133 long style, const wxValidator& validator, const wxString& name )
c801d85f 134{
de6185e2
WS
135 m_acceptsFocus = true;
136 m_needParent = true;
80810ca3 137
4dcaf11a
RR
138 if (!PreCreation( parent, pos, size ) ||
139 !CreateBase( parent, id, pos, size, style, validator, name ))
140 {
223d09f6 141 wxFAIL_MSG( wxT("wxSlider creation failed") );
de6185e2 142 return false;
4dcaf11a 143 }
6de97a3b 144
2d17d68f 145 m_oldPos = 0.0;
c801d85f 146
2e563988 147 if (style & wxSL_VERTICAL)
d3b9f782 148 m_widget = gtk_vscale_new( NULL );
19da4326 149 else
d3b9f782 150 m_widget = gtk_hscale_new( NULL );
80810ca3 151
2e563988 152 if (style & wxSL_LABELS)
858b5bdd 153 {
2e563988 154 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
ef7a25a7 155 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 156
f03fc89f
VZ
157 /* labels need more space and too small window will
158 cause junk to appear on the dialog */
858b5bdd 159 if (style & wxSL_VERTICAL)
f03fc89f
VZ
160 {
161 wxSize sz( size );
162 if (sz.x < 35)
163 {
164 sz.x = 35;
165 SetSize( sz );
166 }
167 }
858b5bdd 168 else
f03fc89f
VZ
169 {
170 wxSize sz( size );
171 if (sz.y < 35)
172 {
173 sz.y = 35;
174 SetSize( sz );
175 }
176 }
858b5bdd 177 }
2e563988
RR
178 else
179 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 180
2d17d68f 181 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3 182
91b167dd 183 GtkEnableEvents();
2b024653
VZ
184 gtk_signal_connect( GTK_OBJECT(m_widget),
185 "button_press_event",
186 (GtkSignalFunc)gtk_slider_button_press_callback,
187 (gpointer) this );
188 gtk_signal_connect( GTK_OBJECT(m_widget),
189 "button_release_event",
190 (GtkSignalFunc)gtk_slider_button_release_callback,
191 (gpointer) this );
91b167dd 192
2d17d68f
RR
193 SetRange( minValue, maxValue );
194 SetValue( value );
80810ca3 195
f03fc89f 196 m_parent->DoAddChild( this );
80810ca3 197
abdeb9e7 198 PostCreation(size);
80810ca3 199
de6185e2 200 return true;
6de97a3b 201}
c801d85f 202
1e1fafb9 203int wxSlider::GetValue() const
c801d85f 204{
23f826bd 205 return wxRound(m_adjust->value);
6de97a3b 206}
c801d85f 207
debe6624 208void wxSlider::SetValue( int value )
c801d85f 209{
2b024653 210 double fpos = (double)value;
2d17d68f 211 m_oldPos = fpos;
2b024653
VZ
212 if ( AreSameAdjustValues(fpos, m_adjust->value) )
213 return;
80810ca3 214
2d17d68f 215 m_adjust->value = fpos;
80810ca3 216
e1b93ccb 217 GtkDisableEvents();
2b024653 218
2d17d68f 219 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
2b024653 220
e1b93ccb 221 GtkEnableEvents();
6de97a3b 222}
c801d85f 223
debe6624 224void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 225{
2b024653
VZ
226 double fmin = (double)minValue;
227 double fmax = (double)maxValue;
80810ca3 228
2d17d68f
RR
229 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
230 (fabs(fmax-m_adjust->upper) < 0.2))
231 {
232 return;
233 }
80810ca3 234
2d17d68f
RR
235 m_adjust->lower = fmin;
236 m_adjust->upper = fmax;
4ee1741f
RR
237 m_adjust->step_increment = 1.0;
238 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 239
e1b93ccb 240 GtkDisableEvents();
2b024653 241
2d17d68f 242 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
2b024653 243
e1b93ccb 244 GtkEnableEvents();
6de97a3b 245}
c801d85f 246
1e1fafb9 247int wxSlider::GetMin() const
c801d85f 248{
b1a39f47 249 return (int)ceil(m_adjust->lower);
6de97a3b 250}
c801d85f 251
1e1fafb9 252int wxSlider::GetMax() const
c801d85f 253{
b1a39f47 254 return (int)ceil(m_adjust->upper);
6de97a3b 255}
c801d85f 256
debe6624 257void wxSlider::SetPageSize( int pageSize )
c801d85f 258{
2b024653 259 double fpage = (double)pageSize;
80810ca3 260
2d17d68f 261 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 262
2d17d68f 263 m_adjust->page_increment = fpage;
c801d85f 264
e1b93ccb 265 GtkDisableEvents();
2b024653 266
2d17d68f 267 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
2b024653 268
e1b93ccb 269 GtkEnableEvents();
6de97a3b 270}
c801d85f 271
1e1fafb9 272int wxSlider::GetPageSize() const
c801d85f 273{
b1a39f47 274 return (int)ceil(m_adjust->page_increment);
6de97a3b 275}
c801d85f 276
debe6624 277void wxSlider::SetThumbLength( int len )
c801d85f 278{
2b024653 279 double flen = (double)len;
80810ca3 280
2d17d68f 281 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 282
2d17d68f 283 m_adjust->page_size = flen;
c801d85f 284
e1b93ccb 285 GtkDisableEvents();
2b024653 286
2d17d68f 287 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
2b024653 288
e1b93ccb 289 GtkEnableEvents();
6de97a3b 290}
c801d85f 291
1e1fafb9 292int wxSlider::GetThumbLength() const
c801d85f 293{
b1a39f47 294 return (int)ceil(m_adjust->page_size);
6de97a3b 295}
c801d85f 296
debe6624 297void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 298{
6de97a3b 299}
c801d85f 300
1e1fafb9 301int wxSlider::GetLineSize() const
c801d85f 302{
2d17d68f 303 return 0;
6de97a3b 304}
c801d85f 305
b4071e91
RR
306bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
307{
2d17d68f 308 GtkRange *range = GTK_RANGE(m_widget);
9e691f46 309 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
310 || (window == range->trough)
311 || (window == range->slider)
312 || (window == range->step_forw)
2b5f62a0 313 || (window == range->step_back) );
b4071e91
RR
314}
315
e1b93ccb
RR
316void wxSlider::GtkDisableEvents()
317{
318 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
319 GTK_SIGNAL_FUNC(gtk_slider_callback),
320 (gpointer) this );
321}
322
323void wxSlider::GtkEnableEvents()
324{
325 gtk_signal_connect( GTK_OBJECT (m_adjust),
326 "value_changed",
327 GTK_SIGNAL_FUNC(gtk_slider_callback),
328 (gpointer) this );
329}
330
9d522606
RD
331// static
332wxVisualAttributes
333wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
334{
335 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
336}
337
de6185e2 338#endif // wxUSE_SLIDER