Added support for SCROLL_TOP and _BOTTOM events
[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 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // "value_changed"
26 //-----------------------------------------------------------------------------
27
28 static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win )
29 {
30 if (!win->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
33 float diff = win->m_adjust->value - win->m_oldPos;
34 if (fabs(diff) < 0.2) return;
35
36 wxEventType command = wxEVT_NULL;
37
38 float line_step = win->m_adjust->step_increment;
39 float page_step = win->m_adjust->page_increment;
40
41 if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM;
42 else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP;
43 else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
44 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
45 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
46 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
47 else command = wxEVT_SCROLL_THUMBTRACK;
48
49 int value = (int)(win->m_adjust->value+0.5);
50
51 int orient = wxHORIZONTAL;
52 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
53
54 wxScrollEvent event( command, win->GetId(), value, orient );
55 event.SetEventObject( win );
56 win->ProcessEvent( event );
57
58 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
59 cevent.SetEventObject( win );
60 win->ProcessEvent( cevent );
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxSlider
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
68
69 wxSlider::wxSlider(void)
70 {
71 }
72
73 wxSlider::~wxSlider(void)
74 {
75 }
76
77 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
78 int value, int minValue, int maxValue,
79 const wxPoint& pos, const wxSize& size,
80 long style, const wxValidator& validator, const wxString& name )
81 {
82 m_needParent = TRUE;
83
84 PreCreation( parent, id, pos, size, style, name );
85
86 SetValidator( validator );
87
88 m_oldPos = 0.0;
89
90 if (style & wxSL_VERTICAL == wxSL_VERTICAL)
91 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
92 else
93 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
94
95 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
96
97 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
98
99 gtk_signal_connect (GTK_OBJECT (m_adjust), "value_changed",
100 (GtkSignalFunc) gtk_slider_callback, (gpointer) this );
101 SetRange( minValue, maxValue );
102 SetValue( value );
103
104 m_parent->AddChild( this );
105
106 (m_parent->m_insertCallback)( m_parent, this );
107
108 PostCreation();
109
110 SetBackgroundColour( parent->GetBackgroundColour() );
111
112 Show( TRUE );
113
114 return TRUE;
115 }
116
117 int wxSlider::GetValue(void) const
118 {
119 return (int)(m_adjust->value+0.5);
120 }
121
122 void wxSlider::SetValue( int value )
123 {
124 float fpos = (float)value;
125 m_oldPos = fpos;
126 if (fabs(fpos-m_adjust->value) < 0.2) return;
127 m_adjust->value = fpos;
128
129 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
130 }
131
132 void wxSlider::SetRange( int minValue, int maxValue )
133 {
134 float fmin = (float)minValue;
135 float fmax = (float)maxValue;
136
137 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
138 (fabs(fmax-m_adjust->upper) < 0.2))
139 return;
140
141 m_adjust->lower = fmin;
142 m_adjust->upper = fmax;
143
144 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
145 }
146
147 int wxSlider::GetMin(void) const
148 {
149 return (int)(m_adjust->lower+0.5);
150 }
151
152 int wxSlider::GetMax(void) const
153 {
154 return (int)(m_adjust->upper+0.5);
155 }
156
157 void wxSlider::SetPageSize( int pageSize )
158 {
159 float fpage = (float)pageSize;
160
161 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
162
163 m_adjust->page_increment = fpage;
164
165 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
166 }
167
168 int wxSlider::GetPageSize(void) const
169 {
170 return (int)(m_adjust->page_increment+0.5);
171 }
172
173 void wxSlider::SetThumbLength( int len )
174 {
175 float flen = (float)len;
176
177 if (fabs(flen-m_adjust->page_size) < 0.2) return;
178
179 m_adjust->page_size = flen;
180
181 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
182 }
183
184 int wxSlider::GetThumbLength(void) const
185 {
186 return (int)(m_adjust->page_size+0.5);
187 }
188
189 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
190 {
191 }
192
193 int wxSlider::GetLineSize(void) const
194 {
195 return 0;
196 }
197
198 void wxSlider::SetTick( int WXUNUSED(tickPos) )
199 {
200 }
201
202 void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
203 {
204 }
205
206 int wxSlider::GetTickFreq(void) const
207 {
208 return 0;
209 }
210
211 void wxSlider::ClearTicks(void)
212 {
213 }
214
215 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
216 {
217 }
218
219 int wxSlider::GetSelEnd(void) const
220 {
221 return 0;
222 }
223
224 int wxSlider::GetSelStart(void) const
225 {
226 return 0;
227 }
228
229 void wxSlider::ClearSel(void)
230 {
231 }
232
233 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
234 {
235 GtkRange *range = GTK_RANGE(m_widget);
236 return ( (window == GTK_WIDGET(range)->window) ||
237 (window == range->trough) ||
238 (window == range->slider) ||
239 (window == range->step_forw) ||
240 (window == range->step_back) );
241 }
242
243 void wxSlider::ApplyWidgetStyle()
244 {
245 SetWidgetStyle();
246 gtk_widget_set_style( m_widget, m_widgetStyle );
247 }