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 const int orient
= win
->HasFlag(wxSL_VERTICAL
) ? wxVERTICAL
58 const int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
60 // if we have any "special" event (i.e. the value changed by a line or a
61 // page), send this specific event first
62 if ( evtType
!= wxEVT_NULL
)
64 wxScrollEvent
event( evtType
, win
->GetId(), value
, orient
);
65 event
.SetEventObject( win
);
66 win
->GetEventHandler()->ProcessEvent( event
);
69 // but, in any case, except if we're dragging the slider (and so the change
70 // is not definitive), send a generic "changed" event
71 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
73 wxScrollEvent
event(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
74 event
.SetEventObject( win
);
75 win
->GetEventHandler()->ProcessEvent( event
);
78 // and also generate a command event for compatibility
79 wxCommandEvent
event( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
80 event
.SetEventObject( win
);
81 event
.SetInt( value
);
82 win
->GetEventHandler()->ProcessEvent( event
);
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
90 static void gtk_slider_callback( GtkAdjustment
*adjust
,
93 if (g_isIdle
) wxapp_install_idle_handler();
95 if (!win
->m_hasVMT
) return;
96 if (g_blockEventsOnDrag
) return;
98 const double dvalue
= adjust
->value
;
99 const double diff
= dvalue
- win
->m_oldPos
;
100 if ( AreSameAdjustValues(diff
, 0) )
104 if ( win
->m_isScrolling
)
105 evtType
= wxEVT_SCROLL_THUMBTRACK
;
106 // it could seem that UP/DOWN are inversed but this is what wxMSW does
107 else if ( AreSameAdjustValues(diff
, adjust
->step_increment
) )
108 evtType
= wxEVT_SCROLL_LINEDOWN
;
109 else if ( AreSameAdjustValues(diff
, -adjust
->step_increment
) )
110 evtType
= wxEVT_SCROLL_LINEUP
;
111 else if ( AreSameAdjustValues(diff
, adjust
->page_increment
) )
112 evtType
= wxEVT_SCROLL_PAGEDOWN
;
113 else if ( AreSameAdjustValues(diff
, -adjust
->page_increment
) )
114 evtType
= wxEVT_SCROLL_PAGEUP
;
115 else if ( AreSameAdjustValues(adjust
->value
, adjust
->lower
) )
116 evtType
= wxEVT_SCROLL_TOP
;
117 else if ( AreSameAdjustValues(adjust
->value
, adjust
->upper
) )
118 evtType
= wxEVT_SCROLL_BOTTOM
;
120 evtType
= wxEVT_NULL
; // wxEVT_SCROLL_CHANGED will still be generated
122 ProcessScrollEvent(win
, evtType
, dvalue
);
124 win
->m_oldPos
= dvalue
;
127 static gint
gtk_slider_button_press_callback( GtkWidget
* /* widget */,
128 GdkEventButton
* /* gdk_event */,
131 // indicate that the thumb is being dragged with the mouse
132 win
->m_isScrolling
= true;
137 static gint
gtk_slider_button_release_callback( GtkWidget
*scale
,
138 GdkEventButton
* /* gdk_event */,
141 // not scrolling any longer
142 win
->m_isScrolling
= false;
144 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
,
145 GTK_RANGE(scale
)->adjustment
->value
);
152 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
156 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
158 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
159 int value
, int minValue
, int maxValue
,
160 const wxPoint
& pos
, const wxSize
& size
,
161 long style
, const wxValidator
& validator
, const wxString
& name
)
163 m_acceptsFocus
= TRUE
;
166 if (!PreCreation( parent
, pos
, size
) ||
167 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
169 wxFAIL_MSG( wxT("wxSlider creation failed") );
175 if (style
& wxSL_VERTICAL
)
176 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
178 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
180 if (style
& wxSL_LABELS
)
182 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
183 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
185 /* labels need more space and too small window will
186 cause junk to appear on the dialog */
187 if (style
& wxSL_VERTICAL
)
207 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
209 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
211 if (style
& wxSL_INVERSE
)
212 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
215 g_signal_connect (m_widget
, "button_press_event",
216 G_CALLBACK (gtk_slider_button_press_callback
),
218 g_signal_connect (m_widget
, "button_release_event",
219 G_CALLBACK (gtk_slider_button_release_callback
),
222 SetRange( minValue
, maxValue
);
225 m_parent
->DoAddChild( this );
232 int wxSlider::GetValue() const
234 return AdjustValueToInt(m_adjust
->value
);
237 void wxSlider::SetValue( int value
)
239 double fpos
= (double)value
;
241 if ( AreSameAdjustValues(fpos
, m_adjust
->value
) )
244 m_adjust
->value
= fpos
;
248 g_signal_emit_by_name (m_adjust
, "value_changed");
253 void wxSlider::SetRange( int minValue
, int maxValue
)
255 double fmin
= (double)minValue
;
256 double fmax
= (double)maxValue
;
258 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
259 (fabs(fmax
-m_adjust
->upper
) < 0.2))
264 m_adjust
->lower
= fmin
;
265 m_adjust
->upper
= fmax
;
266 m_adjust
->step_increment
= 1.0;
267 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
271 g_signal_emit_by_name (m_adjust
, "changed");
276 int wxSlider::GetMin() const
278 return (int)ceil(m_adjust
->lower
);
281 int wxSlider::GetMax() const
283 return (int)ceil(m_adjust
->upper
);
286 void wxSlider::SetPageSize( int pageSize
)
288 double fpage
= (double)pageSize
;
290 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
292 m_adjust
->page_increment
= fpage
;
296 g_signal_emit_by_name (m_adjust
, "changed");
301 int wxSlider::GetPageSize() const
303 return (int)ceil(m_adjust
->page_increment
);
306 void wxSlider::SetThumbLength( int len
)
308 double flen
= (double)len
;
310 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
312 m_adjust
->page_size
= flen
;
316 g_signal_emit_by_name (m_adjust
, "changed");
321 int wxSlider::GetThumbLength() const
323 return (int)ceil(m_adjust
->page_size
);
326 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
330 int wxSlider::GetLineSize() const
335 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
337 GtkRange
*range
= GTK_RANGE(m_widget
);
338 return (range
->event_window
== window
);
341 void wxSlider::GtkDisableEvents()
343 g_signal_handlers_disconnect_by_func (m_adjust
,
344 (gpointer
) gtk_slider_callback
,
348 void wxSlider::GtkEnableEvents()
350 g_signal_connect (m_adjust
, "value_changed",
351 G_CALLBACK (gtk_slider_callback
), this);
356 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
358 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);