]>
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 | ||
66bd6b93 | 41 | static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win ) |
c801d85f | 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; |
c801d85f | 47 | |
2d17d68f RR |
48 | float diff = win->m_adjust->value - win->m_oldPos; |
49 | if (fabs(diff) < 0.2) return; | |
828f655f | 50 | win->m_oldPos = win->m_adjust->value; |
c801d85f | 51 | |
2d17d68f | 52 | wxEventType command = wxEVT_NULL; |
c801d85f | 53 | |
2d17d68f RR |
54 | float line_step = win->m_adjust->step_increment; |
55 | float page_step = win->m_adjust->page_increment; | |
c801d85f | 56 | |
2d17d68f RR |
57 | if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM; |
58 | else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP; | |
59 | else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN; | |
60 | else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP; | |
61 | else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN; | |
62 | else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP; | |
63 | else command = wxEVT_SCROLL_THUMBTRACK; | |
64 | ||
b1a39f47 | 65 | int value = (int)ceil(win->m_adjust->value); |
c801d85f | 66 | |
2d17d68f RR |
67 | int orient = wxHORIZONTAL; |
68 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; | |
c801d85f | 69 | |
2d17d68f RR |
70 | wxScrollEvent event( command, win->GetId(), value, orient ); |
71 | event.SetEventObject( win ); | |
828f655f | 72 | win->GetEventHandler()->ProcessEvent( event ); |
c801d85f | 73 | |
2d17d68f RR |
74 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); |
75 | cevent.SetEventObject( win ); | |
828f655f | 76 | win->GetEventHandler()->ProcessEvent( cevent ); |
6de97a3b | 77 | } |
c801d85f | 78 | |
97b3455a RR |
79 | //----------------------------------------------------------------------------- |
80 | // wxSlider | |
81 | //----------------------------------------------------------------------------- | |
82 | ||
c801d85f KB |
83 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
84 | ||
85 | wxSlider::wxSlider(void) | |
86 | { | |
6de97a3b | 87 | } |
c801d85f KB |
88 | |
89 | wxSlider::~wxSlider(void) | |
90 | { | |
6de97a3b | 91 | } |
c801d85f | 92 | |
debe6624 JS |
93 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
94 | int value, int minValue, int maxValue, | |
c801d85f | 95 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 96 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 97 | { |
b292e2f5 | 98 | m_acceptsFocus = TRUE; |
2d17d68f | 99 | m_needParent = TRUE; |
c801d85f | 100 | |
2d17d68f | 101 | PreCreation( parent, id, pos, size, style, name ); |
c801d85f | 102 | |
ce4169a4 | 103 | #if wxUSE_VALIDATORS |
2d17d68f | 104 | SetValidator( validator ); |
ce4169a4 | 105 | #endif |
6de97a3b | 106 | |
2d17d68f | 107 | m_oldPos = 0.0; |
c801d85f | 108 | |
2e563988 | 109 | if (style & wxSL_VERTICAL) |
2d17d68f | 110 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
111 | else |
112 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
c801d85f | 113 | |
2e563988 | 114 | if (style & wxSL_LABELS) |
858b5bdd | 115 | { |
2e563988 | 116 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
f03fc89f VZ |
117 | |
118 | /* labels need more space and too small window will | |
119 | cause junk to appear on the dialog */ | |
858b5bdd | 120 | if (style & wxSL_VERTICAL) |
f03fc89f VZ |
121 | { |
122 | wxSize sz( size ); | |
123 | if (sz.x < 35) | |
124 | { | |
125 | sz.x = 35; | |
126 | SetSize( sz ); | |
127 | } | |
128 | } | |
858b5bdd | 129 | else |
f03fc89f VZ |
130 | { |
131 | wxSize sz( size ); | |
132 | if (sz.y < 35) | |
133 | { | |
134 | sz.y = 35; | |
135 | SetSize( sz ); | |
136 | } | |
137 | } | |
858b5bdd | 138 | } |
2e563988 RR |
139 | else |
140 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
19da4326 | 141 | |
2d17d68f | 142 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
c801d85f | 143 | |
2d17d68f RR |
144 | gtk_signal_connect( GTK_OBJECT(m_adjust), |
145 | "value_changed", | |
f03fc89f VZ |
146 | (GtkSignalFunc) gtk_slider_callback, |
147 | (gpointer) this ); | |
148 | ||
2d17d68f RR |
149 | SetRange( minValue, maxValue ); |
150 | SetValue( value ); | |
c801d85f | 151 | |
f03fc89f | 152 | m_parent->DoAddChild( this ); |
6ca41e57 | 153 | |
2d17d68f | 154 | PostCreation(); |
c801d85f | 155 | |
2d17d68f | 156 | SetBackgroundColour( parent->GetBackgroundColour() ); |
f96aa4d9 | 157 | |
2d17d68f | 158 | Show( TRUE ); |
c801d85f | 159 | |
2d17d68f | 160 | return TRUE; |
6de97a3b | 161 | } |
c801d85f KB |
162 | |
163 | int wxSlider::GetValue(void) const | |
164 | { | |
2d17d68f | 165 | return (int)(m_adjust->value+0.5); |
6de97a3b | 166 | } |
c801d85f | 167 | |
debe6624 | 168 | void wxSlider::SetValue( int value ) |
c801d85f | 169 | { |
2d17d68f RR |
170 | float fpos = (float)value; |
171 | m_oldPos = fpos; | |
172 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
173 | ||
174 | m_adjust->value = fpos; | |
c801d85f | 175 | |
2d17d68f | 176 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
6de97a3b | 177 | } |
c801d85f | 178 | |
debe6624 | 179 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 180 | { |
2d17d68f RR |
181 | float fmin = (float)minValue; |
182 | float fmax = (float)maxValue; | |
c801d85f | 183 | |
2d17d68f RR |
184 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
185 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
186 | { | |
187 | return; | |
188 | } | |
c801d85f | 189 | |
2d17d68f RR |
190 | m_adjust->lower = fmin; |
191 | m_adjust->upper = fmax; | |
c801d85f | 192 | |
2d17d68f | 193 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 194 | } |
c801d85f KB |
195 | |
196 | int wxSlider::GetMin(void) const | |
197 | { | |
b1a39f47 | 198 | return (int)ceil(m_adjust->lower); |
6de97a3b | 199 | } |
c801d85f KB |
200 | |
201 | int wxSlider::GetMax(void) const | |
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; |
c801d85f | 209 | |
2d17d68f | 210 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
c801d85f | 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 KB |
216 | |
217 | int wxSlider::GetPageSize(void) const | |
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; |
c801d85f | 225 | |
2d17d68f | 226 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
c801d85f | 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 KB |
232 | |
233 | int wxSlider::GetThumbLength(void) const | |
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 KB |
241 | |
242 | int wxSlider::GetLineSize(void) const | |
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 KB |
254 | |
255 | int wxSlider::GetTickFreq(void) const | |
256 | { | |
2d17d68f | 257 | return 0; |
6de97a3b | 258 | } |
c801d85f KB |
259 | |
260 | void wxSlider::ClearTicks(void) | |
261 | { | |
6de97a3b | 262 | } |
c801d85f | 263 | |
debe6624 | 264 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) |
c801d85f | 265 | { |
6de97a3b | 266 | } |
c801d85f KB |
267 | |
268 | int wxSlider::GetSelEnd(void) const | |
269 | { | |
2d17d68f | 270 | return 0; |
6de97a3b | 271 | } |
c801d85f KB |
272 | |
273 | int wxSlider::GetSelStart(void) const | |
274 | { | |
2d17d68f | 275 | return 0; |
6de97a3b | 276 | } |
c801d85f KB |
277 | |
278 | void wxSlider::ClearSel(void) | |
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 |