]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
Various corrections to makefiles, RPMs etc.
[wxWidgets.git] / src / gtk1 / 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 );
828f655f 76 win->GetEventHandler()->ProcessEvent( cevent );
6de97a3b 77}
c801d85f 78
97b3455a
RR
79//-----------------------------------------------------------------------------
80// wxSlider
81//-----------------------------------------------------------------------------
82
c801d85f
KB
83IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
84
debe6624
JS
85bool wxSlider::Create(wxWindow *parent, wxWindowID id,
86 int value, int minValue, int maxValue,
c801d85f 87 const wxPoint& pos, const wxSize& size,
6de97a3b 88 long style, const wxValidator& validator, const wxString& name )
c801d85f 89{
b292e2f5 90 m_acceptsFocus = TRUE;
2d17d68f 91 m_needParent = TRUE;
80810ca3 92
4dcaf11a
RR
93 if (!PreCreation( parent, pos, size ) ||
94 !CreateBase( parent, id, pos, size, style, validator, name ))
95 {
223d09f6 96 wxFAIL_MSG( wxT("wxSlider creation failed") );
5b8a521e 97 return FALSE;
4dcaf11a 98 }
6de97a3b 99
2d17d68f 100 m_oldPos = 0.0;
c801d85f 101
2e563988 102 if (style & wxSL_VERTICAL)
2d17d68f 103 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
19da4326
RR
104 else
105 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
80810ca3 106
2e563988 107 if (style & wxSL_LABELS)
858b5bdd 108 {
2e563988 109 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
4ee1741f 110 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 111
f03fc89f
VZ
112 /* labels need more space and too small window will
113 cause junk to appear on the dialog */
858b5bdd 114 if (style & wxSL_VERTICAL)
f03fc89f
VZ
115 {
116 wxSize sz( size );
117 if (sz.x < 35)
118 {
119 sz.x = 35;
120 SetSize( sz );
121 }
122 }
858b5bdd 123 else
f03fc89f
VZ
124 {
125 wxSize sz( size );
126 if (sz.y < 35)
127 {
128 sz.y = 35;
129 SetSize( sz );
130 }
131 }
858b5bdd 132 }
2e563988
RR
133 else
134 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 135
2d17d68f 136 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3
VZ
137
138 gtk_signal_connect( GTK_OBJECT(m_adjust),
2d17d68f 139 "value_changed",
80810ca3 140 (GtkSignalFunc) gtk_slider_callback,
f03fc89f 141 (gpointer) this );
80810ca3 142
2d17d68f
RR
143 SetRange( minValue, maxValue );
144 SetValue( value );
80810ca3 145
f03fc89f 146 m_parent->DoAddChild( this );
80810ca3 147
2d17d68f 148 PostCreation();
80810ca3 149
2d17d68f 150 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 151
2d17d68f 152 Show( TRUE );
80810ca3 153
2d17d68f 154 return TRUE;
6de97a3b 155}
c801d85f 156
1e1fafb9 157int wxSlider::GetValue() const
c801d85f 158{
2d17d68f 159 return (int)(m_adjust->value+0.5);
6de97a3b 160}
c801d85f 161
debe6624 162void wxSlider::SetValue( int value )
c801d85f 163{
2d17d68f
RR
164 float fpos = (float)value;
165 m_oldPos = fpos;
166 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 167
2d17d68f 168 m_adjust->value = fpos;
80810ca3 169
2d17d68f 170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 171}
c801d85f 172
debe6624 173void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 174{
2d17d68f
RR
175 float fmin = (float)minValue;
176 float fmax = (float)maxValue;
80810ca3 177
2d17d68f
RR
178 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
179 (fabs(fmax-m_adjust->upper) < 0.2))
180 {
181 return;
182 }
80810ca3 183
2d17d68f
RR
184 m_adjust->lower = fmin;
185 m_adjust->upper = fmax;
4ee1741f
RR
186 m_adjust->step_increment = 1.0;
187 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 188
2d17d68f 189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 190}
c801d85f 191
1e1fafb9 192int wxSlider::GetMin() const
c801d85f 193{
b1a39f47 194 return (int)ceil(m_adjust->lower);
6de97a3b 195}
c801d85f 196
1e1fafb9 197int wxSlider::GetMax() const
c801d85f 198{
b1a39f47 199 return (int)ceil(m_adjust->upper);
6de97a3b 200}
c801d85f 201
debe6624 202void wxSlider::SetPageSize( int pageSize )
c801d85f 203{
2d17d68f 204 float fpage = (float)pageSize;
80810ca3 205
2d17d68f 206 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 207
2d17d68f 208 m_adjust->page_increment = fpage;
c801d85f 209
2d17d68f 210 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 211}
c801d85f 212
1e1fafb9 213int wxSlider::GetPageSize() const
c801d85f 214{
b1a39f47 215 return (int)ceil(m_adjust->page_increment);
6de97a3b 216}
c801d85f 217
debe6624 218void wxSlider::SetThumbLength( int len )
c801d85f 219{
2d17d68f 220 float flen = (float)len;
80810ca3 221
2d17d68f 222 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 223
2d17d68f 224 m_adjust->page_size = flen;
c801d85f 225
2d17d68f 226 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 227}
c801d85f 228
1e1fafb9 229int wxSlider::GetThumbLength() const
c801d85f 230{
b1a39f47 231 return (int)ceil(m_adjust->page_size);
6de97a3b 232}
c801d85f 233
debe6624 234void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 235{
6de97a3b 236}
c801d85f 237
1e1fafb9 238int wxSlider::GetLineSize() const
c801d85f 239{
2d17d68f 240 return 0;
6de97a3b 241}
c801d85f 242
debe6624 243void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 244{
6de97a3b 245}
c801d85f 246
debe6624 247void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 248{
6de97a3b 249}
c801d85f 250
1e1fafb9 251int wxSlider::GetTickFreq() const
c801d85f 252{
2d17d68f 253 return 0;
6de97a3b 254}
c801d85f 255
1e1fafb9 256void wxSlider::ClearTicks()
c801d85f 257{
6de97a3b 258}
c801d85f 259
debe6624 260void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 261{
6de97a3b 262}
c801d85f 263
1e1fafb9 264int wxSlider::GetSelEnd() const
c801d85f 265{
2d17d68f 266 return 0;
6de97a3b 267}
c801d85f 268
1e1fafb9 269int wxSlider::GetSelStart() const
c801d85f 270{
2d17d68f 271 return 0;
6de97a3b 272}
c801d85f 273
1e1fafb9 274void wxSlider::ClearSel()
c801d85f 275{
6de97a3b 276}
c801d85f 277
b4071e91
RR
278bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
279{
2d17d68f
RR
280 GtkRange *range = GTK_RANGE(m_widget);
281 return ( (window == GTK_WIDGET(range)->window) ||
282 (window == range->trough) ||
f03fc89f
VZ
283 (window == range->slider) ||
284 (window == range->step_forw) ||
285 (window == range->step_back) );
b4071e91
RR
286}
287
58614078
RR
288void wxSlider::ApplyWidgetStyle()
289{
2d17d68f
RR
290 SetWidgetStyle();
291 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 292}
dcf924a3
RR
293
294#endif