]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
get rid of the built-in drop target in rich text controls before setting the user...
[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
de6185e2
WS
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
19#endif
20
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
66bd6b93 23//-----------------------------------------------------------------------------
add7cadd 24// "value_changed" from scrollbar
66bd6b93
RR
25//-----------------------------------------------------------------------------
26
865bb325 27extern "C" {
add7cadd
PC
28static void
29gtk_value_changed(GtkRange* range, wxScrollBar* win)
a2615ebc 30{
add7cadd
PC
31 wxEventType eventType = win->GetScrollEventType(range);
32 if (eventType != wxEVT_NULL)
7c2f14ee 33 {
add7cadd
PC
34 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
35 const int value = win->GetThumbPosition();
36 wxScrollEvent event(eventType, win->GetId(), value, orient);
37 event.SetEventObject(win);
38 win->GetEventHandler()->ProcessEvent(event);
39 if (!win->m_isScrolling)
40 {
41 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
42 event.SetEventObject(win);
43 win->GetEventHandler()->ProcessEvent(event);
44 }
91af0895 45 }
6de97a3b 46}
865bb325 47}
c801d85f 48
cb43b372 49//-----------------------------------------------------------------------------
add7cadd 50// "button_press_event" from scrollbar
cb43b372 51//-----------------------------------------------------------------------------
add7cadd 52
865bb325 53extern "C" {
add7cadd
PC
54static gboolean
55gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
cb43b372 56{
add7cadd
PC
57 win->m_mouseButtonDown = true;
58 return false;
cb43b372 59}
865bb325 60}
cb43b372 61
c918b2cd
PC
62//-----------------------------------------------------------------------------
63// "event_after" from scrollbar
64//-----------------------------------------------------------------------------
65
66extern "C" {
67static void
68gtk_event_after(GtkRange* range, GdkEvent* event, wxScrollBar* win)
69{
70 if (event->type == GDK_BUTTON_RELEASE)
71 {
72 g_signal_handlers_block_by_func(range, (void*)gtk_event_after, win);
73
74 const int value = win->GetThumbPosition();
75 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
76
77 wxScrollEvent event(wxEVT_SCROLL_THUMBRELEASE, win->GetId(), value, orient);
78 event.SetEventObject(win);
79 win->GetEventHandler()->ProcessEvent(event);
80
81 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
82 event2.SetEventObject(win);
83 win->GetEventHandler()->ProcessEvent(event2);
84 }
85}
86}
87
cb43b372 88//-----------------------------------------------------------------------------
add7cadd 89// "button_release_event" from scrollbar
cb43b372
RR
90//-----------------------------------------------------------------------------
91
865bb325 92extern "C" {
add7cadd 93static gboolean
c918b2cd 94gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win)
cb43b372 95{
add7cadd
PC
96 win->m_mouseButtonDown = false;
97 // If thumb tracking
2a23d363
RR
98 if (win->m_isScrolling)
99 {
add7cadd 100 win->m_isScrolling = false;
c918b2cd 101 // Hook up handler to send thumb release event after this emission is finished.
8ea30e36
PC
102 // To allow setting scroll position from event handler, sending event must
103 // be deferred until after the GtkRange handler for this signal has run
c918b2cd 104 g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win);
add7cadd 105 }
7c2f14ee 106
add7cadd 107 return false;
cb43b372 108}
865bb325 109}
cb43b372 110
b4071e91
RR
111//-----------------------------------------------------------------------------
112// wxScrollBar
113//-----------------------------------------------------------------------------
114
c801d85f
KB
115IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
116
add7cadd
PC
117wxScrollBar::wxScrollBar()
118{
119}
120
0a07a7d8 121wxScrollBar::~wxScrollBar()
c801d85f 122{
6de97a3b 123}
c801d85f 124
debe6624 125bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 126 const wxPoint& pos, const wxSize& size,
6de97a3b 127 long style, const wxValidator& validator, const wxString& name )
c801d85f 128{
4dcaf11a
RR
129 if (!PreCreation( parent, pos, size ) ||
130 !CreateBase( parent, id, pos, size, style, validator, name ))
131 {
223d09f6 132 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
91af0895 133 return false;
4dcaf11a 134 }
6de97a3b 135
add7cadd
PC
136 const bool isVertical = (style & wxSB_VERTICAL) != 0;
137 if (isVertical)
2d17d68f 138 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
d9bd1494
SB
139 else
140 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
141
add7cadd 142 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
a2615ebc 143
40e5ebbf 144 g_signal_connect_after(m_widget, "value_changed",
add7cadd
PC
145 G_CALLBACK(gtk_value_changed), this);
146 g_signal_connect(m_widget, "button_press_event",
147 G_CALLBACK(gtk_button_press_event), this);
148 g_signal_connect(m_widget, "button_release_event",
149 G_CALLBACK(gtk_button_release_event), this);
cb43b372 150
c918b2cd
PC
151 gulong handler_id;
152 handler_id = g_signal_connect(
153 m_widget, "event_after", G_CALLBACK(gtk_event_after), this);
154 g_signal_handler_block(m_widget, handler_id);
155
f03fc89f 156 m_parent->DoAddChild( this );
a2615ebc 157
abdeb9e7 158 PostCreation(size);
a2615ebc 159
91af0895 160 return true;
6de97a3b 161}
c801d85f 162
0a07a7d8 163int wxScrollBar::GetThumbPosition() const
c801d85f 164{
add7cadd
PC
165 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
166 return int(adj->value + 0.5);
6de97a3b 167}
c801d85f
KB
168
169int wxScrollBar::GetThumbSize() const
170{
add7cadd
PC
171 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
172 return int(adj->page_size);
6de97a3b 173}
c801d85f
KB
174
175int wxScrollBar::GetPageSize() const
176{
add7cadd
PC
177 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
178 return int(adj->page_increment);
6de97a3b 179}
c801d85f
KB
180
181int wxScrollBar::GetRange() const
182{
add7cadd
PC
183 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
184 return int(adj->upper);
6de97a3b 185}
c801d85f 186
4fabb575 187void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 188{
add7cadd 189 if (GetThumbPosition() != viewStart)
2d17d68f 190 {
add7cadd 191 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
8ea30e36
PC
192 const int i = (GtkRange*)m_widget == m_scrollBar[1];
193 const int max = int(adj->upper - adj->page_size);
194 if (viewStart > max)
195 viewStart = max;
196 if (viewStart < 0)
197 viewStart = 0;
198
199 m_scrollPos[i] =
200 adj->value = viewStart;
40e5ebbf 201
98264520
PC
202 g_signal_handlers_block_by_func(m_widget,
203 (gpointer)gtk_value_changed, this);
40e5ebbf
RR
204
205 gtk_adjustment_value_changed(adj);
98264520
PC
206
207 g_signal_handlers_unblock_by_func(m_widget,
208 (gpointer)gtk_value_changed, this);
2d17d68f 209 }
6de97a3b 210}
c801d85f 211
add7cadd 212void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
c801d85f 213{
de7bb802
PC
214 if (range == 0)
215 {
216 // GtkRange requires upper > lower
217 range =
218 thumbSize = 1;
219 }
8ea30e36
PC
220 if (position > range - thumbSize)
221 position = range - thumbSize;
222 if (position < 0)
223 position = 0;
add7cadd 224 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
add7cadd
PC
225 adj->step_increment = 1;
226 adj->page_increment = pageSize;
227 adj->page_size = thumbSize;
8ea30e36
PC
228 adj->upper = range;
229 SetThumbPosition(position);
230 gtk_adjustment_changed(adj);
6de97a3b 231}
c801d85f 232
debe6624 233void wxScrollBar::SetPageSize( int pageLength )
c801d85f 234{
add7cadd 235 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
6de97a3b 236}
c801d85f 237
add7cadd 238void wxScrollBar::SetRange(int range)
c801d85f 239{
add7cadd 240 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
6de97a3b 241}
c801d85f 242
ef5c70f9 243GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
b4071e91 244{
10bd1f7d 245 return m_widget->window;
b4071e91 246}
58614078 247
9d522606
RD
248// static
249wxVisualAttributes
250wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
251{
252 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
253}
254
de6185e2 255#endif // wxUSE_SCROLLBAR