]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: gtk/slider.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
c801d85f | 13 | #include "wx/slider.h" |
dcf924a3 RR |
14 | |
15 | #if wxUSE_SLIDER | |
16 | ||
c801d85f | 17 | #include "wx/utils.h" |
463c4d71 | 18 | #include "wx/math.h" |
9e691f46 | 19 | #include "wx/gtk/private.h" |
83624f79 | 20 | |
acfd422a RR |
21 | //----------------------------------------------------------------------------- |
22 | // idle system | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern void wxapp_install_idle_handler(); | |
26 | extern bool g_isIdle; | |
27 | ||
66bd6b93 RR |
28 | //----------------------------------------------------------------------------- |
29 | // data | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
1e6feb95 | 32 | extern bool g_blockEventsOnDrag; |
66bd6b93 | 33 | |
2b024653 VZ |
34 | // ---------------------------------------------------------------------------- |
35 | // helper functions | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | // compare 2 adjustment values up to some (hardcoded) precision | |
39 | static inline bool AreSameAdjustValues(double x, double y) | |
40 | { | |
41 | return fabs(x - y) < 0.02; | |
42 | } | |
43 | ||
44 | static inline int AdjustValueToInt(double x) | |
45 | { | |
46 | // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and | |
47 | // -0.9 is rounded to -1 | |
48 | return (int)(x < 0 ? x - 0.5 : x + 0.5); | |
49 | } | |
50 | ||
51 | // process a scroll event | |
52 | static void | |
53 | ProcessScrollEvent(wxSlider *win, wxEventType evtType, double dvalue) | |
54 | { | |
55 | int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL | |
56 | : wxHORIZONTAL; | |
57 | ||
58 | int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5); | |
59 | wxScrollEvent event( evtType, win->GetId(), value, orient ); | |
60 | event.SetEventObject( win ); | |
61 | win->GetEventHandler()->ProcessEvent( event ); | |
62 | ||
8e3e14c4 VZ |
63 | if ( evtType != wxEVT_SCROLL_THUMBTRACK ) |
64 | { | |
65 | wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient); | |
66 | event2.SetEventObject( win ); | |
67 | win->GetEventHandler()->ProcessEvent( event2 ); | |
68 | } | |
69 | ||
2b024653 VZ |
70 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); |
71 | cevent.SetEventObject( win ); | |
72 | cevent.SetInt( value ); | |
73 | win->GetEventHandler()->ProcessEvent( cevent ); | |
74 | } | |
57a1fd73 | 75 | |
c801d85f | 76 | //----------------------------------------------------------------------------- |
97b3455a | 77 | // "value_changed" |
c801d85f KB |
78 | //----------------------------------------------------------------------------- |
79 | ||
865bb325 | 80 | extern "C" { |
9e691f46 | 81 | static void gtk_slider_callback( GtkAdjustment *adjust, |
9e691f46 | 82 | wxSlider *win ) |
80810ca3 | 83 | { |
acfd422a RR |
84 | if (g_isIdle) wxapp_install_idle_handler(); |
85 | ||
a2053b27 | 86 | if (!win->m_hasVMT) return; |
2d17d68f | 87 | if (g_blockEventsOnDrag) return; |
e1811a01 | 88 | |
2b024653 VZ |
89 | const double dvalue = adjust->value; |
90 | const double diff = dvalue - win->m_oldPos; | |
91 | if ( AreSameAdjustValues(diff, 0) ) | |
92 | return; | |
80810ca3 | 93 | |
2b024653 | 94 | wxEventType evtType; |
2b024653 VZ |
95 | if ( win->m_isScrolling ) |
96 | evtType = wxEVT_SCROLL_THUMBTRACK; | |
97 | // it could seem that UP/DOWN are inversed but this is what wxMSW does | |
98 | else if ( AreSameAdjustValues(diff, adjust->step_increment) ) | |
99 | evtType = wxEVT_SCROLL_LINEDOWN; | |
100 | else if ( AreSameAdjustValues(diff, -adjust->step_increment) ) | |
101 | evtType = wxEVT_SCROLL_LINEUP; | |
102 | else if ( AreSameAdjustValues(diff, adjust->page_increment) ) | |
103 | evtType = wxEVT_SCROLL_PAGEDOWN; | |
104 | else if ( AreSameAdjustValues(diff, -adjust->page_increment) ) | |
105 | evtType = wxEVT_SCROLL_PAGEUP; | |
106 | else if ( AreSameAdjustValues(adjust->value, adjust->lower) ) | |
107 | evtType = wxEVT_SCROLL_TOP; | |
108 | else if ( AreSameAdjustValues(adjust->value, adjust->upper) ) | |
109 | evtType = wxEVT_SCROLL_BOTTOM; | |
2d17d68f | 110 | |
2b024653 | 111 | ProcessScrollEvent(win, evtType, dvalue); |
80810ca3 | 112 | |
2b024653 VZ |
113 | win->m_oldPos = dvalue; |
114 | } | |
80810ca3 | 115 | |
2b024653 VZ |
116 | static gint gtk_slider_button_press_callback( GtkWidget * /* widget */, |
117 | GdkEventButton * /* gdk_event */, | |
118 | wxWindowGTK *win) | |
119 | { | |
120 | // indicate that the thumb is being dragged with the mouse | |
121 | win->m_isScrolling = true; | |
80810ca3 | 122 | |
2b024653 VZ |
123 | return FALSE; |
124 | } | |
80810ca3 | 125 | |
2b024653 VZ |
126 | static gint gtk_slider_button_release_callback( GtkWidget *scale, |
127 | GdkEventButton * /* gdk_event */, | |
128 | wxSlider *win) | |
129 | { | |
130 | // not scrolling any longer | |
131 | win->m_isScrolling = false; | |
132 | ||
133 | ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE, | |
134 | GTK_RANGE(scale)->adjustment->value); | |
135 | ||
136 | return FALSE; | |
6de97a3b | 137 | } |
2b024653 | 138 | |
865bb325 | 139 | } |
c801d85f | 140 | |
97b3455a RR |
141 | //----------------------------------------------------------------------------- |
142 | // wxSlider | |
143 | //----------------------------------------------------------------------------- | |
144 | ||
c801d85f KB |
145 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
146 | ||
debe6624 JS |
147 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
148 | int value, int minValue, int maxValue, | |
c801d85f | 149 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 150 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 151 | { |
b292e2f5 | 152 | m_acceptsFocus = TRUE; |
2d17d68f | 153 | m_needParent = TRUE; |
80810ca3 | 154 | |
4dcaf11a RR |
155 | if (!PreCreation( parent, pos, size ) || |
156 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
157 | { | |
223d09f6 | 158 | wxFAIL_MSG( wxT("wxSlider creation failed") ); |
ef7a25a7 | 159 | return FALSE; |
4dcaf11a | 160 | } |
6de97a3b | 161 | |
2d17d68f | 162 | m_oldPos = 0.0; |
c801d85f | 163 | |
2e563988 | 164 | if (style & wxSL_VERTICAL) |
2d17d68f | 165 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
166 | else |
167 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
80810ca3 | 168 | |
2e563988 | 169 | if (style & wxSL_LABELS) |
858b5bdd | 170 | { |
2e563988 | 171 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
ef7a25a7 | 172 | gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 ); |
80810ca3 | 173 | |
f03fc89f VZ |
174 | /* labels need more space and too small window will |
175 | cause junk to appear on the dialog */ | |
858b5bdd | 176 | if (style & wxSL_VERTICAL) |
f03fc89f VZ |
177 | { |
178 | wxSize sz( size ); | |
179 | if (sz.x < 35) | |
180 | { | |
181 | sz.x = 35; | |
182 | SetSize( sz ); | |
183 | } | |
184 | } | |
858b5bdd | 185 | else |
f03fc89f VZ |
186 | { |
187 | wxSize sz( size ); | |
188 | if (sz.y < 35) | |
189 | { | |
190 | sz.y = 35; | |
191 | SetSize( sz ); | |
192 | } | |
193 | } | |
858b5bdd | 194 | } |
2e563988 RR |
195 | else |
196 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
19da4326 | 197 | |
2d17d68f | 198 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
80810ca3 | 199 | |
1e219378 KH |
200 | if (style & wxSL_INVERSE) |
201 | gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE ); | |
202 | ||
91b167dd | 203 | GtkEnableEvents(); |
9fa72bd2 MR |
204 | g_signal_connect (m_widget, "button_press_event", |
205 | G_CALLBACK (gtk_slider_button_press_callback), | |
206 | this); | |
207 | g_signal_connect (m_widget, "button_release_event", | |
208 | G_CALLBACK (gtk_slider_button_release_callback), | |
209 | this); | |
91b167dd | 210 | |
2d17d68f RR |
211 | SetRange( minValue, maxValue ); |
212 | SetValue( value ); | |
80810ca3 | 213 | |
f03fc89f | 214 | m_parent->DoAddChild( this ); |
80810ca3 | 215 | |
abdeb9e7 | 216 | PostCreation(size); |
80810ca3 | 217 | |
2d17d68f | 218 | return TRUE; |
6de97a3b | 219 | } |
c801d85f | 220 | |
1e1fafb9 | 221 | int wxSlider::GetValue() const |
c801d85f | 222 | { |
2b024653 | 223 | return AdjustValueToInt(m_adjust->value); |
6de97a3b | 224 | } |
c801d85f | 225 | |
debe6624 | 226 | void wxSlider::SetValue( int value ) |
c801d85f | 227 | { |
2b024653 | 228 | double fpos = (double)value; |
2d17d68f | 229 | m_oldPos = fpos; |
2b024653 VZ |
230 | if ( AreSameAdjustValues(fpos, m_adjust->value) ) |
231 | return; | |
80810ca3 | 232 | |
2d17d68f | 233 | m_adjust->value = fpos; |
80810ca3 | 234 | |
e1b93ccb | 235 | GtkDisableEvents(); |
2b024653 | 236 | |
9fa72bd2 | 237 | g_signal_emit_by_name (m_adjust, "value_changed"); |
2b024653 | 238 | |
e1b93ccb | 239 | GtkEnableEvents(); |
6de97a3b | 240 | } |
c801d85f | 241 | |
debe6624 | 242 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 243 | { |
2b024653 VZ |
244 | double fmin = (double)minValue; |
245 | double fmax = (double)maxValue; | |
80810ca3 | 246 | |
2d17d68f RR |
247 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
248 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
249 | { | |
250 | return; | |
251 | } | |
80810ca3 | 252 | |
2d17d68f RR |
253 | m_adjust->lower = fmin; |
254 | m_adjust->upper = fmax; | |
4ee1741f RR |
255 | m_adjust->step_increment = 1.0; |
256 | m_adjust->page_increment = ceil((fmax-fmin) / 10.0); | |
c801d85f | 257 | |
e1b93ccb | 258 | GtkDisableEvents(); |
2b024653 | 259 | |
9fa72bd2 | 260 | g_signal_emit_by_name (m_adjust, "changed"); |
2b024653 | 261 | |
e1b93ccb | 262 | GtkEnableEvents(); |
6de97a3b | 263 | } |
c801d85f | 264 | |
1e1fafb9 | 265 | int wxSlider::GetMin() const |
c801d85f | 266 | { |
b1a39f47 | 267 | return (int)ceil(m_adjust->lower); |
6de97a3b | 268 | } |
c801d85f | 269 | |
1e1fafb9 | 270 | int wxSlider::GetMax() const |
c801d85f | 271 | { |
b1a39f47 | 272 | return (int)ceil(m_adjust->upper); |
6de97a3b | 273 | } |
c801d85f | 274 | |
debe6624 | 275 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f | 276 | { |
2b024653 | 277 | double fpage = (double)pageSize; |
80810ca3 | 278 | |
2d17d68f | 279 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
80810ca3 | 280 | |
2d17d68f | 281 | m_adjust->page_increment = fpage; |
c801d85f | 282 | |
e1b93ccb | 283 | GtkDisableEvents(); |
2b024653 | 284 | |
9fa72bd2 | 285 | g_signal_emit_by_name (m_adjust, "changed"); |
2b024653 | 286 | |
e1b93ccb | 287 | GtkEnableEvents(); |
6de97a3b | 288 | } |
c801d85f | 289 | |
1e1fafb9 | 290 | int wxSlider::GetPageSize() const |
c801d85f | 291 | { |
b1a39f47 | 292 | return (int)ceil(m_adjust->page_increment); |
6de97a3b | 293 | } |
c801d85f | 294 | |
debe6624 | 295 | void wxSlider::SetThumbLength( int len ) |
c801d85f | 296 | { |
2b024653 | 297 | double flen = (double)len; |
80810ca3 | 298 | |
2d17d68f | 299 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
80810ca3 | 300 | |
2d17d68f | 301 | m_adjust->page_size = flen; |
c801d85f | 302 | |
e1b93ccb | 303 | GtkDisableEvents(); |
2b024653 | 304 | |
9fa72bd2 | 305 | g_signal_emit_by_name (m_adjust, "changed"); |
2b024653 | 306 | |
e1b93ccb | 307 | GtkEnableEvents(); |
6de97a3b | 308 | } |
c801d85f | 309 | |
1e1fafb9 | 310 | int wxSlider::GetThumbLength() const |
c801d85f | 311 | { |
b1a39f47 | 312 | return (int)ceil(m_adjust->page_size); |
6de97a3b | 313 | } |
c801d85f | 314 | |
debe6624 | 315 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
c801d85f | 316 | { |
6de97a3b | 317 | } |
c801d85f | 318 | |
1e1fafb9 | 319 | int wxSlider::GetLineSize() const |
c801d85f | 320 | { |
2d17d68f | 321 | return 0; |
6de97a3b | 322 | } |
c801d85f | 323 | |
b4071e91 RR |
324 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
325 | { | |
2d17d68f | 326 | GtkRange *range = GTK_RANGE(m_widget); |
2b5f62a0 | 327 | return (range->event_window == window); |
b4071e91 RR |
328 | } |
329 | ||
e1b93ccb RR |
330 | void wxSlider::GtkDisableEvents() |
331 | { | |
9fa72bd2 MR |
332 | g_signal_handlers_disconnect_by_func (m_adjust, |
333 | (gpointer) gtk_slider_callback, | |
334 | this); | |
e1b93ccb RR |
335 | } |
336 | ||
337 | void wxSlider::GtkEnableEvents() | |
338 | { | |
9fa72bd2 MR |
339 | g_signal_connect (m_adjust, "value_changed", |
340 | G_CALLBACK (gtk_slider_callback), this); | |
e1b93ccb RR |
341 | } |
342 | ||
9d522606 RD |
343 | // static |
344 | wxVisualAttributes | |
345 | wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
346 | { | |
347 | return GetDefaultAttributesFromGTKWidget(gtk_vscale_new); | |
348 | } | |
349 | ||
dcf924a3 | 350 | #endif |