1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/slider.cpp
4 // Author: Robert Roebling
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"
25 #include "wx/gtk/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 static const float sensitivity
= 0.02;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 static void gtk_slider_callback( GtkAdjustment
*adjust
,
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (!win
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 float diff
= adjust
->value
- win
->m_oldPos
;
56 if (fabs(diff
) < sensitivity
) return;
58 win
->m_oldPos
= adjust
->value
;
60 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
62 double dvalue
= adjust
->value
;
63 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
65 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
68 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
69 event
.SetEventObject( win
);
70 win
->GetEventHandler()->ProcessEvent( event
);
72 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
73 cevent
.SetEventObject( win
);
74 cevent
.SetInt( value
);
75 win
->GetEventHandler()->ProcessEvent( cevent
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
84 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
85 int value
, int minValue
, int maxValue
,
86 const wxPoint
& pos
, const wxSize
& size
,
87 long style
, const wxValidator
& validator
, const wxString
& name
)
89 m_acceptsFocus
= TRUE
;
92 if (!PreCreation( parent
, pos
, size
) ||
93 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
95 wxFAIL_MSG( wxT("wxSlider creation failed") );
101 if (style
& wxSL_VERTICAL
)
102 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
104 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
106 if (style
& wxSL_LABELS
)
108 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
109 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
111 /* labels need more space and too small window will
112 cause junk to appear on the dialog */
113 if (style
& wxSL_VERTICAL
)
133 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
135 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
139 SetRange( minValue
, maxValue
);
142 m_parent
->DoAddChild( this );
149 int wxSlider::GetValue() const
151 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
152 // -0.9 is rounded to -1
153 double val
= m_adjust
->value
;
154 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
157 void wxSlider::SetValue( int value
)
159 float fpos
= (float)value
;
161 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
163 m_adjust
->value
= fpos
;
167 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
172 void wxSlider::SetRange( int minValue
, int maxValue
)
174 float fmin
= (float)minValue
;
175 float fmax
= (float)maxValue
;
177 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
178 (fabs(fmax
-m_adjust
->upper
) < 0.2))
183 m_adjust
->lower
= fmin
;
184 m_adjust
->upper
= fmax
;
185 m_adjust
->step_increment
= 1.0;
186 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
195 int wxSlider::GetMin() const
197 return (int)ceil(m_adjust
->lower
);
200 int wxSlider::GetMax() const
202 return (int)ceil(m_adjust
->upper
);
205 void wxSlider::SetPageSize( int pageSize
)
207 float fpage
= (float)pageSize
;
209 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
211 m_adjust
->page_increment
= fpage
;
215 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
220 int wxSlider::GetPageSize() const
222 return (int)ceil(m_adjust
->page_increment
);
225 void wxSlider::SetThumbLength( int len
)
227 float flen
= (float)len
;
229 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
231 m_adjust
->page_size
= flen
;
235 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
240 int wxSlider::GetThumbLength() const
242 return (int)ceil(m_adjust
->page_size
);
245 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
249 int wxSlider::GetLineSize() const
254 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
256 GtkRange
*range
= GTK_RANGE(m_widget
);
258 return (range
->event_window
== window
);
260 return ( (window
== GTK_WIDGET(range
)->window
)
261 || (window
== range
->trough
)
262 || (window
== range
->slider
)
263 || (window
== range
->step_forw
)
264 || (window
== range
->step_back
) );
268 void wxSlider::ApplyWidgetStyle()
271 gtk_widget_set_style( m_widget
, m_widgetStyle
);
274 void wxSlider::GtkDisableEvents()
276 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
277 GTK_SIGNAL_FUNC(gtk_slider_callback
),
281 void wxSlider::GtkEnableEvents()
283 gtk_signal_connect( GTK_OBJECT (m_adjust
),
285 GTK_SIGNAL_FUNC(gtk_slider_callback
),
291 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
293 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);