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 static inline int AdjustValueToInt(double x
)
49 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
50 // -0.9 is rounded to -1
51 return (int)(x
< 0 ? x
- 0.5 : x
+ 0.5);
54 // process a scroll event
56 ProcessScrollEvent(wxSlider
*win
, wxEventType evtType
, double dvalue
)
58 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
61 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
62 wxScrollEvent
event( evtType
, win
->GetId(), value
, orient
);
63 event
.SetEventObject( win
);
64 win
->GetEventHandler()->ProcessEvent( event
);
66 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
68 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
69 event2
.SetEventObject( win
);
70 win
->GetEventHandler()->ProcessEvent( event2
);
73 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
74 cevent
.SetEventObject( win
);
75 cevent
.SetInt( value
);
76 win
->GetEventHandler()->ProcessEvent( cevent
);
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
84 static void gtk_slider_callback( GtkAdjustment
*adjust
,
88 if (g_isIdle
) wxapp_install_idle_handler();
90 if (!win
->m_hasVMT
) return;
91 if (g_blockEventsOnDrag
) return;
93 const double dvalue
= adjust
->value
;
94 const double diff
= dvalue
- win
->m_oldPos
;
95 if ( AreSameAdjustValues(diff
, 0) )
99 evtType
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
101 ProcessScrollEvent(win
, evtType
, dvalue
);
103 win
->m_oldPos
= dvalue
;
106 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
107 GdkEventButton
* /* gdk_event */,
110 // indicate that the thumb is being dragged with the mouse
111 win
->m_isScrolling
= true;
116 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
117 GdkEventButton
* /* gdk_event */,
120 // not scrolling any longer
121 win
->m_isScrolling
= false;
123 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
124 GTK_RANGE(scale
)->adjustment
->value
);
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
137 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
138 int value
, int minValue
, int maxValue
,
139 const wxPoint
& pos
, const wxSize
& size
,
140 long style
, const wxValidator
& validator
, const wxString
& name
)
142 m_acceptsFocus
= true;
145 if (!PreCreation( parent
, pos
, size
) ||
146 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
148 wxFAIL_MSG( wxT("wxSlider creation failed") );
154 if (style
& wxSL_VERTICAL
)
155 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
157 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
159 if (style
& wxSL_LABELS
)
161 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
162 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
164 /* labels need more space and too small window will
165 cause junk to appear on the dialog */
166 if (style
& wxSL_VERTICAL
)
186 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
188 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
191 gtk_signal_connect( GTK_OBJECT(m_widget
),
192 "button_press_event",
193 (GtkSignalFunc
)gtk_slider_button_press_callback
,
195 gtk_signal_connect( GTK_OBJECT(m_widget
),
196 "button_release_event",
197 (GtkSignalFunc
)gtk_slider_button_release_callback
,
200 SetRange( minValue
, maxValue
);
203 m_parent
->DoAddChild( this );
210 int wxSlider::GetValue() const
212 return AdjustValueToInt(m_adjust
->value
);
215 void wxSlider::SetValue( int value
)
217 double fpos
= (double)value
;
219 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
222 m_adjust
->value
= fpos
;
226 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
231 void wxSlider::SetRange( int minValue
, int maxValue
)
233 double fmin
= (double)minValue
;
234 double fmax
= (double)maxValue
;
236 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
237 (fabs(fmax
-m_adjust
->upper
) < 0.2))
242 m_adjust
->lower
= fmin
;
243 m_adjust
->upper
= fmax
;
244 m_adjust
->step_increment
= 1.0;
245 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
249 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
254 int wxSlider::GetMin() const
256 return (int)ceil(m_adjust
->lower
);
259 int wxSlider::GetMax() const
261 return (int)ceil(m_adjust
->upper
);
264 void wxSlider::SetPageSize( int pageSize
)
266 double fpage
= (double)pageSize
;
268 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
270 m_adjust
->page_increment
= fpage
;
274 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
279 int wxSlider::GetPageSize() const
281 return (int)ceil(m_adjust
->page_increment
);
284 void wxSlider::SetThumbLength( int len
)
286 double flen
= (double)len
;
288 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
290 m_adjust
->page_size
= flen
;
294 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
299 int wxSlider::GetThumbLength() const
301 return (int)ceil(m_adjust
->page_size
);
304 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
308 int wxSlider::GetLineSize() const
313 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
315 GtkRange
*range
= GTK_RANGE(m_widget
);
316 return ( (window
== GTK_WIDGET(range
)->window
)
317 || (window
== range
->trough
)
318 || (window
== range
->slider
)
319 || (window
== range
->step_forw
)
320 || (window
== range
->step_back
) );
323 void wxSlider::GtkDisableEvents()
325 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
326 GTK_SIGNAL_FUNC(gtk_slider_callback
),
330 void wxSlider::GtkEnableEvents()
332 gtk_signal_connect( GTK_OBJECT (m_adjust
),
334 GTK_SIGNAL_FUNC(gtk_slider_callback
),
340 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
342 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);
345 #endif // wxUSE_SLIDER