]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/slider.cpp
Dialog items inherit parent's font now
[wxWidgets.git] / src / gtk / slider.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slider.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "slider.h"
12 #endif
13
14 #include "wx/slider.h"
15 #include "wx/utils.h"
16 #include <math.h>
17
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20
21 //-----------------------------------------------------------------------------
22 // data
23 //-----------------------------------------------------------------------------
24
25 extern bool g_blockEventsOnDrag;
26
27 //-----------------------------------------------------------------------------
28 // "value_changed"
29 //-----------------------------------------------------------------------------
30
31 static void gtk_slider_callback( GtkWidget *WXUNUSED(widget), wxSlider *win )
32 {
33 if (!win->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
36 float diff = win->m_adjust->value - win->m_oldPos;
37 if (fabs(diff) < 0.2) return;
38
39 wxEventType command = wxEVT_NULL;
40
41 float line_step = win->m_adjust->step_increment;
42 float page_step = win->m_adjust->page_increment;
43
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);
53
54 int orient = wxHORIZONTAL;
55 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
56
57 wxScrollEvent event( command, win->GetId(), value, orient );
58 event.SetEventObject( win );
59 win->ProcessEvent( event );
60
61 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() );
62 cevent.SetEventObject( win );
63 win->ProcessEvent( cevent );
64 }
65
66 //-----------------------------------------------------------------------------
67 // wxSlider
68 //-----------------------------------------------------------------------------
69
70 IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl)
71
72 wxSlider::wxSlider(void)
73 {
74 }
75
76 wxSlider::~wxSlider(void)
77 {
78 }
79
80 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
81 int value, int minValue, int maxValue,
82 const wxPoint& pos, const wxSize& size,
83 long style, const wxValidator& validator, const wxString& name )
84 {
85 m_acceptsFocus = TRUE;
86 m_needParent = TRUE;
87
88 PreCreation( parent, id, pos, size, style, name );
89
90 SetValidator( validator );
91
92 m_oldPos = 0.0;
93
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 );
98
99 gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE );
100
101 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
102
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 );
110
111 m_parent->AddChild( this );
112
113 (m_parent->m_insertCallback)( m_parent, this );
114
115 PostCreation();
116
117 SetBackgroundColour( parent->GetBackgroundColour() );
118
119 Show( TRUE );
120
121 return TRUE;
122 }
123
124 int wxSlider::GetValue(void) const
125 {
126 return (int)(m_adjust->value+0.5);
127 }
128
129 void wxSlider::SetValue( int value )
130 {
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;
136
137 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
138 }
139
140 void wxSlider::SetRange( int minValue, int maxValue )
141 {
142 float fmin = (float)minValue;
143 float fmax = (float)maxValue;
144
145 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
146 (fabs(fmax-m_adjust->upper) < 0.2))
147 {
148 return;
149 }
150
151 m_adjust->lower = fmin;
152 m_adjust->upper = fmax;
153
154 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
155 }
156
157 int wxSlider::GetMin(void) const
158 {
159 return (int)(m_adjust->lower+0.5);
160 }
161
162 int wxSlider::GetMax(void) const
163 {
164 return (int)(m_adjust->upper+0.5);
165 }
166
167 void wxSlider::SetPageSize( int pageSize )
168 {
169 float fpage = (float)pageSize;
170
171 if (fabs(fpage-m_adjust->page_increment) < 0.2) return;
172
173 m_adjust->page_increment = fpage;
174
175 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
176 }
177
178 int wxSlider::GetPageSize(void) const
179 {
180 return (int)(m_adjust->page_increment+0.5);
181 }
182
183 void wxSlider::SetThumbLength( int len )
184 {
185 float flen = (float)len;
186
187 if (fabs(flen-m_adjust->page_size) < 0.2) return;
188
189 m_adjust->page_size = flen;
190
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
192 }
193
194 int wxSlider::GetThumbLength(void) const
195 {
196 return (int)(m_adjust->page_size+0.5);
197 }
198
199 void wxSlider::SetLineSize( int WXUNUSED(lineSize) )
200 {
201 }
202
203 int wxSlider::GetLineSize(void) const
204 {
205 return 0;
206 }
207
208 void wxSlider::SetTick( int WXUNUSED(tickPos) )
209 {
210 }
211
212 void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) )
213 {
214 }
215
216 int wxSlider::GetTickFreq(void) const
217 {
218 return 0;
219 }
220
221 void wxSlider::ClearTicks(void)
222 {
223 }
224
225 void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) )
226 {
227 }
228
229 int wxSlider::GetSelEnd(void) const
230 {
231 return 0;
232 }
233
234 int wxSlider::GetSelStart(void) const
235 {
236 return 0;
237 }
238
239 void wxSlider::ClearSel(void)
240 {
241 }
242
243 bool wxSlider::IsOwnGtkWindow( GdkWindow *window )
244 {
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) );
251 }
252
253 void wxSlider::ApplyWidgetStyle()
254 {
255 SetWidgetStyle();
256 gtk_widget_set_style( m_widget, m_widgetStyle );
257 }