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