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
,
85 if (g_isIdle
) wxapp_install_idle_handler();
87 if (!win
->m_hasVMT
) return;
88 if (g_blockEventsOnDrag
) return;
90 const double dvalue
= adjust
->value
;
91 const double diff
= dvalue
- win
->m_oldPos
;
92 if ( AreSameAdjustValues(diff
, 0) )
97 if ( win
->m_isScrolling
)
98 evtType
= wxEVT_SCROLL_THUMBTRACK
;
99 // it could seem that UP/DOWN are inversed but this is what wxMSW does
100 else if ( AreSameAdjustValues(diff
, adjust
->step_increment
) )
101 evtType
= wxEVT_SCROLL_LINEDOWN
;
102 else if ( AreSameAdjustValues(diff
, -adjust
->step_increment
) )
103 evtType
= wxEVT_SCROLL_LINEUP
;
104 else if ( AreSameAdjustValues(diff
, adjust
->page_increment
) )
105 evtType
= wxEVT_SCROLL_PAGEDOWN
;
106 else if ( AreSameAdjustValues(diff
, -adjust
->page_increment
) )
107 evtType
= wxEVT_SCROLL_PAGEUP
;
108 else if ( AreSameAdjustValues(adjust
->value
, adjust
->lower
) )
109 evtType
= wxEVT_SCROLL_TOP
;
110 else if ( AreSameAdjustValues(adjust
->value
, adjust
->upper
) )
111 evtType
= wxEVT_SCROLL_BOTTOM
;
113 evtType
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
116 ProcessScrollEvent(win
, evtType
, dvalue
);
118 win
->m_oldPos
= dvalue
;
121 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
122 GdkEventButton
* /* gdk_event */,
125 // indicate that the thumb is being dragged with the mouse
126 win
->m_isScrolling
= true;
131 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
132 GdkEventButton
* /* gdk_event */,
135 // not scrolling any longer
136 win
->m_isScrolling
= false;
138 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
139 GTK_RANGE(scale
)->adjustment
->value
);
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
152 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
153 int value
, int minValue
, int maxValue
,
154 const wxPoint
& pos
, const wxSize
& size
,
155 long style
, const wxValidator
& validator
, const wxString
& name
)
157 m_acceptsFocus
= TRUE
;
160 if (!PreCreation( parent
, pos
, size
) ||
161 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
163 wxFAIL_MSG( wxT("wxSlider creation failed") );
169 if (style
& wxSL_VERTICAL
)
170 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
172 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
174 if (style
& wxSL_LABELS
)
176 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
177 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
179 /* labels need more space and too small window will
180 cause junk to appear on the dialog */
181 if (style
& wxSL_VERTICAL
)
201 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
203 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
206 if (style
& wxSL_INVERSE
)
207 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
211 gtk_signal_connect( GTK_OBJECT(m_widget
),
212 "button_press_event",
213 (GtkSignalFunc
)gtk_slider_button_press_callback
,
215 gtk_signal_connect( GTK_OBJECT(m_widget
),
216 "button_release_event",
217 (GtkSignalFunc
)gtk_slider_button_release_callback
,
220 SetRange( minValue
, maxValue
);
223 m_parent
->DoAddChild( this );
230 int wxSlider::GetValue() const
232 return AdjustValueToInt(m_adjust
->value
);
235 void wxSlider::SetValue( int value
)
237 double fpos
= (double)value
;
239 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
242 m_adjust
->value
= fpos
;
246 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
251 void wxSlider::SetRange( int minValue
, int maxValue
)
253 double fmin
= (double)minValue
;
254 double fmax
= (double)maxValue
;
256 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
257 (fabs(fmax
-m_adjust
->upper
) < 0.2))
262 m_adjust
->lower
= fmin
;
263 m_adjust
->upper
= fmax
;
264 m_adjust
->step_increment
= 1.0;
265 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
269 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
274 int wxSlider::GetMin() const
276 return (int)ceil(m_adjust
->lower
);
279 int wxSlider::GetMax() const
281 return (int)ceil(m_adjust
->upper
);
284 void wxSlider::SetPageSize( int pageSize
)
286 double fpage
= (double)pageSize
;
288 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
290 m_adjust
->page_increment
= fpage
;
294 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
299 int wxSlider::GetPageSize() const
301 return (int)ceil(m_adjust
->page_increment
);
304 void wxSlider::SetThumbLength( int len
)
306 double flen
= (double)len
;
308 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
310 m_adjust
->page_size
= flen
;
314 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
319 int wxSlider::GetThumbLength() const
321 return (int)ceil(m_adjust
->page_size
);
324 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
328 int wxSlider::GetLineSize() const
333 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
335 GtkRange
*range
= GTK_RANGE(m_widget
);
337 return (range
->event_window
== window
);
339 return ( (window
== GTK_WIDGET(range
)->window
)
340 || (window
== range
->trough
)
341 || (window
== range
->slider
)
342 || (window
== range
->step_forw
)
343 || (window
== range
->step_back
) );
347 void wxSlider::GtkDisableEvents()
349 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
350 GTK_SIGNAL_FUNC(gtk_slider_callback
),
354 void wxSlider::GtkEnableEvents()
356 gtk_signal_connect( GTK_OBJECT (m_adjust
),
358 GTK_SIGNAL_FUNC(gtk_slider_callback
),
364 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
366 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);