]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/slider.cpp
missing reversed orientation assignments
[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 "wx/gtk/private.h"
23
24 //-----------------------------------------------------------------------------
25 // data
26 //-----------------------------------------------------------------------------
27
28 extern bool g_blockEventsOnDrag;
29
30 // ----------------------------------------------------------------------------
31 // helper functions
32 // ----------------------------------------------------------------------------
33
34 // process a scroll event
35 static void
36 ProcessScrollEvent(wxSlider *win, wxEventType evtType)
37 {
38 const int orient = win->HasFlag(wxSL_VERTICAL) ? wxVERTICAL
39 : wxHORIZONTAL;
40
41 const int value = win->GetValue();
42
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
54 if ( evtType != wxEVT_SCROLL_THUMBTRACK )
55 {
56 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
57 event.SetEventObject( win );
58 win->GetEventHandler()->ProcessEvent( event );
59 }
60
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 );
66 }
67
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
119 //-----------------------------------------------------------------------------
120 // "value_changed"
121 //-----------------------------------------------------------------------------
122
123 extern "C" {
124 static void
125 gtk_value_changed(GtkRange* range, wxSlider* win)
126 {
127 if (g_isIdle) wxapp_install_idle_handler();
128
129 GtkAdjustment* adj = gtk_range_get_adjustment (range);
130 const int pos = wxRound(adj->value);
131 const double oldPos = win->m_pos;
132 win->m_pos = adj->value;
133
134 if (!win->m_hasVMT || g_blockEventsOnDrag)
135 return;
136
137 if (win->m_blockScrollEvent)
138 {
139 win->m_scrollEventType = GTK_SCROLL_NONE;
140 return;
141 }
142
143 wxEventType eventType = wxEVT_NULL;
144 if (win->m_isScrolling)
145 {
146 eventType = wxEVT_SCROLL_THUMBTRACK;
147 }
148 else if (win->m_scrollEventType != GTK_SCROLL_NONE)
149 {
150 // Scroll event from "move-slider" (keyboard)
151 eventType = GtkScrollTypeToWx(win->m_scrollEventType);
152 }
153 else if (win->m_mouseButtonDown)
154 {
155 // Difference from last change event
156 const double diff = adj->value - oldPos;
157 const bool isDown = diff > 0;
158
159 if (IsScrollIncrement(adj->page_increment, diff))
160 {
161 eventType = isDown ? wxEVT_SCROLL_PAGEDOWN : wxEVT_SCROLL_PAGEUP;
162 }
163 else if (wxIsSameDouble(adj->value, 0))
164 {
165 eventType = wxEVT_SCROLL_PAGEUP;
166 }
167 else if (wxIsSameDouble(adj->value, adj->upper))
168 {
169 eventType = wxEVT_SCROLL_PAGEDOWN;
170 }
171 else
172 {
173 // Assume track event
174 eventType = wxEVT_SCROLL_THUMBTRACK;
175 // Remember that we're tracking
176 win->m_isScrolling = true;
177 }
178 }
179
180 win->m_scrollEventType = GTK_SCROLL_NONE;
181
182 // If integral position has changed
183 if (wxRound(oldPos) != pos)
184 {
185 wxCHECK_RET(eventType != wxEVT_NULL, _T("Unknown slider scroll event type"));
186 ProcessScrollEvent(win, eventType);
187 win->m_needThumbRelease = eventType == wxEVT_SCROLL_THUMBTRACK;
188 }
189 }
190 }
191
192 //-----------------------------------------------------------------------------
193 // "move_slider" (keyboard event)
194 //-----------------------------------------------------------------------------
195
196 extern "C" {
197 static void
198 gtk_move_slider(GtkRange*, GtkScrollType scrollType, wxSlider* win)
199 {
200 // Save keyboard scroll type for "value_changed" handler
201 win->m_scrollEventType = scrollType;
202 }
203 }
204
205 //-----------------------------------------------------------------------------
206 // "button_press_event"
207 //-----------------------------------------------------------------------------
208
209 extern "C" {
210 static gboolean
211 gtk_button_press_event(GtkWidget*, GdkEventButton*, wxSlider* win)
212 {
213 win->m_mouseButtonDown = true;
214
215 return false;
216 }
217 }
218
219 //-----------------------------------------------------------------------------
220 // "event_after"
221 //-----------------------------------------------------------------------------
222
223 extern "C" {
224 static void
225 gtk_event_after(GtkRange* range, GdkEvent* event, wxSlider* win)
226 {
227 if (event->type == GDK_BUTTON_RELEASE)
228 {
229 g_signal_handlers_block_by_func(range, (gpointer) gtk_event_after, win);
230
231 if (win->m_needThumbRelease)
232 {
233 win->m_needThumbRelease = false;
234 ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE);
235 }
236 // Keep slider at an integral position
237 win->BlockScrollEvent();
238 gtk_range_set_value(GTK_RANGE (win->m_widget), win->GetValue());
239 win->UnblockScrollEvent();
240 }
241 }
242 }
243
244 //-----------------------------------------------------------------------------
245 // "button_release_event"
246 //-----------------------------------------------------------------------------
247
248 extern "C" {
249 static gboolean
250 gtk_button_release_event(GtkRange* range, GdkEventButton*, wxSlider* win)
251 {
252 win->m_mouseButtonDown = false;
253 if (win->m_isScrolling)
254 {
255 win->m_isScrolling = false;
256 g_signal_handlers_unblock_by_func(range, (gpointer) gtk_event_after, win);
257 }
258 return false;
259 }
260 }
261
262 //-----------------------------------------------------------------------------
263 // "format_value"
264 //-----------------------------------------------------------------------------
265
266 extern "C" {
267 static gchar* gtk_format_value(GtkScale*, double value, void*)
268 {
269 // Format value as nearest integer
270 return g_strdup_printf("%d", wxRound(value));
271 }
272 }
273
274 //-----------------------------------------------------------------------------
275 // wxSlider
276 //-----------------------------------------------------------------------------
277
278 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
279
280 wxSlider::wxSlider()
281 {
282 m_pos = 0;
283 m_scrollEventType = 0;
284 m_needThumbRelease = false;
285 }
286
287 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
288 int value, int minValue, int maxValue,
289 const wxPoint& pos, const wxSize& size,
290 long style, const wxValidator& validator, const wxString& name )
291 {
292 m_acceptsFocus = true;
293 m_needParent = true;
294
295 if (!PreCreation( parent, pos, size ) ||
296 !CreateBase( parent, id, pos, size, style, validator, name ))
297 {
298 wxFAIL_MSG( wxT("wxSlider creation failed") );
299 return false;
300 }
301
302 m_pos = 0;
303 m_scrollEventType = 0;
304 m_needThumbRelease = false;
305
306 if (style & wxSL_VERTICAL)
307 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
308 else
309 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
310
311 gtk_scale_set_draw_value(GTK_SCALE (m_widget), (style & wxSL_LABELS) != 0);
312 // Keep full precision in position value
313 gtk_scale_set_digits(GTK_SCALE (m_widget), -1);
314
315 if (style & wxSL_INVERSE)
316 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
317
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);
323 gulong handler_id;
324 handler_id = g_signal_connect(
325 m_widget, "event_after", G_CALLBACK(gtk_event_after), this);
326 g_signal_handler_block(m_widget, handler_id);
327
328 SetRange( minValue, maxValue );
329 SetValue( value );
330
331 m_parent->DoAddChild( this );
332
333 PostCreation(size);
334
335 return true;
336 }
337
338 int wxSlider::GetValue() const
339 {
340 return wxRound(m_pos);
341 }
342
343 void wxSlider::SetValue( int value )
344 {
345 if (GetValue() != value)
346 {
347 BlockScrollEvent();
348 gtk_range_set_value(GTK_RANGE (m_widget), value);
349 UnblockScrollEvent();
350 }
351 }
352
353 void wxSlider::SetRange( int minValue, int maxValue )
354 {
355 BlockScrollEvent();
356 gtk_range_set_range(GTK_RANGE (m_widget), minValue, maxValue);
357 gtk_range_set_increments(GTK_RANGE (m_widget), 1, (maxValue - minValue + 9) / 10);
358 UnblockScrollEvent();
359 }
360
361 int wxSlider::GetMin() const
362 {
363 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->lower);
364 }
365
366 int wxSlider::GetMax() const
367 {
368 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->upper);
369 }
370
371 void wxSlider::SetPageSize( int pageSize )
372 {
373 BlockScrollEvent();
374 gtk_range_set_increments(GTK_RANGE (m_widget), GetLineSize(), pageSize);
375 UnblockScrollEvent();
376 }
377
378 int wxSlider::GetPageSize() const
379 {
380 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->page_increment);
381 }
382
383 // GTK does not support changing the size of the slider
384 void wxSlider::SetThumbLength(int)
385 {
386 }
387
388 int wxSlider::GetThumbLength() const
389 {
390 return 0;
391 }
392
393 void wxSlider::SetLineSize( int lineSize )
394 {
395 BlockScrollEvent();
396 gtk_range_set_increments(GTK_RANGE (m_widget), lineSize, GetPageSize());
397 UnblockScrollEvent();
398 }
399
400 int wxSlider::GetLineSize() const
401 {
402 return int(gtk_range_get_adjustment (GTK_RANGE (m_widget))->step_increment);
403 }
404
405 GdkWindow *wxSlider::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
406 {
407 return GTK_RANGE(m_widget)->event_window;
408 }
409
410 // static
411 wxVisualAttributes
412 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
413 {
414 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
415 }
416
417 #endif // wxUSE_SLIDER