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