]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/slider.cpp
-1 is kept -1 as a wxWindowID
[wxWidgets.git] / src / gtk1 / slider.cpp
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 #include "wx/utils.h"
16 #include <math.h>
17
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20
21 //-----------------------------------------------------------------------------
22 // idle system
23 //-----------------------------------------------------------------------------
24
25 extern void wxapp_install_idle_handler();
26 extern bool g_isIdle;
27
28 //-----------------------------------------------------------------------------
29 // data
30 //-----------------------------------------------------------------------------
31
32 extern bool g_blockEventsOnDrag;
33
34 //-----------------------------------------------------------------------------
35 // "value_changed"
36 //-----------------------------------------------------------------------------
37
38 static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win )
39 {
40 if (g_isIdle) wxapp_install_idle_handler();
41
42 if (!win->m_hasVMT) return;
43 if (g_blockEventsOnDrag) return;
44
45 float diff = win->m_adjust->value - win->m_oldPos;
46 if (fabs(diff) < 0.2) return;
47 win->m_oldPos = win->m_adjust->value;
48
49 wxEventType command = wxEVT_NULL;
50
51 float line_step = win->m_adjust->step_increment;
52 float page_step = win->m_adjust->page_increment;
53
54 if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM;
55 else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP;
56 else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
57 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
58 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
59 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
60 else command = wxEVT_SCROLL_THUMBTRACK;
61
62 int value = (int)ceil(win->m_adjust->value);
63
64 int orient = wxHORIZONTAL;
65 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
66
67 wxScrollEvent event( command, win->GetId(), value, orient );
68 event.SetEventObject( win );
69 win->GetEventHandler()->ProcessEvent( event );
70
71 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
73 win->GetEventHandler()->ProcessEvent( cevent );
74 }
75
76 //-----------------------------------------------------------------------------
77 // wxSlider
78 //-----------------------------------------------------------------------------
79
80 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
81
82 wxSlider::wxSlider(void)
83 {
84 }
85
86 wxSlider::~wxSlider(void)
87 {
88 }
89
90 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
91 int value, int minValue, int maxValue,
92 const wxPoint& pos, const wxSize& size,
93 long style, const wxValidator& validator, const wxString& name )
94 {
95 m_acceptsFocus = TRUE;
96 m_needParent = TRUE;
97
98 PreCreation( parent, id, pos, size, style, name );
99
100 SetValidator( validator );
101
102 m_oldPos = 0.0;
103
104 if (style & wxSL_VERTICAL)
105 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
106 else
107 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
108
109 if (style & wxSL_LABELS)
110 {
111 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
112
113 /* labels need more space and too small window will
114 cause junk to appear on the dialog */
115 if (style & wxSL_VERTICAL)
116 {
117 wxSize sz( size );
118 if (sz.x < 35)
119 {
120 sz.x = 35;
121 SetSize( sz );
122 }
123 }
124 else
125 {
126 wxSize sz( size );
127 if (sz.y < 35)
128 {
129 sz.y = 35;
130 SetSize( sz );
131 }
132 }
133 }
134 else
135 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
136
137 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
138
139 gtk_signal_connect( GTK_OBJECT(m_adjust),
140 "value_changed",
141 (GtkSignalFunc) gtk_slider_callback,
142 (gpointer) this );
143
144 SetRange( minValue, maxValue );
145 SetValue( value );
146
147 m_parent->DoAddChild( this );
148
149 PostCreation();
150
151 SetBackgroundColour( parent->GetBackgroundColour() );
152
153 Show( TRUE );
154
155 return TRUE;
156 }
157
158 int wxSlider::GetValue(void) const
159 {
160 return (int)(m_adjust->value+0.5);
161 }
162
163 void wxSlider::SetValue( int value )
164 {
165 float fpos = (float)value;
166 m_oldPos = fpos;
167 if (fabs(fpos-m_adjust->value) < 0.2) return;
168
169 m_adjust->value = fpos;
170
171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
172 }
173
174 void wxSlider::SetRange( int minValue, int maxValue )
175 {
176 float fmin = (float)minValue;
177 float fmax = (float)maxValue;
178
179 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
180 (fabs(fmax-m_adjust->upper) < 0.2))
181 {
182 return;
183 }
184
185 m_adjust->lower = fmin;
186 m_adjust->upper = fmax;
187
188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
189 }
190
191 int wxSlider::GetMin(void) const
192 {
193 return (int)ceil(m_adjust->lower);
194 }
195
196 int wxSlider::GetMax(void) 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(void) 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(void) 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(void) const
238 {
239 return 0;
240 }
241
242 void wxSlider::SetTick( int WXUNUSED(tickPos) )
243 {
244 }
245
246 void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
247 {
248 }
249
250 int wxSlider::GetTickFreq(void) const
251 {
252 return 0;
253 }
254
255 void wxSlider::ClearTicks(void)
256 {
257 }
258
259 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
260 {
261 }
262
263 int wxSlider::GetSelEnd(void) const
264 {
265 return 0;
266 }
267
268 int wxSlider::GetSelStart(void) const
269 {
270 return 0;
271 }
272
273 void wxSlider::ClearSel(void)
274 {
275 }
276
277 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
278 {
279 GtkRange *range = GTK_RANGE(m_widget);
280 return ( (window == GTK_WIDGET(range)->window) ||
281 (window == range->trough) ||
282 (window == range->slider) ||
283 (window == range->step_forw) ||
284 (window == range->step_back) );
285 }
286
287 void wxSlider::ApplyWidgetStyle()
288 {
289 SetWidgetStyle();
290 gtk_widget_set_style( m_widget, m_widgetStyle );
291 }