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