I've put live into Vadim's wxNavigationKeyEvent idea
[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 #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_acceptsFocus = TRUE;
83 m_needParent = TRUE;
84
85 PreCreation( parent, id, pos, size, style, name );
86
87 SetValidator( validator );
88
89 m_oldPos = 0.0;
90
91 if (style & wxSL_VERTICAL == wxSL_VERTICAL)
92 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
93 else
94 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
95
96 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
97
98 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
99
100 gtk_signal_connect (GTK_OBJECT (m_adjust), "value_changed",
101 (GtkSignalFunc) gtk_slider_callback, (gpointer) this );
102 SetRange( minValue, maxValue );
103 SetValue( value );
104
105 m_parent->AddChild( this );
106
107 (m_parent->m_insertCallback)( m_parent, this );
108
109 PostCreation();
110
111 SetBackgroundColour( parent->GetBackgroundColour() );
112
113 Show( TRUE );
114
115 return TRUE;
116 }
117
118 int wxSlider::GetValue(void) const
119 {
120 return (int)(m_adjust->value+0.5);
121 }
122
123 void wxSlider::SetValue( int value )
124 {
125 float fpos = (float)value;
126 m_oldPos = fpos;
127 if (fabs(fpos-m_adjust->value) < 0.2) return;
128 m_adjust->value = fpos;
129
130 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
131 }
132
133 void wxSlider::SetRange( int minValue, int maxValue )
134 {
135 float fmin = (float)minValue;
136 float fmax = (float)maxValue;
137
138 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
139 (fabs(fmax-m_adjust->upper) < 0.2))
140 return;
141
142 m_adjust->lower = fmin;
143 m_adjust->upper = fmax;
144
145 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
146 }
147
148 int wxSlider::GetMin(void) const
149 {
150 return (int)(m_adjust->lower+0.5);
151 }
152
153 int wxSlider::GetMax(void) const
154 {
155 return (int)(m_adjust->upper+0.5);
156 }
157
158 void wxSlider::SetPageSize( int pageSize )
159 {
160 float fpage = (float)pageSize;
161
162 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
163
164 m_adjust->page_increment = fpage;
165
166 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
167 }
168
169 int wxSlider::GetPageSize(void) const
170 {
171 return (int)(m_adjust->page_increment+0.5);
172 }
173
174 void wxSlider::SetThumbLength( int len )
175 {
176 float flen = (float)len;
177
178 if (fabs(flen-m_adjust->page_size) < 0.2) return;
179
180 m_adjust->page_size = flen;
181
182 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
183 }
184
185 int wxSlider::GetThumbLength(void) const
186 {
187 return (int)(m_adjust->page_size+0.5);
188 }
189
190 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
191 {
192 }
193
194 int wxSlider::GetLineSize(void) const
195 {
196 return 0;
197 }
198
199 void wxSlider::SetTick( int WXUNUSED(tickPos) )
200 {
201 }
202
203 void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
204 {
205 }
206
207 int wxSlider::GetTickFreq(void) const
208 {
209 return 0;
210 }
211
212 void wxSlider::ClearTicks(void)
213 {
214 }
215
216 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
217 {
218 }
219
220 int wxSlider::GetSelEnd(void) const
221 {
222 return 0;
223 }
224
225 int wxSlider::GetSelStart(void) const
226 {
227 return 0;
228 }
229
230 void wxSlider::ClearSel(void)
231 {
232 }
233
234 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
235 {
236 GtkRange *range = GTK_RANGE(m_widget);
237 return ( (window == GTK_WIDGET(range)->window) ||
238 (window == range->trough) ||
239 (window == range->slider) ||
240 (window == range->step_forw) ||
241 (window == range->step_back) );
242 }
243
244 void wxSlider::ApplyWidgetStyle()
245 {
246 SetWidgetStyle();
247 gtk_widget_set_style( m_widget, m_widgetStyle );
248 }