1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
15 #include "wx/slider.h"
22 #include "wx/gtk/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern bool g_blockEventsOnDrag
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // compare 2 adjustment values up to some (hardcoded) precision
35 static inline bool AreSameAdjustValues(double x
, double y
)
37 return fabs(x
- y
) < 0.02;
40 static inline int AdjustValueToInt(double x
)
42 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
43 // -0.9 is rounded to -1
44 return (int)(x
< 0 ? x
- 0.5 : x
+ 0.5);
47 // process a scroll event
49 ProcessScrollEvent(wxSlider
*win
, wxEventType evtType
, double dvalue
)
51 const int orient
= win
->HasFlag(wxSL_VERTICAL
) ? wxVERTICAL
54 const int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
56 // if we have any "special" event (i.e. the value changed by a line or a
57 // page), send this specific event first
58 if ( evtType
!= wxEVT_NULL
)
60 wxScrollEvent
event( evtType
, win
->GetId(), value
, orient
);
61 event
.SetEventObject( win
);
62 win
->GetEventHandler()->ProcessEvent( event
);
65 // but, in any case, except if we're dragging the slider (and so the change
66 // is not definitive), send a generic "changed" event
67 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
69 wxScrollEvent
event(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
70 event
.SetEventObject( win
);
71 win
->GetEventHandler()->ProcessEvent( event
);
74 // and also generate a command event for compatibility
75 wxCommandEvent
event( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
76 event
.SetEventObject( win
);
77 event
.SetInt( value
);
78 win
->GetEventHandler()->ProcessEvent( event
);
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
86 static void gtk_slider_callback( GtkAdjustment
*adjust
,
89 if (g_isIdle
) wxapp_install_idle_handler();
91 if (!win
->m_hasVMT
) return;
92 if (g_blockEventsOnDrag
) return;
94 const double dvalue
= adjust
->value
;
95 const double diff
= dvalue
- win
->m_oldPos
;
96 if ( AreSameAdjustValues(diff
, 0) )
100 if ( win
->m_isScrolling
)
101 evtType
= wxEVT_SCROLL_THUMBTRACK
;
102 // it could seem that UP/DOWN are inversed but this is what wxMSW does
103 else if ( AreSameAdjustValues(diff
, adjust
->step_increment
) )
104 evtType
= wxEVT_SCROLL_LINEDOWN
;
105 else if ( AreSameAdjustValues(diff
, -adjust
->step_increment
) )
106 evtType
= wxEVT_SCROLL_LINEUP
;
107 else if ( AreSameAdjustValues(diff
, adjust
->page_increment
) )
108 evtType
= wxEVT_SCROLL_PAGEDOWN
;
109 else if ( AreSameAdjustValues(diff
, -adjust
->page_increment
) )
110 evtType
= wxEVT_SCROLL_PAGEUP
;
111 else if ( AreSameAdjustValues(adjust
->value
, adjust
->lower
) )
112 evtType
= wxEVT_SCROLL_TOP
;
113 else if ( AreSameAdjustValues(adjust
->value
, adjust
->upper
) )
114 evtType
= wxEVT_SCROLL_BOTTOM
;
116 evtType
= wxEVT_NULL
; // wxEVT_SCROLL_CHANGED will still be generated
118 ProcessScrollEvent(win
, evtType
, dvalue
);
120 win
->m_oldPos
= dvalue
;
123 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
124 GdkEventButton
* /* gdk_event */,
127 // indicate that the thumb is being dragged with the mouse
128 win
->m_isScrolling
= true;
133 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
134 GdkEventButton
* /* gdk_event */,
137 // not scrolling any longer
138 win
->m_isScrolling
= false;
140 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
141 GTK_RANGE(scale
)->adjustment
->value
);
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
152 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
154 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
155 int value
, int minValue
, int maxValue
,
156 const wxPoint
& pos
, const wxSize
& size
,
157 long style
, const wxValidator
& validator
, const wxString
& name
)
159 m_acceptsFocus
= true;
162 if (!PreCreation( parent
, pos
, size
) ||
163 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
165 wxFAIL_MSG( wxT("wxSlider creation failed") );
171 if (style
& wxSL_VERTICAL
)
172 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
174 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
176 if (style
& wxSL_LABELS
)
178 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
179 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
181 /* labels need more space and too small window will
182 cause junk to appear on the dialog */
183 if (style
& wxSL_VERTICAL
)
203 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
205 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
207 if (style
& wxSL_INVERSE
)
208 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
211 g_signal_connect (m_widget
, "button_press_event",
212 G_CALLBACK (gtk_slider_button_press_callback
),
214 g_signal_connect (m_widget
, "button_release_event",
215 G_CALLBACK (gtk_slider_button_release_callback
),
218 SetRange( minValue
, maxValue
);
221 m_parent
->DoAddChild( this );
228 int wxSlider::GetValue() const
230 return AdjustValueToInt(m_adjust
->value
);
233 void wxSlider::SetValue( int value
)
235 double fpos
= (double)value
;
237 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
240 m_adjust
->value
= fpos
;
244 g_signal_emit_by_name (m_adjust
, "value_changed");
249 void wxSlider::SetRange( int minValue
, int maxValue
)
251 double fmin
= (double)minValue
;
252 double fmax
= (double)maxValue
;
254 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
255 (fabs(fmax
-m_adjust
->upper
) < 0.2))
260 m_adjust
->lower
= fmin
;
261 m_adjust
->upper
= fmax
;
262 m_adjust
->step_increment
= 1.0;
263 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
267 g_signal_emit_by_name (m_adjust
, "changed");
272 int wxSlider::GetMin() const
274 return (int)ceil(m_adjust
->lower
);
277 int wxSlider::GetMax() const
279 return (int)ceil(m_adjust
->upper
);
282 void wxSlider::SetPageSize( int pageSize
)
284 double fpage
= (double)pageSize
;
286 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
288 m_adjust
->page_increment
= fpage
;
292 g_signal_emit_by_name (m_adjust
, "changed");
297 int wxSlider::GetPageSize() const
299 return (int)ceil(m_adjust
->page_increment
);
302 void wxSlider::SetThumbLength( int len
)
304 double flen
= (double)len
;
306 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
308 m_adjust
->page_size
= flen
;
312 g_signal_emit_by_name (m_adjust
, "changed");
317 int wxSlider::GetThumbLength() const
319 return (int)ceil(m_adjust
->page_size
);
322 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
326 int wxSlider::GetLineSize() const
331 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
333 GtkRange
*range
= GTK_RANGE(m_widget
);
334 return (range
->event_window
== window
);
337 void wxSlider::GtkDisableEvents()
339 g_signal_handlers_disconnect_by_func (m_adjust
,
340 (gpointer
) gtk_slider_callback
,
344 void wxSlider::GtkEnableEvents()
346 g_signal_connect (m_adjust
, "value_changed",
347 G_CALLBACK (gtk_slider_callback
), this);
352 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
354 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);
357 #endif // wxUSE_SLIDER