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