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