1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/slider.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/slider.h"
22 #include "wx/gtk1/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // compare 2 adjustment values up to some (hardcoded) precision
42 static inline bool AreSameAdjustValues(double x
, double y
)
44 return fabs(x
- y
) < 0.02;
47 // process a scroll event
49 ProcessScrollEvent(wxSlider
*win
, wxEventType evtType
, double dvalue
)
51 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
54 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
55 wxScrollEvent
event( evtType
, win
->GetId(), value
, orient
);
56 event
.SetEventObject( win
);
57 win
->GetEventHandler()->ProcessEvent( event
);
59 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
61 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
62 event2
.SetEventObject( win
);
63 win
->GetEventHandler()->ProcessEvent( event2
);
66 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
67 cevent
.SetEventObject( win
);
68 cevent
.SetInt( value
);
69 win
->GetEventHandler()->ProcessEvent( cevent
);
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
77 static void gtk_slider_callback( GtkAdjustment
*adjust
,
81 if (g_isIdle
) wxapp_install_idle_handler();
83 if (!win
->m_hasVMT
) return;
84 if (g_blockEventsOnDrag
) return;
86 const double dvalue
= adjust
->value
;
87 const double diff
= dvalue
- win
->m_oldPos
;
88 if ( AreSameAdjustValues(diff
, 0) )
92 evtType
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
94 ProcessScrollEvent(win
, evtType
, dvalue
);
96 win
->m_oldPos
= dvalue
;
99 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
100 GdkEventButton
* /* gdk_event */,
103 // indicate that the thumb is being dragged with the mouse
104 win
->m_isScrolling
= true;
109 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
110 GdkEventButton
* /* gdk_event */,
113 // not scrolling any longer
114 win
->m_isScrolling
= false;
116 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
117 GTK_RANGE(scale
)->adjustment
->value
);
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
130 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
131 int value
, int minValue
, int maxValue
,
132 const wxPoint
& pos
, const wxSize
& size
,
133 long style
, const wxValidator
& validator
, const wxString
& name
)
135 m_acceptsFocus
= true;
138 if (!PreCreation( parent
, pos
, size
) ||
139 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
141 wxFAIL_MSG( wxT("wxSlider creation failed") );
147 if (style
& wxSL_VERTICAL
)
148 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
150 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
152 if (style
& wxSL_LABELS
)
154 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
155 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
157 /* labels need more space and too small window will
158 cause junk to appear on the dialog */
159 if (style
& wxSL_VERTICAL
)
179 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
181 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
184 gtk_signal_connect( GTK_OBJECT(m_widget
),
185 "button_press_event",
186 (GtkSignalFunc
)gtk_slider_button_press_callback
,
188 gtk_signal_connect( GTK_OBJECT(m_widget
),
189 "button_release_event",
190 (GtkSignalFunc
)gtk_slider_button_release_callback
,
193 SetRange( minValue
, maxValue
);
196 m_parent
->DoAddChild( this );
203 int wxSlider::GetValue() const
205 return wxRound(m_adjust
->value
);
208 void wxSlider::SetValue( int value
)
210 double fpos
= (double)value
;
212 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
215 m_adjust
->value
= fpos
;
219 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
224 void wxSlider::SetRange( int minValue
, int maxValue
)
226 double fmin
= (double)minValue
;
227 double fmax
= (double)maxValue
;
229 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
230 (fabs(fmax
-m_adjust
->upper
) < 0.2))
235 m_adjust
->lower
= fmin
;
236 m_adjust
->upper
= fmax
;
237 m_adjust
->step_increment
= 1.0;
238 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
242 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
247 int wxSlider::GetMin() const
249 return (int)ceil(m_adjust
->lower
);
252 int wxSlider::GetMax() const
254 return (int)ceil(m_adjust
->upper
);
257 void wxSlider::SetPageSize( int pageSize
)
259 double fpage
= (double)pageSize
;
261 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
263 m_adjust
->page_increment
= fpage
;
267 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
272 int wxSlider::GetPageSize() const
274 return (int)ceil(m_adjust
->page_increment
);
277 void wxSlider::SetThumbLength( int len
)
279 double flen
= (double)len
;
281 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
283 m_adjust
->page_size
= flen
;
287 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
292 int wxSlider::GetThumbLength() const
294 return (int)ceil(m_adjust
->page_size
);
297 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
301 int wxSlider::GetLineSize() const
306 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
308 GtkRange
*range
= GTK_RANGE(m_widget
);
309 return ( (window
== GTK_WIDGET(range
)->window
)
310 || (window
== range
->trough
)
311 || (window
== range
->slider
)
312 || (window
== range
->step_forw
)
313 || (window
== range
->step_back
) );
316 void wxSlider::GtkDisableEvents()
318 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
319 GTK_SIGNAL_FUNC(gtk_slider_callback
),
323 void wxSlider::GtkEnableEvents()
325 gtk_signal_connect( GTK_OBJECT (m_adjust
),
327 GTK_SIGNAL_FUNC(gtk_slider_callback
),
333 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
335 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);
338 #endif // wxUSE_SLIDER