1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/slider.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "slider.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/slider.h"
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 static const float sensitivity
= 0.02;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 static void gtk_slider_callback( GtkAdjustment
*adjust
,
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!win
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 float diff
= adjust
->value
- win
->m_oldPos
;
55 if (fabs(diff
) < sensitivity
) return;
57 win
->m_oldPos
= adjust
->value
;
59 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
61 double dvalue
= adjust
->value
;
62 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
64 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
67 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
68 event
.SetEventObject( win
);
69 win
->GetEventHandler()->ProcessEvent( event
);
71 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
72 cevent
.SetEventObject( win
);
73 cevent
.SetInt( value
);
74 win
->GetEventHandler()->ProcessEvent( cevent
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
84 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
85 int value
, int minValue
, int maxValue
,
86 const wxPoint
& pos
, const wxSize
& size
,
87 long style
, const wxValidator
& validator
, const wxString
& name
)
89 m_acceptsFocus
= TRUE
;
92 if (!PreCreation( parent
, pos
, size
) ||
93 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
95 wxFAIL_MSG( wxT("wxSlider creation failed") );
101 if (style
& wxSL_VERTICAL
)
102 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
104 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
106 if (style
& wxSL_LABELS
)
108 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
109 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
111 /* labels need more space and too small window will
112 cause junk to appear on the dialog */
113 if (style
& wxSL_VERTICAL
)
133 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
135 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
138 if (style
& wxSL_INVERSE
)
139 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
144 SetRange( minValue
, maxValue
);
147 m_parent
->DoAddChild( this );
154 int wxSlider::GetValue() const
156 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
157 // -0.9 is rounded to -1
158 double val
= m_adjust
->value
;
159 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
162 void wxSlider::SetValue( int value
)
164 float fpos
= (float)value
;
166 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
168 m_adjust
->value
= fpos
;
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
177 void wxSlider::SetRange( int minValue
, int maxValue
)
179 float fmin
= (float)minValue
;
180 float fmax
= (float)maxValue
;
182 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
183 (fabs(fmax
-m_adjust
->upper
) < 0.2))
188 m_adjust
->lower
= fmin
;
189 m_adjust
->upper
= fmax
;
190 m_adjust
->step_increment
= 1.0;
191 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
195 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
200 int wxSlider::GetMin() const
202 return (int)ceil(m_adjust
->lower
);
205 int wxSlider::GetMax() const
207 return (int)ceil(m_adjust
->upper
);
210 void wxSlider::SetPageSize( int pageSize
)
212 float fpage
= (float)pageSize
;
214 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
216 m_adjust
->page_increment
= fpage
;
220 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
225 int wxSlider::GetPageSize() const
227 return (int)ceil(m_adjust
->page_increment
);
230 void wxSlider::SetThumbLength( int len
)
232 float flen
= (float)len
;
234 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
236 m_adjust
->page_size
= flen
;
240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
245 int wxSlider::GetThumbLength() const
247 return (int)ceil(m_adjust
->page_size
);
250 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
254 int wxSlider::GetLineSize() const
259 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
261 GtkRange
*range
= GTK_RANGE(m_widget
);
263 return (range
->event_window
== window
);
265 return ( (window
== GTK_WIDGET(range
)->window
)
266 || (window
== range
->trough
)
267 || (window
== range
->slider
)
268 || (window
== range
->step_forw
)
269 || (window
== range
->step_back
) );
273 void wxSlider::GtkDisableEvents()
275 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
276 GTK_SIGNAL_FUNC(gtk_slider_callback
),
280 void wxSlider::GtkEnableEvents()
282 gtk_signal_connect( GTK_OBJECT (m_adjust
),
284 GTK_SIGNAL_FUNC(gtk_slider_callback
),
290 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
292 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);