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