]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/slider.cpp
list ctrl and tree ctrl didn't like the new focus code,
[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 win->GetEventHandler()->ProcessEvent( cevent );
77 }
78
79 //-----------------------------------------------------------------------------
80 // wxSlider
81 //-----------------------------------------------------------------------------
82
83 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
84
85 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
86 int value, int minValue, int maxValue,
87 const wxPoint& pos, const wxSize& size,
88 long style, const wxValidator& validator, const wxString& name )
89 {
90 m_acceptsFocus = TRUE;
91 m_needParent = TRUE;
92
93 if (!PreCreation( parent, pos, size ) ||
94 !CreateBase( parent, id, pos, size, style, validator, name ))
95 {
96 wxFAIL_MSG( wxT("wxSlider creation failed") );
97 return FALSE;
98 }
99
100 m_oldPos = 0.0;
101
102 if (style & wxSL_VERTICAL)
103 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
104 else
105 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
106
107 if (style & wxSL_LABELS)
108 {
109 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
110 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
111
112 /* labels need more space and too small window will
113 cause junk to appear on the dialog */
114 if (style & wxSL_VERTICAL)
115 {
116 wxSize sz( size );
117 if (sz.x < 35)
118 {
119 sz.x = 35;
120 SetSize( sz );
121 }
122 }
123 else
124 {
125 wxSize sz( size );
126 if (sz.y < 35)
127 {
128 sz.y = 35;
129 SetSize( sz );
130 }
131 }
132 }
133 else
134 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
135
136 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
137
138 gtk_signal_connect( GTK_OBJECT(m_adjust),
139 "value_changed",
140 (GtkSignalFunc) gtk_slider_callback,
141 (gpointer) this );
142
143 SetRange( minValue, maxValue );
144 SetValue( value );
145
146 m_parent->DoAddChild( this );
147
148 PostCreation();
149
150 SetBackgroundColour( parent->GetBackgroundColour() );
151
152 Show( TRUE );
153
154 return TRUE;
155 }
156
157 int wxSlider::GetValue() const
158 {
159 return (int)(m_adjust->value+0.5);
160 }
161
162 void wxSlider::SetValue( int value )
163 {
164 float fpos = (float)value;
165 m_oldPos = fpos;
166 if (fabs(fpos-m_adjust->value) < 0.2) return;
167
168 m_adjust->value = fpos;
169
170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
171 }
172
173 void wxSlider::SetRange( int minValue, int maxValue )
174 {
175 float fmin = (float)minValue;
176 float fmax = (float)maxValue;
177
178 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
179 (fabs(fmax-m_adjust->upper) < 0.2))
180 {
181 return;
182 }
183
184 m_adjust->lower = fmin;
185 m_adjust->upper = fmax;
186 m_adjust->step_increment = 1.0;
187 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
188
189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
190 }
191
192 int wxSlider::GetMin() const
193 {
194 return (int)ceil(m_adjust->lower);
195 }
196
197 int wxSlider::GetMax() const
198 {
199 return (int)ceil(m_adjust->upper);
200 }
201
202 void wxSlider::SetPageSize( int pageSize )
203 {
204 float fpage = (float)pageSize;
205
206 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
207
208 m_adjust->page_increment = fpage;
209
210 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
211 }
212
213 int wxSlider::GetPageSize() const
214 {
215 return (int)ceil(m_adjust->page_increment);
216 }
217
218 void wxSlider::SetThumbLength( int len )
219 {
220 float flen = (float)len;
221
222 if (fabs(flen-m_adjust->page_size) < 0.2) return;
223
224 m_adjust->page_size = flen;
225
226 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
227 }
228
229 int wxSlider::GetThumbLength() const
230 {
231 return (int)ceil(m_adjust->page_size);
232 }
233
234 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
235 {
236 }
237
238 int wxSlider::GetLineSize() const
239 {
240 return 0;
241 }
242
243 void wxSlider::SetTick( int WXUNUSED(tickPos) )
244 {
245 }
246
247 void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
248 {
249 }
250
251 int wxSlider::GetTickFreq() const
252 {
253 return 0;
254 }
255
256 void wxSlider::ClearTicks()
257 {
258 }
259
260 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
261 {
262 }
263
264 int wxSlider::GetSelEnd() const
265 {
266 return 0;
267 }
268
269 int wxSlider::GetSelStart() const
270 {
271 return 0;
272 }
273
274 void wxSlider::ClearSel()
275 {
276 }
277
278 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
279 {
280 GtkRange *range = GTK_RANGE(m_widget);
281 return ( (window == GTK_WIDGET(range)->window) ||
282 (window == range->trough) ||
283 (window == range->slider) ||
284 (window == range->step_forw) ||
285 (window == range->step_back) );
286 }
287
288 void wxSlider::ApplyWidgetStyle()
289 {
290 SetWidgetStyle();
291 gtk_widget_set_style( m_widget, m_widgetStyle );
292 }
293
294 #endif