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
->HandleWindowEvent( event
);
59 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
61 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
62 event2
.SetEventObject( win
);
63 win
->HandleWindowEvent( event2
);
66 wxCommandEvent
cevent( wxEVT_SLIDER
, win
->GetId() );
67 cevent
.SetEventObject( win
);
68 cevent
.SetInt( value
);
69 win
->HandleWindowEvent( 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 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
129 int value
, int minValue
, int maxValue
,
130 const wxPoint
& pos
, const wxSize
& size
,
131 long style
, const wxValidator
& validator
, const wxString
& name
)
133 m_acceptsFocus
= true;
136 if (!PreCreation( parent
, pos
, size
) ||
137 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
139 wxFAIL_MSG( wxT("wxSlider creation failed") );
145 if (style
& wxSL_VERTICAL
)
146 m_widget
= gtk_vscale_new( NULL
);
148 m_widget
= gtk_hscale_new( NULL
);
150 if (style
& wxSL_LABELS
)
152 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
153 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
155 /* labels need more space and too small window will
156 cause junk to appear on the dialog */
157 if (style
& wxSL_VERTICAL
)
177 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
179 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
182 gtk_signal_connect( GTK_OBJECT(m_widget
),
183 "button_press_event",
184 (GtkSignalFunc
)gtk_slider_button_press_callback
,
186 gtk_signal_connect( GTK_OBJECT(m_widget
),
187 "button_release_event",
188 (GtkSignalFunc
)gtk_slider_button_release_callback
,
191 SetRange( minValue
, maxValue
);
194 m_parent
->DoAddChild( this );
201 int wxSlider::GetValue() const
203 return wxRound(m_adjust
->value
);
206 void wxSlider::SetValue( int value
)
208 double fpos
= (double)value
;
210 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
213 m_adjust
->value
= fpos
;
217 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
222 void wxSlider::SetRange( int minValue
, int maxValue
)
224 double fmin
= (double)minValue
;
225 double fmax
= (double)maxValue
;
227 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
228 (fabs(fmax
-m_adjust
->upper
) < 0.2))
233 m_adjust
->lower
= fmin
;
234 m_adjust
->upper
= fmax
;
235 m_adjust
->step_increment
= 1.0;
236 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
245 int wxSlider::GetMin() const
247 return (int)ceil(m_adjust
->lower
);
250 int wxSlider::GetMax() const
252 return (int)ceil(m_adjust
->upper
);
255 void wxSlider::SetPageSize( int pageSize
)
257 double fpage
= (double)pageSize
;
259 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
261 m_adjust
->page_increment
= fpage
;
265 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
270 int wxSlider::GetPageSize() const
272 return (int)ceil(m_adjust
->page_increment
);
275 void wxSlider::SetThumbLength( int len
)
277 double flen
= (double)len
;
279 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
281 m_adjust
->page_size
= flen
;
285 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
290 int wxSlider::GetThumbLength() const
292 return (int)ceil(m_adjust
->page_size
);
295 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
299 int wxSlider::GetLineSize() const
304 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
306 GtkRange
*range
= GTK_RANGE(m_widget
);
307 return ( (window
== GTK_WIDGET(range
)->window
)
308 || (window
== range
->trough
)
309 || (window
== range
->slider
)
310 || (window
== range
->step_forw
)
311 || (window
== range
->step_back
) );
314 void wxSlider::GtkDisableEvents()
316 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
317 GTK_SIGNAL_FUNC(gtk_slider_callback
),
321 void wxSlider::GtkEnableEvents()
323 gtk_signal_connect( GTK_OBJECT (m_adjust
),
325 GTK_SIGNAL_FUNC(gtk_slider_callback
),
331 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
333 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);
336 #endif // wxUSE_SLIDER