]>
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 ) |
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; |
e1811a01 RR |
47 | |
48 | float diff = adjust->value - win->m_oldPos; | |
2d17d68f | 49 | if (fabs(diff) < 0.2) return; |
e1811a01 RR |
50 | |
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; | |
60 | ||
61 | int value = (int)ceil(adjust->value); | |
c801d85f | 62 | |
2d17d68f RR |
63 | int orient = wxHORIZONTAL; |
64 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; | |
c801d85f | 65 | |
2d17d68f RR |
66 | wxScrollEvent event( command, win->GetId(), value, orient ); |
67 | event.SetEventObject( win ); | |
828f655f | 68 | win->GetEventHandler()->ProcessEvent( event ); |
c801d85f | 69 | |
2d17d68f RR |
70 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); |
71 | cevent.SetEventObject( win ); | |
828f655f | 72 | win->GetEventHandler()->ProcessEvent( cevent ); |
6de97a3b | 73 | } |
c801d85f | 74 | |
97b3455a RR |
75 | //----------------------------------------------------------------------------- |
76 | // wxSlider | |
77 | //----------------------------------------------------------------------------- | |
78 | ||
c801d85f KB |
79 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
80 | ||
81 | wxSlider::wxSlider(void) | |
82 | { | |
6de97a3b | 83 | } |
c801d85f KB |
84 | |
85 | wxSlider::~wxSlider(void) | |
86 | { | |
6de97a3b | 87 | } |
c801d85f | 88 | |
debe6624 JS |
89 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
90 | int value, int minValue, int maxValue, | |
c801d85f | 91 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 92 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 93 | { |
b292e2f5 | 94 | m_acceptsFocus = TRUE; |
2d17d68f | 95 | m_needParent = TRUE; |
c801d85f | 96 | |
4dcaf11a RR |
97 | if (!PreCreation( parent, pos, size ) || |
98 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
99 | { | |
e90c1d2a | 100 | wxFAIL_MSG( T("wxSlider creation failed") ); |
4dcaf11a RR |
101 | return FALSE; |
102 | } | |
6de97a3b | 103 | |
2d17d68f | 104 | m_oldPos = 0.0; |
c801d85f | 105 | |
2e563988 | 106 | if (style & wxSL_VERTICAL) |
2d17d68f | 107 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
19da4326 RR |
108 | else |
109 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); | |
c801d85f | 110 | |
2e563988 | 111 | if (style & wxSL_LABELS) |
858b5bdd | 112 | { |
2e563988 | 113 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
f03fc89f VZ |
114 | |
115 | /* labels need more space and too small window will | |
116 | cause junk to appear on the dialog */ | |
858b5bdd | 117 | if (style & wxSL_VERTICAL) |
f03fc89f VZ |
118 | { |
119 | wxSize sz( size ); | |
120 | if (sz.x < 35) | |
121 | { | |
122 | sz.x = 35; | |
123 | SetSize( sz ); | |
124 | } | |
125 | } | |
858b5bdd | 126 | else |
f03fc89f VZ |
127 | { |
128 | wxSize sz( size ); | |
129 | if (sz.y < 35) | |
130 | { | |
131 | sz.y = 35; | |
132 | SetSize( sz ); | |
133 | } | |
134 | } | |
858b5bdd | 135 | } |
2e563988 RR |
136 | else |
137 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); | |
19da4326 | 138 | |
2d17d68f | 139 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
c801d85f | 140 | |
2d17d68f RR |
141 | gtk_signal_connect( GTK_OBJECT(m_adjust), |
142 | "value_changed", | |
f03fc89f VZ |
143 | (GtkSignalFunc) gtk_slider_callback, |
144 | (gpointer) this ); | |
145 | ||
2d17d68f RR |
146 | SetRange( minValue, maxValue ); |
147 | SetValue( value ); | |
c801d85f | 148 | |
f03fc89f | 149 | m_parent->DoAddChild( this ); |
6ca41e57 | 150 | |
2d17d68f | 151 | PostCreation(); |
c801d85f | 152 | |
2d17d68f | 153 | SetBackgroundColour( parent->GetBackgroundColour() ); |
f96aa4d9 | 154 | |
2d17d68f | 155 | Show( TRUE ); |
c801d85f | 156 | |
2d17d68f | 157 | return TRUE; |
6de97a3b | 158 | } |
c801d85f KB |
159 | |
160 | int wxSlider::GetValue(void) const | |
161 | { | |
2d17d68f | 162 | return (int)(m_adjust->value+0.5); |
6de97a3b | 163 | } |
c801d85f | 164 | |
debe6624 | 165 | void wxSlider::SetValue( int value ) |
c801d85f | 166 | { |
2d17d68f RR |
167 | float fpos = (float)value; |
168 | m_oldPos = fpos; | |
169 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
170 | ||
171 | m_adjust->value = fpos; | |
c801d85f | 172 | |
2d17d68f | 173 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
6de97a3b | 174 | } |
c801d85f | 175 | |
debe6624 | 176 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f | 177 | { |
2d17d68f RR |
178 | float fmin = (float)minValue; |
179 | float fmax = (float)maxValue; | |
c801d85f | 180 | |
2d17d68f RR |
181 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
182 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
183 | { | |
184 | return; | |
185 | } | |
c801d85f | 186 | |
2d17d68f RR |
187 | m_adjust->lower = fmin; |
188 | m_adjust->upper = fmax; | |
c801d85f | 189 | |
2d17d68f | 190 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 191 | } |
c801d85f KB |
192 | |
193 | int wxSlider::GetMin(void) const | |
194 | { | |
b1a39f47 | 195 | return (int)ceil(m_adjust->lower); |
6de97a3b | 196 | } |
c801d85f KB |
197 | |
198 | int wxSlider::GetMax(void) const | |
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; |
c801d85f | 206 | |
2d17d68f | 207 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
c801d85f | 208 | |
2d17d68f | 209 | m_adjust->page_increment = fpage; |
c801d85f | 210 | |
2d17d68f | 211 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 212 | } |
c801d85f KB |
213 | |
214 | int wxSlider::GetPageSize(void) const | |
215 | { | |
b1a39f47 | 216 | return (int)ceil(m_adjust->page_increment); |
6de97a3b | 217 | } |
c801d85f | 218 | |
debe6624 | 219 | void wxSlider::SetThumbLength( int len ) |
c801d85f | 220 | { |
2d17d68f | 221 | float flen = (float)len; |
c801d85f | 222 | |
2d17d68f | 223 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
c801d85f | 224 | |
2d17d68f | 225 | m_adjust->page_size = flen; |
c801d85f | 226 | |
2d17d68f | 227 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
6de97a3b | 228 | } |
c801d85f KB |
229 | |
230 | int wxSlider::GetThumbLength(void) const | |
231 | { | |
b1a39f47 | 232 | return (int)ceil(m_adjust->page_size); |
6de97a3b | 233 | } |
c801d85f | 234 | |
debe6624 | 235 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
c801d85f | 236 | { |
6de97a3b | 237 | } |
c801d85f KB |
238 | |
239 | int wxSlider::GetLineSize(void) const | |
240 | { | |
2d17d68f | 241 | return 0; |
6de97a3b | 242 | } |
c801d85f | 243 | |
debe6624 | 244 | void wxSlider::SetTick( int WXUNUSED(tickPos) ) |
c801d85f | 245 | { |
6de97a3b | 246 | } |
c801d85f | 247 | |
debe6624 | 248 | void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) ) |
c801d85f | 249 | { |
6de97a3b | 250 | } |
c801d85f KB |
251 | |
252 | int wxSlider::GetTickFreq(void) const | |
253 | { | |
2d17d68f | 254 | return 0; |
6de97a3b | 255 | } |
c801d85f KB |
256 | |
257 | void wxSlider::ClearTicks(void) | |
258 | { | |
6de97a3b | 259 | } |
c801d85f | 260 | |
debe6624 | 261 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) |
c801d85f | 262 | { |
6de97a3b | 263 | } |
c801d85f KB |
264 | |
265 | int wxSlider::GetSelEnd(void) const | |
266 | { | |
2d17d68f | 267 | return 0; |
6de97a3b | 268 | } |
c801d85f KB |
269 | |
270 | int wxSlider::GetSelStart(void) const | |
271 | { | |
2d17d68f | 272 | return 0; |
6de97a3b | 273 | } |
c801d85f KB |
274 | |
275 | void wxSlider::ClearSel(void) | |
276 | { | |
6de97a3b | 277 | } |
c801d85f | 278 | |
b4071e91 RR |
279 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
280 | { | |
2d17d68f RR |
281 | GtkRange *range = GTK_RANGE(m_widget); |
282 | return ( (window == GTK_WIDGET(range)->window) || | |
283 | (window == range->trough) || | |
f03fc89f VZ |
284 | (window == range->slider) || |
285 | (window == range->step_forw) || | |
286 | (window == range->step_back) ); | |
b4071e91 RR |
287 | } |
288 | ||
58614078 RR |
289 | void wxSlider::ApplyWidgetStyle() |
290 | { | |
2d17d68f RR |
291 | SetWidgetStyle(); |
292 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
58614078 | 293 | } |
dcf924a3 RR |
294 | |
295 | #endif |