]>
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 | |
c801d85f KB |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
c801d85f KB |
10 | #ifdef __GNUG__ |
11 | #pragma implementation "slider.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/slider.h" | |
15 | #include "wx/utils.h" | |
f5368809 | 16 | #include <math.h> |
c801d85f | 17 | |
66bd6b93 RR |
18 | //----------------------------------------------------------------------------- |
19 | // data | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | extern bool g_blockEventsOnDrag; | |
23 | ||
c801d85f | 24 | //----------------------------------------------------------------------------- |
97b3455a | 25 | // "value_changed" |
c801d85f KB |
26 | //----------------------------------------------------------------------------- |
27 | ||
66bd6b93 | 28 | static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win ) |
c801d85f | 29 | { |
c801d85f | 30 | if (!win->HasVMT()) return; |
66bd6b93 | 31 | if (g_blockEventsOnDrag) return; |
c801d85f KB |
32 | |
33 | float diff = win->m_adjust->value - win->m_oldPos; | |
34 | if (fabs(diff) < 0.2) return; | |
35 | ||
7798a18e | 36 | wxEventType command = wxEVT_NULL; |
c801d85f KB |
37 | |
38 | float line_step = win->m_adjust->step_increment; | |
39 | float page_step = win->m_adjust->page_increment; | |
40 | ||
3bc755fc RR |
41 | if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM; |
42 | else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP; | |
43 | else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN; | |
c801d85f KB |
44 | else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP; |
45 | else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN; | |
46 | else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP; | |
47 | else command = wxEVT_SCROLL_THUMBTRACK; | |
48 | ||
49 | int value = (int)(win->m_adjust->value+0.5); | |
50 | ||
51 | int orient = wxHORIZONTAL; | |
84efdbf1 | 52 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; |
c801d85f KB |
53 | |
54 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
55 | event.SetEventObject( win ); | |
56 | win->ProcessEvent( event ); | |
57 | ||
58 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); | |
59 | cevent.SetEventObject( win ); | |
60 | win->ProcessEvent( cevent ); | |
6de97a3b | 61 | } |
c801d85f | 62 | |
97b3455a RR |
63 | //----------------------------------------------------------------------------- |
64 | // wxSlider | |
65 | //----------------------------------------------------------------------------- | |
66 | ||
c801d85f KB |
67 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
68 | ||
69 | wxSlider::wxSlider(void) | |
70 | { | |
6de97a3b | 71 | } |
c801d85f KB |
72 | |
73 | wxSlider::~wxSlider(void) | |
74 | { | |
6de97a3b | 75 | } |
c801d85f | 76 | |
debe6624 JS |
77 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
78 | int value, int minValue, int maxValue, | |
c801d85f | 79 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 80 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 81 | { |
b292e2f5 | 82 | m_acceptsFocus = TRUE; |
c801d85f KB |
83 | m_needParent = TRUE; |
84 | ||
85 | PreCreation( parent, id, pos, size, style, name ); | |
86 | ||
6de97a3b RR |
87 | SetValidator( validator ); |
88 | ||
c801d85f KB |
89 | m_oldPos = 0.0; |
90 | ||
91 | if (style & wxSL_VERTICAL == wxSL_VERTICAL) | |
c67daf87 | 92 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); |
c801d85f | 93 | else |
c67daf87 | 94 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
c801d85f | 95 | |
58614078 RR |
96 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); |
97 | ||
c801d85f KB |
98 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
99 | ||
100 | gtk_signal_connect (GTK_OBJECT (m_adjust), "value_changed", | |
101 | (GtkSignalFunc) gtk_slider_callback, (gpointer) this ); | |
102 | SetRange( minValue, maxValue ); | |
103 | SetValue( value ); | |
104 | ||
6ca41e57 RR |
105 | m_parent->AddChild( this ); |
106 | ||
107 | (m_parent->m_insertCallback)( m_parent, this ); | |
108 | ||
c801d85f KB |
109 | PostCreation(); |
110 | ||
f96aa4d9 RR |
111 | SetBackgroundColour( parent->GetBackgroundColour() ); |
112 | ||
c801d85f KB |
113 | Show( TRUE ); |
114 | ||
115 | return TRUE; | |
6de97a3b | 116 | } |
c801d85f KB |
117 | |
118 | int wxSlider::GetValue(void) const | |
119 | { | |
120 | return (int)(m_adjust->value+0.5); | |
6de97a3b | 121 | } |
c801d85f | 122 | |
debe6624 | 123 | void wxSlider::SetValue( int value ) |
c801d85f KB |
124 | { |
125 | float fpos = (float)value; | |
126 | m_oldPos = fpos; | |
127 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
128 | m_adjust->value = fpos; | |
129 | ||
130 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
6de97a3b | 131 | } |
c801d85f | 132 | |
debe6624 | 133 | void wxSlider::SetRange( int minValue, int maxValue ) |
c801d85f KB |
134 | { |
135 | float fmin = (float)minValue; | |
136 | float fmax = (float)maxValue; | |
137 | ||
138 | if ((fabs(fmin-m_adjust->lower) < 0.2) && | |
139 | (fabs(fmax-m_adjust->upper) < 0.2)) | |
140 | return; | |
141 | ||
142 | m_adjust->lower = fmin; | |
143 | m_adjust->upper = fmax; | |
144 | ||
145 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
6de97a3b | 146 | } |
c801d85f KB |
147 | |
148 | int wxSlider::GetMin(void) const | |
149 | { | |
150 | return (int)(m_adjust->lower+0.5); | |
6de97a3b | 151 | } |
c801d85f KB |
152 | |
153 | int wxSlider::GetMax(void) const | |
154 | { | |
155 | return (int)(m_adjust->upper+0.5); | |
6de97a3b | 156 | } |
c801d85f | 157 | |
debe6624 | 158 | void wxSlider::SetPageSize( int pageSize ) |
c801d85f KB |
159 | { |
160 | float fpage = (float)pageSize; | |
161 | ||
162 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; | |
163 | ||
164 | m_adjust->page_increment = fpage; | |
165 | ||
166 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
6de97a3b | 167 | } |
c801d85f KB |
168 | |
169 | int wxSlider::GetPageSize(void) const | |
170 | { | |
171 | return (int)(m_adjust->page_increment+0.5); | |
6de97a3b | 172 | } |
c801d85f | 173 | |
debe6624 | 174 | void wxSlider::SetThumbLength( int len ) |
c801d85f KB |
175 | { |
176 | float flen = (float)len; | |
177 | ||
178 | if (fabs(flen-m_adjust->page_size) < 0.2) return; | |
179 | ||
180 | m_adjust->page_size = flen; | |
181 | ||
182 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
6de97a3b | 183 | } |
c801d85f KB |
184 | |
185 | int wxSlider::GetThumbLength(void) const | |
186 | { | |
187 | return (int)(m_adjust->page_size+0.5); | |
6de97a3b | 188 | } |
c801d85f | 189 | |
debe6624 | 190 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
c801d85f | 191 | { |
6de97a3b | 192 | } |
c801d85f KB |
193 | |
194 | int wxSlider::GetLineSize(void) const | |
195 | { | |
196 | return 0; | |
6de97a3b | 197 | } |
c801d85f | 198 | |
debe6624 | 199 | void wxSlider::SetTick( int WXUNUSED(tickPos) ) |
c801d85f | 200 | { |
6de97a3b | 201 | } |
c801d85f | 202 | |
debe6624 | 203 | void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) ) |
c801d85f | 204 | { |
6de97a3b | 205 | } |
c801d85f KB |
206 | |
207 | int wxSlider::GetTickFreq(void) const | |
208 | { | |
209 | return 0; | |
6de97a3b | 210 | } |
c801d85f KB |
211 | |
212 | void wxSlider::ClearTicks(void) | |
213 | { | |
6de97a3b | 214 | } |
c801d85f | 215 | |
debe6624 | 216 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) |
c801d85f | 217 | { |
6de97a3b | 218 | } |
c801d85f KB |
219 | |
220 | int wxSlider::GetSelEnd(void) const | |
221 | { | |
222 | return 0; | |
6de97a3b | 223 | } |
c801d85f KB |
224 | |
225 | int wxSlider::GetSelStart(void) const | |
226 | { | |
227 | return 0; | |
6de97a3b | 228 | } |
c801d85f KB |
229 | |
230 | void wxSlider::ClearSel(void) | |
231 | { | |
6de97a3b | 232 | } |
c801d85f | 233 | |
b4071e91 RR |
234 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
235 | { | |
236 | GtkRange *range = GTK_RANGE(m_widget); | |
237 | return ( (window == GTK_WIDGET(range)->window) || | |
238 | (window == range->trough) || | |
239 | (window == range->slider) || | |
240 | (window == range->step_forw) || | |
241 | (window == range->step_back) ); | |
242 | } | |
243 | ||
58614078 RR |
244 | void wxSlider::ApplyWidgetStyle() |
245 | { | |
246 | SetWidgetStyle(); | |
247 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
248 | } |