]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/slider.cpp
added HAVE_FSYNC and wxHAVE_PTHREAD_CLEANUP (replacing HAVE_THREAD_CLEANUP_FUNCTIONS)
[wxWidgets.git] / src / gtk / slider.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "slider.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/slider.h"
18
19 #if wxUSE_SLIDER
20
21 #include "wx/utils.h"
22 #include "wx/math.h"
23 #include "wx/gtk/private.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 static const float sensitivity = 0.02;
39
40 //-----------------------------------------------------------------------------
41 // "value_changed"
42 //-----------------------------------------------------------------------------
43
44 extern "C" {
45 static void gtk_slider_callback( GtkAdjustment *adjust,
46 SCROLLBAR_CBACK_ARG
47 wxSlider *win )
48 {
49 if (g_isIdle) wxapp_install_idle_handler();
50
51 if (!win->m_hasVMT) return;
52 if (g_blockEventsOnDrag) return;
53
54 float diff = adjust->value - win->m_oldPos;
55 if (fabs(diff) < sensitivity) return;
56
57 win->m_oldPos = adjust->value;
58
59 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
60
61 double dvalue = adjust->value;
62 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
63
64 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
65 : wxHORIZONTAL;
66
67 wxScrollEvent event( command, win->GetId(), value, orient );
68 event.SetEventObject( win );
69 win->GetEventHandler()->ProcessEvent( event );
70
71 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
73 cevent.SetInt( value );
74 win->GetEventHandler()->ProcessEvent( cevent );
75 }
76 }
77
78 //-----------------------------------------------------------------------------
79 // wxSlider
80 //-----------------------------------------------------------------------------
81
82 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
83
84 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
85 int value, int minValue, int maxValue,
86 const wxPoint& pos, const wxSize& size,
87 long style, const wxValidator& validator, const wxString& name )
88 {
89 m_acceptsFocus = TRUE;
90 m_needParent = TRUE;
91
92 if (!PreCreation( parent, pos, size ) ||
93 !CreateBase( parent, id, pos, size, style, validator, name ))
94 {
95 wxFAIL_MSG( wxT("wxSlider creation failed") );
96 return FALSE;
97 }
98
99 m_oldPos = 0.0;
100
101 if (style & wxSL_VERTICAL)
102 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
103 else
104 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
105
106 if (style & wxSL_LABELS)
107 {
108 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
109 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
110
111 /* labels need more space and too small window will
112 cause junk to appear on the dialog */
113 if (style & wxSL_VERTICAL)
114 {
115 wxSize sz( size );
116 if (sz.x < 35)
117 {
118 sz.x = 35;
119 SetSize( sz );
120 }
121 }
122 else
123 {
124 wxSize sz( size );
125 if (sz.y < 35)
126 {
127 sz.y = 35;
128 SetSize( sz );
129 }
130 }
131 }
132 else
133 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
134
135 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
136
137 #ifdef __WXGTK20__
138 if (style & wxSL_INVERSE)
139 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
140 #endif
141
142 GtkEnableEvents();
143
144 SetRange( minValue, maxValue );
145 SetValue( value );
146
147 m_parent->DoAddChild( this );
148
149 PostCreation(size);
150
151 return TRUE;
152 }
153
154 int wxSlider::GetValue() const
155 {
156 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
157 // -0.9 is rounded to -1
158 double val = m_adjust->value;
159 return (int)(val < 0 ? val - 0.5 : val + 0.5);
160 }
161
162 void wxSlider::SetValue( int value )
163 {
164 float fpos = (float)value;
165 m_oldPos = fpos;
166 if (fabs(fpos-m_adjust->value) < 0.2) return;
167
168 m_adjust->value = fpos;
169
170 GtkDisableEvents();
171
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
173
174 GtkEnableEvents();
175 }
176
177 void wxSlider::SetRange( int minValue, int maxValue )
178 {
179 float fmin = (float)minValue;
180 float fmax = (float)maxValue;
181
182 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
183 (fabs(fmax-m_adjust->upper) < 0.2))
184 {
185 return;
186 }
187
188 m_adjust->lower = fmin;
189 m_adjust->upper = fmax;
190 m_adjust->step_increment = 1.0;
191 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
192
193 GtkDisableEvents();
194
195 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
196
197 GtkEnableEvents();
198 }
199
200 int wxSlider::GetMin() const
201 {
202 return (int)ceil(m_adjust->lower);
203 }
204
205 int wxSlider::GetMax() const
206 {
207 return (int)ceil(m_adjust->upper);
208 }
209
210 void wxSlider::SetPageSize( int pageSize )
211 {
212 float fpage = (float)pageSize;
213
214 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
215
216 m_adjust->page_increment = fpage;
217
218 GtkDisableEvents();
219
220 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
221
222 GtkEnableEvents();
223 }
224
225 int wxSlider::GetPageSize() const
226 {
227 return (int)ceil(m_adjust->page_increment);
228 }
229
230 void wxSlider::SetThumbLength( int len )
231 {
232 float flen = (float)len;
233
234 if (fabs(flen-m_adjust->page_size) < 0.2) return;
235
236 m_adjust->page_size = flen;
237
238 GtkDisableEvents();
239
240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
241
242 GtkEnableEvents();
243 }
244
245 int wxSlider::GetThumbLength() const
246 {
247 return (int)ceil(m_adjust->page_size);
248 }
249
250 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
251 {
252 }
253
254 int wxSlider::GetLineSize() const
255 {
256 return 0;
257 }
258
259 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
260 {
261 GtkRange *range = GTK_RANGE(m_widget);
262 #ifdef __WXGTK20__
263 return (range->event_window == window);
264 #else
265 return ( (window == GTK_WIDGET(range)->window)
266 || (window == range->trough)
267 || (window == range->slider)
268 || (window == range->step_forw)
269 || (window == range->step_back) );
270 #endif
271 }
272
273 void wxSlider::GtkDisableEvents()
274 {
275 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
276 GTK_SIGNAL_FUNC(gtk_slider_callback),
277 (gpointer) this );
278 }
279
280 void wxSlider::GtkEnableEvents()
281 {
282 gtk_signal_connect( GTK_OBJECT (m_adjust),
283 "value_changed",
284 GTK_SIGNAL_FUNC(gtk_slider_callback),
285 (gpointer) this );
286 }
287
288 // static
289 wxVisualAttributes
290 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
291 {
292 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
293 }
294
295 #endif