]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
More for wxPython 2.0b9 (hopefully the last...)
[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
RR
109 if (style & wxSL_LABELS)
110 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
111 else
112 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 113
2d17d68f 114 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
c801d85f 115
2d17d68f
RR
116 gtk_signal_connect( GTK_OBJECT(m_adjust),
117 "value_changed",
118 (GtkSignalFunc) gtk_slider_callback,
119 (gpointer) this );
120
121 SetRange( minValue, maxValue );
122 SetValue( value );
c801d85f 123
2d17d68f 124 m_parent->AddChild( this );
6ca41e57 125
2d17d68f 126 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 127
2d17d68f 128 PostCreation();
c801d85f 129
2d17d68f 130 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 131
2d17d68f 132 Show( TRUE );
c801d85f 133
2d17d68f 134 return TRUE;
6de97a3b 135}
c801d85f
KB
136
137int wxSlider::GetValue(void) const
138{
2d17d68f 139 return (int)(m_adjust->value+0.5);
6de97a3b 140}
c801d85f 141
debe6624 142void wxSlider::SetValue( int value )
c801d85f 143{
2d17d68f
RR
144 float fpos = (float)value;
145 m_oldPos = fpos;
146 if (fabs(fpos-m_adjust->value) < 0.2) return;
147
148 m_adjust->value = fpos;
c801d85f 149
2d17d68f 150 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 151}
c801d85f 152
debe6624 153void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 154{
2d17d68f
RR
155 float fmin = (float)minValue;
156 float fmax = (float)maxValue;
c801d85f 157
2d17d68f
RR
158 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
159 (fabs(fmax-m_adjust->upper) < 0.2))
160 {
161 return;
162 }
c801d85f 163
2d17d68f
RR
164 m_adjust->lower = fmin;
165 m_adjust->upper = fmax;
c801d85f 166
2d17d68f 167 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 168}
c801d85f
KB
169
170int wxSlider::GetMin(void) const
171{
b1a39f47 172 return (int)ceil(m_adjust->lower);
6de97a3b 173}
c801d85f
KB
174
175int wxSlider::GetMax(void) const
176{
b1a39f47 177 return (int)ceil(m_adjust->upper);
6de97a3b 178}
c801d85f 179
debe6624 180void wxSlider::SetPageSize( int pageSize )
c801d85f 181{
2d17d68f 182 float fpage = (float)pageSize;
c801d85f 183
2d17d68f 184 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
c801d85f 185
2d17d68f 186 m_adjust->page_increment = fpage;
c801d85f 187
2d17d68f 188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 189}
c801d85f
KB
190
191int wxSlider::GetPageSize(void) const
192{
b1a39f47 193 return (int)ceil(m_adjust->page_increment);
6de97a3b 194}
c801d85f 195
debe6624 196void wxSlider::SetThumbLength( int len )
c801d85f 197{
2d17d68f 198 float flen = (float)len;
c801d85f 199
2d17d68f 200 if (fabs(flen-m_adjust->page_size) < 0.2) return;
c801d85f 201
2d17d68f 202 m_adjust->page_size = flen;
c801d85f 203
2d17d68f 204 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 205}
c801d85f
KB
206
207int wxSlider::GetThumbLength(void) const
208{
b1a39f47 209 return (int)ceil(m_adjust->page_size);
6de97a3b 210}
c801d85f 211
debe6624 212void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 213{
6de97a3b 214}
c801d85f
KB
215
216int wxSlider::GetLineSize(void) const
217{
2d17d68f 218 return 0;
6de97a3b 219}
c801d85f 220
debe6624 221void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 222{
6de97a3b 223}
c801d85f 224
debe6624 225void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 226{
6de97a3b 227}
c801d85f
KB
228
229int wxSlider::GetTickFreq(void) const
230{
2d17d68f 231 return 0;
6de97a3b 232}
c801d85f
KB
233
234void wxSlider::ClearTicks(void)
235{
6de97a3b 236}
c801d85f 237
debe6624 238void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 239{
6de97a3b 240}
c801d85f
KB
241
242int wxSlider::GetSelEnd(void) const
243{
2d17d68f 244 return 0;
6de97a3b 245}
c801d85f
KB
246
247int wxSlider::GetSelStart(void) const
248{
2d17d68f 249 return 0;
6de97a3b 250}
c801d85f
KB
251
252void wxSlider::ClearSel(void)
253{
6de97a3b 254}
c801d85f 255
b4071e91
RR
256bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
257{
2d17d68f
RR
258 GtkRange *range = GTK_RANGE(m_widget);
259 return ( (window == GTK_WIDGET(range)->window) ||
260 (window == range->trough) ||
261 (window == range->slider) ||
262 (window == range->step_forw) ||
263 (window == range->step_back) );
b4071e91
RR
264}
265
58614078
RR
266void wxSlider::ApplyWidgetStyle()
267{
2d17d68f
RR
268 SetWidgetStyle();
269 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 270}