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