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