1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/slider.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/slider.h"
22 #include "wx/gtk/private/gtk2-compat.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern bool g_blockEventsOnDrag
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // process a scroll event
36 ProcessScrollEvent(wxSlider
*win
, wxEventType evtType
)
38 const int orient
= win
->HasFlag(wxSL_VERTICAL
) ? wxVERTICAL
41 const int value
= win
->GetValue();
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
)
47 wxScrollEvent
event( evtType
, win
->GetId(), value
, orient
);
48 event
.SetEventObject( win
);
49 win
->HandleWindowEvent( event
);
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
54 if ( evtType
!= wxEVT_SCROLL_THUMBTRACK
)
56 wxScrollEvent
event(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
57 event
.SetEventObject( win
);
58 win
->HandleWindowEvent( event
);
61 // and also generate a command event for compatibility
62 wxCommandEvent
event( wxEVT_SLIDER
, win
->GetId() );
63 event
.SetEventObject( win
);
64 event
.SetInt( value
);
65 win
->HandleWindowEvent( event
);
68 static inline wxEventType
GtkScrollTypeToWx(int scrollType
)
70 wxEventType eventType
;
73 case GTK_SCROLL_STEP_BACKWARD
:
74 case GTK_SCROLL_STEP_LEFT
:
75 case GTK_SCROLL_STEP_UP
:
76 eventType
= wxEVT_SCROLL_LINEUP
;
78 case GTK_SCROLL_STEP_DOWN
:
79 case GTK_SCROLL_STEP_FORWARD
:
80 case GTK_SCROLL_STEP_RIGHT
:
81 eventType
= wxEVT_SCROLL_LINEDOWN
;
83 case GTK_SCROLL_PAGE_BACKWARD
:
84 case GTK_SCROLL_PAGE_LEFT
:
85 case GTK_SCROLL_PAGE_UP
:
86 eventType
= wxEVT_SCROLL_PAGEUP
;
88 case GTK_SCROLL_PAGE_DOWN
:
89 case GTK_SCROLL_PAGE_FORWARD
:
90 case GTK_SCROLL_PAGE_RIGHT
:
91 eventType
= wxEVT_SCROLL_PAGEDOWN
;
93 case GTK_SCROLL_START
:
94 eventType
= wxEVT_SCROLL_TOP
;
97 eventType
= wxEVT_SCROLL_BOTTOM
;
100 eventType
= wxEVT_SCROLL_THUMBTRACK
;
103 wxFAIL_MSG(wxT("Unknown GtkScrollType"));
104 eventType
= wxEVT_NULL
;
110 // Determine if increment is the same as +/-x, allowing for some small
111 // difference due to possible inexactness in floating point arithmetic
112 static inline bool IsScrollIncrement(double increment
, double x
)
114 wxASSERT(increment
> 0);
115 const double tolerance
= 1.0 / 1024;
116 return fabs(increment
- fabs(x
)) < tolerance
;
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
125 gtk_value_changed(GtkRange
* range
, wxSlider
* win
)
127 const double value
= gtk_range_get_value(range
);
128 const double oldPos
= win
->m_pos
;
131 if (g_blockEventsOnDrag
)
134 if (win
->GTKEventsDisabled())
136 win
->m_scrollEventType
= GTK_SCROLL_NONE
;
140 wxEventType eventType
= wxEVT_NULL
;
141 if (win
->m_isScrolling
)
143 eventType
= wxEVT_SCROLL_THUMBTRACK
;
145 else if (win
->m_scrollEventType
!= GTK_SCROLL_NONE
)
147 // Scroll event from "move-slider" (keyboard)
148 eventType
= GtkScrollTypeToWx(win
->m_scrollEventType
);
150 else if (win
->m_mouseButtonDown
)
152 // Difference from last change event
153 const double diff
= value
- oldPos
;
154 const bool isDown
= diff
> 0;
156 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
157 if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj
), diff
))
159 eventType
= isDown
? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
161 else if (wxIsSameDouble(value
, 0))
163 eventType
= wxEVT_SCROLL_PAGEUP
;
165 else if (wxIsSameDouble(value
, gtk_adjustment_get_upper(adj
)))
167 eventType
= wxEVT_SCROLL_PAGEDOWN
;
171 // Assume track event
172 eventType
= wxEVT_SCROLL_THUMBTRACK
;
173 // Remember that we're tracking
174 win
->m_isScrolling
= true;
178 win
->m_scrollEventType
= GTK_SCROLL_NONE
;
180 // If integral position has changed
181 if (wxRound(oldPos
) != wxRound(value
))
183 ProcessScrollEvent(win
, eventType
);
184 win
->m_needThumbRelease
= eventType
== wxEVT_SCROLL_THUMBTRACK
;
189 //-----------------------------------------------------------------------------
190 // "move_slider" (keyboard event)
191 //-----------------------------------------------------------------------------
195 gtk_move_slider(GtkRange
*, GtkScrollType scrollType
, wxSlider
* win
)
197 // Save keyboard scroll type for "value_changed" handler
198 win
->m_scrollEventType
= scrollType
;
202 //-----------------------------------------------------------------------------
203 // "button_press_event"
204 //-----------------------------------------------------------------------------
208 gtk_button_press_event(GtkWidget
*, GdkEventButton
*, wxSlider
* win
)
210 win
->m_mouseButtonDown
= true;
216 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
222 gtk_event_after(GtkRange
* range
, GdkEvent
* event
, wxSlider
* win
)
224 if (event
->type
== GDK_BUTTON_RELEASE
)
226 g_signal_handlers_block_by_func(range
, (gpointer
) gtk_event_after
, win
);
228 if (win
->m_needThumbRelease
)
230 win
->m_needThumbRelease
= false;
231 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
);
233 // Keep slider at an integral position
234 win
->GTKDisableEvents();
235 gtk_range_set_value(GTK_RANGE (win
->m_scale
), win
->GetValue());
236 win
->GTKEnableEvents();
241 //-----------------------------------------------------------------------------
242 // "button_release_event"
243 //-----------------------------------------------------------------------------
247 gtk_button_release_event(GtkRange
* range
, GdkEventButton
*, wxSlider
* win
)
249 win
->m_mouseButtonDown
= false;
250 if (win
->m_isScrolling
)
252 win
->m_isScrolling
= false;
253 g_signal_handlers_unblock_by_func(range
, (gpointer
) gtk_event_after
, win
);
259 //-----------------------------------------------------------------------------
261 //-----------------------------------------------------------------------------
264 static gchar
* gtk_format_value(GtkScale
*, double value
, void*)
266 // Format value as nearest integer
267 return g_strdup_printf("%d", wxRound(value
));
271 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
280 wxSlider::~wxSlider()
282 if (m_scale
&& m_scale
!= m_widget
)
283 GTKDisconnect(m_scale
);
286 bool wxSlider::Create(wxWindow
*parent
,
294 const wxValidator
& validator
,
295 const wxString
& name
)
298 m_scrollEventType
= GTK_SCROLL_NONE
;
299 m_needThumbRelease
= false;
300 m_blockScrollEvent
= false;
302 if (!PreCreation( parent
, pos
, size
) ||
303 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
305 wxFAIL_MSG( wxT("wxSlider creation failed") );
309 const bool isVertical
= (style
& wxSL_VERTICAL
) != 0;
310 m_scale
= gtk_scale_new(GtkOrientation(isVertical
), NULL
);
312 if (style
& wxSL_MIN_MAX_LABELS
)
314 gtk_widget_show( m_scale
);
316 m_widget
= gtk_box_new(GtkOrientation(!isVertical
), 0);
317 gtk_box_pack_start(GTK_BOX(m_widget
), m_scale
, true, true, 0);
319 GtkWidget
* box
= gtk_box_new(GtkOrientation(isVertical
), 0);
320 gtk_widget_show(box
);
321 gtk_box_pack_start(GTK_BOX(m_widget
), box
, true, true, 0);
323 m_minLabel
= gtk_label_new(NULL
);
324 gtk_widget_show( m_minLabel
);
325 gtk_box_pack_start(GTK_BOX(box
), m_minLabel
, false, false, 0);
327 // expanding empty space between the min/max labels
328 GtkWidget
*space
= gtk_label_new(NULL
);
329 gtk_widget_show( space
);
330 gtk_box_pack_start(GTK_BOX(box
), space
, true, false, 0);
332 m_maxLabel
= gtk_label_new(NULL
);
333 gtk_widget_show( m_maxLabel
);
334 gtk_box_pack_end(GTK_BOX(box
), m_maxLabel
, false, false, 0);
342 g_object_ref(m_widget
);
344 const bool showValueLabel
= (style
& wxSL_VALUE_LABEL
) != 0;
345 gtk_scale_set_draw_value(GTK_SCALE (m_scale
), showValueLabel
);
346 if ( showValueLabel
)
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
)
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
;
359 else // horizontal slider
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
;
367 gtk_scale_set_value_pos( GTK_SCALE(m_scale
), posLabel
);
370 // Keep full precision in position value
371 gtk_scale_set_digits(GTK_SCALE (m_scale
), -1);
373 if (style
& wxSL_INVERSE
)
374 gtk_range_set_inverted( GTK_RANGE(m_scale
), TRUE
);
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
);
384 SetRange( minValue
, maxValue
);
386 // don't call the public SetValue() as it won't do anything unless the
387 // value really changed
388 GTKSetValue( value
);
390 m_parent
->DoAddChild( this );
397 void wxSlider::GTKDisableEvents()
399 m_blockScrollEvent
= true;
402 void wxSlider::GTKEnableEvents()
404 m_blockScrollEvent
= false;
407 bool wxSlider::GTKEventsDisabled() const
409 return m_blockScrollEvent
;
412 int wxSlider::GetValue() const
414 return wxRound(m_pos
);
417 void wxSlider::SetValue( int value
)
419 if (GetValue() != value
)
423 void wxSlider::GTKSetValue(int value
)
426 gtk_range_set_value(GTK_RANGE (m_scale
), value
);
427 // GTK only updates value label if handle moves at least 1 pixel
428 gtk_widget_queue_draw(m_scale
);
432 void wxSlider::SetRange( int minValue
, int maxValue
)
435 if (minValue
== maxValue
)
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);
441 if (HasFlag(wxSL_MIN_MAX_LABELS
))
445 str
.Printf( "%d", minValue
);
446 if (HasFlag(wxSL_INVERSE
))
447 gtk_label_set_text( GTK_LABEL(m_maxLabel
), str
.utf8_str() );
449 gtk_label_set_text( GTK_LABEL(m_minLabel
), str
.utf8_str() );
451 str
.Printf( "%d", maxValue
);
452 if (HasFlag(wxSL_INVERSE
))
453 gtk_label_set_text( GTK_LABEL(m_minLabel
), str
.utf8_str() );
455 gtk_label_set_text( GTK_LABEL(m_maxLabel
), str
.utf8_str() );
460 int wxSlider::GetMin() const
462 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_scale
));
463 return int(gtk_adjustment_get_lower(adj
));
466 int wxSlider::GetMax() const
468 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_scale
));
469 return int(gtk_adjustment_get_upper(adj
));
472 void wxSlider::SetPageSize( int pageSize
)
475 gtk_range_set_increments(GTK_RANGE (m_scale
), GetLineSize(), pageSize
);
479 int wxSlider::GetPageSize() const
481 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_scale
));
482 return int(gtk_adjustment_get_page_increment(adj
));
485 // GTK does not support changing the size of the slider
486 void wxSlider::SetThumbLength(int)
490 int wxSlider::GetThumbLength() const
495 void wxSlider::SetLineSize( int lineSize
)
498 gtk_range_set_increments(GTK_RANGE (m_scale
), lineSize
, GetPageSize());
502 int wxSlider::GetLineSize() const
504 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_scale
));
505 return int(gtk_adjustment_get_step_increment(adj
));
508 GdkWindow
*wxSlider::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
511 // no access to internal GdkWindows
514 return GTK_RANGE(m_scale
)->event_window
;
520 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
522 return GetDefaultAttributesFromGTKWidget(gtk_scale_new(GTK_ORIENTATION_VERTICAL
, NULL
));
525 #endif // wxUSE_SLIDER