]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/slider.cpp
Fixed a minor bug in hotKey handling.
[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
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"
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
a2053b27 42 if (!win->m_hasVMT) return;
2d17d68f 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 );
f03fc89f
VZ
112
113 /* labels need more space and too small window will
114 cause junk to appear on the dialog */
858b5bdd 115 if (style & wxSL_VERTICAL)
f03fc89f
VZ
116 {
117 wxSize sz( size );
118 if (sz.x < 35)
119 {
120 sz.x = 35;
121 SetSize( sz );
122 }
123 }
858b5bdd 124 else
f03fc89f
VZ
125 {
126 wxSize sz( size );
127 if (sz.y < 35)
128 {
129 sz.y = 35;
130 SetSize( sz );
131 }
132 }
858b5bdd 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",
f03fc89f
VZ
141 (GtkSignalFunc) gtk_slider_callback,
142 (gpointer) this );
143
2d17d68f
RR
144 SetRange( minValue, maxValue );
145 SetValue( value );
c801d85f 146
f03fc89f 147 m_parent->DoAddChild( this );
6ca41e57 148
2d17d68f 149 PostCreation();
c801d85f 150
2d17d68f 151 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 152
2d17d68f 153 Show( TRUE );
c801d85f 154
2d17d68f 155 return TRUE;
6de97a3b 156}
c801d85f
KB
157
158int wxSlider::GetValue(void) const
159{
2d17d68f 160 return (int)(m_adjust->value+0.5);
6de97a3b 161}
c801d85f 162
debe6624 163void wxSlider::SetValue( int value )
c801d85f 164{
2d17d68f
RR
165 float fpos = (float)value;
166 m_oldPos = fpos;
167 if (fabs(fpos-m_adjust->value) < 0.2) return;
168
169 m_adjust->value = fpos;
c801d85f 170
2d17d68f 171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 172}
c801d85f 173
debe6624 174void wxSlider::SetRange( int minValue, int maxValue )
c801d85f 175{
2d17d68f
RR
176 float fmin = (float)minValue;
177 float fmax = (float)maxValue;
c801d85f 178
2d17d68f
RR
179 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
180 (fabs(fmax-m_adjust->upper) < 0.2))
181 {
182 return;
183 }
c801d85f 184
2d17d68f
RR
185 m_adjust->lower = fmin;
186 m_adjust->upper = fmax;
c801d85f 187
2d17d68f 188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 189}
c801d85f
KB
190
191int wxSlider::GetMin(void) const
192{
b1a39f47 193 return (int)ceil(m_adjust->lower);
6de97a3b 194}
c801d85f
KB
195
196int wxSlider::GetMax(void) const
197{
b1a39f47 198 return (int)ceil(m_adjust->upper);
6de97a3b 199}
c801d85f 200
debe6624 201void wxSlider::SetPageSize( int pageSize )
c801d85f 202{
2d17d68f 203 float fpage = (float)pageSize;
c801d85f 204
2d17d68f 205 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
c801d85f 206
2d17d68f 207 m_adjust->page_increment = fpage;
c801d85f 208
2d17d68f 209 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 210}
c801d85f
KB
211
212int wxSlider::GetPageSize(void) const
213{
b1a39f47 214 return (int)ceil(m_adjust->page_increment);
6de97a3b 215}
c801d85f 216
debe6624 217void wxSlider::SetThumbLength( int len )
c801d85f 218{
2d17d68f 219 float flen = (float)len;
c801d85f 220
2d17d68f 221 if (fabs(flen-m_adjust->page_size) < 0.2) return;
c801d85f 222
2d17d68f 223 m_adjust->page_size = flen;
c801d85f 224
2d17d68f 225 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 226}
c801d85f
KB
227
228int wxSlider::GetThumbLength(void) const
229{
b1a39f47 230 return (int)ceil(m_adjust->page_size);
6de97a3b 231}
c801d85f 232
debe6624 233void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 234{
6de97a3b 235}
c801d85f
KB
236
237int wxSlider::GetLineSize(void) const
238{
2d17d68f 239 return 0;
6de97a3b 240}
c801d85f 241
debe6624 242void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 243{
6de97a3b 244}
c801d85f 245
debe6624 246void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 247{
6de97a3b 248}
c801d85f
KB
249
250int wxSlider::GetTickFreq(void) const
251{
2d17d68f 252 return 0;
6de97a3b 253}
c801d85f
KB
254
255void wxSlider::ClearTicks(void)
256{
6de97a3b 257}
c801d85f 258
debe6624 259void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 260{
6de97a3b 261}
c801d85f
KB
262
263int wxSlider::GetSelEnd(void) const
264{
2d17d68f 265 return 0;
6de97a3b 266}
c801d85f
KB
267
268int wxSlider::GetSelStart(void) const
269{
2d17d68f 270 return 0;
6de97a3b 271}
c801d85f
KB
272
273void wxSlider::ClearSel(void)
274{
6de97a3b 275}
c801d85f 276
b4071e91
RR
277bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
278{
2d17d68f
RR
279 GtkRange *range = GTK_RANGE(m_widget);
280 return ( (window == GTK_WIDGET(range)->window) ||
281 (window == range->trough) ||
f03fc89f
VZ
282 (window == range->slider) ||
283 (window == range->step_forw) ||
284 (window == range->step_back) );
b4071e91
RR
285}
286
58614078
RR
287void wxSlider::ApplyWidgetStyle()
288{
2d17d68f
RR
289 SetWidgetStyle();
290 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 291}