]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/slider.cpp
Added wxWakeUpMainThread, wxMutexGuiEnter, wxMutexGuiLeave,
[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"
57a1fd73 19
f5368809 20#include <math.h>
c801d85f 21
57a1fd73
RR
22#include <gdk/gdk.h>
23#include <gtk/gtk.h>
83624f79 24
acfd422a
RR
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
66bd6b93
RR
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37
57a1fd73
RR
38static const float sensitivity = 0.02;
39
c801d85f 40//-----------------------------------------------------------------------------
97b3455a 41// "value_changed"
c801d85f
KB
42//-----------------------------------------------------------------------------
43
e1811a01 44static void gtk_slider_callback( GtkAdjustment *adjust, wxSlider *win )
80810ca3 45{
acfd422a
RR
46 if (g_isIdle) wxapp_install_idle_handler();
47
a2053b27 48 if (!win->m_hasVMT) return;
2d17d68f 49 if (g_blockEventsOnDrag) return;
e1811a01
RR
50
51 float diff = adjust->value - win->m_oldPos;
57a1fd73 52 if (fabs(diff) < sensitivity) return;
80810ca3 53
e1811a01 54 win->m_oldPos = adjust->value;
2d17d68f 55
e1811a01
RR
56 GtkRange *range = GTK_RANGE( win->m_widget );
57
58 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
59 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP;
60 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN;
61 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP;
62 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN;
80810ca3 63
0a07a7d8
RR
64 double dvalue = adjust->value;
65 int value = (int)(dvalue >= 0 ? dvalue - 0.5 : dvalue + 0.5);
80810ca3 66
2d17d68f 67 int orient = wxHORIZONTAL;
80810ca3
VZ
68 if ( (win->GetWindowStyleFlag() & wxSB_VERTICAL) == wxSB_VERTICAL)
69 orient = wxVERTICAL;
70
2d17d68f
RR
71 wxScrollEvent event( command, win->GetId(), value, orient );
72 event.SetEventObject( win );
828f655f 73 win->GetEventHandler()->ProcessEvent( event );
80810ca3 74
2d17d68f
RR
75 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
76 cevent.SetEventObject( win );
f6bcfd97 77 cevent.SetInt( value );
828f655f 78 win->GetEventHandler()->ProcessEvent( cevent );
6de97a3b 79}
c801d85f 80
97b3455a
RR
81//-----------------------------------------------------------------------------
82// wxSlider
83//-----------------------------------------------------------------------------
84
c801d85f
KB
85IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
86
debe6624
JS
87bool wxSlider::Create(wxWindow *parent, wxWindowID id,
88 int value, int minValue, int maxValue,
c801d85f 89 const wxPoint& pos, const wxSize& size,
6de97a3b 90 long style, const wxValidator& validator, const wxString& name )
c801d85f 91{
b292e2f5 92 m_acceptsFocus = TRUE;
2d17d68f 93 m_needParent = TRUE;
80810ca3 94
4dcaf11a
RR
95 if (!PreCreation( parent, pos, size ) ||
96 !CreateBase( parent, id, pos, size, style, validator, name ))
97 {
223d09f6 98 wxFAIL_MSG( wxT("wxSlider creation failed") );
ef7a25a7 99 return FALSE;
4dcaf11a 100 }
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 );
80810ca3 108
2e563988 109 if (style & wxSL_LABELS)
858b5bdd 110 {
2e563988 111 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE );
ef7a25a7 112 gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 );
80810ca3 113
f03fc89f
VZ
114 /* labels need more space and too small window will
115 cause junk to appear on the dialog */
858b5bdd 116 if (style & wxSL_VERTICAL)
f03fc89f
VZ
117 {
118 wxSize sz( size );
119 if (sz.x < 35)
120 {
121 sz.x = 35;
122 SetSize( sz );
123 }
124 }
858b5bdd 125 else
f03fc89f
VZ
126 {
127 wxSize sz( size );
128 if (sz.y < 35)
129 {
130 sz.y = 35;
131 SetSize( sz );
132 }
133 }
858b5bdd 134 }
2e563988
RR
135 else
136 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
19da4326 137
2d17d68f 138 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
80810ca3
VZ
139
140 gtk_signal_connect( GTK_OBJECT(m_adjust),
2d17d68f 141 "value_changed",
80810ca3 142 (GtkSignalFunc) gtk_slider_callback,
f03fc89f 143 (gpointer) this );
80810ca3 144
2d17d68f
RR
145 SetRange( minValue, maxValue );
146 SetValue( value );
80810ca3 147
f03fc89f 148 m_parent->DoAddChild( this );
80810ca3 149
2d17d68f 150 PostCreation();
80810ca3 151
2d17d68f 152 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 153
2d17d68f 154 Show( TRUE );
80810ca3 155
2d17d68f 156 return TRUE;
6de97a3b 157}
c801d85f 158
1e1fafb9 159int wxSlider::GetValue() const
c801d85f 160{
ef7a25a7
VZ
161 // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and
162 // -0.9 is rounded to -1
163 double val = m_adjust->value;
164 return (int)(val >= 0 ? val - 0.5 : val + 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;
4ee1741f
RR
191 m_adjust->step_increment = 1.0;
192 m_adjust->page_increment = ceil((fmax-fmin) / 10.0);
c801d85f 193
2d17d68f 194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 195}
c801d85f 196
1e1fafb9 197int wxSlider::GetMin() const
c801d85f 198{
b1a39f47 199 return (int)ceil(m_adjust->lower);
6de97a3b 200}
c801d85f 201
1e1fafb9 202int wxSlider::GetMax() const
c801d85f 203{
b1a39f47 204 return (int)ceil(m_adjust->upper);
6de97a3b 205}
c801d85f 206
debe6624 207void wxSlider::SetPageSize( int pageSize )
c801d85f 208{
2d17d68f 209 float fpage = (float)pageSize;
80810ca3 210
2d17d68f 211 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
80810ca3 212
2d17d68f 213 m_adjust->page_increment = fpage;
c801d85f 214
2d17d68f 215 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 216}
c801d85f 217
1e1fafb9 218int wxSlider::GetPageSize() const
c801d85f 219{
b1a39f47 220 return (int)ceil(m_adjust->page_increment);
6de97a3b 221}
c801d85f 222
debe6624 223void wxSlider::SetThumbLength( int len )
c801d85f 224{
2d17d68f 225 float flen = (float)len;
80810ca3 226
2d17d68f 227 if (fabs(flen-m_adjust->page_size) < 0.2) return;
80810ca3 228
2d17d68f 229 m_adjust->page_size = flen;
c801d85f 230
2d17d68f 231 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 232}
c801d85f 233
1e1fafb9 234int wxSlider::GetThumbLength() const
c801d85f 235{
b1a39f47 236 return (int)ceil(m_adjust->page_size);
6de97a3b 237}
c801d85f 238
debe6624 239void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
c801d85f 240{
6de97a3b 241}
c801d85f 242
1e1fafb9 243int wxSlider::GetLineSize() const
c801d85f 244{
2d17d68f 245 return 0;
6de97a3b 246}
c801d85f 247
debe6624 248void wxSlider::SetTick( int WXUNUSED(tickPos) )
c801d85f 249{
6de97a3b 250}
c801d85f 251
debe6624 252void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
c801d85f 253{
6de97a3b 254}
c801d85f 255
1e1fafb9 256int wxSlider::GetTickFreq() const
c801d85f 257{
2d17d68f 258 return 0;
6de97a3b 259}
c801d85f 260
1e1fafb9 261void wxSlider::ClearTicks()
c801d85f 262{
6de97a3b 263}
c801d85f 264
debe6624 265void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
c801d85f 266{
6de97a3b 267}
c801d85f 268
1e1fafb9 269int wxSlider::GetSelEnd() const
c801d85f 270{
2d17d68f 271 return 0;
6de97a3b 272}
c801d85f 273
1e1fafb9 274int wxSlider::GetSelStart() const
c801d85f 275{
2d17d68f 276 return 0;
6de97a3b 277}
c801d85f 278
1e1fafb9 279void wxSlider::ClearSel()
c801d85f 280{
6de97a3b 281}
c801d85f 282
b4071e91
RR
283bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
284{
2d17d68f
RR
285 GtkRange *range = GTK_RANGE(m_widget);
286 return ( (window == GTK_WIDGET(range)->window) ||
287 (window == range->trough) ||
f03fc89f
VZ
288 (window == range->slider) ||
289 (window == range->step_forw) ||
290 (window == range->step_back) );
b4071e91
RR
291}
292
58614078
RR
293void wxSlider::ApplyWidgetStyle()
294{
2d17d68f
RR
295 SetWidgetStyle();
296 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078 297}
dcf924a3
RR
298
299#endif