]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/slider.cpp
cleaned up the mess caused by FloodFill patch
[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 #ifdef __GNUG__
11 #pragma implementation "slider.h"
12 #endif
13
14 #include "wx/slider.h"
15
16 #if wxUSE_SLIDER
17
18 #include "wx/utils.h"
19
20 #include <math.h>
21
22 #include "wx/gtk/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 static const float sensitivity = 0.02;
38
39 //-----------------------------------------------------------------------------
40 // "value_changed"
41 //-----------------------------------------------------------------------------
42
43 static void gtk_slider_callback( GtkAdjustment *adjust,
44 SCROLLBAR_CBACK_ARG
45 wxSlider *win )
46 {
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
52 float diff = adjust->value - win->m_oldPos;
53 if (fabs(diff) < sensitivity) return;
54
55 win->m_oldPos = adjust->value;
56
57 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
58
59 double dvalue = adjust->value;
60 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
61
62 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
63 : wxHORIZONTAL;
64
65 wxScrollEvent event( command, win->GetId(), value, orient );
66 event.SetEventObject( win );
67 win->GetEventHandler()->ProcessEvent( event );
68
69 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
70 cevent.SetEventObject( win );
71 cevent.SetInt( value );
72 win->GetEventHandler()->ProcessEvent( cevent );
73 }
74
75 //-----------------------------------------------------------------------------
76 // wxSlider
77 //-----------------------------------------------------------------------------
78
79 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
80
81 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
82 int value, int minValue, int maxValue,
83 const wxPoint& pos, const wxSize& size,
84 long style, const wxValidator& validator, const wxString& name )
85 {
86 m_acceptsFocus = TRUE;
87 m_needParent = TRUE;
88
89 if (!PreCreation( parent, pos, size ) ||
90 !CreateBase( parent, id, pos, size, style, validator, name ))
91 {
92 wxFAIL_MSG( wxT("wxSlider creation failed") );
93 return FALSE;
94 }
95
96 m_oldPos = 0.0;
97
98 if (style & wxSL_VERTICAL)
99 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
100 else
101 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
102
103 if (style & wxSL_LABELS)
104 {
105 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
106 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
107
108 /* labels need more space and too small window will
109 cause junk to appear on the dialog */
110 if (style & wxSL_VERTICAL)
111 {
112 wxSize sz( size );
113 if (sz.x < 35)
114 {
115 sz.x = 35;
116 SetSize( sz );
117 }
118 }
119 else
120 {
121 wxSize sz( size );
122 if (sz.y < 35)
123 {
124 sz.y = 35;
125 SetSize( sz );
126 }
127 }
128 }
129 else
130 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
131
132 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
133
134 gtk_signal_connect( GTK_OBJECT(m_adjust),
135 "value_changed",
136 (GtkSignalFunc) gtk_slider_callback,
137 (gpointer) this );
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 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
170 }
171
172 void wxSlider::SetRange( int minValue, int maxValue )
173 {
174 float fmin = (float)minValue;
175 float fmax = (float)maxValue;
176
177 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
178 (fabs(fmax-m_adjust->upper) < 0.2))
179 {
180 return;
181 }
182
183 m_adjust->lower = fmin;
184 m_adjust->upper = fmax;
185 m_adjust->step_increment = 1.0;
186 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
187
188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
189 }
190
191 int wxSlider::GetMin() const
192 {
193 return (int)ceil(m_adjust->lower);
194 }
195
196 int wxSlider::GetMax() const
197 {
198 return (int)ceil(m_adjust->upper);
199 }
200
201 void wxSlider::SetPageSize( int pageSize )
202 {
203 float fpage = (float)pageSize;
204
205 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
206
207 m_adjust->page_increment = fpage;
208
209 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
210 }
211
212 int wxSlider::GetPageSize() const
213 {
214 return (int)ceil(m_adjust->page_increment);
215 }
216
217 void wxSlider::SetThumbLength( int len )
218 {
219 float flen = (float)len;
220
221 if (fabs(flen-m_adjust->page_size) < 0.2) return;
222
223 m_adjust->page_size = flen;
224
225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
226 }
227
228 int wxSlider::GetThumbLength() const
229 {
230 return (int)ceil(m_adjust->page_size);
231 }
232
233 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
234 {
235 }
236
237 int wxSlider::GetLineSize() const
238 {
239 return 0;
240 }
241
242 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
243 {
244 GtkRange *range = GTK_RANGE(m_widget);
245 return ( (window == GTK_WIDGET(range)->window)
246 #ifndef __WXGTK20__
247 || (window == range->trough)
248 || (window == range->slider)
249 || (window == range->step_forw)
250 || (window == range->step_back)
251 #endif // GTK+ 1.x
252 );
253 }
254
255 void wxSlider::ApplyWidgetStyle()
256 {
257 SetWidgetStyle();
258 gtk_widget_set_style( m_widget, m_widgetStyle );
259 }
260
261 #endif