]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/slider.cpp
fixed memory allocation code of wxStreamBuffer to not realloc() new[]ed memory any...
[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
16 #if wxUSE_SLIDER
17
18 #include "wx/utils.h"
19
20 #include <math.h>
21
22 #include <gdk/gdk.h>
23 #include <gtk/gtk.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, wxSlider *win )
45 {
46 if (g_isIdle) wxapp_install_idle_handler();
47
48 if (!win->m_hasVMT) return;
49 if (g_blockEventsOnDrag) return;
50
51 float diff = adjust->value - win->m_oldPos;
52 if (fabs(diff) < sensitivity) return;
53
54 win->m_oldPos = adjust->value;
55
56 GtkRange *range = GTK_RANGE( win->m_widget );
57
58 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
59 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP;
60 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN;
61 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP;
62 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN;
63
64 int value = (int)ceil(adjust->value);
65
66 int orient = wxHORIZONTAL;
67 if ( (win->GetWindowStyleFlag() & wxSB_VERTICAL) == wxSB_VERTICAL)
68 orient = wxVERTICAL;
69
70 wxScrollEvent event( command, win->GetId(), value, orient );
71 event.SetEventObject( win );
72 win->GetEventHandler()->ProcessEvent( event );
73
74 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
75 cevent.SetEventObject( win );
76 cevent.SetInt( value );
77 win->GetEventHandler()->ProcessEvent( cevent );
78 }
79
80 //-----------------------------------------------------------------------------
81 // wxSlider
82 //-----------------------------------------------------------------------------
83
84 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
85
86 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
87 int value, int minValue, int maxValue,
88 const wxPoint& pos, const wxSize& size,
89 long style, const wxValidator& validator, const wxString& name )
90 {
91 m_acceptsFocus = TRUE;
92 m_needParent = TRUE;
93
94 if (!PreCreation( parent, pos, size ) ||
95 !CreateBase( parent, id, pos, size, style, validator, name ))
96 {
97 wxFAIL_MSG( wxT("wxSlider creation failed") );
98 return FALSE;
99 }
100
101 m_oldPos = 0.0;
102
103 if (style & wxSL_VERTICAL)
104 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
105 else
106 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
107
108 if (style & wxSL_LABELS)
109 {
110 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
111 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
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() 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 m_adjust->step_increment = 1.0;
188 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
189
190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
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 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
212 }
213
214 int wxSlider::GetPageSize() 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() 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() 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() const
253 {
254 return 0;
255 }
256
257 void wxSlider::ClearTicks()
258 {
259 }
260
261 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
262 {
263 }
264
265 int wxSlider::GetSelEnd() const
266 {
267 return 0;
268 }
269
270 int wxSlider::GetSelStart() const
271 {
272 return 0;
273 }
274
275 void wxSlider::ClearSel()
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