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