]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
new file added
[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"
23f826bd 19 #include "wx/math.h"
de6185e2 20#endif
dcf924a3 21
a1abca32 22#include <gtk/gtk.h>
385e8575 23#include "wx/gtk/private/gtk2-compat.h"
83624f79 24
66bd6b93
RR
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
1e6feb95 29extern bool g_blockEventsOnDrag;
66bd6b93 30
2b024653
VZ
31// ----------------------------------------------------------------------------
32// helper functions
33// ----------------------------------------------------------------------------
34
2b024653
VZ
35// process a scroll event
36static void
12a480c1 37ProcessScrollEvent(wxSlider *win, wxEventType evtType)
2b024653 38{
46c48053
VZ
39 const int orient = win->HasFlag(wxSL_VERTICAL) ? wxVERTICAL
40 : wxHORIZONTAL;
2b024653 41
12a480c1 42 const int value = win->GetValue();
2b024653 43
46c48053
VZ
44 // if we have any "special" event (i.e. the value changed by a line or a
45 // page), send this specific event first
46 if ( evtType != wxEVT_NULL )
47 {
48 wxScrollEvent event( evtType, win->GetId(), value, orient );
49 event.SetEventObject( win );
937013e0 50 win->HandleWindowEvent( event );
46c48053
VZ
51 }
52
53 // but, in any case, except if we're dragging the slider (and so the change
54 // is not definitive), send a generic "changed" event
8e3e14c4
VZ
55 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
56 {
46c48053
VZ
57 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
58 event.SetEventObject( win );
937013e0 59 win->HandleWindowEvent( event );
8e3e14c4
VZ
60 }
61
46c48053
VZ
62 // and also generate a command event for compatibility
63 wxCommandEvent event( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
64 event.SetEventObject( win );
65 event.SetInt( value );
937013e0 66 win->HandleWindowEvent( event );
2b024653 67}
57a1fd73 68
12a480c1
PC
69static inline wxEventType GtkScrollTypeToWx(int scrollType)
70{
71 wxEventType eventType;
72 switch (scrollType)
73 {
74 case GTK_SCROLL_STEP_BACKWARD:
75 case GTK_SCROLL_STEP_LEFT:
76 case GTK_SCROLL_STEP_UP:
77 eventType = wxEVT_SCROLL_LINEUP;
78 break;
79 case GTK_SCROLL_STEP_DOWN:
80 case GTK_SCROLL_STEP_FORWARD:
81 case GTK_SCROLL_STEP_RIGHT:
82 eventType = wxEVT_SCROLL_LINEDOWN;
83 break;
84 case GTK_SCROLL_PAGE_BACKWARD:
85 case GTK_SCROLL_PAGE_LEFT:
86 case GTK_SCROLL_PAGE_UP:
87 eventType = wxEVT_SCROLL_PAGEUP;
88 break;
89 case GTK_SCROLL_PAGE_DOWN:
90 case GTK_SCROLL_PAGE_FORWARD:
91 case GTK_SCROLL_PAGE_RIGHT:
92 eventType = wxEVT_SCROLL_PAGEDOWN;
93 break;
94 case GTK_SCROLL_START:
95 eventType = wxEVT_SCROLL_TOP;
96 break;
97 case GTK_SCROLL_END:
98 eventType = wxEVT_SCROLL_BOTTOM;
99 break;
100 case GTK_SCROLL_JUMP:
101 eventType = wxEVT_SCROLL_THUMBTRACK;
102 break;
103 default:
9a83f860 104 wxFAIL_MSG(wxT("Unknown GtkScrollType"));
12a480c1
PC
105 eventType = wxEVT_NULL;
106 break;
107 }
108 return eventType;
109}
110
111// Determine if increment is the same as +/-x, allowing for some small
112// difference due to possible inexactness in floating point arithmetic
113static inline bool IsScrollIncrement(double increment, double x)
114{
115 wxASSERT(increment > 0);
116 const double tolerance = 1.0 / 1024;
117 return fabs(increment - fabs(x)) < tolerance;
118}
119
c801d85f 120//-----------------------------------------------------------------------------
97b3455a 121// "value_changed"
c801d85f
KB
122//-----------------------------------------------------------------------------
123
865bb325 124extern "C" {
12a480c1
PC
125static void
126gtk_value_changed(GtkRange* range, wxSlider* win)
80810ca3 127{
385e8575 128 const double value = gtk_range_get_value(range);
12a480c1 129 const double oldPos = win->m_pos;
385e8575 130 win->m_pos = value;
559f60ef
PC
131
132 if (!win->m_hasVMT || g_blockEventsOnDrag)
133 return;
134
c092ed38 135 if (win->GTKEventsDisabled())
12a480c1
PC
136 {
137 win->m_scrollEventType = GTK_SCROLL_NONE;
2b024653 138 return;
12a480c1 139 }
80810ca3 140
12a480c1
PC
141 wxEventType eventType = wxEVT_NULL;
142 if (win->m_isScrolling)
143 {
144 eventType = wxEVT_SCROLL_THUMBTRACK;
145 }
146 else if (win->m_scrollEventType != GTK_SCROLL_NONE)
147 {
148 // Scroll event from "move-slider" (keyboard)
149 eventType = GtkScrollTypeToWx(win->m_scrollEventType);
150 }
151 else if (win->m_mouseButtonDown)
152 {
153 // Difference from last change event
385e8575 154 const double diff = value - oldPos;
12a480c1 155 const bool isDown = diff > 0;
2d17d68f 156
385e8575
PC
157 GtkAdjustment* adj = gtk_range_get_adjustment(range);
158 if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj), diff))
12a480c1
PC
159 {
160 eventType = isDown ? wxEVT_SCROLL_PAGEDOWN : wxEVT_SCROLL_PAGEUP;
161 }
385e8575 162 else if (wxIsSameDouble(value, 0))
12a480c1
PC
163 {
164 eventType = wxEVT_SCROLL_PAGEUP;
165 }
385e8575 166 else if (wxIsSameDouble(value, gtk_adjustment_get_upper(adj)))
12a480c1
PC
167 {
168 eventType = wxEVT_SCROLL_PAGEDOWN;
169 }
170 else
171 {
172 // Assume track event
173 eventType = wxEVT_SCROLL_THUMBTRACK;
174 // Remember that we're tracking
175 win->m_isScrolling = true;
176 }
177 }
178
179 win->m_scrollEventType = GTK_SCROLL_NONE;
180
181 // If integral position has changed
385e8575 182 if (wxRound(oldPos) != wxRound(value))
12a480c1 183 {
12a480c1
PC
184 ProcessScrollEvent(win, eventType);
185 win->m_needThumbRelease = eventType == wxEVT_SCROLL_THUMBTRACK;
186 }
187}
188}
80810ca3 189
12a480c1
PC
190//-----------------------------------------------------------------------------
191// "move_slider" (keyboard event)
192//-----------------------------------------------------------------------------
193
194extern "C" {
195static void
196gtk_move_slider(GtkRange*, GtkScrollType scrollType, wxSlider* win)
197{
198 // Save keyboard scroll type for "value_changed" handler
199 win->m_scrollEventType = scrollType;
200}
2b024653 201}
80810ca3 202
12a480c1
PC
203//-----------------------------------------------------------------------------
204// "button_press_event"
205//-----------------------------------------------------------------------------
206
207extern "C" {
208static gboolean
209gtk_button_press_event(GtkWidget*, GdkEventButton*, wxSlider* win)
2b024653 210{
12a480c1 211 win->m_mouseButtonDown = true;
80810ca3 212
12a480c1
PC
213 return false;
214}
2b024653 215}
80810ca3 216
c918b2cd
PC
217//-----------------------------------------------------------------------------
218// "event_after"
219//-----------------------------------------------------------------------------
220
12a480c1 221extern "C" {
c918b2cd
PC
222static void
223gtk_event_after(GtkRange* range, GdkEvent* event, wxSlider* win)
2b024653 224{
c918b2cd 225 if (event->type == GDK_BUTTON_RELEASE)
12a480c1 226 {
20c3174d 227 g_signal_handlers_block_by_func(range, (gpointer) gtk_event_after, win);
c918b2cd
PC
228
229 if (win->m_needThumbRelease)
230 {
231 win->m_needThumbRelease = false;
232 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE);
233 }
234 // Keep slider at an integral position
c092ed38
RR
235 win->GTKDisableEvents();
236 gtk_range_set_value(GTK_RANGE (win->m_scale), win->GetValue());
237 win->GTKEnableEvents();
12a480c1 238 }
12a480c1
PC
239}
240}
2b024653 241
12a480c1
PC
242//-----------------------------------------------------------------------------
243// "button_release_event"
244//-----------------------------------------------------------------------------
2b024653 245
12a480c1
PC
246extern "C" {
247static gboolean
c918b2cd 248gtk_button_release_event(GtkRange* range, GdkEventButton*, wxSlider* win)
12a480c1
PC
249{
250 win->m_mouseButtonDown = false;
251 if (win->m_isScrolling)
252 {
c918b2cd 253 win->m_isScrolling = false;
20c3174d 254 g_signal_handlers_unblock_by_func(range, (gpointer) gtk_event_after, win);
12a480c1
PC
255 }
256 return false;
257}
6de97a3b 258}
2b024653 259
12a480c1
PC
260//-----------------------------------------------------------------------------
261// "format_value"
262//-----------------------------------------------------------------------------
263
264extern "C" {
265static gchar* gtk_format_value(GtkScale*, double value, void*)
266{
267 // Format value as nearest integer
23f826bd 268 return g_strdup_printf("%d", wxRound(value));
12a480c1 269}
865bb325 270}
c801d85f 271
97b3455a
RR
272//-----------------------------------------------------------------------------
273// wxSlider
274//-----------------------------------------------------------------------------
275
12a480c1
PC
276wxSlider::wxSlider()
277{
278 m_pos = 0;
3824e0db 279 m_scrollEventType = GTK_SCROLL_NONE;
12a480c1 280 m_needThumbRelease = false;
1d591651 281 m_blockScrollEvent = false;
12a480c1
PC
282}
283
e8375af8
VZ
284bool wxSlider::Create(wxWindow *parent,
285 wxWindowID id,
286 int value,
287 int minValue,
288 int maxValue,
289 const wxPoint& pos,
290 const wxSize& size,
291 long style,
292 const wxValidator& validator,
293 const wxString& name)
c801d85f 294{
3824e0db
VZ
295 m_pos = value;
296 m_scrollEventType = GTK_SCROLL_NONE;
297
4dcaf11a
RR
298 if (!PreCreation( parent, pos, size ) ||
299 !CreateBase( parent, id, pos, size, style, validator, name ))
300 {
223d09f6 301 wxFAIL_MSG( wxT("wxSlider creation failed") );
de6185e2 302 return false;
4dcaf11a 303 }
eec4500d 304
6de97a3b 305
2e563988 306 if (style & wxSL_VERTICAL)
c092ed38 307 m_scale = gtk_vscale_new( NULL );
19da4326 308 else
c092ed38
RR
309 m_scale = gtk_hscale_new( NULL );
310 g_object_ref(m_scale);
80810ca3 311
c092ed38
RR
312 if (style & wxSL_MIN_MAX_LABELS)
313 {
314 gtk_widget_show( m_scale );
eec4500d 315
c092ed38
RR
316 if (style & wxSL_VERTICAL)
317 m_widget = gtk_hbox_new(false, 0);
318 else
319 m_widget = gtk_vbox_new(false, 0);
320 g_object_ref(m_widget);
c092ed38
RR
321 gtk_container_add( GTK_CONTAINER(m_widget), m_scale );
322
323 GtkWidget *box;
324 if (style & wxSL_VERTICAL)
325 box = gtk_vbox_new(false,0);
326 else
327 box = gtk_hbox_new(false,0);
328 g_object_ref(box);
329 gtk_widget_show(box);
330 gtk_container_add( GTK_CONTAINER(m_widget), box );
eec4500d 331
c092ed38
RR
332 m_minLabel = gtk_label_new(NULL);
333 g_object_ref(m_minLabel);
334 gtk_widget_show( m_minLabel );
335 gtk_container_add( GTK_CONTAINER(box), m_minLabel );
336 gtk_box_set_child_packing( GTK_BOX(box), m_minLabel, FALSE, FALSE, 0, GTK_PACK_START );
eec4500d 337
c092ed38
RR
338 // expanding empty space between the min/max labels
339 GtkWidget *space = gtk_label_new(NULL);
340 g_object_ref(space);
341 gtk_widget_show( space );
342 gtk_container_add( GTK_CONTAINER(box), space );
343 gtk_box_set_child_packing( GTK_BOX(box), space, TRUE, FALSE, 0, GTK_PACK_START );
eec4500d 344
c092ed38
RR
345 m_maxLabel = gtk_label_new(NULL);
346 g_object_ref(m_maxLabel);
347 gtk_widget_show( m_maxLabel );
348 gtk_container_add( GTK_CONTAINER(box), m_maxLabel );
349 gtk_box_set_child_packing( GTK_BOX(box), m_maxLabel, FALSE, FALSE, 0, GTK_PACK_END );
350 }
351 else
352 {
353 m_widget = m_scale;
354 m_maxLabel = NULL;
355 m_minLabel = NULL;
356 }
eec4500d 357
c0a9fe92
VZ
358 const bool showValueLabel = (style & wxSL_VALUE_LABEL) != 0;
359 gtk_scale_set_draw_value(GTK_SCALE (m_scale), showValueLabel );
360 if ( showValueLabel )
3312ee03 361 {
c0a9fe92
VZ
362 // position the label appropriately: notice that wxSL_DIRECTION flags
363 // specify the position of the ticks, not label, under MSW and so the
364 // label is on the opposite side
365 GtkPositionType posLabel;
366 if ( style & wxSL_VERTICAL )
367 {
368 if ( style & wxSL_LEFT )
369 posLabel = GTK_POS_RIGHT;
370 else // if ( style & wxSL_RIGHT ) -- this is also the default
371 posLabel = GTK_POS_LEFT;
372 }
373 else // horizontal slider
374 {
375 if ( style & wxSL_TOP )
376 posLabel = GTK_POS_BOTTOM;
377 else // if ( style & wxSL_BOTTOM) -- this is again the default
378 posLabel = GTK_POS_TOP;
379 }
eec4500d 380
c0a9fe92 381 gtk_scale_set_value_pos( GTK_SCALE(m_scale), posLabel );
f050bdbd 382 }
eec4500d 383
12a480c1 384 // Keep full precision in position value
c092ed38 385 gtk_scale_set_digits(GTK_SCALE (m_scale), -1);
80810ca3 386
1e219378 387 if (style & wxSL_INVERSE)
c092ed38 388 gtk_range_set_inverted( GTK_RANGE(m_scale), TRUE );
1e219378 389
c092ed38
RR
390 g_signal_connect(m_scale, "button_press_event", G_CALLBACK(gtk_button_press_event), this);
391 g_signal_connect(m_scale, "button_release_event", G_CALLBACK(gtk_button_release_event), this);
392 g_signal_connect(m_scale, "move_slider", G_CALLBACK(gtk_move_slider), this);
393 g_signal_connect(m_scale, "format_value", G_CALLBACK(gtk_format_value), NULL);
394 g_signal_connect(m_scale, "value_changed", G_CALLBACK(gtk_value_changed), this);
395 gulong handler_id = g_signal_connect(m_scale, "event_after", G_CALLBACK(gtk_event_after), this);
396 g_signal_handler_block(m_scale, handler_id);
91b167dd 397
2d17d68f 398 SetRange( minValue, maxValue );
3824e0db
VZ
399
400 // don't call the public SetValue() as it won't do anything unless the
401 // value really changed
402 GTKSetValue( value );
80810ca3 403
f03fc89f 404 m_parent->DoAddChild( this );
80810ca3 405
abdeb9e7 406 PostCreation(size);
80810ca3 407
de6185e2 408 return true;
6de97a3b 409}
c801d85f 410
c092ed38
RR
411void wxSlider::GTKDisableEvents()
412{
413 m_blockScrollEvent = true;
414}
415
416void wxSlider::GTKEnableEvents()
417{
418 m_blockScrollEvent = false;
419}
420
421bool wxSlider::GTKEventsDisabled() const
422{
423 return m_blockScrollEvent;
424}
425
1e1fafb9 426int wxSlider::GetValue() const
c801d85f 427{
23f826bd 428 return wxRound(m_pos);
6de97a3b 429}
c801d85f 430
debe6624 431void wxSlider::SetValue( int value )
c801d85f 432{
12a480c1 433 if (GetValue() != value)
3824e0db
VZ
434 GTKSetValue(value);
435}
436
437void wxSlider::GTKSetValue(int value)
438{
c092ed38
RR
439 GTKDisableEvents();
440 gtk_range_set_value(GTK_RANGE (m_scale), value);
ba9eca1a
PC
441 // GTK only updates value label if handle moves at least 1 pixel
442 gtk_widget_queue_draw(m_scale);
c092ed38 443 GTKEnableEvents();
6de97a3b 444}
c801d85f 445
debe6624 446void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 447{
c092ed38 448 GTKDisableEvents();
908eeca8
RR
449 if (minValue == maxValue)
450 maxValue++;
c092ed38
RR
451 gtk_range_set_range(GTK_RANGE (m_scale), minValue, maxValue);
452 gtk_range_set_increments(GTK_RANGE (m_scale), 1, (maxValue - minValue + 9) / 10);
453 GTKEnableEvents();
eec4500d 454
c092ed38
RR
455 if (HasFlag(wxSL_MIN_MAX_LABELS))
456 {
457 wxString str;
eec4500d 458
c092ed38
RR
459 str.Printf( "%d", minValue );
460 if (HasFlag(wxSL_INVERSE))
461 gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
462 else
463 gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
eec4500d 464
c092ed38
RR
465 str.Printf( "%d", maxValue );
466 if (HasFlag(wxSL_INVERSE))
467 gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
468 else
469 gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
eec4500d 470
c092ed38 471 }
6de97a3b 472}
c801d85f 473
1e1fafb9 474int wxSlider::GetMin() const
c801d85f 475{
385e8575
PC
476 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
477 return int(gtk_adjustment_get_lower(adj));
6de97a3b 478}
c801d85f 479
1e1fafb9 480int wxSlider::GetMax() const
c801d85f 481{
385e8575
PC
482 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
483 return int(gtk_adjustment_get_upper(adj));
6de97a3b 484}
c801d85f 485
debe6624 486void wxSlider::SetPageSize( int pageSize )
c801d85f 487{
c092ed38
RR
488 GTKDisableEvents();
489 gtk_range_set_increments(GTK_RANGE (m_scale), GetLineSize(), pageSize);
490 GTKEnableEvents();
6de97a3b 491}
c801d85f 492
1e1fafb9 493int wxSlider::GetPageSize() const
c801d85f 494{
385e8575
PC
495 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
496 return int(gtk_adjustment_get_page_increment(adj));
6de97a3b 497}
c801d85f 498
12a480c1
PC
499// GTK does not support changing the size of the slider
500void wxSlider::SetThumbLength(int)
c801d85f 501{
6de97a3b 502}
c801d85f 503
1e1fafb9 504int wxSlider::GetThumbLength() const
c801d85f 505{
12a480c1 506 return 0;
6de97a3b 507}
c801d85f 508
98a0564e 509void wxSlider::SetLineSize( int lineSize )
c801d85f 510{
c092ed38
RR
511 GTKDisableEvents();
512 gtk_range_set_increments(GTK_RANGE (m_scale), lineSize, GetPageSize());
513 GTKEnableEvents();
6de97a3b 514}
c801d85f 515
1e1fafb9 516int wxSlider::GetLineSize() const
c801d85f 517{
385e8575
PC
518 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
519 return int(gtk_adjustment_get_step_increment(adj));
6de97a3b 520}
c801d85f 521
ef5c70f9 522GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
b4071e91 523{
9dc44eff
PC
524#ifdef __WXGTK3__
525 // no access to internal GdkWindows
526 return NULL;
527#else
c092ed38 528 return GTK_RANGE(m_scale)->event_window;
9dc44eff 529#endif
b4071e91
RR
530}
531
9d522606
RD
532// static
533wxVisualAttributes
534wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
535{
536 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
537}
538
de6185e2 539#endif // wxUSE_SLIDER