]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/scrolbar.cpp
Make the app object be global in case it is run multiple times, such
[wxWidgets.git] / src / gtk / scrolbar.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/scrolbar.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_SCROLLBAR
14
15#include "wx/scrolbar.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
19#endif
20
21#include "wx/math.h"
22#include "wx/gtk/private.h"
23
24//-----------------------------------------------------------------------------
25// data
26//-----------------------------------------------------------------------------
27
28extern bool g_blockEventsOnDrag;
29static wxEventType g_currentUpDownEvent = wxEVT_NULL;
30
31static const float sensitivity = 0.02;
32
33//-----------------------------------------------------------------------------
34// "value_changed"
35//-----------------------------------------------------------------------------
36
37// FIXME: is GtkScrollType really passed to us as 2nd argument?
38
39extern "C" {
40static void gtk_scrollbar_callback( GtkAdjustment *adjust,
41 wxScrollBar *win )
42{
43 if (g_isIdle) wxapp_install_idle_handler();
44
45 if (!win->m_hasVMT) return;
46 if (g_blockEventsOnDrag) return;
47
48 float diff = adjust->value - win->m_oldPos;
49 if (fabs(diff) < sensitivity) return;
50
51 win->m_oldPos = adjust->value;
52
53 wxEventType command = GtkScrollTypeToWx(GTK_SCROLL_JUMP);
54
55 double dvalue = adjust->value;
56 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
57
58 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
59
60 // throw a LINEUP / LINEDOWN event if necessary
61 if (g_currentUpDownEvent != wxEVT_NULL)
62 {
63 wxScrollEvent event( g_currentUpDownEvent, win->GetId(), value, orient );
64 event.SetEventObject( win );
65 win->GetEventHandler()->ProcessEvent( event );
66 }
67
68 // throw other event (wxEVT_SCROLL_THUMBTRACK)
69 wxScrollEvent event( command, win->GetId(), value, orient );
70 event.SetEventObject( win );
71 win->GetEventHandler()->ProcessEvent( event );
72
73/*
74 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
75 cevent.SetEventObject( win );
76 win->ProcessEvent( cevent );
77*/
78}
79}
80
81//-----------------------------------------------------------------------------
82// "button_press_event" from slider
83//-----------------------------------------------------------------------------
84extern "C" {
85static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
86 GdkEventButton *gdk_event,
87 wxScrollBar *win )
88{
89 if (g_isIdle) wxapp_install_idle_handler();
90
91 // check if a LINEUP/LINEDOWN event must be thrown
92 // I suppose here the size of scrollbar top/bottom buttons is 16px height
93 if (gdk_event->type == GDK_BUTTON_PRESS && gdk_event->button == 1)
94 {
95 int scroll_height, mouse_pos;
96
97 // get the mouse position when the click is done
98 if (win->HasFlag(wxSB_VERTICAL))
99 {
100 scroll_height = GTK_WIDGET(widget)->allocation.height - 16;
101 mouse_pos = (int)gdk_event->y;
102 }
103 else
104 {
105 scroll_height = GTK_WIDGET(widget)->allocation.width - 16;
106 mouse_pos = (int)gdk_event->x;
107 }
108
109 // compare mouse position to scrollbar height
110 if (mouse_pos > scroll_height)
111 g_currentUpDownEvent = wxEVT_SCROLL_LINEDOWN;
112 else if (mouse_pos < 16)
113 g_currentUpDownEvent = wxEVT_SCROLL_LINEUP;
114 }
115
116 return FALSE;
117}
118}
119
120//-----------------------------------------------------------------------------
121// "button_release_event" from slider
122//-----------------------------------------------------------------------------
123
124extern "C" {
125static gint
126gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
127 GdkEventButton *WXUNUSED(gdk_event),
128 wxScrollBar *win )
129{
130 if (g_isIdle)
131 wxapp_install_idle_handler();
132
133 if (win->m_isScrolling)
134 {
135 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
136 int value = (int)ceil(win->m_adjust->value);
137 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
138
139 wxScrollEvent event( command, win->GetId(), value, orient );
140 event.SetEventObject( win );
141 win->GetEventHandler()->ProcessEvent( event );
142 }
143
144 win->m_isScrolling = false;
145
146 // reset the LINEUP/LINEDOWN flag when the mouse button is released
147 g_currentUpDownEvent = wxEVT_NULL;
148
149 return FALSE;
150}
151}
152
153//-----------------------------------------------------------------------------
154// wxScrollBar
155//-----------------------------------------------------------------------------
156
157IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
158
159wxScrollBar::~wxScrollBar()
160{
161}
162
163bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
164 const wxPoint& pos, const wxSize& size,
165 long style, const wxValidator& validator, const wxString& name )
166{
167 m_needParent = true;
168 m_acceptsFocus = true;
169
170 if (!PreCreation( parent, pos, size ) ||
171 !CreateBase( parent, id, pos, size, style, validator, name ))
172 {
173 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
174 return false;
175 }
176
177 m_oldPos = 0.0;
178
179 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
180 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
181 else
182 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
183
184 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
185 if ( style & wxSB_VERTICAL )
186 {
187 SetVScrollAdjustment(m_adjust);
188 }
189
190 g_signal_connect (m_adjust, "value_changed",
191 G_CALLBACK (gtk_scrollbar_callback), this);
192 g_signal_connect (m_widget, "button_press_event",
193 G_CALLBACK (gtk_scrollbar_button_press_callback),
194 this);
195 g_signal_connect (m_widget, "button_release_event",
196 G_CALLBACK (gtk_scrollbar_button_release_callback),
197 this);
198
199 m_parent->DoAddChild( this );
200
201 PostCreation(size);
202
203 return true;
204}
205
206int wxScrollBar::GetThumbPosition() const
207{
208 double val = m_adjust->value;
209 return (int)(val < 0 ? val - 0.5 : val + 0.5);
210}
211
212int wxScrollBar::GetThumbSize() const
213{
214 return (int)(m_adjust->page_size+0.5);
215}
216
217int wxScrollBar::GetPageSize() const
218{
219 return (int)(m_adjust->page_increment+0.5);
220}
221
222int wxScrollBar::GetRange() const
223{
224 return (int)(m_adjust->upper+0.5);
225}
226
227void wxScrollBar::SetThumbPosition( int viewStart )
228{
229 if (m_isScrolling) return;
230
231 float fpos = (float)viewStart;
232 m_oldPos = fpos;
233 if (fabs(fpos-m_adjust->value) < 0.2) return;
234 m_adjust->value = fpos;
235
236 g_signal_handlers_disconnect_by_func (m_adjust,
237 (gpointer) gtk_scrollbar_callback,
238 this);
239
240 g_signal_emit_by_name (m_adjust, "value_changed");
241
242 g_signal_connect (m_adjust, "value_changed",
243 G_CALLBACK (gtk_scrollbar_callback), this);
244}
245
246void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
247 bool WXUNUSED(refresh) )
248{
249 float fpos = (float)position;
250 float frange = (float)range;
251 float fthumb = (float)thumbSize;
252 float fpage = (float)pageSize;
253
254 if ((fabs(frange-m_adjust->upper) < 0.2) &&
255 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
256 (fabs(fpage-m_adjust->page_increment) < 0.2))
257 {
258 SetThumbPosition( position );
259 return;
260 }
261
262 m_oldPos = fpos;
263
264 m_adjust->lower = 0.0;
265 m_adjust->upper = frange;
266 m_adjust->value = fpos;
267 m_adjust->step_increment = 1.0;
268 m_adjust->page_increment = (float)(wxMax(fpage,0));
269 m_adjust->page_size = fthumb;
270
271 g_signal_emit_by_name (m_adjust, "changed");
272}
273
274/* Backward compatibility */
275int wxScrollBar::GetValue() const
276{
277 return GetThumbPosition();
278}
279
280void wxScrollBar::SetValue( int viewStart )
281{
282 SetThumbPosition( viewStart );
283}
284
285void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
286{
287 int pos = (int)(m_adjust->value+0.5);
288 int thumb = (int)(m_adjust->page_size+0.5);
289 int page = (int)(m_adjust->page_increment+0.5);
290 int range = (int)(m_adjust->upper+0.5);
291
292 *viewStart = pos;
293 *viewLength = range;
294 *objectLength = thumb;
295 *pageLength = page;
296}
297
298int wxScrollBar::GetViewLength() const
299{
300 return (int)(m_adjust->upper+0.5);
301}
302
303int wxScrollBar::GetObjectLength() const
304{
305 return (int)(m_adjust->page_size+0.5);
306}
307
308void wxScrollBar::SetPageSize( int pageLength )
309{
310 int pos = (int)(m_adjust->value+0.5);
311 int thumb = (int)(m_adjust->page_size+0.5);
312 int range = (int)(m_adjust->upper+0.5);
313 SetScrollbar( pos, thumb, range, pageLength );
314}
315
316void wxScrollBar::SetObjectLength( int objectLength )
317{
318 int pos = (int)(m_adjust->value+0.5);
319 int page = (int)(m_adjust->page_increment+0.5);
320 int range = (int)(m_adjust->upper+0.5);
321 SetScrollbar( pos, objectLength, range, page );
322}
323
324void wxScrollBar::SetViewLength( int viewLength )
325{
326 int pos = (int)(m_adjust->value+0.5);
327 int thumb = (int)(m_adjust->page_size+0.5);
328 int page = (int)(m_adjust->page_increment+0.5);
329 SetScrollbar( pos, thumb, viewLength, page );
330}
331
332bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
333{
334 GtkRange *range = GTK_RANGE(m_widget);
335 return ( (window == GTK_WIDGET(range)->window) );
336}
337
338wxSize wxScrollBar::DoGetBestSize() const
339{
340 return wxControl::DoGetBestSize();
341}
342
343// static
344wxVisualAttributes
345wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
346{
347 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
348}
349
350#endif // wxUSE_SCROLLBAR