]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
added new files which were generated by bakefile but were not in cvs for some reason
[wxWidgets.git] / src / gtk / slider.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: gtk/slider.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/slider.h"
dcf924a3
RR
14
15#if wxUSE_SLIDER
16
c801d85f 17#include "wx/utils.h"
463c4d71 18#include "wx/math.h"
9e691f46 19#include "wx/gtk/private.h"
83624f79 20
acfd422a
RR
21//-----------------------------------------------------------------------------
22// idle system
23//-----------------------------------------------------------------------------
24
25extern void wxapp_install_idle_handler();
26extern bool g_isIdle;
27
66bd6b93
RR
28//-----------------------------------------------------------------------------
29// data
30//-----------------------------------------------------------------------------
31
1e6feb95 32extern bool g_blockEventsOnDrag;
66bd6b93 33
57a1fd73
RR
34static const float sensitivity = 0.02;
35
c801d85f 36//-----------------------------------------------------------------------------
97b3455a 37// "value_changed"
c801d85f
KB
38//-----------------------------------------------------------------------------
39
865bb325 40extern "C" {
9e691f46
VZ
41static void gtk_slider_callback( GtkAdjustment *adjust,
42 SCROLLBAR_CBACK_ARG
43 wxSlider *win )
80810ca3 44{
acfd422a
RR
45 if (g_isIdle) wxapp_install_idle_handler();
46
a2053b27 47 if (!win->m_hasVMT) return;
2d17d68f 48 if (g_blockEventsOnDrag) return;
e1811a01
RR
49
50 float diff = adjust->value - win->m_oldPos;
57a1fd73 51 if (fabs(diff) < sensitivity) return;
80810ca3 52
e1811a01 53 win->m_oldPos = adjust->value;
2d17d68f 54
9e691f46 55 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
80810ca3 56
0a07a7d8 57 double dvalue = adjust->value;
83c22707 58 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
80810ca3 59
1e6feb95
VZ
60 int orient = win->GetWindowStyleFlag() & wxSL_VERTICAL ? wxVERTICAL
61 : wxHORIZONTAL;
80810ca3 62
2d17d68f
RR
63 wxScrollEvent event( command, win->GetId(), value, orient );
64 event.SetEventObject( win );
828f655f 65 win->GetEventHandler()->ProcessEvent( event );
80810ca3 66
2d17d68f
RR
67 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
68 cevent.SetEventObject( win );
f6bcfd97 69 cevent.SetInt( value );
828f655f 70 win->GetEventHandler()->ProcessEvent( cevent );
6de97a3b 71}
865bb325 72}
c801d85f 73
97b3455a
RR
74//-----------------------------------------------------------------------------
75// wxSlider
76//-----------------------------------------------------------------------------
77
c801d85f
KB
78IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
79
debe6624
JS
80bool wxSlider::Create(wxWindow *parent, wxWindowID id,
81 int value, int minValue, int maxValue,
c801d85f 82 const wxPoint& pos, const wxSize& size,
6de97a3b 83 long style, const wxValidator& validator, const wxString& name )
c801d85f 84{
b292e2f5 85 m_acceptsFocus = TRUE;
2d17d68f 86 m_needParent = TRUE;
80810ca3 87
4dcaf11a
RR
88 if (!PreCreation( parent, pos, size ) ||
89 !CreateBase( parent, id, pos, size, style, validator, name ))
90 {
223d09f6 91 wxFAIL_MSG( wxT("wxSlider creation failed") );
ef7a25a7 92 return FALSE;
4dcaf11a 93 }
6de97a3b 94
2d17d68f 95 m_oldPos = 0.0;
c801d85f 96
2e563988 97 if (style & wxSL_VERTICAL)
2d17d68f 98 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
19da4326
RR
99 else
100 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
80810ca3 101
2e563988 102 if (style & wxSL_LABELS)
858b5bdd 103 {
2e563988 104 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
ef7a25a7 105 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 106
f03fc89f
VZ
107 /* labels need more space and too small window will
108 cause junk to appear on the dialog */
858b5bdd 109 if (style & wxSL_VERTICAL)
f03fc89f
VZ
110 {
111 wxSize sz( size );
112 if (sz.x < 35)
113 {
114 sz.x = 35;
115 SetSize( sz );
116 }
117 }
858b5bdd 118 else
f03fc89f
VZ
119 {
120 wxSize sz( size );
121 if (sz.y < 35)
122 {
123 sz.y = 35;
124 SetSize( sz );
125 }
126 }
858b5bdd 127 }
2e563988
RR
128 else
129 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 130
2d17d68f 131 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3 132
3baba4c6 133#ifdef __WXGTK20__
1e219378
KH
134 if (style & wxSL_INVERSE)
135 gtk_range_set_inverted( GTK_RANGE(m_widget), TRUE );
3baba4c6 136#endif
1e219378 137
91b167dd
JS
138 GtkEnableEvents();
139
2d17d68f
RR
140 SetRange( minValue, maxValue );
141 SetValue( value );
80810ca3 142
f03fc89f 143 m_parent->DoAddChild( this );
80810ca3 144
abdeb9e7 145 PostCreation(size);
80810ca3 146
2d17d68f 147 return TRUE;
6de97a3b 148}
c801d85f 149
1e1fafb9 150int wxSlider::GetValue() const
c801d85f 151{
ef7a25a7
VZ
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;
83c22707 155 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 156}
c801d85f 157
debe6624 158void wxSlider::SetValue( int value )
c801d85f 159{
2d17d68f
RR
160 float fpos = (float)value;
161 m_oldPos = fpos;
162 if (fabs(fpos-m_adjust->value) < 0.2) return;
80810ca3 163
2d17d68f 164 m_adjust->value = fpos;
80810ca3 165
e1b93ccb
RR
166 GtkDisableEvents();
167
2d17d68f 168 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
e1b93ccb
RR
169
170 GtkEnableEvents();
6de97a3b 171}
c801d85f 172
debe6624 173void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 174{
2d17d68f
RR
175 float fmin = (float)minValue;
176 float fmax = (float)maxValue;
80810ca3 177
2d17d68f
RR
178 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
179 (fabs(fmax-m_adjust->upper) < 0.2))
180 {
181 return;
182 }
80810ca3 183
2d17d68f
RR
184 m_adjust->lower = fmin;
185 m_adjust->upper = fmax;
4ee1741f
RR
186 m_adjust->step_increment = 1.0;
187 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 188
e1b93ccb
RR
189 GtkDisableEvents();
190
2d17d68f 191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
192
193 GtkEnableEvents();
6de97a3b 194}
c801d85f 195
1e1fafb9 196int wxSlider::GetMin() const
c801d85f 197{
b1a39f47 198 return (int)ceil(m_adjust->lower);
6de97a3b 199}
c801d85f 200
1e1fafb9 201int wxSlider::GetMax() const
c801d85f 202{
b1a39f47 203 return (int)ceil(m_adjust->upper);
6de97a3b 204}
c801d85f 205
debe6624 206void wxSlider::SetPageSize( int pageSize )
c801d85f 207{
2d17d68f 208 float fpage = (float)pageSize;
80810ca3 209
2d17d68f 210 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 211
2d17d68f 212 m_adjust->page_increment = fpage;
c801d85f 213
e1b93ccb
RR
214 GtkDisableEvents();
215
2d17d68f 216 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
217
218 GtkEnableEvents();
6de97a3b 219}
c801d85f 220
1e1fafb9 221int wxSlider::GetPageSize() const
c801d85f 222{
b1a39f47 223 return (int)ceil(m_adjust->page_increment);
6de97a3b 224}
c801d85f 225
debe6624 226void wxSlider::SetThumbLength( int len )
c801d85f 227{
2d17d68f 228 float flen = (float)len;
80810ca3 229
2d17d68f 230 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 231
2d17d68f 232 m_adjust->page_size = flen;
c801d85f 233
e1b93ccb
RR
234 GtkDisableEvents();
235
2d17d68f 236 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
e1b93ccb
RR
237
238 GtkEnableEvents();
6de97a3b 239}
c801d85f 240
1e1fafb9 241int wxSlider::GetThumbLength() const
c801d85f 242{
b1a39f47 243 return (int)ceil(m_adjust->page_size);
6de97a3b 244}
c801d85f 245
debe6624 246void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 247{
6de97a3b 248}
c801d85f 249
1e1fafb9 250int wxSlider::GetLineSize() const
c801d85f 251{
2d17d68f 252 return 0;
6de97a3b 253}
c801d85f 254
b4071e91
RR
255bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
256{
2d17d68f 257 GtkRange *range = GTK_RANGE(m_widget);
2b5f62a0
VZ
258#ifdef __WXGTK20__
259 return (range->event_window == window);
260#else
9e691f46 261 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
262 || (window == range->trough)
263 || (window == range->slider)
264 || (window == range->step_forw)
2b5f62a0
VZ
265 || (window == range->step_back) );
266#endif
b4071e91
RR
267}
268
e1b93ccb
RR
269void 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
276void 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
9d522606
RD
284// static
285wxVisualAttributes
286wxSlider::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
287{
288 return GetDefaultAttributesFromGTKWidget(gtk_vscale_new);
289}
290
dcf924a3 291#endif