1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/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"
13 #include "wx/slider.h"
19 #include "wx/gtk/private.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // compare 2 adjustment values up to some (hardcoded) precision
39 static inline bool AreSameAdjustValues(double x
, double y
)
41 return fabs(x
- y
) < 0.02;
44 static inline int AdjustValueToInt(double x
)
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);
51 // process a scroll event
53 ProcessScrollEvent(wxSlider
*win
, wxEventType evtType
, double dvalue
)
55 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
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
);
63 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
65 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
66 event2
.SetEventObject( win
);
67 win
->GetEventHandler()->ProcessEvent( event2
);
70 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
71 cevent
.SetEventObject( win
);
72 cevent
.SetInt( value
);
73 win
->GetEventHandler()->ProcessEvent( cevent
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
81 static void gtk_slider_callback( GtkAdjustment
*adjust
,
84 if (g_isIdle
) wxapp_install_idle_handler();
86 if (!win
->m_hasVMT
) return;
87 if (g_blockEventsOnDrag
) return;
89 const double dvalue
= adjust
->value
;
90 const double diff
= dvalue
- win
->m_oldPos
;
91 if ( AreSameAdjustValues(diff
, 0) )
95 if ( win
->m_isScrolling
)
96 evtType
= wxEVT_SCROLL_THUMBTRACK
;
97 // it could seem that UP/DOWN are inversed but this is what wxMSW does
98 else if ( AreSameAdjustValues(diff
, adjust
->step_increment
) )
99 evtType
= wxEVT_SCROLL_LINEDOWN
;
100 else if ( AreSameAdjustValues(diff
, -adjust
->step_increment
) )
101 evtType
= wxEVT_SCROLL_LINEUP
;
102 else if ( AreSameAdjustValues(diff
, adjust
->page_increment
) )
103 evtType
= wxEVT_SCROLL_PAGEDOWN
;
104 else if ( AreSameAdjustValues(diff
, -adjust
->page_increment
) )
105 evtType
= wxEVT_SCROLL_PAGEUP
;
106 else if ( AreSameAdjustValues(adjust
->value
, adjust
->lower
) )
107 evtType
= wxEVT_SCROLL_TOP
;
108 else if ( AreSameAdjustValues(adjust
->value
, adjust
->upper
) )
109 evtType
= wxEVT_SCROLL_BOTTOM
;
111 ProcessScrollEvent(win
, evtType
, dvalue
);
113 win
->m_oldPos
= dvalue
;
116 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
117 GdkEventButton
* /* gdk_event */,
120 // indicate that the thumb is being dragged with the mouse
121 win
->m_isScrolling
= true;
126 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
127 GdkEventButton
* /* gdk_event */,
130 // not scrolling any longer
131 win
->m_isScrolling
= false;
133 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
134 GTK_RANGE(scale
)->adjustment
->value
);
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
145 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
147 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
148 int value
, int minValue
, int maxValue
,
149 const wxPoint
& pos
, const wxSize
& size
,
150 long style
, const wxValidator
& validator
, const wxString
& name
)
152 m_acceptsFocus
= TRUE
;
155 if (!PreCreation( parent
, pos
, size
) ||
156 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
158 wxFAIL_MSG( wxT("wxSlider creation failed") );
164 if (style
& wxSL_VERTICAL
)
165 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
167 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
169 if (style
& wxSL_LABELS
)
171 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
172 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
174 /* labels need more space and too small window will
175 cause junk to appear on the dialog */
176 if (style
& wxSL_VERTICAL
)
196 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
198 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
200 if (style
& wxSL_INVERSE
)
201 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
204 g_signal_connect (m_widget
, "button_press_event",
205 G_CALLBACK (gtk_slider_button_press_callback
),
207 g_signal_connect (m_widget
, "button_release_event",
208 G_CALLBACK (gtk_slider_button_release_callback
),
211 SetRange( minValue
, maxValue
);
214 m_parent
->DoAddChild( this );
221 int wxSlider::GetValue() const
223 return AdjustValueToInt(m_adjust
->value
);
226 void wxSlider::SetValue( int value
)
228 double fpos
= (double)value
;
230 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
233 m_adjust
->value
= fpos
;
237 g_signal_emit_by_name (m_adjust
, "value_changed");
242 void wxSlider::SetRange( int minValue
, int maxValue
)
244 double fmin
= (double)minValue
;
245 double fmax
= (double)maxValue
;
247 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
248 (fabs(fmax
-m_adjust
->upper
) < 0.2))
253 m_adjust
->lower
= fmin
;
254 m_adjust
->upper
= fmax
;
255 m_adjust
->step_increment
= 1.0;
256 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
260 g_signal_emit_by_name (m_adjust
, "changed");
265 int wxSlider::GetMin() const
267 return (int)ceil(m_adjust
->lower
);
270 int wxSlider::GetMax() const
272 return (int)ceil(m_adjust
->upper
);
275 void wxSlider::SetPageSize( int pageSize
)
277 double fpage
= (double)pageSize
;
279 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
281 m_adjust
->page_increment
= fpage
;
285 g_signal_emit_by_name (m_adjust
, "changed");
290 int wxSlider::GetPageSize() const
292 return (int)ceil(m_adjust
->page_increment
);
295 void wxSlider::SetThumbLength( int len
)
297 double flen
= (double)len
;
299 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
301 m_adjust
->page_size
= flen
;
305 g_signal_emit_by_name (m_adjust
, "changed");
310 int wxSlider::GetThumbLength() const
312 return (int)ceil(m_adjust
->page_size
);
315 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
319 int wxSlider::GetLineSize() const
324 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
326 GtkRange
*range
= GTK_RANGE(m_widget
);
327 return (range
->event_window
== window
);
330 void wxSlider::GtkDisableEvents()
332 g_signal_handlers_disconnect_by_func (m_adjust
,
333 (gpointer
) gtk_slider_callback
,
337 void wxSlider::GtkEnableEvents()
339 g_signal_connect (m_adjust
, "value_changed",
340 G_CALLBACK (gtk_slider_callback
), this);
345 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
347 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);