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