1
/////////////////////////////////////////////////////////////////////////////
2
// Name: gtk/slider.cpp
4
// Author: Robert Roebling
5
// Id: $Id: slider.cpp,v 1.58.2.3 2006/03/10 18:45:31 RD Exp $
6
// Copyright: (c) 1998 Robert Roebling
7
// Licence: wxWindows licence
8
/////////////////////////////////////////////////////////////////////////////
10
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11
#pragma implementation "slider.h"
14
// For compilers that support precompilation, includes "wx.h".
15
#include "wx/wxprec.h"
17
#include "wx/slider.h"
23
#include "wx/gtk/private.h"
25
//-----------------------------------------------------------------------------
27
//-----------------------------------------------------------------------------
29
extern void wxapp_install_idle_handler();
32
//-----------------------------------------------------------------------------
34
//-----------------------------------------------------------------------------
36
extern bool g_blockEventsOnDrag;
38
// ----------------------------------------------------------------------------
40
// ----------------------------------------------------------------------------
42
// compare 2 adjustment values up to some (hardcoded) precision
43
static inline bool AreSameAdjustValues(double x, double y)
45
return fabs(x - y) < 0.02;
48
static inline int AdjustValueToInt(double x)
50
// we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
51
// -0.9 is rounded to -1
52
return (int)(x < 0 ? x - 0.5 : x + 0.5);
55
// process a scroll event
57
ProcessScrollEvent(wxSlider *win, wxEventType evtType, double dvalue)
59
const int orient = win->HasFlag(wxSL_VERTICAL) ? wxVERTICAL
62
const int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
64
// if we have any "special" event (i.e. the value changed by a line or a
65
// page), send this specific event first
66
if ( evtType != wxEVT_NULL )
68
wxScrollEvent event( evtType, win->GetId(), value, orient );
69
event.SetEventObject( win );
70
win->GetEventHandler()->ProcessEvent( event );
73
// but, in any case, except if we're dragging the slider (and so the change
74
// is not definitive), send a generic "changed" event
75
if ( evtType != wxEVT_SCROLL_THUMBTRACK )
77
wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
78
event.SetEventObject( win );
79
win->GetEventHandler()->ProcessEvent( event );
82
// and also generate a command event for compatibility
83
wxCommandEvent event( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
84
event.SetEventObject( win );
85
event.SetInt( value );
86
win->GetEventHandler()->ProcessEvent( event );
89
//-----------------------------------------------------------------------------
91
//-----------------------------------------------------------------------------
94
static void gtk_slider_callback( GtkAdjustment *adjust,
98
if (g_isIdle) wxapp_install_idle_handler();
100
if (!win->m_hasVMT) return;
101
if (g_blockEventsOnDrag) return;
103
const double dvalue = adjust->value;
104
const double diff = dvalue - win->m_oldPos;
105
if ( AreSameAdjustValues(diff, 0) )
109
if ( win->m_isScrolling )
110
evtType = wxEVT_SCROLL_THUMBTRACK;
111
// it could seem that UP/DOWN are inversed but this is what wxMSW does
112
else if ( AreSameAdjustValues(diff, adjust->step_increment) )
113
evtType = wxEVT_SCROLL_LINEDOWN;
114
else if ( AreSameAdjustValues(diff, -adjust->step_increment) )
115
evtType = wxEVT_SCROLL_LINEUP;
116
else if ( AreSameAdjustValues(diff, adjust->page_increment) )
117
evtType = wxEVT_SCROLL_PAGEDOWN;
118
else if ( AreSameAdjustValues(diff, -adjust->page_increment) )
119
evtType = wxEVT_SCROLL_PAGEUP;
120
else if ( AreSameAdjustValues(adjust->value, adjust->lower) )
121
evtType = wxEVT_SCROLL_TOP;
122
else if ( AreSameAdjustValues(adjust->value, adjust->upper) )
123
evtType = wxEVT_SCROLL_BOTTOM;
125
evtType = wxEVT_NULL; // wxEVT_SCROLL_CHANGED will still be generated
127
ProcessScrollEvent(win, evtType, dvalue);
129
win->m_oldPos = dvalue;
133
static gint gtk_slider_button_press_callback( GtkWidget * /* widget */,
134
GdkEventButton * /* gdk_event */,
137
// indicate that the thumb is being dragged with the mouse
138
win->m_isScrolling = true;
143
static gint gtk_slider_button_release_callback( GtkWidget *scale,
144
GdkEventButton * /* gdk_event */,
147
// not scrolling any longer
148
win->m_isScrolling = false;
150
ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE,
151
GTK_RANGE(scale)->adjustment->value);
159
//-----------------------------------------------------------------------------
161
//-----------------------------------------------------------------------------
163
IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
165
bool wxSlider::Create(wxWindow *parent, wxWindowID id,
166
int value, int minValue, int maxValue,
167
const wxPoint& pos, const wxSize& size,
168
long style, const wxValidator& validator, const wxString& name )
170
m_acceptsFocus = TRUE;
173
if (!PreCreation( parent, pos, size ) ||
174
!CreateBase( parent, id, pos, size, style, validator, name ))
176
wxFAIL_MSG( wxT("wxSlider creation failed") );
182
if (style & wxSL_VERTICAL)
183
m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
185
m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
187
if (style & wxSL_LABELS)
189
gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
190
gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
192
/* labels need more space and too small window will
193
cause junk to appear on the dialog */
194
if (style & wxSL_VERTICAL)
214
gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
216
m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
219
if (style & wxSL_INVERSE)
220
gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
226
g_signal_connect (m_widget, "button_press_event",
227
G_CALLBACK (gtk_slider_button_press_callback),
229
g_signal_connect (m_widget, "button_release_event",
230
G_CALLBACK (gtk_slider_button_release_callback),
234
SetRange( minValue, maxValue );
237
m_parent->DoAddChild( this );
244
int wxSlider::GetValue() const
246
return AdjustValueToInt(m_adjust->value);
249
void wxSlider::SetValue( int value )
251
double fpos = (double)value;
253
if ( AreSameAdjustValues(fpos, m_adjust->value) )
256
m_adjust->value = fpos;
260
gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
265
void wxSlider::SetRange( int minValue, int maxValue )
267
double fmin = (double)minValue;
268
double fmax = (double)maxValue;
270
if ((fabs(fmin-m_adjust->lower) < 0.2) &&
271
(fabs(fmax-m_adjust->upper) < 0.2))
276
m_adjust->lower = fmin;
277
m_adjust->upper = fmax;
278
m_adjust->step_increment = 1.0;
279
m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
283
gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
288
int wxSlider::GetMin() const
290
return (int)ceil(m_adjust->lower);
293
int wxSlider::GetMax() const
295
return (int)ceil(m_adjust->upper);
298
void wxSlider::SetPageSize( int pageSize )
300
double fpage = (double)pageSize;
302
if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
304
m_adjust->page_increment = fpage;
308
gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
313
int wxSlider::GetPageSize() const
315
return (int)ceil(m_adjust->page_increment);
318
void wxSlider::SetThumbLength( int len )
320
double flen = (double)len;
322
if (fabs(flen-m_adjust->page_size) < 0.2) return;
324
m_adjust->page_size = flen;
328
gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
333
int wxSlider::GetThumbLength() const
335
return (int)ceil(m_adjust->page_size);
338
void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
342
int wxSlider::GetLineSize() const
347
bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
349
GtkRange *range = GTK_RANGE(m_widget);
351
return (range->event_window == window);
353
return ( (window == GTK_WIDGET(range)->window)
354
|| (window == range->trough)
355
|| (window == range->slider)
356
|| (window == range->step_forw)
357
|| (window == range->step_back) );
361
void wxSlider::GtkDisableEvents()
363
gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
364
GTK_SIGNAL_FUNC(gtk_slider_callback),
368
void wxSlider::GtkEnableEvents()
370
gtk_signal_connect( GTK_OBJECT (m_adjust),
372
GTK_SIGNAL_FUNC(gtk_slider_callback),
378
wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
380
return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);