]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/slider.cpp
Fix for specific filename syntax of OpenVMS
[wxWidgets.git] / src / gtk / slider.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/slider.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SLIDER
14
15 #include "wx/slider.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #include "wx/math.h"
20 #endif
21
22 #include <gtk/gtk.h>
23 #include "wx/gtk/private/gtk2-compat.h"
24
25 //-----------------------------------------------------------------------------
26 // data
27 //-----------------------------------------------------------------------------
28
29 extern bool g_blockEventsOnDrag;
30
31 // ----------------------------------------------------------------------------
32 // helper functions
33 // ----------------------------------------------------------------------------
34
35 // process a scroll event
36 static void
37 ProcessScrollEvent(wxSlider *win, wxEventType evtType)
38 {
39 const int orient = win->HasFlag(wxSL_VERTICAL) ? wxVERTICAL
40 : wxHORIZONTAL;
41
42 const int value = win->GetValue();
43
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 );
50 win->HandleWindowEvent( event );
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
55 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
56 {
57 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
58 event.SetEventObject( win );
59 win->HandleWindowEvent( event );
60 }
61
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 );
66 win->HandleWindowEvent( event );
67 }
68
69 static 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:
104 wxFAIL_MSG(wxT("Unknown GtkScrollType"));
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
113 static 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
120 //-----------------------------------------------------------------------------
121 // "value_changed"
122 //-----------------------------------------------------------------------------
123
124 extern "C" {
125 static void
126 gtk_value_changed(GtkRange* range, wxSlider* win)
127 {
128 const double value = gtk_range_get_value(range);
129 const double oldPos = win->m_pos;
130 win->m_pos = value;
131
132 if (g_blockEventsOnDrag)
133 return;
134
135 if (win->GTKEventsDisabled())
136 {
137 win->m_scrollEventType = GTK_SCROLL_NONE;
138 return;
139 }
140
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 = value - oldPos;
155 const bool isDown = diff > 0;
156
157 GtkAdjustment* adj = gtk_range_get_adjustment(range);
158 if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj), diff))
159 {
160 eventType = isDown ? wxEVT_SCROLL_PAGEDOWN : wxEVT_SCROLL_PAGEUP;
161 }
162 else if (wxIsSameDouble(value, 0))
163 {
164 eventType = wxEVT_SCROLL_PAGEUP;
165 }
166 else if (wxIsSameDouble(value, gtk_adjustment_get_upper(adj)))
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
182 if (wxRound(oldPos) != wxRound(value))
183 {
184 ProcessScrollEvent(win, eventType);
185 win->m_needThumbRelease = eventType == wxEVT_SCROLL_THUMBTRACK;
186 }
187 }
188 }
189
190 //-----------------------------------------------------------------------------
191 // "move_slider" (keyboard event)
192 //-----------------------------------------------------------------------------
193
194 extern "C" {
195 static void
196 gtk_move_slider(GtkRange*, GtkScrollType scrollType, wxSlider* win)
197 {
198 // Save keyboard scroll type for "value_changed" handler
199 win->m_scrollEventType = scrollType;
200 }
201 }
202
203 //-----------------------------------------------------------------------------
204 // "button_press_event"
205 //-----------------------------------------------------------------------------
206
207 extern "C" {
208 static gboolean
209 gtk_button_press_event(GtkWidget*, GdkEventButton*, wxSlider* win)
210 {
211 win->m_mouseButtonDown = true;
212
213 return false;
214 }
215 }
216
217 //-----------------------------------------------------------------------------
218 // "event_after"
219 //-----------------------------------------------------------------------------
220
221 extern "C" {
222 static void
223 gtk_event_after(GtkRange* range, GdkEvent* event, wxSlider* win)
224 {
225 if (event->type == GDK_BUTTON_RELEASE)
226 {
227 g_signal_handlers_block_by_func(range, (gpointer) gtk_event_after, win);
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
235 win->GTKDisableEvents();
236 gtk_range_set_value(GTK_RANGE (win->m_scale), win->GetValue());
237 win->GTKEnableEvents();
238 }
239 }
240 }
241
242 //-----------------------------------------------------------------------------
243 // "button_release_event"
244 //-----------------------------------------------------------------------------
245
246 extern "C" {
247 static gboolean
248 gtk_button_release_event(GtkRange* range, GdkEventButton*, wxSlider* win)
249 {
250 win->m_mouseButtonDown = false;
251 if (win->m_isScrolling)
252 {
253 win->m_isScrolling = false;
254 g_signal_handlers_unblock_by_func(range, (gpointer) gtk_event_after, win);
255 }
256 return false;
257 }
258 }
259
260 //-----------------------------------------------------------------------------
261 // "format_value"
262 //-----------------------------------------------------------------------------
263
264 extern "C" {
265 static gchar* gtk_format_value(GtkScale*, double value, void*)
266 {
267 // Format value as nearest integer
268 return g_strdup_printf("%d", wxRound(value));
269 }
270 }
271
272 //-----------------------------------------------------------------------------
273 // wxSlider
274 //-----------------------------------------------------------------------------
275
276 wxSlider::wxSlider()
277 {
278 m_scale = NULL;
279 }
280
281 wxSlider::~wxSlider()
282 {
283 if (m_scale && m_scale != m_widget)
284 GTKDisconnect(m_scale);
285 }
286
287 bool wxSlider::Create(wxWindow *parent,
288 wxWindowID id,
289 int value,
290 int minValue,
291 int maxValue,
292 const wxPoint& pos,
293 const wxSize& size,
294 long style,
295 const wxValidator& validator,
296 const wxString& name)
297 {
298 m_pos = value;
299 m_scrollEventType = GTK_SCROLL_NONE;
300 m_needThumbRelease = false;
301 m_blockScrollEvent = false;
302
303 if (!PreCreation( parent, pos, size ) ||
304 !CreateBase( parent, id, pos, size, style, validator, name ))
305 {
306 wxFAIL_MSG( wxT("wxSlider creation failed") );
307 return false;
308 }
309
310
311 if (style & wxSL_VERTICAL)
312 m_scale = gtk_vscale_new( NULL );
313 else
314 m_scale = gtk_hscale_new( NULL );
315 g_object_ref(m_scale);
316
317 if (style & wxSL_MIN_MAX_LABELS)
318 {
319 gtk_widget_show( m_scale );
320
321 if (style & wxSL_VERTICAL)
322 m_widget = gtk_hbox_new(false, 0);
323 else
324 m_widget = gtk_vbox_new(false, 0);
325 g_object_ref(m_widget);
326 gtk_container_add( GTK_CONTAINER(m_widget), m_scale );
327
328 GtkWidget *box;
329 if (style & wxSL_VERTICAL)
330 box = gtk_vbox_new(false,0);
331 else
332 box = gtk_hbox_new(false,0);
333 g_object_ref(box);
334 gtk_widget_show(box);
335 gtk_container_add( GTK_CONTAINER(m_widget), box );
336
337 m_minLabel = gtk_label_new(NULL);
338 g_object_ref(m_minLabel);
339 gtk_widget_show( m_minLabel );
340 gtk_container_add( GTK_CONTAINER(box), m_minLabel );
341 gtk_box_set_child_packing( GTK_BOX(box), m_minLabel, FALSE, FALSE, 0, GTK_PACK_START );
342
343 // expanding empty space between the min/max labels
344 GtkWidget *space = gtk_label_new(NULL);
345 g_object_ref(space);
346 gtk_widget_show( space );
347 gtk_container_add( GTK_CONTAINER(box), space );
348 gtk_box_set_child_packing( GTK_BOX(box), space, TRUE, FALSE, 0, GTK_PACK_START );
349
350 m_maxLabel = gtk_label_new(NULL);
351 g_object_ref(m_maxLabel);
352 gtk_widget_show( m_maxLabel );
353 gtk_container_add( GTK_CONTAINER(box), m_maxLabel );
354 gtk_box_set_child_packing( GTK_BOX(box), m_maxLabel, FALSE, FALSE, 0, GTK_PACK_END );
355 }
356 else
357 {
358 m_widget = m_scale;
359 m_maxLabel = NULL;
360 m_minLabel = NULL;
361 }
362
363 const bool showValueLabel = (style & wxSL_VALUE_LABEL) != 0;
364 gtk_scale_set_draw_value(GTK_SCALE (m_scale), showValueLabel );
365 if ( showValueLabel )
366 {
367 // position the label appropriately: notice that wxSL_DIRECTION flags
368 // specify the position of the ticks, not label, under MSW and so the
369 // label is on the opposite side
370 GtkPositionType posLabel;
371 if ( style & wxSL_VERTICAL )
372 {
373 if ( style & wxSL_LEFT )
374 posLabel = GTK_POS_RIGHT;
375 else // if ( style & wxSL_RIGHT ) -- this is also the default
376 posLabel = GTK_POS_LEFT;
377 }
378 else // horizontal slider
379 {
380 if ( style & wxSL_TOP )
381 posLabel = GTK_POS_BOTTOM;
382 else // if ( style & wxSL_BOTTOM) -- this is again the default
383 posLabel = GTK_POS_TOP;
384 }
385
386 gtk_scale_set_value_pos( GTK_SCALE(m_scale), posLabel );
387 }
388
389 // Keep full precision in position value
390 gtk_scale_set_digits(GTK_SCALE (m_scale), -1);
391
392 if (style & wxSL_INVERSE)
393 gtk_range_set_inverted( GTK_RANGE(m_scale), TRUE );
394
395 g_signal_connect(m_scale, "button_press_event", G_CALLBACK(gtk_button_press_event), this);
396 g_signal_connect(m_scale, "button_release_event", G_CALLBACK(gtk_button_release_event), this);
397 g_signal_connect(m_scale, "move_slider", G_CALLBACK(gtk_move_slider), this);
398 g_signal_connect(m_scale, "format_value", G_CALLBACK(gtk_format_value), NULL);
399 g_signal_connect(m_scale, "value_changed", G_CALLBACK(gtk_value_changed), this);
400 gulong handler_id = g_signal_connect(m_scale, "event_after", G_CALLBACK(gtk_event_after), this);
401 g_signal_handler_block(m_scale, handler_id);
402
403 SetRange( minValue, maxValue );
404
405 // don't call the public SetValue() as it won't do anything unless the
406 // value really changed
407 GTKSetValue( value );
408
409 m_parent->DoAddChild( this );
410
411 PostCreation(size);
412
413 return true;
414 }
415
416 void wxSlider::GTKDisableEvents()
417 {
418 m_blockScrollEvent = true;
419 }
420
421 void wxSlider::GTKEnableEvents()
422 {
423 m_blockScrollEvent = false;
424 }
425
426 bool wxSlider::GTKEventsDisabled() const
427 {
428 return m_blockScrollEvent;
429 }
430
431 int wxSlider::GetValue() const
432 {
433 return wxRound(m_pos);
434 }
435
436 void wxSlider::SetValue( int value )
437 {
438 if (GetValue() != value)
439 GTKSetValue(value);
440 }
441
442 void wxSlider::GTKSetValue(int value)
443 {
444 GTKDisableEvents();
445 gtk_range_set_value(GTK_RANGE (m_scale), value);
446 // GTK only updates value label if handle moves at least 1 pixel
447 gtk_widget_queue_draw(m_scale);
448 GTKEnableEvents();
449 }
450
451 void wxSlider::SetRange( int minValue, int maxValue )
452 {
453 GTKDisableEvents();
454 if (minValue == maxValue)
455 maxValue++;
456 gtk_range_set_range(GTK_RANGE (m_scale), minValue, maxValue);
457 gtk_range_set_increments(GTK_RANGE (m_scale), 1, (maxValue - minValue + 9) / 10);
458 GTKEnableEvents();
459
460 if (HasFlag(wxSL_MIN_MAX_LABELS))
461 {
462 wxString str;
463
464 str.Printf( "%d", minValue );
465 if (HasFlag(wxSL_INVERSE))
466 gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
467 else
468 gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
469
470 str.Printf( "%d", maxValue );
471 if (HasFlag(wxSL_INVERSE))
472 gtk_label_set_text( GTK_LABEL(m_minLabel), str.utf8_str() );
473 else
474 gtk_label_set_text( GTK_LABEL(m_maxLabel), str.utf8_str() );
475
476 }
477 }
478
479 int wxSlider::GetMin() const
480 {
481 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
482 return int(gtk_adjustment_get_lower(adj));
483 }
484
485 int wxSlider::GetMax() const
486 {
487 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
488 return int(gtk_adjustment_get_upper(adj));
489 }
490
491 void wxSlider::SetPageSize( int pageSize )
492 {
493 GTKDisableEvents();
494 gtk_range_set_increments(GTK_RANGE (m_scale), GetLineSize(), pageSize);
495 GTKEnableEvents();
496 }
497
498 int wxSlider::GetPageSize() const
499 {
500 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
501 return int(gtk_adjustment_get_page_increment(adj));
502 }
503
504 // GTK does not support changing the size of the slider
505 void wxSlider::SetThumbLength(int)
506 {
507 }
508
509 int wxSlider::GetThumbLength() const
510 {
511 return 0;
512 }
513
514 void wxSlider::SetLineSize( int lineSize )
515 {
516 GTKDisableEvents();
517 gtk_range_set_increments(GTK_RANGE (m_scale), lineSize, GetPageSize());
518 GTKEnableEvents();
519 }
520
521 int wxSlider::GetLineSize() const
522 {
523 GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_scale));
524 return int(gtk_adjustment_get_step_increment(adj));
525 }
526
527 GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
528 {
529 #ifdef __WXGTK3__
530 // no access to internal GdkWindows
531 return NULL;
532 #else
533 return GTK_RANGE(m_scale)->event_window;
534 #endif
535 }
536
537 // static
538 wxVisualAttributes
539 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
540 {
541 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
542 }
543
544 #endif // wxUSE_SLIDER