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