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