1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/slider.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/slider.h"
21 #include "wx/gtk1/private.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // compare 2 adjustment values up to some (hardcoded) precision
41 static inline bool AreSameAdjustValues(double x
, double y
)
43 return fabs(x
- y
) < 0.02;
46 // process a scroll event
48 ProcessScrollEvent(wxSlider
*win
, wxEventType evtType
, double dvalue
)
50 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
53 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
54 wxScrollEvent
event( evtType
, win
->GetId(), value
, orient
);
55 event
.SetEventObject( win
);
56 win
->HandleWindowEvent( event
);
58 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
60 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
61 event2
.SetEventObject( win
);
62 win
->HandleWindowEvent( event2
);
65 wxCommandEvent
cevent( wxEVT_SLIDER
, win
->GetId() );
66 cevent
.SetEventObject( win
);
67 cevent
.SetInt( value
);
68 win
->HandleWindowEvent( cevent
);
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
76 static void gtk_slider_callback( GtkAdjustment
*adjust
,
80 if (g_isIdle
) wxapp_install_idle_handler();
82 if (!win
->m_hasVMT
) return;
83 if (g_blockEventsOnDrag
) return;
85 const double dvalue
= adjust
->value
;
86 const double diff
= dvalue
- win
->m_oldPos
;
87 if ( AreSameAdjustValues(diff
, 0) )
91 evtType
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
93 ProcessScrollEvent(win
, evtType
, dvalue
);
95 win
->m_oldPos
= dvalue
;
98 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
99 GdkEventButton
* /* gdk_event */,
102 // indicate that the thumb is being dragged with the mouse
103 win
->m_isScrolling
= true;
108 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
109 GdkEventButton
* /* gdk_event */,
112 // not scrolling any longer
113 win
->m_isScrolling
= false;
115 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
116 GTK_RANGE(scale
)->adjustment
->value
);
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
128 int value
, int minValue
, int maxValue
,
129 const wxPoint
& pos
, const wxSize
& size
,
130 long style
, const wxValidator
& validator
, const wxString
& name
)
132 m_acceptsFocus
= true;
135 if (!PreCreation( parent
, pos
, size
) ||
136 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
138 wxFAIL_MSG( wxT("wxSlider creation failed") );
144 if (style
& wxSL_VERTICAL
)
145 m_widget
= gtk_vscale_new( NULL
);
147 m_widget
= gtk_hscale_new( NULL
);
149 if (style
& wxSL_LABELS
)
151 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
152 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
154 /* labels need more space and too small window will
155 cause junk to appear on the dialog */
156 if (style
& wxSL_VERTICAL
)
176 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
178 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
181 gtk_signal_connect( GTK_OBJECT(m_widget
),
182 "button_press_event",
183 (GtkSignalFunc
)gtk_slider_button_press_callback
,
185 gtk_signal_connect( GTK_OBJECT(m_widget
),
186 "button_release_event",
187 (GtkSignalFunc
)gtk_slider_button_release_callback
,
190 SetRange( minValue
, maxValue
);
193 m_parent
->DoAddChild( this );
200 int wxSlider::GetValue() const
202 return wxRound(m_adjust
->value
);
205 void wxSlider::SetValue( int value
)
207 double fpos
= (double)value
;
209 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
212 m_adjust
->value
= fpos
;
216 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
221 void wxSlider::SetRange( int minValue
, int maxValue
)
223 double fmin
= (double)minValue
;
224 double fmax
= (double)maxValue
;
226 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
227 (fabs(fmax
-m_adjust
->upper
) < 0.2))
232 m_adjust
->lower
= fmin
;
233 m_adjust
->upper
= fmax
;
234 m_adjust
->step_increment
= 1.0;
235 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
239 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
244 int wxSlider::GetMin() const
246 return (int)ceil(m_adjust
->lower
);
249 int wxSlider::GetMax() const
251 return (int)ceil(m_adjust
->upper
);
254 void wxSlider::SetPageSize( int pageSize
)
256 double fpage
= (double)pageSize
;
258 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
260 m_adjust
->page_increment
= fpage
;
264 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
269 int wxSlider::GetPageSize() const
271 return (int)ceil(m_adjust
->page_increment
);
274 void wxSlider::SetThumbLength( int len
)
276 double flen
= (double)len
;
278 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
280 m_adjust
->page_size
= flen
;
284 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
289 int wxSlider::GetThumbLength() const
291 return (int)ceil(m_adjust
->page_size
);
294 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
298 int wxSlider::GetLineSize() const
303 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
305 GtkRange
*range
= GTK_RANGE(m_widget
);
306 return ( (window
== GTK_WIDGET(range
)->window
)
307 || (window
== range
->trough
)
308 || (window
== range
->slider
)
309 || (window
== range
->step_forw
)
310 || (window
== range
->step_back
) );
313 void wxSlider::GtkDisableEvents()
315 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
316 GTK_SIGNAL_FUNC(gtk_slider_callback
),
320 void wxSlider::GtkEnableEvents()
322 gtk_signal_connect( GTK_OBJECT (m_adjust
),
324 GTK_SIGNAL_FUNC(gtk_slider_callback
),
330 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
332 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);
335 #endif // wxUSE_SLIDER