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"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern bool g_blockEventsOnDrag
;
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 static void gtk_slider_callback( GtkWidget
*WXUNUSED(widget
), wxSlider
*win
)
30 if (!win
->HasVMT()) return;
31 if (g_blockEventsOnDrag
) return;
33 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
34 if (fabs(diff
) < 0.2) return;
36 wxEventType command
= wxEVT_NULL
;
38 float line_step
= win
->m_adjust
->step_increment
;
39 float page_step
= win
->m_adjust
->page_increment
;
41 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
42 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
43 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
44 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
45 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
46 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
47 else command
= wxEVT_SCROLL_THUMBTRACK
;
49 int value
= (int)(win
->m_adjust
->value
+0.5);
51 int orient
= wxHORIZONTAL
;
52 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
54 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
55 event
.SetEventObject( win
);
56 win
->ProcessEvent( event
);
58 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
59 cevent
.SetEventObject( win
);
60 win
->ProcessEvent( cevent
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
69 wxSlider::wxSlider(void)
73 wxSlider::~wxSlider(void)
77 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
78 int value
, int minValue
, int maxValue
,
79 const wxPoint
& pos
, const wxSize
& size
,
80 long style
, const wxValidator
& validator
, const wxString
& name
)
82 m_acceptsFocus
= TRUE
;
85 PreCreation( parent
, id
, pos
, size
, style
, name
);
87 SetValidator( validator
);
91 if (style
& wxSL_VERTICAL
== wxSL_VERTICAL
)
92 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
94 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
96 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
98 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
100 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
101 (GtkSignalFunc
) gtk_slider_callback
, (gpointer
) this );
102 SetRange( minValue
, maxValue
);
105 m_parent
->AddChild( this );
107 (m_parent
->m_insertCallback
)( m_parent
, this );
111 SetBackgroundColour( parent
->GetBackgroundColour() );
118 int wxSlider::GetValue(void) const
120 return (int)(m_adjust
->value
+0.5);
123 void wxSlider::SetValue( int value
)
125 float fpos
= (float)value
;
127 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
128 m_adjust
->value
= fpos
;
130 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
133 void wxSlider::SetRange( int minValue
, int maxValue
)
135 float fmin
= (float)minValue
;
136 float fmax
= (float)maxValue
;
138 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
139 (fabs(fmax
-m_adjust
->upper
) < 0.2))
142 m_adjust
->lower
= fmin
;
143 m_adjust
->upper
= fmax
;
145 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
148 int wxSlider::GetMin(void) const
150 return (int)(m_adjust
->lower
+0.5);
153 int wxSlider::GetMax(void) const
155 return (int)(m_adjust
->upper
+0.5);
158 void wxSlider::SetPageSize( int pageSize
)
160 float fpage
= (float)pageSize
;
162 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
164 m_adjust
->page_increment
= fpage
;
166 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
169 int wxSlider::GetPageSize(void) const
171 return (int)(m_adjust
->page_increment
+0.5);
174 void wxSlider::SetThumbLength( int len
)
176 float flen
= (float)len
;
178 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
180 m_adjust
->page_size
= flen
;
182 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
185 int wxSlider::GetThumbLength(void) const
187 return (int)(m_adjust
->page_size
+0.5);
190 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
194 int wxSlider::GetLineSize(void) const
199 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
203 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
207 int wxSlider::GetTickFreq(void) const
212 void wxSlider::ClearTicks(void)
216 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
220 int wxSlider::GetSelEnd(void) const
225 int wxSlider::GetSelStart(void) const
230 void wxSlider::ClearSel(void)
234 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
236 GtkRange
*range
= GTK_RANGE(m_widget
);
237 return ( (window
== GTK_WIDGET(range
)->window
) ||
238 (window
== range
->trough
) ||
239 (window
== range
->slider
) ||
240 (window
== range
->step_forw
) ||
241 (window
== range
->step_back
) );
244 void wxSlider::ApplyWidgetStyle()
247 gtk_widget_set_style( m_widget
, m_widgetStyle
);