]>
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 VZ |
81 | static void gtk_slider_callback( GtkAdjustment *adjust, |
82 | SCROLLBAR_CBACK_ARG | |
83 | wxSlider *win ) | |
80810ca3 | 84 | { |
acfd422a RR |
85 | if (g_isIdle) wxapp_install_idle_handler(); |
86 | ||
a2053b27 | 87 | if (!win->m_hasVMT) return; |
2d17d68f | 88 | if (g_blockEventsOnDrag) return; |
e1811a01 | 89 | |
2b024653 VZ |
90 | const double dvalue = adjust->value; |
91 | const double diff = dvalue - win->m_oldPos; | |
92 | if ( AreSameAdjustValues(diff, 0) ) | |
93 | return; | |
80810ca3 | 94 | |
2b024653 | 95 | wxEventType evtType; |
2b024653 VZ |
96 | if ( win->m_isScrolling ) |
97 | evtType = wxEVT_SCROLL_THUMBTRACK; | |
98 | // it could seem that UP/DOWN are inversed but this is what wxMSW does | |
99 | else if ( AreSameAdjustValues(diff, adjust->step_increment) ) | |
100 | evtType = wxEVT_SCROLL_LINEDOWN; | |
101 | else if ( AreSameAdjustValues(diff, -adjust->step_increment) ) | |
102 | evtType = wxEVT_SCROLL_LINEUP; | |
103 | else if ( AreSameAdjustValues(diff, adjust->page_increment) ) | |
104 | evtType = wxEVT_SCROLL_PAGEDOWN; | |
105 | else if ( AreSameAdjustValues(diff, -adjust->page_increment) ) | |
106 | evtType = wxEVT_SCROLL_PAGEUP; | |
107 | else if ( AreSameAdjustValues(adjust->value, adjust->lower) ) | |
108 | evtType = wxEVT_SCROLL_TOP; | |
109 | else if ( AreSameAdjustValues(adjust->value, adjust->upper) ) | |
110 | evtType = wxEVT_SCROLL_BOTTOM; | |
2d17d68f | 111 | |
2b024653 | 112 | ProcessScrollEvent(win, evtType, dvalue); |
80810ca3 | 113 | |
2b024653 VZ |
114 | win->m_oldPos = dvalue; |
115 | } | |
80810ca3 | 116 | |
2b024653 VZ |
117 | static gint gtk_slider_button_press_callback( GtkWidget * /* widget */, |
118 | GdkEventButton * /* gdk_event */, | |
119 | wxWindowGTK *win) | |
120 | { | |
121 | // indicate that the thumb is being dragged with the mouse | |
122 | win->m_isScrolling = true; | |
80810ca3 | 123 | |
2b024653 VZ |
124 | return FALSE; |
125 | } | |
80810ca3 | 126 | |
2b024653 VZ |
127 | static gint gtk_slider_button_release_callback( GtkWidget *scale, |
128 | GdkEventButton * /* gdk_event */, | |
129 | wxSlider *win) | |
130 | { | |
131 | // not scrolling any longer | |
132 | win->m_isScrolling = false; | |
133 | ||
134 | ProcessScrollEvent(win, wxEVT_SCROLL_THUMBRELEASE, | |
135 | GTK_RANGE(scale)->adjustment->value); | |
136 | ||
137 | return FALSE; | |
6de97a3b | 138 | } |
2b024653 | 139 | |
865bb325 | 140 | } |
c801d85f | 141 | |
97b3455a RR |
142 | //----------------------------------------------------------------------------- |
143 | // wxSlider | |
144 | //----------------------------------------------------------------------------- | |
145 | ||
c801d85f KB |
146 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
147 | ||
debe6624 JS |
148 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
149 | int value, int minValue, int maxValue, | |
c801d85f | 150 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 151 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 152 | { |
b292e2f5 | 153 | m_acceptsFocus = TRUE; |
2d17d68f | 154 | m_needParent = TRUE; |
80810ca3 | 155 | |
4dcaf11a RR |
156 | if (!PreCreation( parent, pos, size ) || |
157 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
158 | { | |
223d09f6 | 159 | wxFAIL_MSG( wxT("wxSlider creation failed") ); |
ef7a25a7 | 160 | return FALSE; |
4dcaf11a | 161 | } |
6de97a3b | 162 | |
2d17d68f | 163 | m_oldPos = 0.0; |
c801d85f | 164 | |
2e563988 | 165 | if (style & wxSL_VERTICAL) |
2d17d68f | 166 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
167 | else |
168 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
80810ca3 | 169 | |
2e563988 | 170 | if (style & wxSL_LABELS) |
858b5bdd | 171 | { |
2e563988 | 172 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
ef7a25a7 | 173 | gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 ); |
80810ca3 | 174 | |
f03fc89f VZ |
175 | /* labels need more space and too small window will |
176 | cause junk to appear on the dialog */ | |
858b5bdd | 177 | if (style & wxSL_VERTICAL) |
f03fc89f VZ |
178 | { |
179 | wxSize sz( size ); | |
180 | if (sz.x < 35) | |
181 | { | |
182 | sz.x = 35; | |
183 | SetSize( sz ); | |
184 | } | |
185 | } | |
858b5bdd | 186 | else |
f03fc89f VZ |
187 | { |
188 | wxSize sz( size ); | |
189 | if (sz.y < 35) | |
190 | { | |
191 | sz.y = 35; | |
192 | SetSize( sz ); | |
193 | } | |
194 | } | |
858b5bdd | 195 | } |
2e563988 RR |
196 | else |
197 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
19da4326 | 198 | |
2d17d68f | 199 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
80810ca3 | 200 | |
1e219378 KH |
201 | if (style & wxSL_INVERSE) |
202 | gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE ); | |
203 | ||
91b167dd | 204 | GtkEnableEvents(); |
9fa72bd2 MR |
205 | g_signal_connect (m_widget, "button_press_event", |
206 | G_CALLBACK (gtk_slider_button_press_callback), | |
207 | this); | |
208 | g_signal_connect (m_widget, "button_release_event", | |
209 | G_CALLBACK (gtk_slider_button_release_callback), | |
210 | this); | |
91b167dd | 211 | |
2d17d68f RR |
212 | SetRange( minValue, maxValue ); |
213 | SetValue( value ); | |
80810ca3 | 214 | |
f03fc89f | 215 | m_parent->DoAddChild( this ); |
80810ca3 | 216 | |
abdeb9e7 | 217 | PostCreation(size); |
80810ca3 | 218 | |
2d17d68f | 219 | return TRUE; |
6de97a3b | 220 | } |
c801d85f | 221 | |
1e1fafb9 | 222 | int wxSlider::GetValue() const |
c801d85f | 223 | { |
2b024653 | 224 | return AdjustValueToInt(m_adjust->value); |
6de97a3b | 225 | } |
c801d85f | 226 | |
debe6624 | 227 | void wxSlider::SetValue( int value ) |
c801d85f | 228 | { |
2b024653 | 229 | double fpos = (double)value; |
2d17d68f | 230 | m_oldPos = fpos; |
2b024653 VZ |
231 | if ( AreSameAdjustValues(fpos, m_adjust->value) ) |
232 | return; | |
80810ca3 | 233 | |
2d17d68f | 234 | m_adjust->value = fpos; |
80810ca3 | 235 | |
e1b93ccb | 236 | GtkDisableEvents(); |
2b024653 | 237 | |
9fa72bd2 | 238 | g_signal_emit_by_name (m_adjust, "value_changed"); |
2b024653 | 239 | |
e1b93ccb | 240 | GtkEnableEvents(); |
6de97a3b | 241 | } |
c801d85f | 242 | |
debe6624 | 243 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 244 | { |
2b024653 VZ |
245 | double fmin = (double)minValue; |
246 | double fmax = (double)maxValue; | |
80810ca3 | 247 | |
2d17d68f RR |
248 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
249 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
250 | { | |
251 | return; | |
252 | } | |
80810ca3 | 253 | |
2d17d68f RR |
254 | m_adjust->lower = fmin; |
255 | m_adjust->upper = fmax; | |
4ee1741f RR |
256 | m_adjust->step_increment = 1.0; |
257 | m_adjust->page_increment = ceil((fmax-fmin) / 10.0); | |
c801d85f | 258 | |
e1b93ccb | 259 | GtkDisableEvents(); |
2b024653 | 260 | |
9fa72bd2 | 261 | g_signal_emit_by_name (m_adjust, "changed"); |
2b024653 | 262 | |
e1b93ccb | 263 | GtkEnableEvents(); |
6de97a3b | 264 | } |
c801d85f | 265 | |
1e1fafb9 | 266 | int wxSlider::GetMin() const |
c801d85f | 267 | { |
b1a39f47 | 268 | return (int)ceil(m_adjust->lower); |
6de97a3b | 269 | } |
c801d85f | 270 | |
1e1fafb9 | 271 | int wxSlider::GetMax() const |
c801d85f | 272 | { |
b1a39f47 | 273 | return (int)ceil(m_adjust->upper); |
6de97a3b | 274 | } |
c801d85f | 275 | |
debe6624 | 276 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f | 277 | { |
2b024653 | 278 | double fpage = (double)pageSize; |
80810ca3 | 279 | |
2d17d68f | 280 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
80810ca3 | 281 | |
2d17d68f | 282 | m_adjust->page_increment = fpage; |
c801d85f | 283 | |
e1b93ccb | 284 | GtkDisableEvents(); |
2b024653 | 285 | |
9fa72bd2 | 286 | g_signal_emit_by_name (m_adjust, "changed"); |
2b024653 | 287 | |
e1b93ccb | 288 | GtkEnableEvents(); |
6de97a3b | 289 | } |
c801d85f | 290 | |
1e1fafb9 | 291 | int wxSlider::GetPageSize() const |
c801d85f | 292 | { |
b1a39f47 | 293 | return (int)ceil(m_adjust->page_increment); |
6de97a3b | 294 | } |
c801d85f | 295 | |
debe6624 | 296 | void wxSlider::SetThumbLength( int len ) |
c801d85f | 297 | { |
2b024653 | 298 | double flen = (double)len; |
80810ca3 | 299 | |
2d17d68f | 300 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
80810ca3 | 301 | |
2d17d68f | 302 | m_adjust->page_size = flen; |
c801d85f | 303 | |
e1b93ccb | 304 | GtkDisableEvents(); |
2b024653 | 305 | |
9fa72bd2 | 306 | g_signal_emit_by_name (m_adjust, "changed"); |
2b024653 | 307 | |
e1b93ccb | 308 | GtkEnableEvents(); |
6de97a3b | 309 | } |
c801d85f | 310 | |
1e1fafb9 | 311 | int wxSlider::GetThumbLength() const |
c801d85f | 312 | { |
b1a39f47 | 313 | return (int)ceil(m_adjust->page_size); |
6de97a3b | 314 | } |
c801d85f | 315 | |
debe6624 | 316 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
c801d85f | 317 | { |
6de97a3b | 318 | } |
c801d85f | 319 | |
1e1fafb9 | 320 | int wxSlider::GetLineSize() const |
c801d85f | 321 | { |
2d17d68f | 322 | return 0; |
6de97a3b | 323 | } |
c801d85f | 324 | |
b4071e91 RR |
325 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
326 | { | |
2d17d68f | 327 | GtkRange *range = GTK_RANGE(m_widget); |
2b5f62a0 | 328 | return (range->event_window == window); |
b4071e91 RR |
329 | } |
330 | ||
e1b93ccb RR |
331 | void wxSlider::GtkDisableEvents() |
332 | { | |
9fa72bd2 MR |
333 | g_signal_handlers_disconnect_by_func (m_adjust, |
334 | (gpointer) gtk_slider_callback, | |
335 | this); | |
e1b93ccb RR |
336 | } |
337 | ||
338 | void wxSlider::GtkEnableEvents() | |
339 | { | |
9fa72bd2 MR |
340 | g_signal_connect (m_adjust, "value_changed", |
341 | G_CALLBACK (gtk_slider_callback), this); | |
e1b93ccb RR |
342 | } |
343 | ||
9d522606 RD |
344 | // static |
345 | wxVisualAttributes | |
346 | wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
347 | { | |
348 | return GetDefaultAttributesFromGTKWidget(gtk_vscale_new); | |
349 | } | |
350 | ||
dcf924a3 | 351 | #endif |