]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / gtk / slider.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: slider.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
f03fc89f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
c801d85f
KB
10#ifdef __GNUG__
11#pragma implementation "slider.h"
12#endif
13
14#include "wx/slider.h"
dcf924a3
RR
15
16#if wxUSE_SLIDER
17
c801d85f 18#include "wx/utils.h"
57a1fd73 19
f5368809 20#include <math.h>
c801d85f 21
57a1fd73
RR
22#include <gdk/gdk.h>
23#include <gtk/gtk.h>
83624f79 24
acfd422a
RR
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
66bd6b93
RR
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37
57a1fd73
RR
38static const float sensitivity = 0.02;
39
c801d85f 40//-----------------------------------------------------------------------------
97b3455a 41// "value_changed"
c801d85f
KB
42//-----------------------------------------------------------------------------
43
e1811a01 44static void gtk_slider_callback( GtkAdjustment *adjust, wxSlider *win )
80810ca3 45{
acfd422a
RR
46 if (g_isIdle) wxapp_install_idle_handler();
47
a2053b27 48 if (!win->m_hasVMT) return;
2d17d68f 49 if (g_blockEventsOnDrag) return;
e1811a01
RR
50
51 float diff = adjust->value - win->m_oldPos;
57a1fd73 52 if (fabs(diff) < sensitivity) return;
80810ca3 53
e1811a01 54 win->m_oldPos = adjust->value;
2d17d68f 55
e1811a01
RR
56 GtkRange *range = GTK_RANGE( win->m_widget );
57
58 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
59 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP;
60 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN;
61 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP;
62 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN;
80810ca3 63
e1811a01 64 int value = (int)ceil(adjust->value);
80810ca3 65
2d17d68f 66 int orient = wxHORIZONTAL;
80810ca3
VZ
67 if ( (win->GetWindowStyleFlag() & wxSB_VERTICAL) == wxSB_VERTICAL)
68 orient = wxVERTICAL;
69
2d17d68f
RR
70 wxScrollEvent event( command, win->GetId(), value, orient );
71 event.SetEventObject( win );
828f655f 72 win->GetEventHandler()->ProcessEvent( event );
80810ca3 73
2d17d68f
RR
74 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
75 cevent.SetEventObject( win );
f6bcfd97 76 cevent.SetInt( value );
828f655f 77 win->GetEventHandler()->ProcessEvent( cevent );
6de97a3b 78}
c801d85f 79
97b3455a
RR
80//-----------------------------------------------------------------------------
81// wxSlider
82//-----------------------------------------------------------------------------
83
c801d85f
KB
84IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
85
debe6624
JS
86bool wxSlider::Create(wxWindow *parent, wxWindowID id,
87 int value, int minValue, int maxValue,
c801d85f 88 const wxPoint& pos, const wxSize& size,
6de97a3b 89 long style, const wxValidator& validator, const wxString& name )
c801d85f 90{
b292e2f5 91 m_acceptsFocus = TRUE;
2d17d68f 92 m_needParent = TRUE;
80810ca3 93
4dcaf11a
RR
94 if (!PreCreation( parent, pos, size ) ||
95 !CreateBase( parent, id, pos, size, style, validator, name ))
96 {
223d09f6 97 wxFAIL_MSG( wxT("wxSlider creation failed") );
5b8a521e 98 return FALSE;
4dcaf11a 99 }
6de97a3b 100
2d17d68f 101 m_oldPos = 0.0;
c801d85f 102
2e563988 103 if (style & wxSL_VERTICAL)
2d17d68f 104 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
19da4326
RR
105 else
106 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
80810ca3 107
2e563988 108 if (style & wxSL_LABELS)
858b5bdd 109 {
2e563988 110 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
4ee1741f 111 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 112
f03fc89f
VZ
113 /* labels need more space and too small window will
114 cause junk to appear on the dialog */
858b5bdd 115 if (style & wxSL_VERTICAL)
f03fc89f
VZ
116 {
117 wxSize sz( size );
118 if (sz.x < 35)
119 {
120 sz.x = 35;
121 SetSize( sz );
122 }
123 }
858b5bdd 124 else
f03fc89f
VZ
125 {
126 wxSize sz( size );
127 if (sz.y < 35)
128 {
129 sz.y = 35;
130 SetSize( sz );
131 }
132 }
858b5bdd 133 }
2e563988
RR
134 else
135 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 136
2d17d68f 137 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3
VZ
138
139 gtk_signal_connect( GTK_OBJECT(m_adjust),
2d17d68f 140 "value_changed",
80810ca3 141 (GtkSignalFunc) gtk_slider_callback,
f03fc89f 142 (gpointer) this );
80810ca3 143
2d17d68f
RR
144 SetRange( minValue, maxValue );
145 SetValue( value );
80810ca3 146
f03fc89f 147 m_parent->DoAddChild( this );
80810ca3 148
2d17d68f 149 PostCreation();
80810ca3 150
2d17d68f 151 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 152
2d17d68f 153 Show( TRUE );
80810ca3 154
2d17d68f 155 return TRUE;
6de97a3b 156}
c801d85f 157
1e1fafb9 158int wxSlider::GetValue() const
c801d85f 159{
2d17d68f 160 return (int)(m_adjust->value+0.5);
6de97a3b 161}
c801d85f 162
debe6624 163void wxSlider::SetValue( int value )
c801d85f 164{
2d17d68f
RR
165 float fpos = (float)value;
166 m_oldPos = fpos;
167 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 168
2d17d68f 169 m_adjust->value = fpos;
80810ca3 170
2d17d68f 171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 172}
c801d85f 173
debe6624 174void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 175{
2d17d68f
RR
176 float fmin = (float)minValue;
177 float fmax = (float)maxValue;
80810ca3 178
2d17d68f
RR
179 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
180 (fabs(fmax-m_adjust->upper) < 0.2))
181 {
182 return;
183 }
80810ca3 184
2d17d68f
RR
185 m_adjust->lower = fmin;
186 m_adjust->upper = fmax;
4ee1741f
RR
187 m_adjust->step_increment = 1.0;
188 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 189
2d17d68f 190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 191}
c801d85f 192
1e1fafb9 193int wxSlider::GetMin() const
c801d85f 194{
b1a39f47 195 return (int)ceil(m_adjust->lower);
6de97a3b 196}
c801d85f 197
1e1fafb9 198int wxSlider::GetMax() const
c801d85f 199{
b1a39f47 200 return (int)ceil(m_adjust->upper);
6de97a3b 201}
c801d85f 202
debe6624 203void wxSlider::SetPageSize( int pageSize )
c801d85f 204{
2d17d68f 205 float fpage = (float)pageSize;
80810ca3 206
2d17d68f 207 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 208
2d17d68f 209 m_adjust->page_increment = fpage;
c801d85f 210
2d17d68f 211 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 212}
c801d85f 213
1e1fafb9 214int wxSlider::GetPageSize() const
c801d85f 215{
b1a39f47 216 return (int)ceil(m_adjust->page_increment);
6de97a3b 217}
c801d85f 218
debe6624 219void wxSlider::SetThumbLength( int len )
c801d85f 220{
2d17d68f 221 float flen = (float)len;
80810ca3 222
2d17d68f 223 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 224
2d17d68f 225 m_adjust->page_size = flen;
c801d85f 226
2d17d68f 227 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 228}
c801d85f 229
1e1fafb9 230int wxSlider::GetThumbLength() const
c801d85f 231{
b1a39f47 232 return (int)ceil(m_adjust->page_size);
6de97a3b 233}
c801d85f 234
debe6624 235void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 236{
6de97a3b 237}
c801d85f 238
1e1fafb9 239int wxSlider::GetLineSize() const
c801d85f 240{
2d17d68f 241 return 0;
6de97a3b 242}
c801d85f 243
debe6624 244void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 245{
6de97a3b 246}
c801d85f 247
debe6624 248void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 249{
6de97a3b 250}
c801d85f 251
1e1fafb9 252int wxSlider::GetTickFreq() const
c801d85f 253{
2d17d68f 254 return 0;
6de97a3b 255}
c801d85f 256
1e1fafb9 257void wxSlider::ClearTicks()
c801d85f 258{
6de97a3b 259}
c801d85f 260
debe6624 261void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 262{
6de97a3b 263}
c801d85f 264
1e1fafb9 265int wxSlider::GetSelEnd() const
c801d85f 266{
2d17d68f 267 return 0;
6de97a3b 268}
c801d85f 269
1e1fafb9 270int wxSlider::GetSelStart() const
c801d85f 271{
2d17d68f 272 return 0;
6de97a3b 273}
c801d85f 274
1e1fafb9 275void wxSlider::ClearSel()
c801d85f 276{
6de97a3b 277}
c801d85f 278
b4071e91
RR
279bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
280{
2d17d68f
RR
281 GtkRange *range = GTK_RANGE(m_widget);
282 return ( (window == GTK_WIDGET(range)->window) ||
283 (window == range->trough) ||
f03fc89f
VZ
284 (window == range->slider) ||
285 (window == range->step_forw) ||
286 (window == range->step_back) );
b4071e91
RR
287}
288
58614078
RR
289void wxSlider::ApplyWidgetStyle()
290{
2d17d68f
RR
291 SetWidgetStyle();
292 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 293}
dcf924a3
RR
294
295#endif