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"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 static void gtk_slider_callback( GtkWidget
*WXUNUSED(widget
), wxSlider
*win
)
33 if (!win
->HasVMT()) return;
34 if (g_blockEventsOnDrag
) return;
36 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
37 if (fabs(diff
) < 0.2) return;
38 win
->m_oldPos
= win
->m_adjust
->value
;
40 wxEventType command
= wxEVT_NULL
;
42 float line_step
= win
->m_adjust
->step_increment
;
43 float page_step
= win
->m_adjust
->page_increment
;
45 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
46 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
47 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
48 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
49 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
50 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
51 else command
= wxEVT_SCROLL_THUMBTRACK
;
53 int value
= (int)ceil(win
->m_adjust
->value
);
55 int orient
= wxHORIZONTAL
;
56 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
58 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
59 event
.SetEventObject( win
);
60 win
->GetEventHandler()->ProcessEvent( event
);
62 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
63 cevent
.SetEventObject( win
);
64 win
->GetEventHandler()->ProcessEvent( cevent
);
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
73 wxSlider::wxSlider(void)
77 wxSlider::~wxSlider(void)
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 PreCreation( parent
, id
, pos
, size
, style
, name
);
91 SetValidator( validator
);
95 if (style
& wxSL_VERTICAL
== wxSL_VERTICAL
)
96 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
98 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
100 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
102 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
104 gtk_signal_connect( GTK_OBJECT(m_adjust
),
106 (GtkSignalFunc
) gtk_slider_callback
,
109 SetRange( minValue
, maxValue
);
112 m_parent
->AddChild( this );
114 (m_parent
->m_insertCallback
)( m_parent
, this );
118 SetBackgroundColour( parent
->GetBackgroundColour() );
125 int wxSlider::GetValue(void) const
127 return (int)(m_adjust
->value
+0.5);
130 void wxSlider::SetValue( int value
)
132 float fpos
= (float)value
;
134 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
136 m_adjust
->value
= fpos
;
138 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
141 void wxSlider::SetRange( int minValue
, int maxValue
)
143 float fmin
= (float)minValue
;
144 float fmax
= (float)maxValue
;
146 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
147 (fabs(fmax
-m_adjust
->upper
) < 0.2))
152 m_adjust
->lower
= fmin
;
153 m_adjust
->upper
= fmax
;
155 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
158 int wxSlider::GetMin(void) const
160 return (int)ceil(m_adjust
->lower
);
163 int wxSlider::GetMax(void) const
165 return (int)ceil(m_adjust
->upper
);
168 void wxSlider::SetPageSize( int pageSize
)
170 float fpage
= (float)pageSize
;
172 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
174 m_adjust
->page_increment
= fpage
;
176 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
179 int wxSlider::GetPageSize(void) const
181 return (int)ceil(m_adjust
->page_increment
);
184 void wxSlider::SetThumbLength( int len
)
186 float flen
= (float)len
;
188 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
190 m_adjust
->page_size
= flen
;
192 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
195 int wxSlider::GetThumbLength(void) const
197 return (int)ceil(m_adjust
->page_size
);
200 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
204 int wxSlider::GetLineSize(void) const
209 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
213 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
217 int wxSlider::GetTickFreq(void) const
222 void wxSlider::ClearTicks(void)
226 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
230 int wxSlider::GetSelEnd(void) const
235 int wxSlider::GetSelStart(void) const
240 void wxSlider::ClearSel(void)
244 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
246 GtkRange
*range
= GTK_RANGE(m_widget
);
247 return ( (window
== GTK_WIDGET(range
)->window
) ||
248 (window
== range
->trough
) ||
249 (window
== range
->slider
) ||
250 (window
== range
->step_forw
) ||
251 (window
== range
->step_back
) );
254 void wxSlider::ApplyWidgetStyle()
257 gtk_widget_set_style( m_widget
, m_widgetStyle
);