1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider.h"
16 #include "wx/slider.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 static void gtk_slider_callback( GtkWidget
*WXUNUSED(widget
), wxSlider
*win
)
31 if (!win
->HasVMT()) return;
32 if (g_blockEventsOnDrag
) return;
34 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
35 if (fabs(diff
) < 0.2) return;
37 wxEventType command
= wxEVT_NULL
;
39 float line_step
= win
->m_adjust
->step_increment
;
40 float page_step
= win
->m_adjust
->page_increment
;
42 if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
43 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
44 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
45 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
46 else command
= wxEVT_SCROLL_THUMBTRACK
;
48 int value
= (int)(win
->m_adjust
->value
+0.5);
50 int orient
= wxHORIZONTAL
;
51 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
53 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
54 event
.SetEventObject( win
);
55 win
->ProcessEvent( event
);
57 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
58 cevent
.SetEventObject( win
);
59 win
->ProcessEvent( cevent
);
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
68 wxSlider::wxSlider(void)
72 wxSlider::~wxSlider(void)
76 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
77 int value
, int minValue
, int maxValue
,
78 const wxPoint
& pos
, const wxSize
& size
,
79 long style
, const wxValidator
& validator
, const wxString
& name
)
83 PreCreation( parent
, id
, pos
, size
, style
, name
);
85 SetValidator( validator
);
89 if (style
& wxSL_VERTICAL
== wxSL_VERTICAL
)
90 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
92 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
94 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
96 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
97 (GtkSignalFunc
) gtk_slider_callback
, (gpointer
) this );
98 SetRange( minValue
, maxValue
);
108 int wxSlider::GetValue(void) const
110 return (int)(m_adjust
->value
+0.5);
113 void wxSlider::SetValue( int value
)
115 float fpos
= (float)value
;
117 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
118 m_adjust
->value
= fpos
;
120 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
123 void wxSlider::SetRange( int minValue
, int maxValue
)
125 float fmin
= (float)minValue
;
126 float fmax
= (float)maxValue
;
128 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
129 (fabs(fmax
-m_adjust
->upper
) < 0.2))
132 m_adjust
->lower
= fmin
;
133 m_adjust
->upper
= fmax
;
135 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
138 int wxSlider::GetMin(void) const
140 return (int)(m_adjust
->lower
+0.5);
143 int wxSlider::GetMax(void) const
145 return (int)(m_adjust
->upper
+0.5);
148 void wxSlider::SetPageSize( int pageSize
)
150 float fpage
= (float)pageSize
;
152 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
154 m_adjust
->page_increment
= fpage
;
156 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
159 int wxSlider::GetPageSize(void) const
161 return (int)(m_adjust
->page_increment
+0.5);
164 void wxSlider::SetThumbLength( int len
)
166 float flen
= (float)len
;
168 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
170 m_adjust
->page_size
= flen
;
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
175 int wxSlider::GetThumbLength(void) const
177 return (int)(m_adjust
->page_size
+0.5);
180 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
184 int wxSlider::GetLineSize(void) const
189 // not supported in wxGTK (and GTK)
191 void wxSlider::GetSize( int *x
, int *y
) const
193 wxWindow::GetSize( x
, y
);
196 void wxSlider::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
198 wxWindow::SetSize( x
, y
, width
, height
, sizeFlags
);
201 void wxSlider::GetPosition( int *x
, int *y
) const
203 wxWindow::GetPosition( x
, y
);
206 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
210 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
214 int wxSlider::GetTickFreq(void) const
219 void wxSlider::ClearTicks(void)
223 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
227 int wxSlider::GetSelEnd(void) const
232 int wxSlider::GetSelStart(void) const
237 void wxSlider::ClearSel(void)
241 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
243 GtkRange
*range
= GTK_RANGE(m_widget
);
244 return ( (window
== GTK_WIDGET(range
)->window
) ||
245 (window
== range
->trough
) ||
246 (window
== range
->slider
) ||
247 (window
== range
->step_forw
) ||
248 (window
== range
->step_back
) );