Added wxRadioButton (not tested)
[wxWidgets.git] / src / gtk1 / 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 // wxSlider
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 = wxHORIZONTAL;
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 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
63
64 wxSlider::wxSlider(void)
65 {
66 }
67
68 wxSlider::~wxSlider(void)
69 {
70 }
71
72 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
73 int value, int minValue, int maxValue,
74 const wxPoint& pos, const wxSize& size,
75 long style, const wxValidator& validator, const wxString& name )
76 {
77 m_needParent = TRUE;
78
79 PreCreation( parent, id, pos, size, style, name );
80
81 SetValidator( validator );
82
83 m_oldPos = 0.0;
84
85 if (style & wxSL_VERTICAL == wxSL_VERTICAL)
86 m_widget = gtk_hscale_new( NULL );
87 else
88 m_widget = gtk_vscale_new( NULL );
89
90 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
91
92 gtk_signal_connect (GTK_OBJECT (m_adjust), "value_changed",
93 (GtkSignalFunc) gtk_slider_callback, (gpointer) this );
94 SetRange( minValue, maxValue );
95 SetValue( value );
96
97 PostCreation();
98
99 Show( TRUE );
100
101 return TRUE;
102 }
103
104 int wxSlider::GetValue(void) const
105 {
106 return (int)(m_adjust->value+0.5);
107 }
108
109 void wxSlider::SetValue( int value )
110 {
111 float fpos = (float)value;
112 m_oldPos = fpos;
113 if (fabs(fpos-m_adjust->value) < 0.2) return;
114 m_adjust->value = fpos;
115
116 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
117 }
118
119 void wxSlider::SetRange( int minValue, int maxValue )
120 {
121 float fmin = (float)minValue;
122 float fmax = (float)maxValue;
123
124 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
125 (fabs(fmax-m_adjust->upper) < 0.2))
126 return;
127
128 m_adjust->lower = fmin;
129 m_adjust->upper = fmax;
130
131 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
132 }
133
134 int wxSlider::GetMin(void) const
135 {
136 return (int)(m_adjust->lower+0.5);
137 }
138
139 int wxSlider::GetMax(void) const
140 {
141 return (int)(m_adjust->upper+0.5);
142 }
143
144 void wxSlider::SetPageSize( int pageSize )
145 {
146 float fpage = (float)pageSize;
147
148 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
149
150 m_adjust->page_increment = fpage;
151
152 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
153 }
154
155 int wxSlider::GetPageSize(void) const
156 {
157 return (int)(m_adjust->page_increment+0.5);
158 }
159
160 void wxSlider::SetThumbLength( int len )
161 {
162 float flen = (float)len;
163
164 if (fabs(flen-m_adjust->page_size) < 0.2) return;
165
166 m_adjust->page_size = flen;
167
168 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
169 }
170
171 int wxSlider::GetThumbLength(void) const
172 {
173 return (int)(m_adjust->page_size+0.5);
174 }
175
176 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
177 {
178 }
179
180 int wxSlider::GetLineSize(void) const
181 {
182 return 0;
183 }
184
185 // not supported in wxGTK (and GTK)
186
187 void wxSlider::GetSize( int *x, int *y ) const
188 {
189 wxWindow::GetSize( x, y );
190 }
191
192 void wxSlider::SetSize( int x, int y, int width, int height, int sizeFlags )
193 {
194 wxWindow::SetSize( x, y, width, height, sizeFlags );
195 }
196
197 void wxSlider::GetPosition( int *x, int *y ) const
198 {
199 wxWindow::GetPosition( x, y );
200 }
201
202 void wxSlider::SetTick( int WXUNUSED(tickPos) )
203 {
204 }
205
206 void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
207 {
208 }
209
210 int wxSlider::GetTickFreq(void) const
211 {
212 return 0;
213 }
214
215 void wxSlider::ClearTicks(void)
216 {
217 }
218
219 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
220 {
221 }
222
223 int wxSlider::GetSelEnd(void) const
224 {
225 return 0;
226 }
227
228 int wxSlider::GetSelStart(void) const
229 {
230 return 0;
231 }
232
233 void wxSlider::ClearSel(void)
234 {
235 }
236