]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
Add another missing #if wxUSE_MARKUP check.
[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>
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
VZ
61 // and also generate a command event for compatibility
62 wxCommandEvent event( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
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{
20c3174d 127 GtkAdjustment* adj = gtk_range_get_adjustment (range);
23f826bd 128 const int pos = wxRound(adj->value);
12a480c1
PC
129 const double oldPos = win->m_pos;
130 win->m_pos = adj->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
154 const double diff = adj->value - oldPos;
155 const bool isDown = diff > 0;
2d17d68f 156
12a480c1
PC
157 if (IsScrollIncrement(adj->page_increment, diff))
158 {
159 eventType = isDown ? wxEVT_SCROLL_PAGEDOWN : wxEVT_SCROLL_PAGEUP;
160 }
161 else if (wxIsSameDouble(adj->value, 0))
162 {
163 eventType = wxEVT_SCROLL_PAGEUP;
164 }
165 else if (wxIsSameDouble(adj->value, adj->upper))
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
23f826bd 181 if (wxRound(oldPos) != pos)
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{
277 m_pos = 0;
3824e0db 278 m_scrollEventType = GTK_SCROLL_NONE;
12a480c1 279 m_needThumbRelease = false;
1d591651 280 m_blockScrollEvent = false;
12a480c1
PC
281}
282
e8375af8
VZ
283bool wxSlider::Create(wxWindow *parent,
284 wxWindowID id,
285 int value,
286 int minValue,
287 int maxValue,
288 const wxPoint& pos,
289 const wxSize& size,
290 long style,
291 const wxValidator& validator,
292 const wxString& name)
c801d85f 293{
3824e0db
VZ
294 m_pos = value;
295 m_scrollEventType = GTK_SCROLL_NONE;
296
4dcaf11a
RR
297 if (!PreCreation( parent, pos, size ) ||
298 !CreateBase( parent, id, pos, size, style, validator, name ))
299 {
223d09f6 300 wxFAIL_MSG( wxT("wxSlider creation failed") );
de6185e2 301 return false;
4dcaf11a 302 }
eec4500d 303
6de97a3b 304
2e563988 305 if (style & wxSL_VERTICAL)
c092ed38 306 m_scale = gtk_vscale_new( NULL );
19da4326 307 else
c092ed38
RR
308 m_scale = gtk_hscale_new( NULL );
309 g_object_ref(m_scale);
80810ca3 310
c092ed38
RR
311 if (style & wxSL_MIN_MAX_LABELS)
312 {
313 gtk_widget_show( m_scale );
eec4500d 314
c092ed38
RR
315 if (style & wxSL_VERTICAL)
316 m_widget = gtk_hbox_new(false, 0);
317 else
318 m_widget = gtk_vbox_new(false, 0);
319 g_object_ref(m_widget);
320 gtk_widget_show( m_widget );
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);
441 GTKEnableEvents();
6de97a3b 442}
c801d85f 443
debe6624 444void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 445{
c092ed38 446 GTKDisableEvents();
908eeca8
RR
447 if (minValue == maxValue)
448 maxValue++;
c092ed38
RR
449 gtk_range_set_range(GTK_RANGE (m_scale), minValue, maxValue);
450 gtk_range_set_increments(GTK_RANGE (m_scale), 1, (maxValue - minValue + 9) / 10);
451 GTKEnableEvents();
eec4500d 452
c092ed38
RR
453 if (HasFlag(wxSL_MIN_MAX_LABELS))
454 {
455 wxString str;
eec4500d 456
c092ed38
RR
457 str.Printf( "%d", minValue );
458 if (HasFlag(wxSL_INVERSE))
459 gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
460 else
461 gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
eec4500d 462
c092ed38
RR
463 str.Printf( "%d", maxValue );
464 if (HasFlag(wxSL_INVERSE))
465 gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
466 else
467 gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
eec4500d 468
c092ed38 469 }
6de97a3b 470}
c801d85f 471
1e1fafb9 472int wxSlider::GetMin() const
c801d85f 473{
c092ed38 474 return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->lower);
6de97a3b 475}
c801d85f 476
1e1fafb9 477int wxSlider::GetMax() const
c801d85f 478{
c092ed38 479 return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->upper);
6de97a3b 480}
c801d85f 481
debe6624 482void wxSlider::SetPageSize( int pageSize )
c801d85f 483{
c092ed38
RR
484 GTKDisableEvents();
485 gtk_range_set_increments(GTK_RANGE (m_scale), GetLineSize(), pageSize);
486 GTKEnableEvents();
6de97a3b 487}
c801d85f 488
1e1fafb9 489int wxSlider::GetPageSize() const
c801d85f 490{
c092ed38 491 return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->page_increment);
6de97a3b 492}
c801d85f 493
12a480c1
PC
494// GTK does not support changing the size of the slider
495void wxSlider::SetThumbLength(int)
c801d85f 496{
6de97a3b 497}
c801d85f 498
1e1fafb9 499int wxSlider::GetThumbLength() const
c801d85f 500{
12a480c1 501 return 0;
6de97a3b 502}
c801d85f 503
98a0564e 504void wxSlider::SetLineSize( int lineSize )
c801d85f 505{
c092ed38
RR
506 GTKDisableEvents();
507 gtk_range_set_increments(GTK_RANGE (m_scale), lineSize, GetPageSize());
508 GTKEnableEvents();
6de97a3b 509}
c801d85f 510
1e1fafb9 511int wxSlider::GetLineSize() const
c801d85f 512{
c092ed38 513 return int(gtk_range_get_adjustment (GTK_RANGE (m_scale))->step_increment);
6de97a3b 514}
c801d85f 515
ef5c70f9 516GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
b4071e91 517{
c092ed38 518 return GTK_RANGE(m_scale)->event_window;
b4071e91
RR
519}
520
9d522606
RD
521// static
522wxVisualAttributes
523wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
524{
525 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
526}
527
de6185e2 528#endif // wxUSE_SLIDER