]>
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 | |
4dcaf11a RR |
101 | if (!PreCreation( parent, pos, size ) || |
102 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
103 | { | |
104 | wxFAIL_MSG( _T("wxSlider creation failed") ); | |
105 | return FALSE; | |
106 | } | |
6de97a3b | 107 | |
2d17d68f | 108 | m_oldPos = 0.0; |
c801d85f | 109 | |
2e563988 | 110 | if (style & wxSL_VERTICAL) |
2d17d68f | 111 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
112 | else |
113 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
c801d85f | 114 | |
2e563988 | 115 | if (style & wxSL_LABELS) |
858b5bdd | 116 | { |
2e563988 | 117 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
f03fc89f VZ |
118 | |
119 | /* labels need more space and too small window will | |
120 | cause junk to appear on the dialog */ | |
858b5bdd | 121 | if (style & wxSL_VERTICAL) |
f03fc89f VZ |
122 | { |
123 | wxSize sz( size ); | |
124 | if (sz.x < 35) | |
125 | { | |
126 | sz.x = 35; | |
127 | SetSize( sz ); | |
128 | } | |
129 | } | |
858b5bdd | 130 | else |
f03fc89f VZ |
131 | { |
132 | wxSize sz( size ); | |
133 | if (sz.y < 35) | |
134 | { | |
135 | sz.y = 35; | |
136 | SetSize( sz ); | |
137 | } | |
138 | } | |
858b5bdd | 139 | } |
2e563988 RR |
140 | else |
141 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
19da4326 | 142 | |
2d17d68f | 143 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
c801d85f | 144 | |
2d17d68f RR |
145 | gtk_signal_connect( GTK_OBJECT(m_adjust), |
146 | "value_changed", | |
f03fc89f VZ |
147 | (GtkSignalFunc) gtk_slider_callback, |
148 | (gpointer) this ); | |
149 | ||
2d17d68f RR |
150 | SetRange( minValue, maxValue ); |
151 | SetValue( value ); | |
c801d85f | 152 | |
f03fc89f | 153 | m_parent->DoAddChild( this ); |
6ca41e57 | 154 | |
2d17d68f | 155 | PostCreation(); |
c801d85f | 156 | |
2d17d68f | 157 | SetBackgroundColour( parent->GetBackgroundColour() ); |
f96aa4d9 | 158 | |
2d17d68f | 159 | Show( TRUE ); |
c801d85f | 160 | |
2d17d68f | 161 | return TRUE; |
6de97a3b | 162 | } |
c801d85f KB |
163 | |
164 | int wxSlider::GetValue(void) const | |
165 | { | |
2d17d68f | 166 | return (int)(m_adjust->value+0.5); |
6de97a3b | 167 | } |
c801d85f | 168 | |
debe6624 | 169 | void wxSlider::SetValue( int value ) |
c801d85f | 170 | { |
2d17d68f RR |
171 | float fpos = (float)value; |
172 | m_oldPos = fpos; | |
173 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
174 | ||
175 | m_adjust->value = fpos; | |
c801d85f | 176 | |
2d17d68f | 177 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
6de97a3b | 178 | } |
c801d85f | 179 | |
debe6624 | 180 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 181 | { |
2d17d68f RR |
182 | float fmin = (float)minValue; |
183 | float fmax = (float)maxValue; | |
c801d85f | 184 | |
2d17d68f RR |
185 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
186 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
187 | { | |
188 | return; | |
189 | } | |
c801d85f | 190 | |
2d17d68f RR |
191 | m_adjust->lower = fmin; |
192 | m_adjust->upper = fmax; | |
c801d85f | 193 | |
2d17d68f | 194 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 195 | } |
c801d85f KB |
196 | |
197 | int wxSlider::GetMin(void) const | |
198 | { | |
b1a39f47 | 199 | return (int)ceil(m_adjust->lower); |
6de97a3b | 200 | } |
c801d85f KB |
201 | |
202 | int wxSlider::GetMax(void) const | |
203 | { | |
b1a39f47 | 204 | return (int)ceil(m_adjust->upper); |
6de97a3b | 205 | } |
c801d85f | 206 | |
debe6624 | 207 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f | 208 | { |
2d17d68f | 209 | float fpage = (float)pageSize; |
c801d85f | 210 | |
2d17d68f | 211 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
c801d85f | 212 | |
2d17d68f | 213 | m_adjust->page_increment = fpage; |
c801d85f | 214 | |
2d17d68f | 215 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 216 | } |
c801d85f KB |
217 | |
218 | int wxSlider::GetPageSize(void) const | |
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; |
c801d85f | 226 | |
2d17d68f | 227 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
c801d85f | 228 | |
2d17d68f | 229 | m_adjust->page_size = flen; |
c801d85f | 230 | |
2d17d68f | 231 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 232 | } |
c801d85f KB |
233 | |
234 | int wxSlider::GetThumbLength(void) const | |
235 | { | |
b1a39f47 | 236 | return (int)ceil(m_adjust->page_size); |
6de97a3b | 237 | } |
c801d85f | 238 | |
debe6624 | 239 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
c801d85f | 240 | { |
6de97a3b | 241 | } |
c801d85f KB |
242 | |
243 | int wxSlider::GetLineSize(void) const | |
244 | { | |
2d17d68f | 245 | return 0; |
6de97a3b | 246 | } |
c801d85f | 247 | |
debe6624 | 248 | void wxSlider::SetTick( int WXUNUSED(tickPos) ) |
c801d85f | 249 | { |
6de97a3b | 250 | } |
c801d85f | 251 | |
debe6624 | 252 | void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) ) |
c801d85f | 253 | { |
6de97a3b | 254 | } |
c801d85f KB |
255 | |
256 | int wxSlider::GetTickFreq(void) const | |
257 | { | |
2d17d68f | 258 | return 0; |
6de97a3b | 259 | } |
c801d85f KB |
260 | |
261 | void wxSlider::ClearTicks(void) | |
262 | { | |
6de97a3b | 263 | } |
c801d85f | 264 | |
debe6624 | 265 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) |
c801d85f | 266 | { |
6de97a3b | 267 | } |
c801d85f KB |
268 | |
269 | int wxSlider::GetSelEnd(void) const | |
270 | { | |
2d17d68f | 271 | return 0; |
6de97a3b | 272 | } |
c801d85f KB |
273 | |
274 | int wxSlider::GetSelStart(void) const | |
275 | { | |
2d17d68f | 276 | return 0; |
6de97a3b | 277 | } |
c801d85f KB |
278 | |
279 | void wxSlider::ClearSel(void) | |
280 | { | |
6de97a3b | 281 | } |
c801d85f | 282 | |
b4071e91 RR |
283 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
284 | { | |
2d17d68f RR |
285 | GtkRange *range = GTK_RANGE(m_widget); |
286 | return ( (window == GTK_WIDGET(range)->window) || | |
287 | (window == range->trough) || | |
f03fc89f VZ |
288 | (window == range->slider) || |
289 | (window == range->step_forw) || | |
290 | (window == range->step_back) ); | |
b4071e91 RR |
291 | } |
292 | ||
58614078 RR |
293 | void wxSlider::ApplyWidgetStyle() |
294 | { | |
2d17d68f RR |
295 | SetWidgetStyle(); |
296 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
58614078 | 297 | } |
dcf924a3 RR |
298 | |
299 | #endif |