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"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 static void gtk_slider_callback( GtkAdjustment
*adjust
, wxSlider
*win
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (!win
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 float diff
= adjust
->value
- win
->m_oldPos
;
49 if (fabs(diff
) < 0.2) return;
51 win
->m_oldPos
= adjust
->value
;
53 GtkRange
*range
= GTK_RANGE( win
->m_widget
);
55 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
56 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLL_LINEUP
;
57 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLL_LINEDOWN
;
58 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLL_PAGEUP
;
59 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLL_PAGEDOWN
;
61 int value
= (int)ceil(adjust
->value
);
63 int orient
= wxHORIZONTAL
;
64 if ( (win
->GetWindowStyleFlag() & wxSB_VERTICAL
) == wxSB_VERTICAL
)
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 win
->GetEventHandler()->ProcessEvent( cevent
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
82 wxSlider::wxSlider(void)
86 wxSlider::~wxSlider(void)
90 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
91 int value
, int minValue
, int maxValue
,
92 const wxPoint
& pos
, const wxSize
& size
,
93 long style
, const wxValidator
& validator
, const wxString
& name
)
95 m_acceptsFocus
= TRUE
;
98 if (!PreCreation( parent
, pos
, size
) ||
99 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
101 wxFAIL_MSG( wxT("wxSlider creation failed") );
107 if (style
& wxSL_VERTICAL
)
108 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
110 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
112 if (style
& wxSL_LABELS
)
114 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
116 /* labels need more space and too small window will
117 cause junk to appear on the dialog */
118 if (style
& wxSL_VERTICAL
)
138 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
140 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
142 gtk_signal_connect( GTK_OBJECT(m_adjust
),
144 (GtkSignalFunc
) gtk_slider_callback
,
147 SetRange( minValue
, maxValue
);
150 m_parent
->DoAddChild( this );
154 SetBackgroundColour( parent
->GetBackgroundColour() );
161 int wxSlider::GetValue(void) const
163 return (int)(m_adjust
->value
+0.5);
166 void wxSlider::SetValue( int value
)
168 float fpos
= (float)value
;
170 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
172 m_adjust
->value
= fpos
;
174 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
;
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
194 int wxSlider::GetMin(void) const
196 return (int)ceil(m_adjust
->lower
);
199 int wxSlider::GetMax(void) const
201 return (int)ceil(m_adjust
->upper
);
204 void wxSlider::SetPageSize( int pageSize
)
206 float fpage
= (float)pageSize
;
208 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
210 m_adjust
->page_increment
= fpage
;
212 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
215 int wxSlider::GetPageSize(void) const
217 return (int)ceil(m_adjust
->page_increment
);
220 void wxSlider::SetThumbLength( int len
)
222 float flen
= (float)len
;
224 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
226 m_adjust
->page_size
= flen
;
228 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
231 int wxSlider::GetThumbLength(void) const
233 return (int)ceil(m_adjust
->page_size
);
236 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
240 int wxSlider::GetLineSize(void) const
245 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
249 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
253 int wxSlider::GetTickFreq(void) const
258 void wxSlider::ClearTicks(void)
262 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
266 int wxSlider::GetSelEnd(void) const
271 int wxSlider::GetSelStart(void) const
276 void wxSlider::ClearSel(void)
280 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
282 GtkRange
*range
= GTK_RANGE(m_widget
);
283 return ( (window
== GTK_WIDGET(range
)->window
) ||
284 (window
== range
->trough
) ||
285 (window
== range
->slider
) ||
286 (window
== range
->step_forw
) ||
287 (window
== range
->step_back
) );
290 void wxSlider::ApplyWidgetStyle()
293 gtk_widget_set_style( m_widget
, m_widgetStyle
);