]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
Add wxSpinCtrl::SetBase() to allow entering hexadecimal numbers.
[wxWidgets.git] / src / gtk1 / slider.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
de6185e2 2// Name: src/gtk1/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
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
de6185e2
WS
13#if wxUSE_SLIDER
14
c801d85f 15#include "wx/slider.h"
dcf924a3 16
de6185e2
WS
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
18680f86 19 #include "wx/math.h"
de6185e2 20#endif
dcf924a3 21
3cbab641 22#include "wx/gtk1/private.h"
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
1e6feb95 35extern bool g_blockEventsOnDrag;
66bd6b93 36
2b024653
VZ
37// ----------------------------------------------------------------------------
38// helper functions
39// ----------------------------------------------------------------------------
40
41// compare 2 adjustment values up to some (hardcoded) precision
42static inline bool AreSameAdjustValues(double x, double y)
43{
44 return fabs(x - y) < 0.02;
45}
46
2b024653
VZ
47// process a scroll event
48static void
49ProcessScrollEvent(wxSlider *win, wxEventType evtType, double dvalue)
50{
51 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
52 : wxHORIZONTAL;
53
54 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
55 wxScrollEvent event( evtType, win->GetId(), value, orient );
56 event.SetEventObject( win );
937013e0 57 win->HandleWindowEvent( event );
2b024653 58
8e3e14c4
VZ
59 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
60 {
61 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
62 event2.SetEventObject( win );
937013e0 63 win->HandleWindowEvent( event2 );
8e3e14c4
VZ
64 }
65
2b024653
VZ
66 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
67 cevent.SetEventObject( win );
68 cevent.SetInt( value );
937013e0 69 win->HandleWindowEvent( cevent );
2b024653 70}
57a1fd73 71
c801d85f 72//-----------------------------------------------------------------------------
97b3455a 73// "value_changed"
c801d85f
KB
74//-----------------------------------------------------------------------------
75
865bb325 76extern "C" {
9e691f46
VZ
77static void gtk_slider_callback( GtkAdjustment *adjust,
78 SCROLLBAR_CBACK_ARG
79 wxSlider *win )
80810ca3 80{
acfd422a
RR
81 if (g_isIdle) wxapp_install_idle_handler();
82
a2053b27 83 if (!win->m_hasVMT) return;
2d17d68f 84 if (g_blockEventsOnDrag) return;
e1811a01 85
2b024653
VZ
86 const double dvalue = adjust->value;
87 const double diff = dvalue - win->m_oldPos;
88 if ( AreSameAdjustValues(diff, 0) )
89 return;
80810ca3 90
2b024653 91 wxEventType evtType;
2b024653 92 evtType = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
2d17d68f 93
2b024653 94 ProcessScrollEvent(win, evtType, dvalue);
80810ca3 95
2b024653
VZ
96 win->m_oldPos = dvalue;
97}
80810ca3 98
2b024653
VZ
99static gint gtk_slider_button_press_callback( GtkWidget * /* widget */,
100 GdkEventButton * /* gdk_event */,
101 wxWindowGTK *win)
102{
103 // indicate that the thumb is being dragged with the mouse
104 win->m_isScrolling = true;
80810ca3 105
2b024653
VZ
106 return FALSE;
107}
80810ca3 108
2b024653
VZ
109static gint gtk_slider_button_release_callback( GtkWidget *scale,
110 GdkEventButton * /* gdk_event */,
111 wxSlider *win)
112{
113 // not scrolling any longer
114 win->m_isScrolling = false;
115
116 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE,
117 GTK_RANGE(scale)->adjustment->value);
118
119 return FALSE;
6de97a3b 120}
2b024653 121
865bb325 122}
c801d85f 123
97b3455a
RR
124//-----------------------------------------------------------------------------
125// wxSlider
126//-----------------------------------------------------------------------------
127
debe6624
JS
128bool wxSlider::Create(wxWindow *parent, wxWindowID id,
129 int value, int minValue, int maxValue,
c801d85f 130 const wxPoint& pos, const wxSize& size,
6de97a3b 131 long style, const wxValidator& validator, const wxString& name )
c801d85f 132{
de6185e2
WS
133 m_acceptsFocus = true;
134 m_needParent = true;
80810ca3 135
4dcaf11a
RR
136 if (!PreCreation( parent, pos, size ) ||
137 !CreateBase( parent, id, pos, size, style, validator, name ))
138 {
223d09f6 139 wxFAIL_MSG( wxT("wxSlider creation failed") );
de6185e2 140 return false;
4dcaf11a 141 }
6de97a3b 142
2d17d68f 143 m_oldPos = 0.0;
c801d85f 144
2e563988 145 if (style & wxSL_VERTICAL)
d3b9f782 146 m_widget = gtk_vscale_new( NULL );
19da4326 147 else
d3b9f782 148 m_widget = gtk_hscale_new( NULL );
80810ca3 149
2e563988 150 if (style & wxSL_LABELS)
858b5bdd 151 {
2e563988 152 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
ef7a25a7 153 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 154
f03fc89f
VZ
155 /* labels need more space and too small window will
156 cause junk to appear on the dialog */
858b5bdd 157 if (style & wxSL_VERTICAL)
f03fc89f
VZ
158 {
159 wxSize sz( size );
160 if (sz.x < 35)
161 {
162 sz.x = 35;
163 SetSize( sz );
164 }
165 }
858b5bdd 166 else
f03fc89f
VZ
167 {
168 wxSize sz( size );
169 if (sz.y < 35)
170 {
171 sz.y = 35;
172 SetSize( sz );
173 }
174 }
858b5bdd 175 }
2e563988
RR
176 else
177 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 178
2d17d68f 179 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3 180
91b167dd 181 GtkEnableEvents();
2b024653
VZ
182 gtk_signal_connect( GTK_OBJECT(m_widget),
183 "button_press_event",
184 (GtkSignalFunc)gtk_slider_button_press_callback,
185 (gpointer) this );
186 gtk_signal_connect( GTK_OBJECT(m_widget),
187 "button_release_event",
188 (GtkSignalFunc)gtk_slider_button_release_callback,
189 (gpointer) this );
91b167dd 190
2d17d68f
RR
191 SetRange( minValue, maxValue );
192 SetValue( value );
80810ca3 193
f03fc89f 194 m_parent->DoAddChild( this );
80810ca3 195
abdeb9e7 196 PostCreation(size);
80810ca3 197
de6185e2 198 return true;
6de97a3b 199}
c801d85f 200
1e1fafb9 201int wxSlider::GetValue() const
c801d85f 202{
23f826bd 203 return wxRound(m_adjust->value);
6de97a3b 204}
c801d85f 205
debe6624 206void wxSlider::SetValue( int value )
c801d85f 207{
2b024653 208 double fpos = (double)value;
2d17d68f 209 m_oldPos = fpos;
2b024653
VZ
210 if ( AreSameAdjustValues(fpos, m_adjust->value) )
211 return;
80810ca3 212
2d17d68f 213 m_adjust->value = fpos;
80810ca3 214
e1b93ccb 215 GtkDisableEvents();
2b024653 216
2d17d68f 217 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
2b024653 218
e1b93ccb 219 GtkEnableEvents();
6de97a3b 220}
c801d85f 221
debe6624 222void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 223{
2b024653
VZ
224 double fmin = (double)minValue;
225 double fmax = (double)maxValue;
80810ca3 226
2d17d68f
RR
227 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
228 (fabs(fmax-m_adjust->upper) < 0.2))
229 {
230 return;
231 }
80810ca3 232
2d17d68f
RR
233 m_adjust->lower = fmin;
234 m_adjust->upper = fmax;
4ee1741f
RR
235 m_adjust->step_increment = 1.0;
236 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 237
e1b93ccb 238 GtkDisableEvents();
2b024653 239
2d17d68f 240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
2b024653 241
e1b93ccb 242 GtkEnableEvents();
6de97a3b 243}
c801d85f 244
1e1fafb9 245int wxSlider::GetMin() const
c801d85f 246{
b1a39f47 247 return (int)ceil(m_adjust->lower);
6de97a3b 248}
c801d85f 249
1e1fafb9 250int wxSlider::GetMax() const
c801d85f 251{
b1a39f47 252 return (int)ceil(m_adjust->upper);
6de97a3b 253}
c801d85f 254
debe6624 255void wxSlider::SetPageSize( int pageSize )
c801d85f 256{
2b024653 257 double fpage = (double)pageSize;
80810ca3 258
2d17d68f 259 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 260
2d17d68f 261 m_adjust->page_increment = fpage;
c801d85f 262
e1b93ccb 263 GtkDisableEvents();
2b024653 264
2d17d68f 265 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
2b024653 266
e1b93ccb 267 GtkEnableEvents();
6de97a3b 268}
c801d85f 269
1e1fafb9 270int wxSlider::GetPageSize() const
c801d85f 271{
b1a39f47 272 return (int)ceil(m_adjust->page_increment);
6de97a3b 273}
c801d85f 274
debe6624 275void wxSlider::SetThumbLength( int len )
c801d85f 276{
2b024653 277 double flen = (double)len;
80810ca3 278
2d17d68f 279 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 280
2d17d68f 281 m_adjust->page_size = flen;
c801d85f 282
e1b93ccb 283 GtkDisableEvents();
2b024653 284
2d17d68f 285 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
2b024653 286
e1b93ccb 287 GtkEnableEvents();
6de97a3b 288}
c801d85f 289
1e1fafb9 290int wxSlider::GetThumbLength() const
c801d85f 291{
b1a39f47 292 return (int)ceil(m_adjust->page_size);
6de97a3b 293}
c801d85f 294
debe6624 295void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 296{
6de97a3b 297}
c801d85f 298
1e1fafb9 299int wxSlider::GetLineSize() const
c801d85f 300{
2d17d68f 301 return 0;
6de97a3b 302}
c801d85f 303
b4071e91
RR
304bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
305{
2d17d68f 306 GtkRange *range = GTK_RANGE(m_widget);
9e691f46 307 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
308 || (window == range->trough)
309 || (window == range->slider)
310 || (window == range->step_forw)
2b5f62a0 311 || (window == range->step_back) );
b4071e91
RR
312}
313
e1b93ccb
RR
314void wxSlider::GtkDisableEvents()
315{
316 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
317 GTK_SIGNAL_FUNC(gtk_slider_callback),
318 (gpointer) this );
319}
320
321void wxSlider::GtkEnableEvents()
322{
323 gtk_signal_connect( GTK_OBJECT (m_adjust),
324 "value_changed",
325 GTK_SIGNAL_FUNC(gtk_slider_callback),
326 (gpointer) this );
327}
328
9d522606
RD
329// static
330wxVisualAttributes
331wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
332{
333 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
334}
335
de6185e2 336#endif // wxUSE_SLIDER