]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
Fixed compile errors, warnings and other DSMs, and improved comments.
[wxWidgets.git] / src / gtk / slider.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: gtk/slider.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
1e6feb95 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
1e6feb95 36extern bool g_blockEventsOnDrag;
66bd6b93 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
0a07a7d8 64 double dvalue = adjust->value;
83c22707 65 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
80810ca3 66
1e6feb95
VZ
67 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
68 : wxHORIZONTAL;
80810ca3 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") );
ef7a25a7 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 );
ef7a25a7 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{
ef7a25a7
VZ
160 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
161 // -0.9 is rounded to -1
162 double val = m_adjust->value;
83c22707 163 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 164}
c801d85f 165
debe6624 166void wxSlider::SetValue( int value )
c801d85f 167{
2d17d68f
RR
168 float fpos = (float)value;
169 m_oldPos = fpos;
170 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 171
2d17d68f 172 m_adjust->value = fpos;
80810ca3 173
2d17d68f 174 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 175}
c801d85f 176
debe6624 177void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 178{
2d17d68f
RR
179 float fmin = (float)minValue;
180 float fmax = (float)maxValue;
80810ca3 181
2d17d68f
RR
182 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
183 (fabs(fmax-m_adjust->upper) < 0.2))
184 {
185 return;
186 }
80810ca3 187
2d17d68f
RR
188 m_adjust->lower = fmin;
189 m_adjust->upper = fmax;
4ee1741f
RR
190 m_adjust->step_increment = 1.0;
191 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 192
2d17d68f 193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 194}
c801d85f 195
1e1fafb9 196int wxSlider::GetMin() const
c801d85f 197{
b1a39f47 198 return (int)ceil(m_adjust->lower);
6de97a3b 199}
c801d85f 200
1e1fafb9 201int wxSlider::GetMax() const
c801d85f 202{
b1a39f47 203 return (int)ceil(m_adjust->upper);
6de97a3b 204}
c801d85f 205
debe6624 206void wxSlider::SetPageSize( int pageSize )
c801d85f 207{
2d17d68f 208 float fpage = (float)pageSize;
80810ca3 209
2d17d68f 210 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 211
2d17d68f 212 m_adjust->page_increment = fpage;
c801d85f 213
2d17d68f 214 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 215}
c801d85f 216
1e1fafb9 217int wxSlider::GetPageSize() const
c801d85f 218{
b1a39f47 219 return (int)ceil(m_adjust->page_increment);
6de97a3b 220}
c801d85f 221
debe6624 222void wxSlider::SetThumbLength( int len )
c801d85f 223{
2d17d68f 224 float flen = (float)len;
80810ca3 225
2d17d68f 226 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 227
2d17d68f 228 m_adjust->page_size = flen;
c801d85f 229
2d17d68f 230 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 231}
c801d85f 232
1e1fafb9 233int wxSlider::GetThumbLength() const
c801d85f 234{
b1a39f47 235 return (int)ceil(m_adjust->page_size);
6de97a3b 236}
c801d85f 237
debe6624 238void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 239{
6de97a3b 240}
c801d85f 241
1e1fafb9 242int wxSlider::GetLineSize() const
c801d85f 243{
2d17d68f 244 return 0;
6de97a3b 245}
c801d85f 246
b4071e91
RR
247bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
248{
2d17d68f
RR
249 GtkRange *range = GTK_RANGE(m_widget);
250 return ( (window == GTK_WIDGET(range)->window) ||
251 (window == range->trough) ||
f03fc89f
VZ
252 (window == range->slider) ||
253 (window == range->step_forw) ||
254 (window == range->step_back) );
b4071e91
RR
255}
256
58614078
RR
257void wxSlider::ApplyWidgetStyle()
258{
2d17d68f
RR
259 SetWidgetStyle();
260 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 261}
dcf924a3
RR
262
263#endif