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;
39 wxEventType command
= wxEVT_NULL
;
41 float line_step
= win
->m_adjust
->step_increment
;
42 float page_step
= win
->m_adjust
->page_increment
;
44 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
45 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
46 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
47 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
48 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
49 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
50 else command
= wxEVT_SCROLL_THUMBTRACK
;
52 int value
= (int)(win
->m_adjust
->value
+0.5);
54 int orient
= wxHORIZONTAL
;
55 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
57 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
58 event
.SetEventObject( win
);
59 win
->ProcessEvent( event
);
61 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
62 cevent
.SetEventObject( win
);
63 win
->ProcessEvent( cevent
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
72 wxSlider::wxSlider(void)
76 wxSlider::~wxSlider(void)
80 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
81 int value
, int minValue
, int maxValue
,
82 const wxPoint
& pos
, const wxSize
& size
,
83 long style
, const wxValidator
& validator
, const wxString
& name
)
85 m_acceptsFocus
= TRUE
;
88 PreCreation( parent
, id
, pos
, size
, style
, name
);
90 SetValidator( validator
);
94 if (style
& wxSL_VERTICAL
== wxSL_VERTICAL
)
95 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
97 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
99 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
101 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
103 gtk_signal_connect( GTK_OBJECT(m_adjust
),
105 (GtkSignalFunc
) gtk_slider_callback
,
108 SetRange( minValue
, maxValue
);
111 m_parent
->AddChild( this );
113 (m_parent
->m_insertCallback
)( m_parent
, this );
117 SetBackgroundColour( parent
->GetBackgroundColour() );
124 int wxSlider::GetValue(void) const
126 return (int)(m_adjust
->value
+0.5);
129 void wxSlider::SetValue( int value
)
131 float fpos
= (float)value
;
133 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
135 m_adjust
->value
= fpos
;
137 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
140 void wxSlider::SetRange( int minValue
, int maxValue
)
142 float fmin
= (float)minValue
;
143 float fmax
= (float)maxValue
;
145 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
146 (fabs(fmax
-m_adjust
->upper
) < 0.2))
151 m_adjust
->lower
= fmin
;
152 m_adjust
->upper
= fmax
;
154 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
157 int wxSlider::GetMin(void) const
159 return (int)(m_adjust
->lower
+0.5);
162 int wxSlider::GetMax(void) const
164 return (int)(m_adjust
->upper
+0.5);
167 void wxSlider::SetPageSize( int pageSize
)
169 float fpage
= (float)pageSize
;
171 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
173 m_adjust
->page_increment
= fpage
;
175 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
178 int wxSlider::GetPageSize(void) const
180 return (int)(m_adjust
->page_increment
+0.5);
183 void wxSlider::SetThumbLength( int len
)
185 float flen
= (float)len
;
187 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
189 m_adjust
->page_size
= flen
;
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
194 int wxSlider::GetThumbLength(void) const
196 return (int)(m_adjust
->page_size
+0.5);
199 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
203 int wxSlider::GetLineSize(void) const
208 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
212 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
216 int wxSlider::GetTickFreq(void) const
221 void wxSlider::ClearTicks(void)
225 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
229 int wxSlider::GetSelEnd(void) const
234 int wxSlider::GetSelStart(void) const
239 void wxSlider::ClearSel(void)
243 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
245 GtkRange
*range
= GTK_RANGE(m_widget
);
246 return ( (window
== GTK_WIDGET(range
)->window
) ||
247 (window
== range
->trough
) ||
248 (window
== range
->slider
) ||
249 (window
== range
->step_forw
) ||
250 (window
== range
->step_back
) );
253 void wxSlider::ApplyWidgetStyle()
256 gtk_widget_set_style( m_widget
, m_widgetStyle
);