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