1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/slider.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/slider.h"
22 #include "wx/gtk/private.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
->GetEventHandler()->ProcessEvent( 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
->GetEventHandler()->ProcessEvent( event
);
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
);
65 win
->GetEventHandler()->ProcessEvent( 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(_T("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 if (g_isIdle
) wxapp_install_idle_handler();
129 if (!win
->m_hasVMT
) return;
130 if (g_blockEventsOnDrag
) return;
132 GtkAdjustment
* adj
= gtk_range_get_adjustment (range
);
133 const int pos
= wxRound(adj
->value
);
134 const double oldPos
= win
->m_pos
;
135 win
->m_pos
= adj
->value
;
136 if (win
->m_blockScrollEvent
)
138 win
->m_scrollEventType
= GTK_SCROLL_NONE
;
142 wxEventType eventType
= wxEVT_NULL
;
143 if (win
->m_isScrolling
)
145 eventType
= wxEVT_SCROLL_THUMBTRACK
;
147 else if (win
->m_scrollEventType
!= GTK_SCROLL_NONE
)
149 // Scroll event from "move-slider" (keyboard)
150 eventType
= GtkScrollTypeToWx(win
->m_scrollEventType
);
152 else if (win
->m_mouseButtonDown
)
154 // Difference from last change event
155 const double diff
= adj
->value
- oldPos
;
156 const bool isDown
= diff
> 0;
158 if (IsScrollIncrement(adj
->page_increment
, diff
))
160 eventType
= isDown
? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
162 else if (wxIsSameDouble(adj
->value
, 0))
164 eventType
= wxEVT_SCROLL_PAGEUP
;
166 else if (wxIsSameDouble(adj
->value
, adj
->upper
))
168 eventType
= wxEVT_SCROLL_PAGEDOWN
;
172 // Assume track event
173 eventType
= wxEVT_SCROLL_THUMBTRACK
;
174 // Remember that we're tracking
175 win
->m_isScrolling
= true;
179 win
->m_scrollEventType
= GTK_SCROLL_NONE
;
181 // If integral position has changed
182 if (wxRound(oldPos
) != pos
)
184 wxCHECK_RET(eventType
!= wxEVT_NULL
, _T("Unknown slider scroll event type"));
185 ProcessScrollEvent(win
, eventType
);
186 win
->m_needThumbRelease
= eventType
== wxEVT_SCROLL_THUMBTRACK
;
191 //-----------------------------------------------------------------------------
192 // "move_slider" (keyboard event)
193 //-----------------------------------------------------------------------------
197 gtk_move_slider(GtkRange
*, GtkScrollType scrollType
, wxSlider
* win
)
199 // Save keyboard scroll type for "value_changed" handler
200 win
->m_scrollEventType
= scrollType
;
204 //-----------------------------------------------------------------------------
205 // "button_press_event"
206 //-----------------------------------------------------------------------------
210 gtk_button_press_event(GtkWidget
*, GdkEventButton
*, wxSlider
* win
)
212 win
->m_mouseButtonDown
= true;
218 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
224 gtk_event_after(GtkRange
* range
, GdkEvent
* event
, wxSlider
* win
)
226 if (event
->type
== GDK_BUTTON_RELEASE
)
228 g_signal_handlers_block_by_func(range
, (gpointer
) gtk_event_after
, win
);
230 if (win
->m_needThumbRelease
)
232 win
->m_needThumbRelease
= false;
233 ProcessScrollEvent(win
, wxEVT_SCROLL_THUMBRELEASE
);
235 // Keep slider at an integral position
236 win
->BlockScrollEvent();
237 gtk_range_set_value(GTK_RANGE (win
->m_widget
), win
->GetValue());
238 win
->UnblockScrollEvent();
243 //-----------------------------------------------------------------------------
244 // "button_release_event"
245 //-----------------------------------------------------------------------------
249 gtk_button_release_event(GtkRange
* range
, GdkEventButton
*, wxSlider
* win
)
251 win
->m_mouseButtonDown
= false;
252 if (win
->m_isScrolling
)
254 win
->m_isScrolling
= false;
255 g_signal_handlers_unblock_by_func(range
, (gpointer
) gtk_event_after
, win
);
261 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
266 static gchar
* gtk_format_value(GtkScale
*, double value
, void*)
268 // Format value as nearest integer
269 return g_strdup_printf("%d", wxRound(value
));
273 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 IMPLEMENT_DYNAMIC_CLASS(wxSlider
,wxControl
)
282 m_scrollEventType
= 0;
283 m_needThumbRelease
= false;
286 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
287 int value
, int minValue
, int maxValue
,
288 const wxPoint
& pos
, const wxSize
& size
,
289 long style
, const wxValidator
& validator
, const wxString
& name
)
291 m_acceptsFocus
= true;
294 if (!PreCreation( parent
, pos
, size
) ||
295 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
297 wxFAIL_MSG( wxT("wxSlider creation failed") );
302 m_scrollEventType
= 0;
303 m_needThumbRelease
= false;
305 if (style
& wxSL_VERTICAL
)
306 m_widget
= gtk_vscale_new( (GtkAdjustment
*) NULL
);
308 m_widget
= gtk_hscale_new( (GtkAdjustment
*) NULL
);
310 gtk_scale_set_draw_value(GTK_SCALE (m_widget
), (style
& wxSL_LABELS
) != 0);
311 // Keep full precision in position value
312 gtk_scale_set_digits(GTK_SCALE (m_widget
), -1);
314 if (style
& wxSL_INVERSE
)
315 gtk_range_set_inverted( GTK_RANGE(m_widget
), TRUE
);
317 g_signal_connect(m_widget
, "button_press_event", G_CALLBACK(gtk_button_press_event
), this);
318 g_signal_connect(m_widget
, "button_release_event", G_CALLBACK(gtk_button_release_event
), this);
319 g_signal_connect(m_widget
, "move_slider", G_CALLBACK(gtk_move_slider
), this);
320 g_signal_connect(m_widget
, "format_value", G_CALLBACK(gtk_format_value
), NULL
);
321 g_signal_connect(m_widget
, "value_changed", G_CALLBACK(gtk_value_changed
), this);
323 handler_id
= g_signal_connect(
324 m_widget
, "event_after", G_CALLBACK(gtk_event_after
), this);
325 g_signal_handler_block(m_widget
, handler_id
);
327 SetRange( minValue
, maxValue
);
330 m_parent
->DoAddChild( this );
337 int wxSlider::GetValue() const
339 return wxRound(m_pos
);
342 void wxSlider::SetValue( int value
)
344 if (GetValue() != value
)
347 gtk_range_set_value(GTK_RANGE (m_widget
), value
);
348 UnblockScrollEvent();
352 void wxSlider::SetRange( int minValue
, int maxValue
)
355 gtk_range_set_range(GTK_RANGE (m_widget
), minValue
, maxValue
);
356 gtk_range_set_increments(GTK_RANGE (m_widget
), 1, (maxValue
- minValue
+ 9) / 10);
357 UnblockScrollEvent();
360 int wxSlider::GetMin() const
362 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget
))->lower
);
365 int wxSlider::GetMax() const
367 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget
))->upper
);
370 void wxSlider::SetPageSize( int pageSize
)
373 gtk_range_set_increments(GTK_RANGE (m_widget
), GetLineSize(), pageSize
);
374 UnblockScrollEvent();
377 int wxSlider::GetPageSize() const
379 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget
))->page_increment
);
382 // GTK does not support changing the size of the slider
383 void wxSlider::SetThumbLength(int)
387 int wxSlider::GetThumbLength() const
392 void wxSlider::SetLineSize( int lineSize
)
395 gtk_range_set_increments(GTK_RANGE (m_widget
), lineSize
, GetPageSize());
396 UnblockScrollEvent();
399 int wxSlider::GetLineSize() const
401 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget
))->step_increment
);
404 bool wxSlider::IsOwnGtkWindow( GdkWindow
*window
)
406 GtkRange
*range
= GTK_RANGE(m_widget
);
407 return (range
->event_window
== window
);
412 wxSlider::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
414 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new
);
417 #endif // wxUSE_SLIDER