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