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( GtkWidget
*WXUNUSED(widget
), wxSlider
*win
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (!win
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
49 if (fabs(diff
) < 0.2) return;
50 win
->m_oldPos
= win
->m_adjust
->value
;
52 wxEventType command
= wxEVT_NULL
;
54 float line_step
= win
->m_adjust
->step_increment
;
55 float page_step
= win
->m_adjust
->page_increment
;
57 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
58 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
59 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
60 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
61 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
62 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
63 else command
= wxEVT_SCROLL_THUMBTRACK
;
65 int value
= (int)ceil(win
->m_adjust
->value
);
67 int orient
= wxHORIZONTAL
;
68 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
70 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
71 event
.SetEventObject( win
);
72 win
->GetEventHandler()->ProcessEvent( event
);
74 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, win
->GetId() );
75 cevent
.SetEventObject( win
);
76 win
->GetEventHandler()->ProcessEvent( cevent
);
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
85 wxSlider::wxSlider(void)
89 wxSlider::~wxSlider(void)
93 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
94 int value
, int minValue
, int maxValue
,
95 const wxPoint
& pos
, const wxSize
& size
,
96 long style
, const wxValidator
& validator
, const wxString
& name
)
98 m_acceptsFocus
= TRUE
;
101 PreCreation( parent
, id
, pos
, size
, style
, name
);
104 SetValidator( validator
);
109 if (style
& wxSL_VERTICAL
)
110 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
112 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
114 if (style
& wxSL_LABELS
)
116 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), TRUE
);
118 /* labels need more space and too small window will
119 cause junk to appear on the dialog */
120 if (style
& wxSL_VERTICAL
)
140 gtk_scale_set_draw_value( GTK_SCALE( m_widget
), FALSE
);
142 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
144 gtk_signal_connect( GTK_OBJECT(m_adjust
),
146 (GtkSignalFunc
) gtk_slider_callback
,
149 SetRange( minValue
, maxValue
);
152 m_parent
->DoAddChild( this );
156 SetBackgroundColour( parent
->GetBackgroundColour() );
163 int wxSlider::GetValue(void) const
165 return (int)(m_adjust
->value
+0.5);
168 void wxSlider::SetValue( int value
)
170 float fpos
= (float)value
;
172 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
174 m_adjust
->value
= fpos
;
176 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
179 void wxSlider::SetRange( int minValue
, int maxValue
)
181 float fmin
= (float)minValue
;
182 float fmax
= (float)maxValue
;
184 if ((fabs(fmin
-m_adjust
->lower
) < 0.2) &&
185 (fabs(fmax
-m_adjust
->upper
) < 0.2))
190 m_adjust
->lower
= fmin
;
191 m_adjust
->upper
= fmax
;
193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
196 int wxSlider::GetMin(void) const
198 return (int)ceil(m_adjust
->lower
);
201 int wxSlider::GetMax(void) const
203 return (int)ceil(m_adjust
->upper
);
206 void wxSlider::SetPageSize( int pageSize
)
208 float fpage
= (float)pageSize
;
210 if (fabs(fpage
-m_adjust
->page_increment
) < 0.2) return;
212 m_adjust
->page_increment
= fpage
;
214 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
217 int wxSlider::GetPageSize(void) const
219 return (int)ceil(m_adjust
->page_increment
);
222 void wxSlider::SetThumbLength( int len
)
224 float flen
= (float)len
;
226 if (fabs(flen
-m_adjust
->page_size
) < 0.2) return;
228 m_adjust
->page_size
= flen
;
230 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
233 int wxSlider::GetThumbLength(void) const
235 return (int)ceil(m_adjust
->page_size
);
238 void wxSlider::SetLineSize( int WXUNUSED(lineSize
) )
242 int wxSlider::GetLineSize(void) const
247 void wxSlider::SetTick( int WXUNUSED(tickPos
) )
251 void wxSlider::SetTickFreq( int WXUNUSED(n
), int WXUNUSED(pos
) )
255 int wxSlider::GetTickFreq(void) const
260 void wxSlider::ClearTicks(void)
264 void wxSlider::SetSelection( int WXUNUSED(minPos
), int WXUNUSED(maxPos
) )
268 int wxSlider::GetSelEnd(void) const
273 int wxSlider::GetSelStart(void) const
278 void wxSlider::ClearSel(void)
282 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
284 GtkRange
*range
= GTK_RANGE(m_widget
);
285 return ( (window
== GTK_WIDGET(range
)->window
) ||
286 (window
== range
->trough
) ||
287 (window
== range
->slider
) ||
288 (window
== range
->step_forw
) ||
289 (window
== range
->step_back
) );
292 void wxSlider::ApplyWidgetStyle()
295 gtk_widget_set_style( m_widget
, m_widgetStyle
);