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