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) )
96 if ( win
->m_isScrolling
)
97 evtType
= wxEVT_SCROLL_THUMBTRACK
;
98 // it could seem that UP/DOWN are inversed but this is what wxMSW does
99 else if ( AreSameAdjustValues(diff
, adjust
->step_increment
) )
100 evtType
= wxEVT_SCROLL_LINEDOWN
;
101 else if ( AreSameAdjustValues(diff
, -adjust
->step_increment
) )
102 evtType
= wxEVT_SCROLL_LINEUP
;
103 else if ( AreSameAdjustValues(diff
, adjust
->page_increment
) )
104 evtType
= wxEVT_SCROLL_PAGEDOWN
;
105 else if ( AreSameAdjustValues(diff
, -adjust
->page_increment
) )
106 evtType
= wxEVT_SCROLL_PAGEUP
;
107 else if ( AreSameAdjustValues(adjust
->value
, adjust
->lower
) )
108 evtType
= wxEVT_SCROLL_TOP
;
109 else if ( AreSameAdjustValues(adjust
->value
, adjust
->upper
) )
110 evtType
= wxEVT_SCROLL_BOTTOM
;
112 ProcessScrollEvent(win
, evtType
, dvalue
);
114 win
->m_oldPos
= dvalue
;
117 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
118 GdkEventButton
* /* gdk_event */,
121 // indicate that the thumb is being dragged with the mouse
122 win
->m_isScrolling
= true;
127 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
128 GdkEventButton
* /* gdk_event */,
131 // not scrolling any longer
132 win
->m_isScrolling
= false;
134 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
135 GTK_RANGE(scale
)->adjustment
->value
);
142 //-----------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------
146 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
148 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
149 int value
, int minValue
, int maxValue
,
150 const wxPoint
& pos
, const wxSize
& size
,
151 long style
, const wxValidator
& validator
, const wxString
& name
)
153 m_acceptsFocus
= TRUE
;
156 if (!PreCreation( parent
, pos
, size
) ||
157 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
159 wxFAIL_MSG( wxT("wxSlider creation failed") );
165 if (style
& wxSL_VERTICAL
)
166 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
168 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
170 if (style
& wxSL_LABELS
)
172 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
173 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
175 /* labels need more space and too small window will
176 cause junk to appear on the dialog */
177 if (style
& wxSL_VERTICAL
)
197 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
199 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
201 if (style
& wxSL_INVERSE
)
202 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
205 g_signal_connect (m_widget
, "button_press_event",
206 G_CALLBACK (gtk_slider_button_press_callback
),
208 g_signal_connect (m_widget
, "button_release_event",
209 G_CALLBACK (gtk_slider_button_release_callback
),
212 SetRange( minValue
, maxValue
);
215 m_parent
->DoAddChild( this );
222 int wxSlider::GetValue() const
224 return AdjustValueToInt(m_adjust
->value
);
227 void wxSlider::SetValue( int value
)
229 double fpos
= (double)value
;
231 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
234 m_adjust
->value
= fpos
;
238 g_signal_emit_by_name (m_adjust
, "value_changed");
243 void wxSlider::SetRange( int minValue
, int maxValue
)
245 double fmin
= (double)minValue
;
246 double fmax
= (double)maxValue
;
248 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
249 (fabs(fmax
-m_adjust
->upper
) < 0.2))
254 m_adjust
->lower
= fmin
;
255 m_adjust
->upper
= fmax
;
256 m_adjust
->step_increment
= 1.0;
257 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
261 g_signal_emit_by_name (m_adjust
, "changed");
266 int wxSlider::GetMin() const
268 return (int)ceil(m_adjust
->lower
);
271 int wxSlider::GetMax() const
273 return (int)ceil(m_adjust
->upper
);
276 void wxSlider::SetPageSize( int pageSize
)
278 double fpage
= (double)pageSize
;
280 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
282 m_adjust
->page_increment
= fpage
;
286 g_signal_emit_by_name (m_adjust
, "changed");
291 int wxSlider::GetPageSize() const
293 return (int)ceil(m_adjust
->page_increment
);
296 void wxSlider::SetThumbLength( int len
)
298 double flen
= (double)len
;
300 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
302 m_adjust
->page_size
= flen
;
306 g_signal_emit_by_name (m_adjust
, "changed");
311 int wxSlider::GetThumbLength() const
313 return (int)ceil(m_adjust
->page_size
);
316 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
320 int wxSlider::GetLineSize() const
325 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
327 GtkRange
*range
= GTK_RANGE(m_widget
);
328 return (range
->event_window
== window
);
331 void wxSlider::GtkDisableEvents()
333 g_signal_handlers_disconnect_by_func (m_adjust
,
334 (gpointer
) gtk_slider_callback
,
338 void wxSlider::GtkEnableEvents()
340 g_signal_connect (m_adjust
, "value_changed",
341 G_CALLBACK (gtk_slider_callback
), this);
346 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
348 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);