added support for gcc precompiled headers
[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
23 #include <math.h>
24
25 #include "wx/gtk/private.h"
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 //-----------------------------------------------------------------------------
35 // data
36 //-----------------------------------------------------------------------------
37
38 extern bool g_blockEventsOnDrag;
39
40 static const float sensitivity = 0.02;
41
42 //-----------------------------------------------------------------------------
43 // "value_changed"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_slider_callback( GtkAdjustment *adjust,
47 SCROLLBAR_CBACK_ARG
48 wxSlider *win )
49 {
50 if (g_isIdle) wxapp_install_idle_handler();
51
52 if (!win->m_hasVMT) return;
53 if (g_blockEventsOnDrag) return;
54
55 float diff = adjust->value - win->m_oldPos;
56 if (fabs(diff) < sensitivity) return;
57
58 win->m_oldPos = adjust->value;
59
60 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
61
62 double dvalue = adjust->value;
63 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
64
65 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
66 : wxHORIZONTAL;
67
68 wxScrollEvent event( command, win->GetId(), value, orient );
69 event.SetEventObject( win );
70 win->GetEventHandler()->ProcessEvent( event );
71
72 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
73 cevent.SetEventObject( win );
74 cevent.SetInt( value );
75 win->GetEventHandler()->ProcessEvent( cevent );
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 GtkEnableEvents();
138
139 SetRange( minValue, maxValue );
140 SetValue( value );
141
142 m_parent->DoAddChild( this );
143
144 PostCreation();
145
146 SetBackgroundColour( parent->GetBackgroundColour() );
147
148 Show( TRUE );
149
150 return TRUE;
151 }
152
153 int wxSlider::GetValue() const
154 {
155 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
156 // -0.9 is rounded to -1
157 double val = m_adjust->value;
158 return (int)(val < 0 ? val - 0.5 : val + 0.5);
159 }
160
161 void wxSlider::SetValue( int value )
162 {
163 float fpos = (float)value;
164 m_oldPos = fpos;
165 if (fabs(fpos-m_adjust->value) < 0.2) return;
166
167 m_adjust->value = fpos;
168
169 GtkDisableEvents();
170
171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
172
173 GtkEnableEvents();
174 }
175
176 void wxSlider::SetRange( int minValue, int maxValue )
177 {
178 float fmin = (float)minValue;
179 float fmax = (float)maxValue;
180
181 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
182 (fabs(fmax-m_adjust->upper) < 0.2))
183 {
184 return;
185 }
186
187 m_adjust->lower = fmin;
188 m_adjust->upper = fmax;
189 m_adjust->step_increment = 1.0;
190 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
191
192 GtkDisableEvents();
193
194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
195
196 GtkEnableEvents();
197 }
198
199 int wxSlider::GetMin() const
200 {
201 return (int)ceil(m_adjust->lower);
202 }
203
204 int wxSlider::GetMax() const
205 {
206 return (int)ceil(m_adjust->upper);
207 }
208
209 void wxSlider::SetPageSize( int pageSize )
210 {
211 float fpage = (float)pageSize;
212
213 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
214
215 m_adjust->page_increment = fpage;
216
217 GtkDisableEvents();
218
219 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
220
221 GtkEnableEvents();
222 }
223
224 int wxSlider::GetPageSize() const
225 {
226 return (int)ceil(m_adjust->page_increment);
227 }
228
229 void wxSlider::SetThumbLength( int len )
230 {
231 float flen = (float)len;
232
233 if (fabs(flen-m_adjust->page_size) < 0.2) return;
234
235 m_adjust->page_size = flen;
236
237 GtkDisableEvents();
238
239 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
240
241 GtkEnableEvents();
242 }
243
244 int wxSlider::GetThumbLength() const
245 {
246 return (int)ceil(m_adjust->page_size);
247 }
248
249 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
250 {
251 }
252
253 int wxSlider::GetLineSize() const
254 {
255 return 0;
256 }
257
258 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
259 {
260 GtkRange *range = GTK_RANGE(m_widget);
261 #ifdef __WXGTK20__
262 return (range->event_window == window);
263 #else
264 return ( (window == GTK_WIDGET(range)->window)
265 || (window == range->trough)
266 || (window == range->slider)
267 || (window == range->step_forw)
268 || (window == range->step_back) );
269 #endif
270 }
271
272 void wxSlider::ApplyWidgetStyle()
273 {
274 SetWidgetStyle();
275 gtk_widget_set_style( m_widget, m_widgetStyle );
276 }
277
278 void wxSlider::GtkDisableEvents()
279 {
280 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
281 GTK_SIGNAL_FUNC(gtk_slider_callback),
282 (gpointer) this );
283 }
284
285 void wxSlider::GtkEnableEvents()
286 {
287 gtk_signal_connect( GTK_OBJECT (m_adjust),
288 "value_changed",
289 GTK_SIGNAL_FUNC(gtk_slider_callback),
290 (gpointer) this );
291 }
292
293 #endif