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"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 static void gtk_slider_callback( GtkAdjustment
*adjust
, wxSlider
*win
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (!win
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 float diff
= adjust
->value
- win
->m_oldPos
;
49 if (fabs(diff
) < 0.2) return;
51 win
->m_oldPos
= adjust
->value
;
53 GtkRange
*range
= GTK_RANGE( win
->m_widget
);
55 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
56 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLL_LINEUP
;
57 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLL_LINEDOWN
;
58 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLL_PAGEUP
;
59 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLL_PAGEDOWN
;
61 int value
= (int)ceil(adjust
->value
);
63 int orient
= wxHORIZONTAL
;
64 if ( (win
->GetWindowStyleFlag() & wxSB_VERTICAL
) == wxSB_VERTICAL
)
67 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
68 event
.SetEventObject( win
);
69 win
->GetEventHandler()->ProcessEvent( event
);
71 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
72 cevent
.SetEventObject( win
);
73 win
->GetEventHandler()->ProcessEvent( cevent
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
82 wxSlider::wxSlider(void)
86 wxSlider::~wxSlider(void)
90 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
91 int value
, int minValue
, int maxValue
,
92 const wxPoint
& pos
, const wxSize
& size
,
93 long style
, const wxValidator
& validator
, const wxString
& name
)
95 m_acceptsFocus
= TRUE
;
98 if (!PreCreation( parent
, pos
, size
) ||
99 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
101 wxFAIL_MSG( wxT("wxSlider creation failed") );
107 if (style
& wxSL_VERTICAL
)
108 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
110 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
112 if (style
& wxSL_LABELS
)
114 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
115 gtk_scale_set_digits( GTK_SCALE( m_widget
), 0 );
117 /* labels need more space and too small window will
118 cause junk to appear on the dialog */
119 if (style
& wxSL_VERTICAL
)
139 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
141 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
143 gtk_signal_connect( GTK_OBJECT(m_adjust
),
145 (GtkSignalFunc
) gtk_slider_callback
,
148 SetRange( minValue
, maxValue
);
151 m_parent
->DoAddChild( this );
155 SetBackgroundColour( parent
->GetBackgroundColour() );
162 int wxSlider::GetValue(void) const
164 return (int)(m_adjust
->value
+0.5);
167 void wxSlider::SetValue( int value
)
169 float fpos
= (float)value
;
171 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
173 m_adjust
->value
= fpos
;
175 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
178 void wxSlider::SetRange( int minValue
, int maxValue
)
180 float fmin
= (float)minValue
;
181 float fmax
= (float)maxValue
;
183 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
184 (fabs(fmax
-m_adjust
->upper
) < 0.2))
189 m_adjust
->lower
= fmin
;
190 m_adjust
->upper
= fmax
;
192 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
195 int wxSlider::GetMin(void) const
197 return (int)ceil(m_adjust
->lower
);
200 int wxSlider::GetMax(void) const
202 return (int)ceil(m_adjust
->upper
);
205 void wxSlider::SetPageSize( int pageSize
)
207 float fpage
= (float)pageSize
;
209 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
211 m_adjust
->page_increment
= fpage
;
213 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
216 int wxSlider::GetPageSize(void) const
218 return (int)ceil(m_adjust
->page_increment
);
221 void wxSlider::SetThumbLength( int len
)
223 float flen
= (float)len
;
225 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
227 m_adjust
->page_size
= flen
;
229 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
232 int wxSlider::GetThumbLength(void) const
234 return (int)ceil(m_adjust
->page_size
);
237 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
241 int wxSlider::GetLineSize(void) const
246 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
250 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
254 int wxSlider::GetTickFreq(void) const
259 void wxSlider::ClearTicks(void)
263 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
267 int wxSlider::GetSelEnd(void) const
272 int wxSlider::GetSelStart(void) const
277 void wxSlider::ClearSel(void)
281 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
283 GtkRange
*range
= GTK_RANGE(m_widget
);
284 return ( (window
== GTK_WIDGET(range
)->window
) ||
285 (window
== range
->trough
) ||
286 (window
== range
->slider
) ||
287 (window
== range
->step_forw
) ||
288 (window
== range
->step_back
) );
291 void wxSlider::ApplyWidgetStyle()
294 gtk_widget_set_style( m_widget
, m_widgetStyle
);