]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #include "wx/slider.h" | |
14 | ||
15 | #if wxUSE_SLIDER | |
16 | ||
17 | #include "wx/utils.h" | |
18 | #include "wx/math.h" | |
19 | #include "wx/gtk/private.h" | |
20 | ||
21 | //----------------------------------------------------------------------------- | |
22 | // idle system | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern void wxapp_install_idle_handler(); | |
26 | extern bool g_isIdle; | |
27 | ||
28 | //----------------------------------------------------------------------------- | |
29 | // data | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | extern bool g_blockEventsOnDrag; | |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
35 | // helper functions | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | // compare 2 adjustment values up to some (hardcoded) precision | |
39 | static inline bool AreSameAdjustValues(double x, double y) | |
40 | { | |
41 | return fabs(x - y) < 0.02; | |
42 | } | |
43 | ||
44 | static inline int AdjustValueToInt(double x) | |
45 | { | |
46 | // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and | |
47 | // -0.9 is rounded to -1 | |
48 | return (int)(x < 0 ? x - 0.5 : x + 0.5); | |
49 | } | |
50 | ||
51 | // process a scroll event | |
52 | static void | |
53 | ProcessScrollEvent(wxSlider *win, wxEventType evtType, double dvalue) | |
54 | { | |
55 | int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL | |
56 | : wxHORIZONTAL; | |
57 | ||
58 | int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5); | |
59 | wxScrollEvent event( evtType, win->GetId(), value, orient ); | |
60 | event.SetEventObject( win ); | |
61 | win->GetEventHandler()->ProcessEvent( event ); | |
62 | ||
63 | if ( evtType != wxEVT_SCROLL_THUMBTRACK ) | |
64 | { | |
65 | wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient); | |
66 | event2.SetEventObject( win ); | |
67 | win->GetEventHandler()->ProcessEvent( event2 ); | |
68 | } | |
69 | ||
70 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); | |
71 | cevent.SetEventObject( win ); | |
72 | cevent.SetInt( value ); | |
73 | win->GetEventHandler()->ProcessEvent( cevent ); | |
74 | } | |
75 | ||
76 | //----------------------------------------------------------------------------- | |
77 | // "value_changed" | |
78 | //----------------------------------------------------------------------------- | |
79 | ||
80 | extern "C" { | |
81 | static void gtk_slider_callback( GtkAdjustment *adjust, | |
82 | wxSlider *win ) | |
83 | { | |
84 | if (g_isIdle) wxapp_install_idle_handler(); | |
85 | ||
86 | if (!win->m_hasVMT) return; | |
87 | if (g_blockEventsOnDrag) return; | |
88 | ||
89 | const double dvalue = adjust->value; | |
90 | const double diff = dvalue - win->m_oldPos; | |
91 | if ( AreSameAdjustValues(diff, 0) ) | |
92 | return; | |
93 | ||
94 | wxEventType evtType; | |
95 | if ( win->m_isScrolling ) | |
96 | evtType = wxEVT_SCROLL_THUMBTRACK; | |
97 | // it could seem that UP/DOWN are inversed but this is what wxMSW does | |
98 | else if ( AreSameAdjustValues(diff, adjust->step_increment) ) | |
99 | evtType = wxEVT_SCROLL_LINEDOWN; | |
100 | else if ( AreSameAdjustValues(diff, -adjust->step_increment) ) | |
101 | evtType = wxEVT_SCROLL_LINEUP; | |
102 | else if ( AreSameAdjustValues(diff, adjust->page_increment) ) | |
103 | evtType = wxEVT_SCROLL_PAGEDOWN; | |
104 | else if ( AreSameAdjustValues(diff, -adjust->page_increment) ) | |
105 | evtType = wxEVT_SCROLL_PAGEUP; | |
106 | else if ( AreSameAdjustValues(adjust->value, adjust->lower) ) | |
107 | evtType = wxEVT_SCROLL_TOP; | |
108 | else if ( AreSameAdjustValues(adjust->value, adjust->upper) ) | |
109 | evtType = wxEVT_SCROLL_BOTTOM; | |
110 | ||
111 | ProcessScrollEvent(win, evtType, dvalue); | |
112 | ||
113 | win->m_oldPos = dvalue; | |
114 | } | |
115 | ||
116 | static gint gtk_slider_button_press_callback( GtkWidget * /* widget */, | |
117 | GdkEventButton * /* gdk_event */, | |
118 | wxWindowGTK *win) | |
119 | { | |
120 | // indicate that the thumb is being dragged with the mouse | |
121 | win->m_isScrolling = true; | |
122 | ||
123 | return FALSE; | |
124 | } | |
125 | ||
126 | static gint gtk_slider_button_release_callback( GtkWidget *scale, | |
127 | GdkEventButton * /* gdk_event */, | |
128 | wxSlider *win) | |
129 | { | |
130 | // not scrolling any longer | |
131 | win->m_isScrolling = false; | |
132 | ||
133 | ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE, | |
134 | GTK_RANGE(scale)->adjustment->value); | |
135 | ||
136 | return FALSE; | |
137 | } | |
138 | ||
139 | } | |
140 | ||
141 | //----------------------------------------------------------------------------- | |
142 | // wxSlider | |
143 | //----------------------------------------------------------------------------- | |
144 | ||
145 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) | |
146 | ||
147 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, | |
148 | int value, int minValue, int maxValue, | |
149 | const wxPoint& pos, const wxSize& size, | |
150 | long style, const wxValidator& validator, const wxString& name ) | |
151 | { | |
152 | m_acceptsFocus = TRUE; | |
153 | m_needParent = TRUE; | |
154 | ||
155 | if (!PreCreation( parent, pos, size ) || | |
156 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
157 | { | |
158 | wxFAIL_MSG( wxT("wxSlider creation failed") ); | |
159 | return FALSE; | |
160 | } | |
161 | ||
162 | m_oldPos = 0.0; | |
163 | ||
164 | if (style & wxSL_VERTICAL) | |
165 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); | |
166 | else | |
167 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
168 | ||
169 | if (style & wxSL_LABELS) | |
170 | { | |
171 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); | |
172 | gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 ); | |
173 | ||
174 | /* labels need more space and too small window will | |
175 | cause junk to appear on the dialog */ | |
176 | if (style & wxSL_VERTICAL) | |
177 | { | |
178 | wxSize sz( size ); | |
179 | if (sz.x < 35) | |
180 | { | |
181 | sz.x = 35; | |
182 | SetSize( sz ); | |
183 | } | |
184 | } | |
185 | else | |
186 | { | |
187 | wxSize sz( size ); | |
188 | if (sz.y < 35) | |
189 | { | |
190 | sz.y = 35; | |
191 | SetSize( sz ); | |
192 | } | |
193 | } | |
194 | } | |
195 | else | |
196 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
197 | ||
198 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); | |
199 | ||
200 | if (style & wxSL_INVERSE) | |
201 | gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE ); | |
202 | ||
203 | GtkEnableEvents(); | |
204 | g_signal_connect (m_widget, "button_press_event", | |
205 | G_CALLBACK (gtk_slider_button_press_callback), | |
206 | this); | |
207 | g_signal_connect (m_widget, "button_release_event", | |
208 | G_CALLBACK (gtk_slider_button_release_callback), | |
209 | this); | |
210 | ||
211 | SetRange( minValue, maxValue ); | |
212 | SetValue( value ); | |
213 | ||
214 | m_parent->DoAddChild( this ); | |
215 | ||
216 | PostCreation(size); | |
217 | ||
218 | return TRUE; | |
219 | } | |
220 | ||
221 | int wxSlider::GetValue() const | |
222 | { | |
223 | return AdjustValueToInt(m_adjust->value); | |
224 | } | |
225 | ||
226 | void wxSlider::SetValue( int value ) | |
227 | { | |
228 | double fpos = (double)value; | |
229 | m_oldPos = fpos; | |
230 | if ( AreSameAdjustValues(fpos, m_adjust->value) ) | |
231 | return; | |
232 | ||
233 | m_adjust->value = fpos; | |
234 | ||
235 | GtkDisableEvents(); | |
236 | ||
237 | g_signal_emit_by_name (m_adjust, "value_changed"); | |
238 | ||
239 | GtkEnableEvents(); | |
240 | } | |
241 | ||
242 | void wxSlider::SetRange( int minValue, int maxValue ) | |
243 | { | |
244 | double fmin = (double)minValue; | |
245 | double fmax = (double)maxValue; | |
246 | ||
247 | if ((fabs(fmin-m_adjust->lower) < 0.2) && | |
248 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
249 | { | |
250 | return; | |
251 | } | |
252 | ||
253 | m_adjust->lower = fmin; | |
254 | m_adjust->upper = fmax; | |
255 | m_adjust->step_increment = 1.0; | |
256 | m_adjust->page_increment = ceil((fmax-fmin) / 10.0); | |
257 | ||
258 | GtkDisableEvents(); | |
259 | ||
260 | g_signal_emit_by_name (m_adjust, "changed"); | |
261 | ||
262 | GtkEnableEvents(); | |
263 | } | |
264 | ||
265 | int wxSlider::GetMin() const | |
266 | { | |
267 | return (int)ceil(m_adjust->lower); | |
268 | } | |
269 | ||
270 | int wxSlider::GetMax() const | |
271 | { | |
272 | return (int)ceil(m_adjust->upper); | |
273 | } | |
274 | ||
275 | void wxSlider::SetPageSize( int pageSize ) | |
276 | { | |
277 | double fpage = (double)pageSize; | |
278 | ||
279 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; | |
280 | ||
281 | m_adjust->page_increment = fpage; | |
282 | ||
283 | GtkDisableEvents(); | |
284 | ||
285 | g_signal_emit_by_name (m_adjust, "changed"); | |
286 | ||
287 | GtkEnableEvents(); | |
288 | } | |
289 | ||
290 | int wxSlider::GetPageSize() const | |
291 | { | |
292 | return (int)ceil(m_adjust->page_increment); | |
293 | } | |
294 | ||
295 | void wxSlider::SetThumbLength( int len ) | |
296 | { | |
297 | double flen = (double)len; | |
298 | ||
299 | if (fabs(flen-m_adjust->page_size) < 0.2) return; | |
300 | ||
301 | m_adjust->page_size = flen; | |
302 | ||
303 | GtkDisableEvents(); | |
304 | ||
305 | g_signal_emit_by_name (m_adjust, "changed"); | |
306 | ||
307 | GtkEnableEvents(); | |
308 | } | |
309 | ||
310 | int wxSlider::GetThumbLength() const | |
311 | { | |
312 | return (int)ceil(m_adjust->page_size); | |
313 | } | |
314 | ||
315 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) | |
316 | { | |
317 | } | |
318 | ||
319 | int wxSlider::GetLineSize() const | |
320 | { | |
321 | return 0; | |
322 | } | |
323 | ||
324 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) | |
325 | { | |
326 | GtkRange *range = GTK_RANGE(m_widget); | |
327 | return (range->event_window == window); | |
328 | } | |
329 | ||
330 | void wxSlider::GtkDisableEvents() | |
331 | { | |
332 | g_signal_handlers_disconnect_by_func (m_adjust, | |
333 | (gpointer) gtk_slider_callback, | |
334 | this); | |
335 | } | |
336 | ||
337 | void wxSlider::GtkEnableEvents() | |
338 | { | |
339 | g_signal_connect (m_adjust, "value_changed", | |
340 | G_CALLBACK (gtk_slider_callback), this); | |
341 | } | |
342 | ||
343 | // static | |
344 | wxVisualAttributes | |
345 | wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
346 | { | |
347 | return GetDefaultAttributesFromGTKWidget(gtk_vscale_new); | |
348 | } | |
349 | ||
350 | #endif |