1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/slider.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "slider.h"
14 #include "wx/slider.h"
22 #include "wx/gtk/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 static const float sensitivity
= 0.02;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static void gtk_slider_callback( GtkAdjustment
*adjust
,
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!win
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 float diff
= adjust
->value
- win
->m_oldPos
;
53 if (fabs(diff
) < sensitivity
) return;
55 win
->m_oldPos
= adjust
->value
;
57 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
59 double dvalue
= adjust
->value
;
60 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
62 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
65 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
66 event
.SetEventObject( win
);
67 win
->GetEventHandler()->ProcessEvent( event
);
69 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
70 cevent
.SetEventObject( win
);
71 cevent
.SetInt( value
);
72 win
->GetEventHandler()->ProcessEvent( cevent
);
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
79 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
81 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
82 int value
, int minValue
, int maxValue
,
83 const wxPoint
& pos
, const wxSize
& size
,
84 long style
, const wxValidator
& validator
, const wxString
& name
)
86 m_acceptsFocus
= TRUE
;
89 if (!PreCreation( parent
, pos
, size
) ||
90 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
92 wxFAIL_MSG( wxT("wxSlider creation failed") );
98 if (style
& wxSL_VERTICAL
)
99 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
101 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
103 if (style
& wxSL_LABELS
)
105 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
106 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
108 /* labels need more space and too small window will
109 cause junk to appear on the dialog */
110 if (style
& wxSL_VERTICAL
)
130 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
132 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
136 SetRange( minValue
, maxValue
);
139 m_parent
->DoAddChild( this );
143 SetBackgroundColour( parent
->GetBackgroundColour() );
150 int wxSlider::GetValue() const
152 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
153 // -0.9 is rounded to -1
154 double val
= m_adjust
->value
;
155 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
158 void wxSlider::SetValue( int value
)
160 float fpos
= (float)value
;
162 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
164 m_adjust
->value
= fpos
;
168 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
173 void wxSlider::SetRange( int minValue
, int maxValue
)
175 float fmin
= (float)minValue
;
176 float fmax
= (float)maxValue
;
178 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
179 (fabs(fmax
-m_adjust
->upper
) < 0.2))
184 m_adjust
->lower
= fmin
;
185 m_adjust
->upper
= fmax
;
186 m_adjust
->step_increment
= 1.0;
187 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
196 int wxSlider::GetMin() const
198 return (int)ceil(m_adjust
->lower
);
201 int wxSlider::GetMax() const
203 return (int)ceil(m_adjust
->upper
);
206 void wxSlider::SetPageSize( int pageSize
)
208 float fpage
= (float)pageSize
;
210 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
212 m_adjust
->page_increment
= fpage
;
216 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
221 int wxSlider::GetPageSize() const
223 return (int)ceil(m_adjust
->page_increment
);
226 void wxSlider::SetThumbLength( int len
)
228 float flen
= (float)len
;
230 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
232 m_adjust
->page_size
= flen
;
236 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
241 int wxSlider::GetThumbLength() const
243 return (int)ceil(m_adjust
->page_size
);
246 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
250 int wxSlider::GetLineSize() const
255 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
257 GtkRange
*range
= GTK_RANGE(m_widget
);
259 return (range
->event_window
== window
);
261 return ( (window
== GTK_WIDGET(range
)->window
)
262 || (window
== range
->trough
)
263 || (window
== range
->slider
)
264 || (window
== range
->step_forw
)
265 || (window
== range
->step_back
) );
269 void wxSlider::ApplyWidgetStyle()
272 gtk_widget_set_style( m_widget
, m_widgetStyle
);
275 void wxSlider::GtkDisableEvents()
277 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
278 GTK_SIGNAL_FUNC(gtk_slider_callback
),
282 void wxSlider::GtkEnableEvents()
284 gtk_signal_connect( GTK_OBJECT (m_adjust
),
286 GTK_SIGNAL_FUNC(gtk_slider_callback
),