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 //-----------------------------------------------------------------------------
44 static void gtk_slider_callback( GtkAdjustment
*adjust
,
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!win
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 float diff
= adjust
->value
- win
->m_oldPos
;
54 if (fabs(diff
) < sensitivity
) return;
56 win
->m_oldPos
= adjust
->value
;
58 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
60 double dvalue
= adjust
->value
;
61 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
63 int orient
= win
->GetWindowStyleFlag() & wxSL_VERTICAL
? wxVERTICAL
66 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
67 event
.SetEventObject( win
);
68 win
->GetEventHandler()->ProcessEvent( event
);
70 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
71 cevent
.SetEventObject( win
);
72 cevent
.SetInt( value
);
73 win
->GetEventHandler()->ProcessEvent( cevent
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
82 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
83 int value
, int minValue
, int maxValue
,
84 const wxPoint
& pos
, const wxSize
& size
,
85 long style
, const wxValidator
& validator
, const wxString
& name
)
87 m_acceptsFocus
= TRUE
;
90 if (!PreCreation( parent
, pos
, size
) ||
91 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
93 wxFAIL_MSG( wxT("wxSlider creation failed") );
99 if (style
& wxSL_VERTICAL
)
100 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
102 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
104 if (style
& wxSL_LABELS
)
106 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
107 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
109 /* labels need more space and too small window will
110 cause junk to appear on the dialog */
111 if (style
& wxSL_VERTICAL
)
131 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
133 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
136 if (style
& wxSL_INVERSE
)
137 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
142 SetRange( minValue
, maxValue
);
145 m_parent
->DoAddChild( this );
152 int wxSlider::GetValue() const
154 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
155 // -0.9 is rounded to -1
156 double val
= m_adjust
->value
;
157 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
160 void wxSlider::SetValue( int value
)
162 float fpos
= (float)value
;
164 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
166 m_adjust
->value
= fpos
;
170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
175 void wxSlider::SetRange( int minValue
, int maxValue
)
177 float fmin
= (float)minValue
;
178 float fmax
= (float)maxValue
;
180 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
181 (fabs(fmax
-m_adjust
->upper
) < 0.2))
186 m_adjust
->lower
= fmin
;
187 m_adjust
->upper
= fmax
;
188 m_adjust
->step_increment
= 1.0;
189 m_adjust
->page_increment
= ceil((fmax
-fmin
) / 10.0);
193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
198 int wxSlider::GetMin() const
200 return (int)ceil(m_adjust
->lower
);
203 int wxSlider::GetMax() const
205 return (int)ceil(m_adjust
->upper
);
208 void wxSlider::SetPageSize( int pageSize
)
210 float fpage
= (float)pageSize
;
212 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
214 m_adjust
->page_increment
= fpage
;
218 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
223 int wxSlider::GetPageSize() const
225 return (int)ceil(m_adjust
->page_increment
);
228 void wxSlider::SetThumbLength( int len
)
230 float flen
= (float)len
;
232 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
234 m_adjust
->page_size
= flen
;
238 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
243 int wxSlider::GetThumbLength() const
245 return (int)ceil(m_adjust
->page_size
);
248 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
252 int wxSlider::GetLineSize() const
257 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
259 GtkRange
*range
= GTK_RANGE(m_widget
);
261 return (range
->event_window
== window
);
263 return ( (window
== GTK_WIDGET(range
)->window
)
264 || (window
== range
->trough
)
265 || (window
== range
->slider
)
266 || (window
== range
->step_forw
)
267 || (window
== range
->step_back
) );
271 void wxSlider::GtkDisableEvents()
273 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
274 GTK_SIGNAL_FUNC(gtk_slider_callback
),
278 void wxSlider::GtkEnableEvents()
280 gtk_signal_connect( GTK_OBJECT (m_adjust
),
282 GTK_SIGNAL_FUNC(gtk_slider_callback
),
288 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
290 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);