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