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