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