]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
a bit of debug code
[wxWidgets.git] / src / gtk / slider.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: slider.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
c801d85f
KB
10#ifdef __GNUG__
11#pragma implementation "slider.h"
12#endif
13
14#include "wx/slider.h"
15#include "wx/utils.h"
f5368809 16#include <math.h>
c801d85f 17
83624f79
RR
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
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
32extern bool g_blockEventsOnDrag;
33
c801d85f 34//-----------------------------------------------------------------------------
97b3455a 35// "value_changed"
c801d85f
KB
36//-----------------------------------------------------------------------------
37
66bd6b93 38static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win )
c801d85f 39{
acfd422a
RR
40 if (g_isIdle) wxapp_install_idle_handler();
41
2d17d68f
RR
42 if (!win->HasVMT()) return;
43 if (g_blockEventsOnDrag) return;
c801d85f 44
2d17d68f
RR
45 float diff = win->m_adjust->value - win->m_oldPos;
46 if (fabs(diff) < 0.2) return;
828f655f 47 win->m_oldPos = win->m_adjust->value;
c801d85f 48
2d17d68f 49 wxEventType command = wxEVT_NULL;
c801d85f 50
2d17d68f
RR
51 float line_step = win->m_adjust->step_increment;
52 float page_step = win->m_adjust->page_increment;
c801d85f 53
2d17d68f
RR
54 if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM;
55 else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP;
56 else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
57 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
58 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
59 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
60 else command = wxEVT_SCROLL_THUMBTRACK;
61
b1a39f47 62 int value = (int)ceil(win->m_adjust->value);
c801d85f 63
2d17d68f
RR
64 int orient = wxHORIZONTAL;
65 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
c801d85f 66
2d17d68f
RR
67 wxScrollEvent event( command, win->GetId(), value, orient );
68 event.SetEventObject( win );
828f655f 69 win->GetEventHandler()->ProcessEvent( event );
c801d85f 70
2d17d68f
RR
71 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
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
82wxSlider::wxSlider(void)
83{
6de97a3b 84}
c801d85f
KB
85
86wxSlider::~wxSlider(void)
87{
6de97a3b 88}
c801d85f 89
debe6624
JS
90bool wxSlider::Create(wxWindow *parent, wxWindowID id,
91 int value, int minValue, int maxValue,
c801d85f 92 const wxPoint& pos, const wxSize& size,
6de97a3b 93 long style, const wxValidator& validator, const wxString& name )
c801d85f 94{
b292e2f5 95 m_acceptsFocus = TRUE;
2d17d68f 96 m_needParent = TRUE;
c801d85f 97
2d17d68f 98 PreCreation( parent, id, pos, size, style, name );
c801d85f 99
2d17d68f 100 SetValidator( validator );
6de97a3b 101
2d17d68f 102 m_oldPos = 0.0;
c801d85f 103
2e563988 104 if (style & wxSL_VERTICAL)
2d17d68f 105 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
19da4326
RR
106 else
107 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
c801d85f 108
2e563988 109 if (style & wxSL_LABELS)
858b5bdd 110 {
2e563988 111 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
858b5bdd
RR
112
113 /* labels need more space and too small window will
114 cause junk to appear on the dialog */
115 if (style & wxSL_VERTICAL)
116 {
117 wxSize sz( size );
118 if (sz.x < 35)
119 {
120 sz.x = 35;
121 SetSize( sz );
122 }
123 }
124 else
125 {
126 wxSize sz( size );
127 if (sz.y < 35)
128 {
129 sz.y = 35;
130 SetSize( sz );
131 }
132 }
133 }
2e563988
RR
134 else
135 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 136
2d17d68f 137 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
c801d85f 138
2d17d68f
RR
139 gtk_signal_connect( GTK_OBJECT(m_adjust),
140 "value_changed",
141 (GtkSignalFunc) gtk_slider_callback,
142 (gpointer) this );
143
144 SetRange( minValue, maxValue );
145 SetValue( value );
c801d85f 146
2d17d68f 147 m_parent->AddChild( this );
6ca41e57 148
2d17d68f 149 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 150
2d17d68f 151 PostCreation();
c801d85f 152
2d17d68f 153 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 154
2d17d68f 155 Show( TRUE );
c801d85f 156
2d17d68f 157 return TRUE;
6de97a3b 158}
c801d85f
KB
159
160int wxSlider::GetValue(void) const
161{
2d17d68f 162 return (int)(m_adjust->value+0.5);
6de97a3b 163}
c801d85f 164
debe6624 165void wxSlider::SetValue( int value )
c801d85f 166{
2d17d68f
RR
167 float fpos = (float)value;
168 m_oldPos = fpos;
169 if (fabs(fpos-m_adjust->value) < 0.2) return;
170
171 m_adjust->value = fpos;
c801d85f 172
2d17d68f 173 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 174}
c801d85f 175
debe6624 176void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 177{
2d17d68f
RR
178 float fmin = (float)minValue;
179 float fmax = (float)maxValue;
c801d85f 180
2d17d68f
RR
181 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
182 (fabs(fmax-m_adjust->upper) < 0.2))
183 {
184 return;
185 }
c801d85f 186
2d17d68f
RR
187 m_adjust->lower = fmin;
188 m_adjust->upper = fmax;
c801d85f 189
2d17d68f 190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 191}
c801d85f
KB
192
193int wxSlider::GetMin(void) const
194{
b1a39f47 195 return (int)ceil(m_adjust->lower);
6de97a3b 196}
c801d85f
KB
197
198int wxSlider::GetMax(void) const
199{
b1a39f47 200 return (int)ceil(m_adjust->upper);
6de97a3b 201}
c801d85f 202
debe6624 203void wxSlider::SetPageSize( int pageSize )
c801d85f 204{
2d17d68f 205 float fpage = (float)pageSize;
c801d85f 206
2d17d68f 207 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
c801d85f 208
2d17d68f 209 m_adjust->page_increment = fpage;
c801d85f 210
2d17d68f 211 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 212}
c801d85f
KB
213
214int wxSlider::GetPageSize(void) const
215{
b1a39f47 216 return (int)ceil(m_adjust->page_increment);
6de97a3b 217}
c801d85f 218
debe6624 219void wxSlider::SetThumbLength( int len )
c801d85f 220{
2d17d68f 221 float flen = (float)len;
c801d85f 222
2d17d68f 223 if (fabs(flen-m_adjust->page_size) < 0.2) return;
c801d85f 224
2d17d68f 225 m_adjust->page_size = flen;
c801d85f 226
2d17d68f 227 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 228}
c801d85f
KB
229
230int wxSlider::GetThumbLength(void) const
231{
b1a39f47 232 return (int)ceil(m_adjust->page_size);
6de97a3b 233}
c801d85f 234
debe6624 235void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 236{
6de97a3b 237}
c801d85f
KB
238
239int wxSlider::GetLineSize(void) const
240{
2d17d68f 241 return 0;
6de97a3b 242}
c801d85f 243
debe6624 244void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 245{
6de97a3b 246}
c801d85f 247
debe6624 248void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 249{
6de97a3b 250}
c801d85f
KB
251
252int wxSlider::GetTickFreq(void) const
253{
2d17d68f 254 return 0;
6de97a3b 255}
c801d85f
KB
256
257void wxSlider::ClearTicks(void)
258{
6de97a3b 259}
c801d85f 260
debe6624 261void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 262{
6de97a3b 263}
c801d85f
KB
264
265int wxSlider::GetSelEnd(void) const
266{
2d17d68f 267 return 0;
6de97a3b 268}
c801d85f
KB
269
270int wxSlider::GetSelStart(void) const
271{
2d17d68f 272 return 0;
6de97a3b 273}
c801d85f
KB
274
275void wxSlider::ClearSel(void)
276{
6de97a3b 277}
c801d85f 278
b4071e91
RR
279bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
280{
2d17d68f
RR
281 GtkRange *range = GTK_RANGE(m_widget);
282 return ( (window == GTK_WIDGET(range)->window) ||
283 (window == range->trough) ||
284 (window == range->slider) ||
285 (window == range->step_forw) ||
286 (window == range->step_back) );
b4071e91
RR
287}
288
58614078
RR
289void wxSlider::ApplyWidgetStyle()
290{
2d17d68f
RR
291 SetWidgetStyle();
292 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 293}