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