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 void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 static void gtk_slider_callback( GtkWidget
*WXUNUSED(widget
), wxSlider
*win
)
40 if (g_isIdle
) wxapp_install_idle_handler();
42 if (!win
->m_hasVMT
) return;
43 if (g_blockEventsOnDrag
) return;
45 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
46 if (fabs(diff
) < 0.2) return;
47 win
->m_oldPos
= win
->m_adjust
->value
;
49 wxEventType command
= wxEVT_NULL
;
51 float line_step
= win
->m_adjust
->step_increment
;
52 float page_step
= win
->m_adjust
->page_increment
;
54 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
55 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
56 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
57 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
58 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
59 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
60 else command
= wxEVT_SCROLL_THUMBTRACK
;
62 int value
= (int)ceil(win
->m_adjust
->value
);
64 int orient
= wxHORIZONTAL
;
65 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
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 PreCreation( parent
, id
, pos
, size
, style
, name
);
100 SetValidator( validator
);
104 if (style
& wxSL_VERTICAL
)
105 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
107 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
109 if (style
& wxSL_LABELS
)
111 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
113 /* labels need more space and too small window will
114 cause junk to appear on the dialog */
115 if (style
& wxSL_VERTICAL
)
135 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
137 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
139 gtk_signal_connect( GTK_OBJECT(m_adjust
),
141 (GtkSignalFunc
) gtk_slider_callback
,
144 SetRange( minValue
, maxValue
);
147 m_parent
->DoAddChild( this );
151 SetBackgroundColour( parent
->GetBackgroundColour() );
158 int wxSlider::GetValue(void) const
160 return (int)(m_adjust
->value
+0.5);
163 void wxSlider::SetValue( int value
)
165 float fpos
= (float)value
;
167 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
169 m_adjust
->value
= fpos
;
171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
174 void wxSlider::SetRange( int minValue
, int maxValue
)
176 float fmin
= (float)minValue
;
177 float fmax
= (float)maxValue
;
179 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
180 (fabs(fmax
-m_adjust
->upper
) < 0.2))
185 m_adjust
->lower
= fmin
;
186 m_adjust
->upper
= fmax
;
188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
191 int wxSlider::GetMin(void) const
193 return (int)ceil(m_adjust
->lower
);
196 int wxSlider::GetMax(void) const
198 return (int)ceil(m_adjust
->upper
);
201 void wxSlider::SetPageSize( int pageSize
)
203 float fpage
= (float)pageSize
;
205 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
207 m_adjust
->page_increment
= fpage
;
209 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
212 int wxSlider::GetPageSize(void) const
214 return (int)ceil(m_adjust
->page_increment
);
217 void wxSlider::SetThumbLength( int len
)
219 float flen
= (float)len
;
221 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
223 m_adjust
->page_size
= flen
;
225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
228 int wxSlider::GetThumbLength(void) const
230 return (int)ceil(m_adjust
->page_size
);
233 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
237 int wxSlider::GetLineSize(void) const
242 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
246 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
250 int wxSlider::GetTickFreq(void) const
255 void wxSlider::ClearTicks(void)
259 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
263 int wxSlider::GetSelEnd(void) const
268 int wxSlider::GetSelStart(void) const
273 void wxSlider::ClearSel(void)
277 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
279 GtkRange
*range
= GTK_RANGE(m_widget
);
280 return ( (window
== GTK_WIDGET(range
)->window
) ||
281 (window
== range
->trough
) ||
282 (window
== range
->slider
) ||
283 (window
== range
->step_forw
) ||
284 (window
== range
->step_back
) );
287 void wxSlider::ApplyWidgetStyle()
290 gtk_widget_set_style( m_widget
, m_widgetStyle
);