]>
Commit | Line | Data |
---|---|---|
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 | |
9e691f46 | 22 | #include "wx/gtk/private.h" |
83624f79 | 23 | |
66bd6b93 RR |
24 | //----------------------------------------------------------------------------- |
25 | // data | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
1e6feb95 | 28 | extern bool g_blockEventsOnDrag; |
66bd6b93 | 29 | |
2b024653 VZ |
30 | // ---------------------------------------------------------------------------- |
31 | // helper functions | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
2b024653 VZ |
34 | // process a scroll event |
35 | static void | |
12a480c1 | 36 | ProcessScrollEvent(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 ); | |
49 | win->GetEventHandler()->ProcessEvent( event ); | |
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 ); | |
58 | win->GetEventHandler()->ProcessEvent( 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 ); | |
65 | win->GetEventHandler()->ProcessEvent( event ); | |
2b024653 | 66 | } |
57a1fd73 | 67 | |
12a480c1 PC |
68 | static 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: | |
103 | wxFAIL_MSG(_T("Unknown GtkScrollType")); | |
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 | |
112 | static 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 | 123 | extern "C" { |
12a480c1 PC |
124 | static void |
125 | gtk_value_changed(GtkRange* range, wxSlider* win) | |
80810ca3 | 126 | { |
acfd422a RR |
127 | if (g_isIdle) wxapp_install_idle_handler(); |
128 | ||
a2053b27 | 129 | if (!win->m_hasVMT) return; |
2d17d68f | 130 | if (g_blockEventsOnDrag) return; |
e1811a01 | 131 | |
20c3174d | 132 | GtkAdjustment* adj = gtk_range_get_adjustment (range); |
23f826bd | 133 | const int pos = wxRound(adj->value); |
12a480c1 PC |
134 | const double oldPos = win->m_pos; |
135 | win->m_pos = adj->value; | |
136 | if (win->m_blockScrollEvent) | |
137 | { | |
138 | win->m_scrollEventType = GTK_SCROLL_NONE; | |
2b024653 | 139 | return; |
12a480c1 | 140 | } |
80810ca3 | 141 | |
12a480c1 PC |
142 | wxEventType eventType = wxEVT_NULL; |
143 | if (win->m_isScrolling) | |
144 | { | |
145 | eventType = wxEVT_SCROLL_THUMBTRACK; | |
146 | } | |
147 | else if (win->m_scrollEventType != GTK_SCROLL_NONE) | |
148 | { | |
149 | // Scroll event from "move-slider" (keyboard) | |
150 | eventType = GtkScrollTypeToWx(win->m_scrollEventType); | |
151 | } | |
152 | else if (win->m_mouseButtonDown) | |
153 | { | |
154 | // Difference from last change event | |
155 | const double diff = adj->value - oldPos; | |
156 | const bool isDown = diff > 0; | |
2d17d68f | 157 | |
12a480c1 PC |
158 | if (IsScrollIncrement(adj->page_increment, diff)) |
159 | { | |
160 | eventType = isDown ? wxEVT_SCROLL_PAGEDOWN : wxEVT_SCROLL_PAGEUP; | |
161 | } | |
162 | else if (wxIsSameDouble(adj->value, 0)) | |
163 | { | |
164 | eventType = wxEVT_SCROLL_PAGEUP; | |
165 | } | |
166 | else if (wxIsSameDouble(adj->value, adj->upper)) | |
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 | |
23f826bd | 182 | if (wxRound(oldPos) != pos) |
12a480c1 PC |
183 | { |
184 | wxCHECK_RET(eventType != wxEVT_NULL, _T("Unknown slider scroll event type")); | |
185 | ProcessScrollEvent(win, eventType); | |
186 | win->m_needThumbRelease = eventType == wxEVT_SCROLL_THUMBTRACK; | |
187 | } | |
188 | } | |
189 | } | |
80810ca3 | 190 | |
12a480c1 PC |
191 | //----------------------------------------------------------------------------- |
192 | // "move_slider" (keyboard event) | |
193 | //----------------------------------------------------------------------------- | |
194 | ||
195 | extern "C" { | |
196 | static void | |
197 | gtk_move_slider(GtkRange*, GtkScrollType scrollType, wxSlider* win) | |
198 | { | |
199 | // Save keyboard scroll type for "value_changed" handler | |
200 | win->m_scrollEventType = scrollType; | |
201 | } | |
2b024653 | 202 | } |
80810ca3 | 203 | |
12a480c1 PC |
204 | //----------------------------------------------------------------------------- |
205 | // "button_press_event" | |
206 | //----------------------------------------------------------------------------- | |
207 | ||
208 | extern "C" { | |
209 | static gboolean | |
210 | gtk_button_press_event(GtkWidget*, GdkEventButton*, wxSlider* win) | |
2b024653 | 211 | { |
12a480c1 | 212 | win->m_mouseButtonDown = true; |
80810ca3 | 213 | |
12a480c1 PC |
214 | return false; |
215 | } | |
2b024653 | 216 | } |
80810ca3 | 217 | |
c918b2cd PC |
218 | //----------------------------------------------------------------------------- |
219 | // "event_after" | |
220 | //----------------------------------------------------------------------------- | |
221 | ||
12a480c1 | 222 | extern "C" { |
c918b2cd PC |
223 | static void |
224 | gtk_event_after(GtkRange* range, GdkEvent* event, wxSlider* win) | |
2b024653 | 225 | { |
c918b2cd | 226 | if (event->type == GDK_BUTTON_RELEASE) |
12a480c1 | 227 | { |
20c3174d | 228 | g_signal_handlers_block_by_func(range, (gpointer) gtk_event_after, win); |
c918b2cd PC |
229 | |
230 | if (win->m_needThumbRelease) | |
231 | { | |
232 | win->m_needThumbRelease = false; | |
233 | ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE); | |
234 | } | |
235 | // Keep slider at an integral position | |
236 | win->BlockScrollEvent(); | |
20c3174d | 237 | gtk_range_set_value(GTK_RANGE (win->m_widget), win->GetValue()); |
c918b2cd | 238 | win->UnblockScrollEvent(); |
12a480c1 | 239 | } |
12a480c1 PC |
240 | } |
241 | } | |
2b024653 | 242 | |
12a480c1 PC |
243 | //----------------------------------------------------------------------------- |
244 | // "button_release_event" | |
245 | //----------------------------------------------------------------------------- | |
2b024653 | 246 | |
12a480c1 PC |
247 | extern "C" { |
248 | static gboolean | |
c918b2cd | 249 | gtk_button_release_event(GtkRange* range, GdkEventButton*, wxSlider* win) |
12a480c1 PC |
250 | { |
251 | win->m_mouseButtonDown = false; | |
252 | if (win->m_isScrolling) | |
253 | { | |
c918b2cd | 254 | win->m_isScrolling = false; |
20c3174d | 255 | g_signal_handlers_unblock_by_func(range, (gpointer) gtk_event_after, win); |
12a480c1 PC |
256 | } |
257 | return false; | |
258 | } | |
6de97a3b | 259 | } |
2b024653 | 260 | |
12a480c1 PC |
261 | //----------------------------------------------------------------------------- |
262 | // "format_value" | |
263 | //----------------------------------------------------------------------------- | |
264 | ||
265 | extern "C" { | |
266 | static gchar* gtk_format_value(GtkScale*, double value, void*) | |
267 | { | |
268 | // Format value as nearest integer | |
23f826bd | 269 | return g_strdup_printf("%d", wxRound(value)); |
12a480c1 | 270 | } |
865bb325 | 271 | } |
c801d85f | 272 | |
97b3455a RR |
273 | //----------------------------------------------------------------------------- |
274 | // wxSlider | |
275 | //----------------------------------------------------------------------------- | |
276 | ||
c801d85f KB |
277 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
278 | ||
12a480c1 PC |
279 | wxSlider::wxSlider() |
280 | { | |
281 | m_pos = 0; | |
282 | m_scrollEventType = 0; | |
283 | m_needThumbRelease = false; | |
284 | } | |
285 | ||
debe6624 JS |
286 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
287 | int value, int minValue, int maxValue, | |
c801d85f | 288 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 289 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 290 | { |
de6185e2 WS |
291 | m_acceptsFocus = true; |
292 | m_needParent = true; | |
80810ca3 | 293 | |
4dcaf11a RR |
294 | if (!PreCreation( parent, pos, size ) || |
295 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
296 | { | |
223d09f6 | 297 | wxFAIL_MSG( wxT("wxSlider creation failed") ); |
de6185e2 | 298 | return false; |
4dcaf11a | 299 | } |
6de97a3b | 300 | |
12a480c1 PC |
301 | m_pos = 0; |
302 | m_scrollEventType = 0; | |
303 | m_needThumbRelease = false; | |
c801d85f | 304 | |
2e563988 | 305 | if (style & wxSL_VERTICAL) |
2d17d68f | 306 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
307 | else |
308 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
80810ca3 | 309 | |
20c3174d | 310 | gtk_scale_set_draw_value(GTK_SCALE (m_widget), (style & wxSL_LABELS) != 0); |
12a480c1 | 311 | // Keep full precision in position value |
20c3174d | 312 | gtk_scale_set_digits(GTK_SCALE (m_widget), -1); |
80810ca3 | 313 | |
1e219378 KH |
314 | if (style & wxSL_INVERSE) |
315 | gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE ); | |
316 | ||
12a480c1 PC |
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); | |
c918b2cd PC |
322 | gulong handler_id; |
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); | |
91b167dd | 326 | |
2d17d68f RR |
327 | SetRange( minValue, maxValue ); |
328 | SetValue( value ); | |
80810ca3 | 329 | |
f03fc89f | 330 | m_parent->DoAddChild( this ); |
80810ca3 | 331 | |
abdeb9e7 | 332 | PostCreation(size); |
80810ca3 | 333 | |
de6185e2 | 334 | return true; |
6de97a3b | 335 | } |
c801d85f | 336 | |
1e1fafb9 | 337 | int wxSlider::GetValue() const |
c801d85f | 338 | { |
23f826bd | 339 | return wxRound(m_pos); |
6de97a3b | 340 | } |
c801d85f | 341 | |
debe6624 | 342 | void wxSlider::SetValue( int value ) |
c801d85f | 343 | { |
12a480c1 PC |
344 | if (GetValue() != value) |
345 | { | |
346 | BlockScrollEvent(); | |
20c3174d | 347 | gtk_range_set_value(GTK_RANGE (m_widget), value); |
12a480c1 | 348 | UnblockScrollEvent(); |
f20a4018 RD |
349 | |
350 | // keep m_pos in sync in case the value_changed callback didn't didn't | |
351 | // get called, such as when the widget is first created | |
352 | if (GetValue() != value) | |
353 | { | |
354 | m_pos = gtk_range_get_value(GTK_RANGE(m_widget)); | |
355 | } | |
12a480c1 | 356 | } |
6de97a3b | 357 | } |
c801d85f | 358 | |
debe6624 | 359 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 360 | { |
12a480c1 | 361 | BlockScrollEvent(); |
20c3174d MR |
362 | gtk_range_set_range(GTK_RANGE (m_widget), minValue, maxValue); |
363 | gtk_range_set_increments(GTK_RANGE (m_widget), 1, (maxValue - minValue + 9) / 10); | |
12a480c1 | 364 | UnblockScrollEvent(); |
6de97a3b | 365 | } |
c801d85f | 366 | |
1e1fafb9 | 367 | int wxSlider::GetMin() const |
c801d85f | 368 | { |
20c3174d | 369 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->lower); |
6de97a3b | 370 | } |
c801d85f | 371 | |
1e1fafb9 | 372 | int wxSlider::GetMax() const |
c801d85f | 373 | { |
20c3174d | 374 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->upper); |
6de97a3b | 375 | } |
c801d85f | 376 | |
debe6624 | 377 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f | 378 | { |
12a480c1 | 379 | BlockScrollEvent(); |
98a0564e | 380 | gtk_range_set_increments(GTK_RANGE (m_widget), GetLineSize(), pageSize); |
12a480c1 | 381 | UnblockScrollEvent(); |
6de97a3b | 382 | } |
c801d85f | 383 | |
1e1fafb9 | 384 | int wxSlider::GetPageSize() const |
c801d85f | 385 | { |
20c3174d | 386 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->page_increment); |
6de97a3b | 387 | } |
c801d85f | 388 | |
12a480c1 PC |
389 | // GTK does not support changing the size of the slider |
390 | void wxSlider::SetThumbLength(int) | |
c801d85f | 391 | { |
6de97a3b | 392 | } |
c801d85f | 393 | |
1e1fafb9 | 394 | int wxSlider::GetThumbLength() const |
c801d85f | 395 | { |
12a480c1 | 396 | return 0; |
6de97a3b | 397 | } |
c801d85f | 398 | |
98a0564e | 399 | void wxSlider::SetLineSize( int lineSize ) |
c801d85f | 400 | { |
98a0564e MR |
401 | BlockScrollEvent(); |
402 | gtk_range_set_increments(GTK_RANGE (m_widget), lineSize, GetPageSize()); | |
403 | UnblockScrollEvent(); | |
6de97a3b | 404 | } |
c801d85f | 405 | |
1e1fafb9 | 406 | int wxSlider::GetLineSize() const |
c801d85f | 407 | { |
98a0564e | 408 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->step_increment); |
6de97a3b | 409 | } |
c801d85f | 410 | |
ef5c70f9 | 411 | GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
b4071e91 | 412 | { |
ef5c70f9 | 413 | return GTK_RANGE(m_widget)->event_window; |
b4071e91 RR |
414 | } |
415 | ||
9d522606 RD |
416 | // static |
417 | wxVisualAttributes | |
418 | wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
419 | { | |
420 | return GetDefaultAttributesFromGTKWidget(gtk_vscale_new); | |
421 | } | |
422 | ||
de6185e2 | 423 | #endif // wxUSE_SLIDER |