1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "slider.h"
15 #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(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
42 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
43 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
44 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
45 else command
= wxEVT_SCROLL_THUMBTRACK
;
47 int value
= (int)(win
->m_adjust
->value
+0.5);
49 int orient
= wxHORIZONTAL
;
50 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
52 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
53 event
.SetEventObject( win
);
54 win
->ProcessEvent( event
);
56 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
57 cevent
.SetEventObject( win
);
58 win
->ProcessEvent( cevent
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
67 wxSlider::wxSlider(void)
71 wxSlider::~wxSlider(void)
75 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
76 int value
, int minValue
, int maxValue
,
77 const wxPoint
& pos
, const wxSize
& size
,
78 long style
, const wxValidator
& validator
, const wxString
& name
)
82 PreCreation( parent
, id
, pos
, size
, style
, name
);
84 SetValidator( validator
);
88 if (style
& wxSL_VERTICAL
== wxSL_VERTICAL
)
89 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
91 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
93 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
95 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
97 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
98 (GtkSignalFunc
) gtk_slider_callback
, (gpointer
) this );
99 SetRange( minValue
, maxValue
);
104 SetBackgroundColour( parent
->GetBackgroundColour() );
105 SetForegroundColour( parent
->GetForegroundColour() );
112 int wxSlider::GetValue(void) const
114 return (int)(m_adjust
->value
+0.5);
117 void wxSlider::SetValue( int value
)
119 float fpos
= (float)value
;
121 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
122 m_adjust
->value
= fpos
;
124 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
127 void wxSlider::SetRange( int minValue
, int maxValue
)
129 float fmin
= (float)minValue
;
130 float fmax
= (float)maxValue
;
132 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
133 (fabs(fmax
-m_adjust
->upper
) < 0.2))
136 m_adjust
->lower
= fmin
;
137 m_adjust
->upper
= fmax
;
139 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
142 int wxSlider::GetMin(void) const
144 return (int)(m_adjust
->lower
+0.5);
147 int wxSlider::GetMax(void) const
149 return (int)(m_adjust
->upper
+0.5);
152 void wxSlider::SetPageSize( int pageSize
)
154 float fpage
= (float)pageSize
;
156 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
158 m_adjust
->page_increment
= fpage
;
160 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
163 int wxSlider::GetPageSize(void) const
165 return (int)(m_adjust
->page_increment
+0.5);
168 void wxSlider::SetThumbLength( int len
)
170 float flen
= (float)len
;
172 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
174 m_adjust
->page_size
= flen
;
176 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
179 int wxSlider::GetThumbLength(void) const
181 return (int)(m_adjust
->page_size
+0.5);
184 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
188 int wxSlider::GetLineSize(void) const
193 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
197 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
201 int wxSlider::GetTickFreq(void) const
206 void wxSlider::ClearTicks(void)
210 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
214 int wxSlider::GetSelEnd(void) const
219 int wxSlider::GetSelStart(void) const
224 void wxSlider::ClearSel(void)
228 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
230 GtkRange
*range
= GTK_RANGE(m_widget
);
231 return ( (window
== GTK_WIDGET(range
)->window
) ||
232 (window
== range
->trough
) ||
233 (window
== range
->slider
) ||
234 (window
== range
->step_forw
) ||
235 (window
== range
->step_back
) );
238 void wxSlider::ApplyWidgetStyle()
241 gtk_widget_set_style( m_widget
, m_widgetStyle
);