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