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