]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: slider.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
f03fc89f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
c801d85f KB |
10 | #ifdef __GNUG__ |
11 | #pragma implementation "slider.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/slider.h" | |
dcf924a3 RR |
15 | |
16 | #if wxUSE_SLIDER | |
17 | ||
c801d85f | 18 | #include "wx/utils.h" |
57a1fd73 | 19 | |
f5368809 | 20 | #include <math.h> |
c801d85f | 21 | |
57a1fd73 RR |
22 | #include <gdk/gdk.h> |
23 | #include <gtk/gtk.h> | |
83624f79 | 24 | |
acfd422a RR |
25 | //----------------------------------------------------------------------------- |
26 | // idle system | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | extern void wxapp_install_idle_handler(); | |
30 | extern bool g_isIdle; | |
31 | ||
66bd6b93 RR |
32 | //----------------------------------------------------------------------------- |
33 | // data | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | extern bool g_blockEventsOnDrag; | |
37 | ||
57a1fd73 RR |
38 | static const float sensitivity = 0.02; |
39 | ||
c801d85f | 40 | //----------------------------------------------------------------------------- |
97b3455a | 41 | // "value_changed" |
c801d85f KB |
42 | //----------------------------------------------------------------------------- |
43 | ||
e1811a01 | 44 | static void gtk_slider_callback( GtkAdjustment *adjust, wxSlider *win ) |
80810ca3 | 45 | { |
acfd422a RR |
46 | if (g_isIdle) wxapp_install_idle_handler(); |
47 | ||
a2053b27 | 48 | if (!win->m_hasVMT) return; |
2d17d68f | 49 | if (g_blockEventsOnDrag) return; |
e1811a01 RR |
50 | |
51 | float diff = adjust->value - win->m_oldPos; | |
57a1fd73 | 52 | if (fabs(diff) < sensitivity) return; |
80810ca3 | 53 | |
e1811a01 | 54 | win->m_oldPos = adjust->value; |
2d17d68f | 55 | |
e1811a01 RR |
56 | GtkRange *range = GTK_RANGE( win->m_widget ); |
57 | ||
58 | wxEventType command = wxEVT_SCROLL_THUMBTRACK; | |
59 | if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP; | |
60 | else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN; | |
61 | else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP; | |
62 | else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN; | |
80810ca3 | 63 | |
e1811a01 | 64 | int value = (int)ceil(adjust->value); |
80810ca3 | 65 | |
2d17d68f | 66 | int orient = wxHORIZONTAL; |
80810ca3 VZ |
67 | if ( (win->GetWindowStyleFlag() & wxSB_VERTICAL) == wxSB_VERTICAL) |
68 | orient = wxVERTICAL; | |
69 | ||
2d17d68f RR |
70 | wxScrollEvent event( command, win->GetId(), value, orient ); |
71 | event.SetEventObject( win ); | |
828f655f | 72 | win->GetEventHandler()->ProcessEvent( event ); |
80810ca3 | 73 | |
2d17d68f RR |
74 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); |
75 | cevent.SetEventObject( win ); | |
f6bcfd97 | 76 | cevent.SetInt( value ); |
828f655f | 77 | win->GetEventHandler()->ProcessEvent( cevent ); |
6de97a3b | 78 | } |
c801d85f | 79 | |
97b3455a RR |
80 | //----------------------------------------------------------------------------- |
81 | // wxSlider | |
82 | //----------------------------------------------------------------------------- | |
83 | ||
c801d85f KB |
84 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
85 | ||
debe6624 JS |
86 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
87 | int value, int minValue, int maxValue, | |
c801d85f | 88 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 89 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 90 | { |
b292e2f5 | 91 | m_acceptsFocus = TRUE; |
2d17d68f | 92 | m_needParent = TRUE; |
80810ca3 | 93 | |
4dcaf11a RR |
94 | if (!PreCreation( parent, pos, size ) || |
95 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
96 | { | |
223d09f6 | 97 | wxFAIL_MSG( wxT("wxSlider creation failed") ); |
ef7a25a7 | 98 | return FALSE; |
4dcaf11a | 99 | } |
6de97a3b | 100 | |
2d17d68f | 101 | m_oldPos = 0.0; |
c801d85f | 102 | |
2e563988 | 103 | if (style & wxSL_VERTICAL) |
2d17d68f | 104 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
105 | else |
106 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
80810ca3 | 107 | |
2e563988 | 108 | if (style & wxSL_LABELS) |
858b5bdd | 109 | { |
2e563988 | 110 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
ef7a25a7 | 111 | gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 ); |
80810ca3 | 112 | |
f03fc89f VZ |
113 | /* labels need more space and too small window will |
114 | cause junk to appear on the dialog */ | |
858b5bdd | 115 | if (style & wxSL_VERTICAL) |
f03fc89f VZ |
116 | { |
117 | wxSize sz( size ); | |
118 | if (sz.x < 35) | |
119 | { | |
120 | sz.x = 35; | |
121 | SetSize( sz ); | |
122 | } | |
123 | } | |
858b5bdd | 124 | else |
f03fc89f VZ |
125 | { |
126 | wxSize sz( size ); | |
127 | if (sz.y < 35) | |
128 | { | |
129 | sz.y = 35; | |
130 | SetSize( sz ); | |
131 | } | |
132 | } | |
858b5bdd | 133 | } |
2e563988 RR |
134 | else |
135 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
19da4326 | 136 | |
2d17d68f | 137 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
80810ca3 VZ |
138 | |
139 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
2d17d68f | 140 | "value_changed", |
80810ca3 | 141 | (GtkSignalFunc) gtk_slider_callback, |
f03fc89f | 142 | (gpointer) this ); |
80810ca3 | 143 | |
2d17d68f RR |
144 | SetRange( minValue, maxValue ); |
145 | SetValue( value ); | |
80810ca3 | 146 | |
f03fc89f | 147 | m_parent->DoAddChild( this ); |
80810ca3 | 148 | |
2d17d68f | 149 | PostCreation(); |
80810ca3 | 150 | |
2d17d68f | 151 | SetBackgroundColour( parent->GetBackgroundColour() ); |
f96aa4d9 | 152 | |
2d17d68f | 153 | Show( TRUE ); |
80810ca3 | 154 | |
2d17d68f | 155 | return TRUE; |
6de97a3b | 156 | } |
c801d85f | 157 | |
1e1fafb9 | 158 | int wxSlider::GetValue() const |
c801d85f | 159 | { |
ef7a25a7 VZ |
160 | // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and |
161 | // -0.9 is rounded to -1 | |
162 | double val = m_adjust->value; | |
163 | return (int)(val >= 0 ? val - 0.5 : val + 0.5); | |
6de97a3b | 164 | } |
c801d85f | 165 | |
debe6624 | 166 | void wxSlider::SetValue( int value ) |
c801d85f | 167 | { |
2d17d68f RR |
168 | float fpos = (float)value; |
169 | m_oldPos = fpos; | |
170 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
80810ca3 | 171 | |
2d17d68f | 172 | m_adjust->value = fpos; |
80810ca3 | 173 | |
2d17d68f | 174 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
6de97a3b | 175 | } |
c801d85f | 176 | |
debe6624 | 177 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 178 | { |
2d17d68f RR |
179 | float fmin = (float)minValue; |
180 | float fmax = (float)maxValue; | |
80810ca3 | 181 | |
2d17d68f RR |
182 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
183 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
184 | { | |
185 | return; | |
186 | } | |
80810ca3 | 187 | |
2d17d68f RR |
188 | m_adjust->lower = fmin; |
189 | m_adjust->upper = fmax; | |
4ee1741f RR |
190 | m_adjust->step_increment = 1.0; |
191 | m_adjust->page_increment = ceil((fmax-fmin) / 10.0); | |
c801d85f | 192 | |
2d17d68f | 193 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 194 | } |
c801d85f | 195 | |
1e1fafb9 | 196 | int wxSlider::GetMin() const |
c801d85f | 197 | { |
b1a39f47 | 198 | return (int)ceil(m_adjust->lower); |
6de97a3b | 199 | } |
c801d85f | 200 | |
1e1fafb9 | 201 | int wxSlider::GetMax() const |
c801d85f | 202 | { |
b1a39f47 | 203 | return (int)ceil(m_adjust->upper); |
6de97a3b | 204 | } |
c801d85f | 205 | |
debe6624 | 206 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f | 207 | { |
2d17d68f | 208 | float fpage = (float)pageSize; |
80810ca3 | 209 | |
2d17d68f | 210 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
80810ca3 | 211 | |
2d17d68f | 212 | m_adjust->page_increment = fpage; |
c801d85f | 213 | |
2d17d68f | 214 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 215 | } |
c801d85f | 216 | |
1e1fafb9 | 217 | int wxSlider::GetPageSize() const |
c801d85f | 218 | { |
b1a39f47 | 219 | return (int)ceil(m_adjust->page_increment); |
6de97a3b | 220 | } |
c801d85f | 221 | |
debe6624 | 222 | void wxSlider::SetThumbLength( int len ) |
c801d85f | 223 | { |
2d17d68f | 224 | float flen = (float)len; |
80810ca3 | 225 | |
2d17d68f | 226 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
80810ca3 | 227 | |
2d17d68f | 228 | m_adjust->page_size = flen; |
c801d85f | 229 | |
2d17d68f | 230 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 231 | } |
c801d85f | 232 | |
1e1fafb9 | 233 | int wxSlider::GetThumbLength() const |
c801d85f | 234 | { |
b1a39f47 | 235 | return (int)ceil(m_adjust->page_size); |
6de97a3b | 236 | } |
c801d85f | 237 | |
debe6624 | 238 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
c801d85f | 239 | { |
6de97a3b | 240 | } |
c801d85f | 241 | |
1e1fafb9 | 242 | int wxSlider::GetLineSize() const |
c801d85f | 243 | { |
2d17d68f | 244 | return 0; |
6de97a3b | 245 | } |
c801d85f | 246 | |
debe6624 | 247 | void wxSlider::SetTick( int WXUNUSED(tickPos) ) |
c801d85f | 248 | { |
6de97a3b | 249 | } |
c801d85f | 250 | |
debe6624 | 251 | void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) ) |
c801d85f | 252 | { |
6de97a3b | 253 | } |
c801d85f | 254 | |
1e1fafb9 | 255 | int wxSlider::GetTickFreq() const |
c801d85f | 256 | { |
2d17d68f | 257 | return 0; |
6de97a3b | 258 | } |
c801d85f | 259 | |
1e1fafb9 | 260 | void wxSlider::ClearTicks() |
c801d85f | 261 | { |
6de97a3b | 262 | } |
c801d85f | 263 | |
debe6624 | 264 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) |
c801d85f | 265 | { |
6de97a3b | 266 | } |
c801d85f | 267 | |
1e1fafb9 | 268 | int wxSlider::GetSelEnd() const |
c801d85f | 269 | { |
2d17d68f | 270 | return 0; |
6de97a3b | 271 | } |
c801d85f | 272 | |
1e1fafb9 | 273 | int wxSlider::GetSelStart() const |
c801d85f | 274 | { |
2d17d68f | 275 | return 0; |
6de97a3b | 276 | } |
c801d85f | 277 | |
1e1fafb9 | 278 | void wxSlider::ClearSel() |
c801d85f | 279 | { |
6de97a3b | 280 | } |
c801d85f | 281 | |
b4071e91 RR |
282 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
283 | { | |
2d17d68f RR |
284 | GtkRange *range = GTK_RANGE(m_widget); |
285 | return ( (window == GTK_WIDGET(range)->window) || | |
286 | (window == range->trough) || | |
f03fc89f VZ |
287 | (window == range->slider) || |
288 | (window == range->step_forw) || | |
289 | (window == range->step_back) ); | |
b4071e91 RR |
290 | } |
291 | ||
58614078 RR |
292 | void wxSlider::ApplyWidgetStyle() |
293 | { | |
2d17d68f RR |
294 | SetWidgetStyle(); |
295 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
58614078 | 296 | } |
dcf924a3 RR |
297 | |
298 | #endif |