]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
Always use XPMs in this sample
[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
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
2d17d68f 144 PostCreation();
e8e24dfa 145 InheritAttributes();
f96aa4d9 146
2d17d68f 147 Show( TRUE );
80810ca3 148
2d17d68f 149 return TRUE;
6de97a3b 150}
c801d85f 151
1e1fafb9 152int wxSlider::GetValue() const
c801d85f 153{
ef7a25a7
VZ
154 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
155 // -0.9 is rounded to -1
156 double val = m_adjust->value;
83c22707 157 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 158}
c801d85f 159
debe6624 160void wxSlider::SetValue( int value )
c801d85f 161{
2d17d68f
RR
162 float fpos = (float)value;
163 m_oldPos = fpos;
164 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 165
2d17d68f 166 m_adjust->value = fpos;
80810ca3 167
e1b93ccb
RR
168 GtkDisableEvents();
169
2d17d68f 170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
e1b93ccb
RR
171
172 GtkEnableEvents();
6de97a3b 173}
c801d85f 174
debe6624 175void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 176{
2d17d68f
RR
177 float fmin = (float)minValue;
178 float fmax = (float)maxValue;
80810ca3 179
2d17d68f
RR
180 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
181 (fabs(fmax-m_adjust->upper) < 0.2))
182 {
183 return;
184 }
80810ca3 185
2d17d68f
RR
186 m_adjust->lower = fmin;
187 m_adjust->upper = fmax;
4ee1741f
RR
188 m_adjust->step_increment = 1.0;
189 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 190
e1b93ccb
RR
191 GtkDisableEvents();
192
2d17d68f 193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
194
195 GtkEnableEvents();
6de97a3b 196}
c801d85f 197
1e1fafb9 198int wxSlider::GetMin() const
c801d85f 199{
b1a39f47 200 return (int)ceil(m_adjust->lower);
6de97a3b 201}
c801d85f 202
1e1fafb9 203int wxSlider::GetMax() const
c801d85f 204{
b1a39f47 205 return (int)ceil(m_adjust->upper);
6de97a3b 206}
c801d85f 207
debe6624 208void wxSlider::SetPageSize( int pageSize )
c801d85f 209{
2d17d68f 210 float fpage = (float)pageSize;
80810ca3 211
2d17d68f 212 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 213
2d17d68f 214 m_adjust->page_increment = fpage;
c801d85f 215
e1b93ccb
RR
216 GtkDisableEvents();
217
2d17d68f 218 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
219
220 GtkEnableEvents();
6de97a3b 221}
c801d85f 222
1e1fafb9 223int wxSlider::GetPageSize() const
c801d85f 224{
b1a39f47 225 return (int)ceil(m_adjust->page_increment);
6de97a3b 226}
c801d85f 227
debe6624 228void wxSlider::SetThumbLength( int len )
c801d85f 229{
2d17d68f 230 float flen = (float)len;
80810ca3 231
2d17d68f 232 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 233
2d17d68f 234 m_adjust->page_size = flen;
c801d85f 235
e1b93ccb
RR
236 GtkDisableEvents();
237
2d17d68f 238 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
239
240 GtkEnableEvents();
6de97a3b 241}
c801d85f 242
1e1fafb9 243int wxSlider::GetThumbLength() const
c801d85f 244{
b1a39f47 245 return (int)ceil(m_adjust->page_size);
6de97a3b 246}
c801d85f 247
debe6624 248void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 249{
6de97a3b 250}
c801d85f 251
1e1fafb9 252int wxSlider::GetLineSize() const
c801d85f 253{
2d17d68f 254 return 0;
6de97a3b 255}
c801d85f 256
b4071e91
RR
257bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
258{
2d17d68f 259 GtkRange *range = GTK_RANGE(m_widget);
2b5f62a0
VZ
260#ifdef __WXGTK20__
261 return (range->event_window == window);
262#else
9e691f46 263 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
264 || (window == range->trough)
265 || (window == range->slider)
266 || (window == range->step_forw)
2b5f62a0
VZ
267 || (window == range->step_back) );
268#endif
b4071e91
RR
269}
270
58614078
RR
271void wxSlider::ApplyWidgetStyle()
272{
2d17d68f
RR
273 SetWidgetStyle();
274 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 275}
dcf924a3 276
e1b93ccb
RR
277void wxSlider::GtkDisableEvents()
278{
279 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
280 GTK_SIGNAL_FUNC(gtk_slider_callback),
281 (gpointer) this );
282}
283
284void wxSlider::GtkEnableEvents()
285{
286 gtk_signal_connect( GTK_OBJECT (m_adjust),
287 "value_changed",
288 GTK_SIGNAL_FUNC(gtk_slider_callback),
289 (gpointer) this );
290}
291
dcf924a3 292#endif