]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/slider.cpp
Use GetFont not m_font for GetCharHeight
[wxWidgets.git] / src / gtk / slider.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/slider.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/slider.h"
14
15 #if wxUSE_SLIDER
16
17 #include "wx/utils.h"
18 #include "wx/math.h"
19 #include "wx/gtk/private.h"
20
21 //-----------------------------------------------------------------------------
22 // idle system
23 //-----------------------------------------------------------------------------
24
25 extern void wxapp_install_idle_handler();
26 extern bool g_isIdle;
27
28 //-----------------------------------------------------------------------------
29 // data
30 //-----------------------------------------------------------------------------
31
32 extern bool g_blockEventsOnDrag;
33
34 static const float sensitivity = 0.02;
35
36 //-----------------------------------------------------------------------------
37 // "value_changed"
38 //-----------------------------------------------------------------------------
39
40 extern "C" {
41 static void gtk_slider_callback( GtkAdjustment *adjust,
42 SCROLLBAR_CBACK_ARG
43 wxSlider *win )
44 {
45 if (g_isIdle) wxapp_install_idle_handler();
46
47 if (!win->m_hasVMT) return;
48 if (g_blockEventsOnDrag) return;
49
50 float diff = adjust->value - win->m_oldPos;
51 if (fabs(diff) < sensitivity) return;
52
53 win->m_oldPos = adjust->value;
54
55 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
56
57 double dvalue = adjust->value;
58 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
59
60 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
61 : wxHORIZONTAL;
62
63 wxScrollEvent event( command, win->GetId(), value, orient );
64 event.SetEventObject( win );
65 win->GetEventHandler()->ProcessEvent( event );
66
67 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
68 cevent.SetEventObject( win );
69 cevent.SetInt( value );
70 win->GetEventHandler()->ProcessEvent( cevent );
71 }
72 }
73
74 //-----------------------------------------------------------------------------
75 // wxSlider
76 //-----------------------------------------------------------------------------
77
78 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
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 if (!PreCreation( parent, pos, size ) ||
89 !CreateBase( parent, id, pos, size, style, validator, name ))
90 {
91 wxFAIL_MSG( wxT("wxSlider creation failed") );
92 return FALSE;
93 }
94
95 m_oldPos = 0.0;
96
97 if (style & wxSL_VERTICAL)
98 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
99 else
100 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
101
102 if (style & wxSL_LABELS)
103 {
104 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
105 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
106
107 /* labels need more space and too small window will
108 cause junk to appear on the dialog */
109 if (style & wxSL_VERTICAL)
110 {
111 wxSize sz( size );
112 if (sz.x < 35)
113 {
114 sz.x = 35;
115 SetSize( sz );
116 }
117 }
118 else
119 {
120 wxSize sz( size );
121 if (sz.y < 35)
122 {
123 sz.y = 35;
124 SetSize( sz );
125 }
126 }
127 }
128 else
129 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
130
131 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
132
133 #ifdef __WXGTK20__
134 if (style & wxSL_INVERSE)
135 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
136 #endif
137
138 GtkEnableEvents();
139
140 SetRange( minValue, maxValue );
141 SetValue( value );
142
143 m_parent->DoAddChild( this );
144
145 PostCreation(size);
146
147 return TRUE;
148 }
149
150 int wxSlider::GetValue() const
151 {
152 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
153 // -0.9 is rounded to -1
154 double val = m_adjust->value;
155 return (int)(val < 0 ? val - 0.5 : val + 0.5);
156 }
157
158 void wxSlider::SetValue( int value )
159 {
160 float fpos = (float)value;
161 m_oldPos = fpos;
162 if (fabs(fpos-m_adjust->value) < 0.2) return;
163
164 m_adjust->value = fpos;
165
166 GtkDisableEvents();
167
168 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
169
170 GtkEnableEvents();
171 }
172
173 void wxSlider::SetRange( int minValue, int maxValue )
174 {
175 float fmin = (float)minValue;
176 float fmax = (float)maxValue;
177
178 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
179 (fabs(fmax-m_adjust->upper) < 0.2))
180 {
181 return;
182 }
183
184 m_adjust->lower = fmin;
185 m_adjust->upper = fmax;
186 m_adjust->step_increment = 1.0;
187 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
188
189 GtkDisableEvents();
190
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
192
193 GtkEnableEvents();
194 }
195
196 int wxSlider::GetMin() const
197 {
198 return (int)ceil(m_adjust->lower);
199 }
200
201 int wxSlider::GetMax() const
202 {
203 return (int)ceil(m_adjust->upper);
204 }
205
206 void wxSlider::SetPageSize( int pageSize )
207 {
208 float fpage = (float)pageSize;
209
210 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
211
212 m_adjust->page_increment = fpage;
213
214 GtkDisableEvents();
215
216 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
217
218 GtkEnableEvents();
219 }
220
221 int wxSlider::GetPageSize() const
222 {
223 return (int)ceil(m_adjust->page_increment);
224 }
225
226 void wxSlider::SetThumbLength( int len )
227 {
228 float flen = (float)len;
229
230 if (fabs(flen-m_adjust->page_size) < 0.2) return;
231
232 m_adjust->page_size = flen;
233
234 GtkDisableEvents();
235
236 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
237
238 GtkEnableEvents();
239 }
240
241 int wxSlider::GetThumbLength() const
242 {
243 return (int)ceil(m_adjust->page_size);
244 }
245
246 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
247 {
248 }
249
250 int wxSlider::GetLineSize() const
251 {
252 return 0;
253 }
254
255 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
256 {
257 GtkRange *range = GTK_RANGE(m_widget);
258 #ifdef __WXGTK20__
259 return (range->event_window == window);
260 #else
261 return ( (window == GTK_WIDGET(range)->window)
262 || (window == range->trough)
263 || (window == range->slider)
264 || (window == range->step_forw)
265 || (window == range->step_back) );
266 #endif
267 }
268
269 void wxSlider::GtkDisableEvents()
270 {
271 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
272 GTK_SIGNAL_FUNC(gtk_slider_callback),
273 (gpointer) this );
274 }
275
276 void wxSlider::GtkEnableEvents()
277 {
278 gtk_signal_connect( GTK_OBJECT (m_adjust),
279 "value_changed",
280 GTK_SIGNAL_FUNC(gtk_slider_callback),
281 (gpointer) this );
282 }
283
284 // static
285 wxVisualAttributes
286 wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
287 {
288 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
289 }
290
291 #endif