1 /////////////////////////////////////////////////////////////////////////////
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"
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
, wxSlider
*win
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!win
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 float diff
= adjust
->value
- win
->m_oldPos
;
52 if (fabs(diff
) < sensitivity
) return;
54 win
->m_oldPos
= adjust
->value
;
56 GtkRange
*range
= GTK_RANGE( win
->m_widget
);
58 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
59 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLL_LINEUP
;
60 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLL_LINEDOWN
;
61 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLL_PAGEUP
;
62 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLL_PAGEDOWN
;
64 int value
= (int)ceil(adjust
->value
);
66 int orient
= wxHORIZONTAL
;
67 if ( (win
->GetWindowStyleFlag() & wxSB_VERTICAL
) == wxSB_VERTICAL
)
70 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
71 event
.SetEventObject( win
);
72 win
->GetEventHandler()->ProcessEvent( event
);
74 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
75 cevent
.SetEventObject( win
);
76 win
->GetEventHandler()->ProcessEvent( cevent
);
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
85 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
86 int value
, int minValue
, int maxValue
,
87 const wxPoint
& pos
, const wxSize
& size
,
88 long style
, const wxValidator
& validator
, const wxString
& name
)
90 m_acceptsFocus
= TRUE
;
93 if (!PreCreation( parent
, pos
, size
) ||
94 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
96 wxFAIL_MSG( wxT("wxSlider creation failed") );
102 if (style
& wxSL_VERTICAL
)
103 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
105 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
107 if (style
& wxSL_LABELS
)
109 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
110 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
112 /* labels need more space and too small window will
113 cause junk to appear on the dialog */
114 if (style
& wxSL_VERTICAL
)
134 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
136 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
138 gtk_signal_connect( GTK_OBJECT(m_adjust
),
140 (GtkSignalFunc
) gtk_slider_callback
,
143 SetRange( minValue
, maxValue
);
146 m_parent
->DoAddChild( this );
150 SetBackgroundColour( parent
->GetBackgroundColour() );
157 int wxSlider::GetValue() const
159 return (int)(m_adjust
->value
+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
;
170 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
;
187 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
190 int wxSlider::GetMin() const
192 return (int)ceil(m_adjust
->lower
);
195 int wxSlider::GetMax() const
197 return (int)ceil(m_adjust
->upper
);
200 void wxSlider::SetPageSize( int pageSize
)
202 float fpage
= (float)pageSize
;
204 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
206 m_adjust
->page_increment
= fpage
;
208 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
211 int wxSlider::GetPageSize() const
213 return (int)ceil(m_adjust
->page_increment
);
216 void wxSlider::SetThumbLength( int len
)
218 float flen
= (float)len
;
220 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
222 m_adjust
->page_size
= flen
;
224 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
227 int wxSlider::GetThumbLength() const
229 return (int)ceil(m_adjust
->page_size
);
232 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
236 int wxSlider::GetLineSize() const
241 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
245 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
249 int wxSlider::GetTickFreq() const
254 void wxSlider::ClearTicks()
258 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
262 int wxSlider::GetSelEnd() const
267 int wxSlider::GetSelStart() const
272 void wxSlider::ClearSel()
276 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
278 GtkRange
*range
= GTK_RANGE(m_widget
);
279 return ( (window
== GTK_WIDGET(range
)->window
) ||
280 (window
== range
->trough
) ||
281 (window
== range
->slider
) ||
282 (window
== range
->step_forw
) ||
283 (window
== range
->step_back
) );
286 void wxSlider::ApplyWidgetStyle()
289 gtk_widget_set_style( m_widget
, m_widgetStyle
);