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"
17 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 extern bool g_blockEventsOnDrag
;
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 static void gtk_slider_callback( GtkWidget
*WXUNUSED(widget
), wxSlider
*win
)
29 if (!win
->HasVMT()) return;
30 if (g_blockEventsOnDrag
) return;
32 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
33 if (fabs(diff
) < 0.2) return;
35 wxEventType command
= wxEVT_NULL
;
37 float line_step
= win
->m_adjust
->step_increment
;
38 float page_step
= win
->m_adjust
->page_increment
;
40 if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
41 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
42 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
43 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
44 else command
= wxEVT_SCROLL_THUMBTRACK
;
46 int value
= (int)(win
->m_adjust
->value
+0.5);
48 int orient
= wxHORIZONTAL
;
49 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
51 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
52 event
.SetEventObject( win
);
53 win
->ProcessEvent( event
);
55 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
56 cevent
.SetEventObject( win
);
57 win
->ProcessEvent( cevent
);
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
66 wxSlider::wxSlider(void)
70 wxSlider::~wxSlider(void)
74 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
75 int value
, int minValue
, int maxValue
,
76 const wxPoint
& pos
, const wxSize
& size
,
77 long style
, const wxValidator
& validator
, const wxString
& name
)
81 PreCreation( parent
, id
, pos
, size
, style
, name
);
83 SetValidator( validator
);
87 if (style
& wxSL_VERTICAL
== wxSL_VERTICAL
)
88 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
90 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
92 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
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
);
101 m_parent
->AddChild( this );
103 (m_parent
->m_insertCallback
)( m_parent
, this );
107 SetBackgroundColour( parent
->GetBackgroundColour() );
114 int wxSlider::GetValue(void) const
116 return (int)(m_adjust
->value
+0.5);
119 void wxSlider::SetValue( int value
)
121 float fpos
= (float)value
;
123 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
124 m_adjust
->value
= fpos
;
126 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
129 void wxSlider::SetRange( int minValue
, int maxValue
)
131 float fmin
= (float)minValue
;
132 float fmax
= (float)maxValue
;
134 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
135 (fabs(fmax
-m_adjust
->upper
) < 0.2))
138 m_adjust
->lower
= fmin
;
139 m_adjust
->upper
= fmax
;
141 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
144 int wxSlider::GetMin(void) const
146 return (int)(m_adjust
->lower
+0.5);
149 int wxSlider::GetMax(void) const
151 return (int)(m_adjust
->upper
+0.5);
154 void wxSlider::SetPageSize( int pageSize
)
156 float fpage
= (float)pageSize
;
158 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
160 m_adjust
->page_increment
= fpage
;
162 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
165 int wxSlider::GetPageSize(void) const
167 return (int)(m_adjust
->page_increment
+0.5);
170 void wxSlider::SetThumbLength( int len
)
172 float flen
= (float)len
;
174 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
176 m_adjust
->page_size
= flen
;
178 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
181 int wxSlider::GetThumbLength(void) const
183 return (int)(m_adjust
->page_size
+0.5);
186 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
190 int wxSlider::GetLineSize(void) const
195 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
199 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
203 int wxSlider::GetTickFreq(void) const
208 void wxSlider::ClearTicks(void)
212 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
216 int wxSlider::GetSelEnd(void) const
221 int wxSlider::GetSelStart(void) const
226 void wxSlider::ClearSel(void)
230 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
232 GtkRange
*range
= GTK_RANGE(m_widget
);
233 return ( (window
== GTK_WIDGET(range
)->window
) ||
234 (window
== range
->trough
) ||
235 (window
== range
->slider
) ||
236 (window
== range
->step_forw
) ||
237 (window
== range
->step_back
) );
240 void wxSlider::ApplyWidgetStyle()
243 gtk_widget_set_style( m_widget
, m_widgetStyle
);