]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
Bug fix in GetLabelFromText
[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") );
4dcaf11a
RR
97 return FALSE;
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 );
572c6194 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;
c801d85f 186
2d17d68f 187 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 188}
c801d85f 189
1e1fafb9 190int wxSlider::GetMin() const
c801d85f 191{
b1a39f47 192 return (int)ceil(m_adjust->lower);
6de97a3b 193}
c801d85f 194
1e1fafb9 195int wxSlider::GetMax() const
c801d85f 196{
b1a39f47 197 return (int)ceil(m_adjust->upper);
6de97a3b 198}
c801d85f 199
debe6624 200void wxSlider::SetPageSize( int pageSize )
c801d85f 201{
2d17d68f 202 float fpage = (float)pageSize;
80810ca3 203
2d17d68f 204 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 205
2d17d68f 206 m_adjust->page_increment = fpage;
c801d85f 207
2d17d68f 208 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 209}
c801d85f 210
1e1fafb9 211int wxSlider::GetPageSize() const
c801d85f 212{
b1a39f47 213 return (int)ceil(m_adjust->page_increment);
6de97a3b 214}
c801d85f 215
debe6624 216void wxSlider::SetThumbLength( int len )
c801d85f 217{
2d17d68f 218 float flen = (float)len;
80810ca3 219
2d17d68f 220 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 221
2d17d68f 222 m_adjust->page_size = flen;
c801d85f 223
2d17d68f 224 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 225}
c801d85f 226
1e1fafb9 227int wxSlider::GetThumbLength() const
c801d85f 228{
b1a39f47 229 return (int)ceil(m_adjust->page_size);
6de97a3b 230}
c801d85f 231
debe6624 232void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 233{
6de97a3b 234}
c801d85f 235
1e1fafb9 236int wxSlider::GetLineSize() const
c801d85f 237{
2d17d68f 238 return 0;
6de97a3b 239}
c801d85f 240
debe6624 241void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 242{
6de97a3b 243}
c801d85f 244
debe6624 245void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 246{
6de97a3b 247}
c801d85f 248
1e1fafb9 249int wxSlider::GetTickFreq() const
c801d85f 250{
2d17d68f 251 return 0;
6de97a3b 252}
c801d85f 253
1e1fafb9 254void wxSlider::ClearTicks()
c801d85f 255{
6de97a3b 256}
c801d85f 257
debe6624 258void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 259{
6de97a3b 260}
c801d85f 261
1e1fafb9 262int wxSlider::GetSelEnd() const
c801d85f 263{
2d17d68f 264 return 0;
6de97a3b 265}
c801d85f 266
1e1fafb9 267int wxSlider::GetSelStart() const
c801d85f 268{
2d17d68f 269 return 0;
6de97a3b 270}
c801d85f 271
1e1fafb9 272void wxSlider::ClearSel()
c801d85f 273{
6de97a3b 274}
c801d85f 275
b4071e91
RR
276bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
277{
2d17d68f
RR
278 GtkRange *range = GTK_RANGE(m_widget);
279 return ( (window == GTK_WIDGET(range)->window) ||
280 (window == range->trough) ||
f03fc89f
VZ
281 (window == range->slider) ||
282 (window == range->step_forw) ||
283 (window == range->step_back) );
b4071e91
RR
284}
285
58614078
RR
286void wxSlider::ApplyWidgetStyle()
287{
2d17d68f
RR
288 SetWidgetStyle();
289 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 290}
dcf924a3
RR
291
292#endif