]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
HP-UX support added
[wxWidgets.git] / src / gtk1 / 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
66bd6b93
RR
21//-----------------------------------------------------------------------------
22// data
23//-----------------------------------------------------------------------------
24
25extern bool g_blockEventsOnDrag;
26
c801d85f 27//-----------------------------------------------------------------------------
97b3455a 28// "value_changed"
c801d85f
KB
29//-----------------------------------------------------------------------------
30
66bd6b93 31static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win )
c801d85f 32{
2d17d68f
RR
33 if (!win->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
c801d85f 35
2d17d68f
RR
36 float diff = win->m_adjust->value - win->m_oldPos;
37 if (fabs(diff) < 0.2) return;
c801d85f 38
2d17d68f 39 wxEventType command = wxEVT_NULL;
c801d85f 40
2d17d68f
RR
41 float line_step = win->m_adjust->step_increment;
42 float page_step = win->m_adjust->page_increment;
c801d85f 43
2d17d68f
RR
44 if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM;
45 else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP;
46 else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
47 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
48 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
49 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
50 else command = wxEVT_SCROLL_THUMBTRACK;
51
52 int value = (int)(win->m_adjust->value+0.5);
c801d85f 53
2d17d68f
RR
54 int orient = wxHORIZONTAL;
55 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
c801d85f 56
2d17d68f
RR
57 wxScrollEvent event( command, win->GetId(), value, orient );
58 event.SetEventObject( win );
59 win->ProcessEvent( event );
c801d85f 60
2d17d68f
RR
61 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
62 cevent.SetEventObject( win );
63 win->ProcessEvent( cevent );
6de97a3b 64}
c801d85f 65
97b3455a
RR
66//-----------------------------------------------------------------------------
67// wxSlider
68//-----------------------------------------------------------------------------
69
c801d85f
KB
70IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
71
72wxSlider::wxSlider(void)
73{
6de97a3b 74}
c801d85f
KB
75
76wxSlider::~wxSlider(void)
77{
6de97a3b 78}
c801d85f 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;
c801d85f 87
2d17d68f 88 PreCreation( parent, id, pos, size, style, name );
c801d85f 89
2d17d68f 90 SetValidator( validator );
6de97a3b 91
2d17d68f 92 m_oldPos = 0.0;
c801d85f 93
2d17d68f
RR
94 if (style & wxSL_VERTICAL == wxSL_VERTICAL)
95 m_widget = gtk_hscale_new( (GtkAdjustment *) NULL );
96 else
97 m_widget = gtk_vscale_new( (GtkAdjustment *) NULL );
c801d85f 98
2d17d68f 99 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
58614078 100
2d17d68f 101 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
c801d85f 102
2d17d68f
RR
103 gtk_signal_connect( GTK_OBJECT(m_adjust),
104 "value_changed",
105 (GtkSignalFunc) gtk_slider_callback,
106 (gpointer) this );
107
108 SetRange( minValue, maxValue );
109 SetValue( value );
c801d85f 110
2d17d68f 111 m_parent->AddChild( this );
6ca41e57 112
2d17d68f 113 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 114
2d17d68f 115 PostCreation();
c801d85f 116
2d17d68f 117 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 118
2d17d68f 119 Show( TRUE );
c801d85f 120
2d17d68f 121 return TRUE;
6de97a3b 122}
c801d85f
KB
123
124int wxSlider::GetValue(void) const
125{
2d17d68f 126 return (int)(m_adjust->value+0.5);
6de97a3b 127}
c801d85f 128
debe6624 129void wxSlider::SetValue( int value )
c801d85f 130{
2d17d68f
RR
131 float fpos = (float)value;
132 m_oldPos = fpos;
133 if (fabs(fpos-m_adjust->value) < 0.2) return;
134
135 m_adjust->value = fpos;
c801d85f 136
2d17d68f 137 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 138}
c801d85f 139
debe6624 140void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 141{
2d17d68f
RR
142 float fmin = (float)minValue;
143 float fmax = (float)maxValue;
c801d85f 144
2d17d68f
RR
145 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
146 (fabs(fmax-m_adjust->upper) < 0.2))
147 {
148 return;
149 }
c801d85f 150
2d17d68f
RR
151 m_adjust->lower = fmin;
152 m_adjust->upper = fmax;
c801d85f 153
2d17d68f 154 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 155}
c801d85f
KB
156
157int wxSlider::GetMin(void) const
158{
2d17d68f 159 return (int)(m_adjust->lower+0.5);
6de97a3b 160}
c801d85f
KB
161
162int wxSlider::GetMax(void) const
163{
2d17d68f 164 return (int)(m_adjust->upper+0.5);
6de97a3b 165}
c801d85f 166
debe6624 167void wxSlider::SetPageSize( int pageSize )
c801d85f 168{
2d17d68f 169 float fpage = (float)pageSize;
c801d85f 170
2d17d68f 171 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
c801d85f 172
2d17d68f 173 m_adjust->page_increment = fpage;
c801d85f 174
2d17d68f 175 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 176}
c801d85f
KB
177
178int wxSlider::GetPageSize(void) const
179{
2d17d68f 180 return (int)(m_adjust->page_increment+0.5);
6de97a3b 181}
c801d85f 182
debe6624 183void wxSlider::SetThumbLength( int len )
c801d85f 184{
2d17d68f 185 float flen = (float)len;
c801d85f 186
2d17d68f 187 if (fabs(flen-m_adjust->page_size) < 0.2) return;
c801d85f 188
2d17d68f 189 m_adjust->page_size = flen;
c801d85f 190
2d17d68f 191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 192}
c801d85f
KB
193
194int wxSlider::GetThumbLength(void) const
195{
2d17d68f 196 return (int)(m_adjust->page_size+0.5);
6de97a3b 197}
c801d85f 198
debe6624 199void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 200{
6de97a3b 201}
c801d85f
KB
202
203int wxSlider::GetLineSize(void) const
204{
2d17d68f 205 return 0;
6de97a3b 206}
c801d85f 207
debe6624 208void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 209{
6de97a3b 210}
c801d85f 211
debe6624 212void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 213{
6de97a3b 214}
c801d85f
KB
215
216int wxSlider::GetTickFreq(void) const
217{
2d17d68f 218 return 0;
6de97a3b 219}
c801d85f
KB
220
221void wxSlider::ClearTicks(void)
222{
6de97a3b 223}
c801d85f 224
debe6624 225void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 226{
6de97a3b 227}
c801d85f
KB
228
229int wxSlider::GetSelEnd(void) const
230{
2d17d68f 231 return 0;
6de97a3b 232}
c801d85f
KB
233
234int wxSlider::GetSelStart(void) const
235{
2d17d68f 236 return 0;
6de97a3b 237}
c801d85f
KB
238
239void wxSlider::ClearSel(void)
240{
6de97a3b 241}
c801d85f 242
b4071e91
RR
243bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
244{
2d17d68f
RR
245 GtkRange *range = GTK_RANGE(m_widget);
246 return ( (window == GTK_WIDGET(range)->window) ||
247 (window == range->trough) ||
248 (window == range->slider) ||
249 (window == range->step_forw) ||
250 (window == range->step_back) );
b4071e91
RR
251}
252
58614078
RR
253void wxSlider::ApplyWidgetStyle()
254{
2d17d68f
RR
255 SetWidgetStyle();
256 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 257}