Use InheritAttributes for wxGTK widgets so they will check
[wxWidgets.git] / src / gtk1 / 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 InheritAttributes();
146
147 Show( TRUE );
148
149 return TRUE;
150 }
151
152 int wxSlider::GetValue() const
153 {
154 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
155 // -0.9 is rounded to -1
156 double val = m_adjust->value;
157 return (int)(val < 0 ? val - 0.5 : val + 0.5);
158 }
159
160 void wxSlider::SetValue( int value )
161 {
162 float fpos = (float)value;
163 m_oldPos = fpos;
164 if (fabs(fpos-m_adjust->value) < 0.2) return;
165
166 m_adjust->value = fpos;
167
168 GtkDisableEvents();
169
170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
171
172 GtkEnableEvents();
173 }
174
175 void wxSlider::SetRange( int minValue, int maxValue )
176 {
177 float fmin = (float)minValue;
178 float fmax = (float)maxValue;
179
180 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
181 (fabs(fmax-m_adjust->upper) < 0.2))
182 {
183 return;
184 }
185
186 m_adjust->lower = fmin;
187 m_adjust->upper = fmax;
188 m_adjust->step_increment = 1.0;
189 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
190
191 GtkDisableEvents();
192
193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
194
195 GtkEnableEvents();
196 }
197
198 int wxSlider::GetMin() const
199 {
200 return (int)ceil(m_adjust->lower);
201 }
202
203 int wxSlider::GetMax() const
204 {
205 return (int)ceil(m_adjust->upper);
206 }
207
208 void wxSlider::SetPageSize( int pageSize )
209 {
210 float fpage = (float)pageSize;
211
212 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
213
214 m_adjust->page_increment = fpage;
215
216 GtkDisableEvents();
217
218 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
219
220 GtkEnableEvents();
221 }
222
223 int wxSlider::GetPageSize() const
224 {
225 return (int)ceil(m_adjust->page_increment);
226 }
227
228 void wxSlider::SetThumbLength( int len )
229 {
230 float flen = (float)len;
231
232 if (fabs(flen-m_adjust->page_size) < 0.2) return;
233
234 m_adjust->page_size = flen;
235
236 GtkDisableEvents();
237
238 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
239
240 GtkEnableEvents();
241 }
242
243 int wxSlider::GetThumbLength() const
244 {
245 return (int)ceil(m_adjust->page_size);
246 }
247
248 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
249 {
250 }
251
252 int wxSlider::GetLineSize() const
253 {
254 return 0;
255 }
256
257 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
258 {
259 GtkRange *range = GTK_RANGE(m_widget);
260 #ifdef __WXGTK20__
261 return (range->event_window == window);
262 #else
263 return ( (window == GTK_WIDGET(range)->window)
264 || (window == range->trough)
265 || (window == range->slider)
266 || (window == range->step_forw)
267 || (window == range->step_back) );
268 #endif
269 }
270
271 void wxSlider::ApplyWidgetStyle()
272 {
273 SetWidgetStyle();
274 gtk_widget_set_style( m_widget, m_widgetStyle );
275 }
276
277 void wxSlider::GtkDisableEvents()
278 {
279 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
280 GTK_SIGNAL_FUNC(gtk_slider_callback),
281 (gpointer) this );
282 }
283
284 void wxSlider::GtkEnableEvents()
285 {
286 gtk_signal_connect( GTK_OBJECT (m_adjust),
287 "value_changed",
288 GTK_SIGNAL_FUNC(gtk_slider_callback),
289 (gpointer) this );
290 }
291
292 #endif