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/gtk1/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) )
96 evtType
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
98 ProcessScrollEvent(win
, evtType
, dvalue
);
100 win
->m_oldPos
= dvalue
;
103 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
104 GdkEventButton
* /* gdk_event */,
107 // indicate that the thumb is being dragged with the mouse
108 win
->m_isScrolling
= true;
113 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
114 GdkEventButton
* /* gdk_event */,
117 // not scrolling any longer
118 win
->m_isScrolling
= false;
120 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
121 GTK_RANGE(scale
)->adjustment
->value
);
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
134 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
135 int value
, int minValue
, int maxValue
,
136 const wxPoint
& pos
, const wxSize
& size
,
137 long style
, const wxValidator
& validator
, const wxString
& name
)
139 m_acceptsFocus
= TRUE
;
142 if (!PreCreation( parent
, pos
, size
) ||
143 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
145 wxFAIL_MSG( wxT("wxSlider creation failed") );
151 if (style
& wxSL_VERTICAL
)
152 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
154 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
156 if (style
& wxSL_LABELS
)
158 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
159 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
161 /* labels need more space and too small window will
162 cause junk to appear on the dialog */
163 if (style
& wxSL_VERTICAL
)
183 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
185 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
188 gtk_signal_connect( GTK_OBJECT(m_widget
),
189 "button_press_event",
190 (GtkSignalFunc
)gtk_slider_button_press_callback
,
192 gtk_signal_connect( GTK_OBJECT(m_widget
),
193 "button_release_event",
194 (GtkSignalFunc
)gtk_slider_button_release_callback
,
197 SetRange( minValue
, maxValue
);
200 m_parent
->DoAddChild( this );
207 int wxSlider::GetValue() const
209 return AdjustValueToInt(m_adjust
->value
);
212 void wxSlider::SetValue( int value
)
214 double fpos
= (double)value
;
216 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
219 m_adjust
->value
= fpos
;
223 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
228 void wxSlider::SetRange( int minValue
, int maxValue
)
230 double fmin
= (double)minValue
;
231 double fmax
= (double)maxValue
;
233 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
234 (fabs(fmax
-m_adjust
->upper
) < 0.2))
239 m_adjust
->lower
= fmin
;
240 m_adjust
->upper
= fmax
;
241 m_adjust
->step_increment
= 1.0;
242 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
246 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
251 int wxSlider::GetMin() const
253 return (int)ceil(m_adjust
->lower
);
256 int wxSlider::GetMax() const
258 return (int)ceil(m_adjust
->upper
);
261 void wxSlider::SetPageSize( int pageSize
)
263 double fpage
= (double)pageSize
;
265 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
267 m_adjust
->page_increment
= fpage
;
271 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
276 int wxSlider::GetPageSize() const
278 return (int)ceil(m_adjust
->page_increment
);
281 void wxSlider::SetThumbLength( int len
)
283 double flen
= (double)len
;
285 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
287 m_adjust
->page_size
= flen
;
291 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
296 int wxSlider::GetThumbLength() const
298 return (int)ceil(m_adjust
->page_size
);
301 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
305 int wxSlider::GetLineSize() const
310 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
312 GtkRange
*range
= GTK_RANGE(m_widget
);
313 return ( (window
== GTK_WIDGET(range
)->window
)
314 || (window
== range
->trough
)
315 || (window
== range
->slider
)
316 || (window
== range
->step_forw
)
317 || (window
== range
->step_back
) );
320 void wxSlider::GtkDisableEvents()
322 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
323 GTK_SIGNAL_FUNC(gtk_slider_callback
),
327 void wxSlider::GtkEnableEvents()
329 gtk_signal_connect( GTK_OBJECT (m_adjust
),
331 GTK_SIGNAL_FUNC(gtk_slider_callback
),
337 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
339 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);