]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/scrolbar.cpp
added wxUSE_DEBUG_NEW_ALWAYS to --enable-mem_tracing (quite useless otherwise)
[wxWidgets.git] / src / gtk1 / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: scrolbar.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "scrolbar.h"
13 #endif
14
15 #include "wx/scrolbar.h"
16
17 #if wxUSE_SCROLLBAR
18
19 #include "wx/utils.h"
20
21 #include <math.h>
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 //-----------------------------------------------------------------------------
34 // data
35 //-----------------------------------------------------------------------------
36
37 extern bool g_blockEventsOnDrag;
38 extern bool g_blockEventsOnScroll;
39
40 static const float sensitivity = 0.02;
41
42 //-----------------------------------------------------------------------------
43 // "value_changed"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_scrollbar_callback( GtkAdjustment *adjust, wxScrollBar *win )
47 {
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (!win->m_hasVMT) return;
51 if (g_blockEventsOnDrag) return;
52
53 float diff = adjust->value - win->m_oldPos;
54 if (fabs(diff) < sensitivity) return;
55
56 win->m_oldPos = adjust->value;
57
58 GtkRange *range = GTK_RANGE( win->m_widget );
59
60 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
61 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP;
62 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN;
63 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP;
64 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN;
65
66 int value = (int)ceil(adjust->value);
67
68 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
69
70 wxScrollEvent event( command, win->GetId(), value, orient );
71 event.SetEventObject( win );
72 win->GetEventHandler()->ProcessEvent( event );
73
74 /*
75 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
76 cevent.SetEventObject( win );
77 win->ProcessEvent( cevent );
78 */
79 }
80
81 //-----------------------------------------------------------------------------
82 // "button_press_event" from slider
83 //-----------------------------------------------------------------------------
84
85 static gint gtk_scrollbar_button_press_callback( GtkRange *WXUNUSED(widget),
86 GdkEventButton *WXUNUSED(gdk_event),
87 wxScrollBar *win )
88 {
89 if (g_isIdle) wxapp_install_idle_handler();
90
91 win->m_isScrolling = TRUE;
92 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
93
94 return FALSE;
95 }
96
97 //-----------------------------------------------------------------------------
98 // "button_release_event" from slider
99 //-----------------------------------------------------------------------------
100
101 static gint gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
102 GdkEventButton *WXUNUSED(gdk_event),
103 wxScrollBar *win )
104 {
105 if (g_isIdle) wxapp_install_idle_handler();
106
107 wxASSERT( win->m_isScrolling );
108
109 win->m_isScrolling = FALSE;
110 // g_blockEventsOnScroll = FALSE;
111
112 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
113 int value = (int)ceil(win->m_adjust->value);
114 int dir = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
115
116 wxScrollEvent event( command, value, dir );
117 event.SetScrolling( FALSE );
118 event.SetEventObject( win );
119 win->GetEventHandler()->ProcessEvent( event );
120
121 return FALSE;
122 }
123
124 //-----------------------------------------------------------------------------
125 // wxScrollBar
126 //-----------------------------------------------------------------------------
127
128 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
129
130 wxScrollBar::~wxScrollBar(void)
131 {
132 }
133
134 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
135 const wxPoint& pos, const wxSize& size,
136 long style, const wxValidator& validator, const wxString& name )
137 {
138 m_needParent = TRUE;
139 m_acceptsFocus = TRUE;
140
141 if (!PreCreation( parent, pos, size ) ||
142 !CreateBase( parent, id, pos, size, style, validator, name ))
143 {
144 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
145 return FALSE;
146 }
147
148 m_oldPos = 0.0;
149
150 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
151 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
152 else
153 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
154
155 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
156
157 gtk_signal_connect( GTK_OBJECT(m_adjust),
158 "value_changed",
159 (GtkSignalFunc) gtk_scrollbar_callback,
160 (gpointer) this );
161
162 gtk_signal_connect( GTK_OBJECT(m_widget),
163 "button_press_event",
164 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
165 (gpointer) this );
166
167 gtk_signal_connect( GTK_OBJECT(m_widget),
168 "button_release_event",
169 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
170 (gpointer) this );
171
172 m_parent->DoAddChild( this );
173
174 PostCreation();
175
176 SetBackgroundColour( parent->GetBackgroundColour() );
177
178 Show( TRUE );
179
180 return TRUE;
181 }
182
183 int wxScrollBar::GetThumbPosition(void) const
184 {
185 return (int)(m_adjust->value+0.5);
186 }
187
188 int wxScrollBar::GetThumbSize() const
189 {
190 return (int)(m_adjust->page_size+0.5);
191 }
192
193 int wxScrollBar::GetPageSize() const
194 {
195 return (int)(m_adjust->page_increment+0.5);
196 }
197
198 int wxScrollBar::GetRange() const
199 {
200 return (int)(m_adjust->upper+0.5);
201 }
202
203 void wxScrollBar::SetThumbPosition( int viewStart )
204 {
205 if (m_isScrolling) return;
206
207 float fpos = (float)viewStart;
208 m_oldPos = fpos;
209 if (fabs(fpos-m_adjust->value) < 0.2) return;
210 m_adjust->value = fpos;
211
212 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
213 }
214
215 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
216 bool WXUNUSED(refresh) )
217 {
218 float fpos = (float)position;
219 float frange = (float)range;
220 float fthumb = (float)thumbSize;
221 float fpage = (float)pageSize;
222
223 if ((fabs(frange-m_adjust->upper) < 0.2) &&
224 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
225 (fabs(fpage-m_adjust->page_increment) < 0.2))
226 {
227 SetThumbPosition( position );
228 return;
229 }
230
231 m_oldPos = fpos;
232
233 m_adjust->lower = 0.0;
234 m_adjust->upper = frange;
235 m_adjust->value = fpos;
236 m_adjust->step_increment = 1.0;
237 m_adjust->page_increment = (float)(wxMax(fpage,0));
238 m_adjust->page_size = fthumb;
239
240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
241 }
242
243 /* Backward compatibility */
244 int wxScrollBar::GetValue(void) const
245 {
246 return GetThumbPosition();
247 }
248
249 void wxScrollBar::SetValue( int viewStart )
250 {
251 SetThumbPosition( viewStart );
252 }
253
254 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
255 {
256 int pos = (int)(m_adjust->value+0.5);
257 int thumb = (int)(m_adjust->page_size+0.5);
258 int page = (int)(m_adjust->page_increment+0.5);
259 int range = (int)(m_adjust->upper+0.5);
260
261 *viewStart = pos;
262 *viewLength = range;
263 *objectLength = thumb;
264 *pageLength = page;
265 }
266
267 int wxScrollBar::GetViewLength() const
268 {
269 return (int)(m_adjust->upper+0.5);
270 }
271
272 int wxScrollBar::GetObjectLength() const
273 {
274 return (int)(m_adjust->page_size+0.5);
275 }
276
277 void wxScrollBar::SetPageSize( int pageLength )
278 {
279 int pos = (int)(m_adjust->value+0.5);
280 int thumb = (int)(m_adjust->page_size+0.5);
281 int range = (int)(m_adjust->upper+0.5);
282 SetScrollbar( pos, thumb, range, pageLength );
283 }
284
285 void wxScrollBar::SetObjectLength( int objectLength )
286 {
287 int pos = (int)(m_adjust->value+0.5);
288 int page = (int)(m_adjust->page_increment+0.5);
289 int range = (int)(m_adjust->upper+0.5);
290 SetScrollbar( pos, objectLength, range, page );
291 }
292
293 void wxScrollBar::SetViewLength( int viewLength )
294 {
295 int pos = (int)(m_adjust->value+0.5);
296 int thumb = (int)(m_adjust->page_size+0.5);
297 int page = (int)(m_adjust->page_increment+0.5);
298 SetScrollbar( pos, thumb, viewLength, page );
299 }
300
301 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
302 {
303 GtkRange *range = GTK_RANGE(m_widget);
304 return ( (window == GTK_WIDGET(range)->window) ||
305 (window == range->trough) ||
306 (window == range->slider) ||
307 (window == range->step_forw) ||
308 (window == range->step_back) );
309 }
310
311 void wxScrollBar::ApplyWidgetStyle()
312 {
313 SetWidgetStyle();
314 gtk_widget_set_style( m_widget, m_widgetStyle );
315 }
316
317 #endif