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