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