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