]>
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 | |
a1abca32 | 22 | #include <gtk/gtk.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 | { |
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 | ||
12a480c1 PC |
135 | if (win->m_blockScrollEvent) |
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 | ||
193 | extern "C" { | |
194 | static void | |
195 | gtk_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 | ||
206 | extern "C" { | |
207 | static gboolean | |
208 | gtk_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 | 220 | extern "C" { |
c918b2cd PC |
221 | static void |
222 | gtk_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 | |
1d591651 | 234 | win->m_blockScrollEvent = true; |
20c3174d | 235 | gtk_range_set_value(GTK_RANGE (win->m_widget), win->GetValue()); |
1d591651 | 236 | win->m_blockScrollEvent = false; |
12a480c1 | 237 | } |
12a480c1 PC |
238 | } |
239 | } | |
2b024653 | 240 | |
12a480c1 PC |
241 | //----------------------------------------------------------------------------- |
242 | // "button_release_event" | |
243 | //----------------------------------------------------------------------------- | |
2b024653 | 244 | |
12a480c1 PC |
245 | extern "C" { |
246 | static gboolean | |
c918b2cd | 247 | gtk_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 | ||
263 | extern "C" { | |
264 | static 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 | ||
c801d85f KB |
275 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
276 | ||
12a480c1 PC |
277 | wxSlider::wxSlider() |
278 | { | |
279 | m_pos = 0; | |
3824e0db | 280 | m_scrollEventType = GTK_SCROLL_NONE; |
12a480c1 | 281 | m_needThumbRelease = false; |
1d591651 | 282 | m_blockScrollEvent = false; |
12a480c1 PC |
283 | } |
284 | ||
e8375af8 VZ |
285 | bool wxSlider::Create(wxWindow *parent, |
286 | wxWindowID id, | |
287 | int value, | |
288 | int minValue, | |
289 | int maxValue, | |
290 | const wxPoint& pos, | |
291 | const wxSize& size, | |
292 | long style, | |
293 | const wxValidator& validator, | |
294 | const wxString& name) | |
c801d85f | 295 | { |
3824e0db VZ |
296 | m_pos = value; |
297 | m_scrollEventType = GTK_SCROLL_NONE; | |
298 | ||
4dcaf11a RR |
299 | if (!PreCreation( parent, pos, size ) || |
300 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
301 | { | |
223d09f6 | 302 | wxFAIL_MSG( wxT("wxSlider creation failed") ); |
de6185e2 | 303 | return false; |
4dcaf11a | 304 | } |
6de97a3b | 305 | |
2e563988 | 306 | if (style & wxSL_VERTICAL) |
2d17d68f | 307 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
308 | else |
309 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
80810ca3 | 310 | |
20c3174d | 311 | gtk_scale_set_draw_value(GTK_SCALE (m_widget), (style & wxSL_LABELS) != 0); |
12a480c1 | 312 | // Keep full precision in position value |
20c3174d | 313 | gtk_scale_set_digits(GTK_SCALE (m_widget), -1); |
80810ca3 | 314 | |
1e219378 KH |
315 | if (style & wxSL_INVERSE) |
316 | gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE ); | |
317 | ||
12a480c1 PC |
318 | g_signal_connect(m_widget, "button_press_event", G_CALLBACK(gtk_button_press_event), this); |
319 | g_signal_connect(m_widget, "button_release_event", G_CALLBACK(gtk_button_release_event), this); | |
320 | g_signal_connect(m_widget, "move_slider", G_CALLBACK(gtk_move_slider), this); | |
321 | g_signal_connect(m_widget, "format_value", G_CALLBACK(gtk_format_value), NULL); | |
322 | g_signal_connect(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this); | |
3824e0db | 323 | gulong handler_id = g_signal_connect(m_widget, "event_after", G_CALLBACK(gtk_event_after), this); |
c918b2cd | 324 | g_signal_handler_block(m_widget, handler_id); |
91b167dd | 325 | |
2d17d68f | 326 | SetRange( minValue, maxValue ); |
3824e0db VZ |
327 | |
328 | // don't call the public SetValue() as it won't do anything unless the | |
329 | // value really changed | |
330 | GTKSetValue( value ); | |
80810ca3 | 331 | |
f03fc89f | 332 | m_parent->DoAddChild( this ); |
80810ca3 | 333 | |
abdeb9e7 | 334 | PostCreation(size); |
80810ca3 | 335 | |
de6185e2 | 336 | return true; |
6de97a3b | 337 | } |
c801d85f | 338 | |
1e1fafb9 | 339 | int wxSlider::GetValue() const |
c801d85f | 340 | { |
23f826bd | 341 | return wxRound(m_pos); |
6de97a3b | 342 | } |
c801d85f | 343 | |
debe6624 | 344 | void wxSlider::SetValue( int value ) |
c801d85f | 345 | { |
12a480c1 | 346 | if (GetValue() != value) |
3824e0db VZ |
347 | GTKSetValue(value); |
348 | } | |
349 | ||
350 | void wxSlider::GTKSetValue(int value) | |
351 | { | |
352 | m_blockScrollEvent = true; | |
353 | gtk_range_set_value(GTK_RANGE (m_widget), value); | |
354 | m_blockScrollEvent = false; | |
6de97a3b | 355 | } |
c801d85f | 356 | |
debe6624 | 357 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 358 | { |
1d591651 | 359 | m_blockScrollEvent = true; |
20c3174d MR |
360 | gtk_range_set_range(GTK_RANGE (m_widget), minValue, maxValue); |
361 | gtk_range_set_increments(GTK_RANGE (m_widget), 1, (maxValue - minValue + 9) / 10); | |
1d591651 | 362 | m_blockScrollEvent = false; |
6de97a3b | 363 | } |
c801d85f | 364 | |
1e1fafb9 | 365 | int wxSlider::GetMin() const |
c801d85f | 366 | { |
20c3174d | 367 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->lower); |
6de97a3b | 368 | } |
c801d85f | 369 | |
1e1fafb9 | 370 | int wxSlider::GetMax() const |
c801d85f | 371 | { |
20c3174d | 372 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->upper); |
6de97a3b | 373 | } |
c801d85f | 374 | |
debe6624 | 375 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f | 376 | { |
1d591651 | 377 | m_blockScrollEvent = true; |
98a0564e | 378 | gtk_range_set_increments(GTK_RANGE (m_widget), GetLineSize(), pageSize); |
1d591651 | 379 | m_blockScrollEvent = false; |
6de97a3b | 380 | } |
c801d85f | 381 | |
1e1fafb9 | 382 | int wxSlider::GetPageSize() const |
c801d85f | 383 | { |
20c3174d | 384 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->page_increment); |
6de97a3b | 385 | } |
c801d85f | 386 | |
12a480c1 PC |
387 | // GTK does not support changing the size of the slider |
388 | void wxSlider::SetThumbLength(int) | |
c801d85f | 389 | { |
6de97a3b | 390 | } |
c801d85f | 391 | |
1e1fafb9 | 392 | int wxSlider::GetThumbLength() const |
c801d85f | 393 | { |
12a480c1 | 394 | return 0; |
6de97a3b | 395 | } |
c801d85f | 396 | |
98a0564e | 397 | void wxSlider::SetLineSize( int lineSize ) |
c801d85f | 398 | { |
1d591651 | 399 | m_blockScrollEvent = true; |
98a0564e | 400 | gtk_range_set_increments(GTK_RANGE (m_widget), lineSize, GetPageSize()); |
1d591651 | 401 | m_blockScrollEvent = false; |
6de97a3b | 402 | } |
c801d85f | 403 | |
1e1fafb9 | 404 | int wxSlider::GetLineSize() const |
c801d85f | 405 | { |
98a0564e | 406 | return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->step_increment); |
6de97a3b | 407 | } |
c801d85f | 408 | |
ef5c70f9 | 409 | GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
b4071e91 | 410 | { |
ef5c70f9 | 411 | return GTK_RANGE(m_widget)->event_window; |
b4071e91 RR |
412 | } |
413 | ||
9d522606 RD |
414 | // static |
415 | wxVisualAttributes | |
416 | wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
417 | { | |
418 | return GetDefaultAttributesFromGTKWidget(gtk_vscale_new); | |
419 | } | |
420 | ||
de6185e2 | 421 | #endif // wxUSE_SLIDER |