]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #include <math.h> | |
20 | ||
21 | #include "gdk/gdk.h" | |
22 | #include "gtk/gtk.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 | // "value_changed" | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | static void gtk_slider_callback( GtkAdjustment *adjust, wxSlider *win ) | |
42 | { | |
43 | if (g_isIdle) wxapp_install_idle_handler(); | |
44 | ||
45 | if (!win->m_hasVMT) return; | |
46 | if (g_blockEventsOnDrag) return; | |
47 | ||
48 | float diff = adjust->value - win->m_oldPos; | |
49 | if (fabs(diff) < 0.2) return; | |
50 | ||
51 | win->m_oldPos = adjust->value; | |
52 | ||
53 | GtkRange *range = GTK_RANGE( win->m_widget ); | |
54 | ||
55 | wxEventType command = wxEVT_SCROLL_THUMBTRACK; | |
56 | if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP; | |
57 | else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN; | |
58 | else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP; | |
59 | else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN; | |
60 | ||
61 | int value = (int)ceil(adjust->value); | |
62 | ||
63 | int orient = wxHORIZONTAL; | |
64 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; | |
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 | win->GetEventHandler()->ProcessEvent( cevent ); | |
73 | } | |
74 | ||
75 | //----------------------------------------------------------------------------- | |
76 | // wxSlider | |
77 | //----------------------------------------------------------------------------- | |
78 | ||
79 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) | |
80 | ||
81 | wxSlider::wxSlider(void) | |
82 | { | |
83 | } | |
84 | ||
85 | wxSlider::~wxSlider(void) | |
86 | { | |
87 | } | |
88 | ||
89 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, | |
90 | int value, int minValue, int maxValue, | |
91 | const wxPoint& pos, const wxSize& size, | |
92 | long style, const wxValidator& validator, const wxString& name ) | |
93 | { | |
94 | m_acceptsFocus = TRUE; | |
95 | m_needParent = TRUE; | |
96 | ||
97 | if (!PreCreation( parent, pos, size ) || | |
98 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
99 | { | |
100 | wxFAIL_MSG( T("wxSlider creation failed") ); | |
101 | return FALSE; | |
102 | } | |
103 | ||
104 | m_oldPos = 0.0; | |
105 | ||
106 | if (style & wxSL_VERTICAL) | |
107 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); | |
108 | else | |
109 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
110 | ||
111 | if (style & wxSL_LABELS) | |
112 | { | |
113 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); | |
114 | ||
115 | /* labels need more space and too small window will | |
116 | cause junk to appear on the dialog */ | |
117 | if (style & wxSL_VERTICAL) | |
118 | { | |
119 | wxSize sz( size ); | |
120 | if (sz.x < 35) | |
121 | { | |
122 | sz.x = 35; | |
123 | SetSize( sz ); | |
124 | } | |
125 | } | |
126 | else | |
127 | { | |
128 | wxSize sz( size ); | |
129 | if (sz.y < 35) | |
130 | { | |
131 | sz.y = 35; | |
132 | SetSize( sz ); | |
133 | } | |
134 | } | |
135 | } | |
136 | else | |
137 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
138 | ||
139 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); | |
140 | ||
141 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
142 | "value_changed", | |
143 | (GtkSignalFunc) gtk_slider_callback, | |
144 | (gpointer) this ); | |
145 | ||
146 | SetRange( minValue, maxValue ); | |
147 | SetValue( value ); | |
148 | ||
149 | m_parent->DoAddChild( this ); | |
150 | ||
151 | PostCreation(); | |
152 | ||
153 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
154 | ||
155 | Show( TRUE ); | |
156 | ||
157 | return TRUE; | |
158 | } | |
159 | ||
160 | int wxSlider::GetValue(void) const | |
161 | { | |
162 | return (int)(m_adjust->value+0.5); | |
163 | } | |
164 | ||
165 | void wxSlider::SetValue( int value ) | |
166 | { | |
167 | float fpos = (float)value; | |
168 | m_oldPos = fpos; | |
169 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
170 | ||
171 | m_adjust->value = fpos; | |
172 | ||
173 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
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 | ||
190 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
191 | } | |
192 | ||
193 | int wxSlider::GetMin(void) const | |
194 | { | |
195 | return (int)ceil(m_adjust->lower); | |
196 | } | |
197 | ||
198 | int wxSlider::GetMax(void) 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 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
212 | } | |
213 | ||
214 | int wxSlider::GetPageSize(void) const | |
215 | { | |
216 | return (int)ceil(m_adjust->page_increment); | |
217 | } | |
218 | ||
219 | void wxSlider::SetThumbLength( int len ) | |
220 | { | |
221 | float flen = (float)len; | |
222 | ||
223 | if (fabs(flen-m_adjust->page_size) < 0.2) return; | |
224 | ||
225 | m_adjust->page_size = flen; | |
226 | ||
227 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
228 | } | |
229 | ||
230 | int wxSlider::GetThumbLength(void) const | |
231 | { | |
232 | return (int)ceil(m_adjust->page_size); | |
233 | } | |
234 | ||
235 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) | |
236 | { | |
237 | } | |
238 | ||
239 | int wxSlider::GetLineSize(void) const | |
240 | { | |
241 | return 0; | |
242 | } | |
243 | ||
244 | void wxSlider::SetTick( int WXUNUSED(tickPos) ) | |
245 | { | |
246 | } | |
247 | ||
248 | void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) ) | |
249 | { | |
250 | } | |
251 | ||
252 | int wxSlider::GetTickFreq(void) const | |
253 | { | |
254 | return 0; | |
255 | } | |
256 | ||
257 | void wxSlider::ClearTicks(void) | |
258 | { | |
259 | } | |
260 | ||
261 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) | |
262 | { | |
263 | } | |
264 | ||
265 | int wxSlider::GetSelEnd(void) const | |
266 | { | |
267 | return 0; | |
268 | } | |
269 | ||
270 | int wxSlider::GetSelStart(void) const | |
271 | { | |
272 | return 0; | |
273 | } | |
274 | ||
275 | void wxSlider::ClearSel(void) | |
276 | { | |
277 | } | |
278 | ||
279 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) | |
280 | { | |
281 | GtkRange *range = GTK_RANGE(m_widget); | |
282 | return ( (window == GTK_WIDGET(range)->window) || | |
283 | (window == range->trough) || | |
284 | (window == range->slider) || | |
285 | (window == range->step_forw) || | |
286 | (window == range->step_back) ); | |
287 | } | |
288 | ||
289 | void wxSlider::ApplyWidgetStyle() | |
290 | { | |
291 | SetWidgetStyle(); | |
292 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
293 | } | |
294 | ||
295 | #endif |