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