]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolwin.cpp
update to the latest version of the patch at #10320 the wxApp::DoYield implementation
[wxWidgets.git] / src / gtk / scrolwin.cpp
CommitLineData
30954328 1/////////////////////////////////////////////////////////////////////////////
e1437456 2// Name: gtk/scrolwin.cpp
30954328 3// Purpose: wxScrolledWindow implementation
47a3ff38 4// Author: Robert Roebling
566d84a7 5// Modified by: Ron Lee
d32e78bd 6// Vadim Zeitlin: removed 90% of duplicated common code
30954328
RR
7// Created: 01/02/97
8// RCS-ID: $Id$
47a3ff38 9// Copyright: (c) Robert Roebling
65571936 10// Licence: wxWindows licence
30954328
RR
11/////////////////////////////////////////////////////////////////////////////
12
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
30954328
RR
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
e1bf3ad3 28#include "wx/scrolwin.h"
9e691f46 29#include "wx/gtk/private.h"
30954328
RR
30
31// ============================================================================
32// implementation
33// ============================================================================
34
30954328 35// ----------------------------------------------------------------------------
d32e78bd 36// wxScrollHelper implementation
30954328
RR
37// ----------------------------------------------------------------------------
38
29e1398f
VZ
39void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
40 int noUnitsX, int noUnitsY,
41 int xPos, int yPos,
42 bool noRefresh)
30954328
RR
43{
44 m_xScrollPixelsPerLine = pixelsPerUnitX;
45 m_yScrollPixelsPerLine = pixelsPerUnitY;
39c869a6 46
cd38dd5b 47 m_win->m_scrollBar[wxWindow::ScrollDir_Horz]->adjustment->value =
add7cadd 48 m_xScrollPosition = xPos;
cd38dd5b 49 m_win->m_scrollBar[wxWindow::ScrollDir_Vert]->adjustment->value =
add7cadd 50 m_yScrollPosition = yPos;
30954328 51
f18f464c
VZ
52 int w = noUnitsX * pixelsPerUnitX;
53 int h = noUnitsY * pixelsPerUnitY;
54 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
55 h ? h : wxDefaultCoord);
2b5f62a0 56
0295d448
PC
57 // Query view start after m_targetWindow->SetVirtualSize(...) since
58 // that call can change the current=old scrolling position!
59 int xs, ys;
60 GetViewStart(& xs, & ys);
61 int old_x = m_xScrollPixelsPerLine * xs;
62 int old_y = m_yScrollPixelsPerLine * ys;
80e8b5de 63
add7cadd
PC
64 // If the target is not the same as the window with the scrollbars,
65 // then we need to update the scrollbars here, since they won't have
66 // been updated by SetVirtualSize().
67 if (m_targetWindow != m_win)
68 {
69 AdjustScrollbars();
70 }
0295d448
PC
71
72 if (!noRefresh)
73 {
74 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
75 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
76
77 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
78 }
30954328
RR
79}
80
29e1398f
VZ
81void wxScrollHelper::DoAdjustScrollbar(GtkRange* range,
82 int pixelsPerLine,
83 int winSize,
84 int virtSize,
85 int *pos,
86 int *lines,
87 int *linesPerPage)
30954328 88{
f6814d01
PC
89 int upper;
90 int page_size;
fb18afdd 91 if (pixelsPerLine > 0 && winSize > 0 && winSize < virtSize)
4ca24f18 92 {
f6814d01
PC
93 upper = (virtSize + pixelsPerLine - 1) / pixelsPerLine;
94 page_size = winSize / pixelsPerLine;
5713b349
RR
95 *lines = upper;
96 *linesPerPage = page_size;
5713b349
RR
97 }
98 else
99 {
100 // GtkRange won't allow upper == lower, so for disabled state use [0,1]
101 // with a page size of 1. This will also clamp position to 0.
f6814d01
PC
102 upper = 1;
103 page_size = 1;
5713b349
RR
104 *lines = 0;
105 *linesPerPage = 0;
4ca24f18 106 }
9ec927f8 107
f6814d01
PC
108 GtkAdjustment* adj = range->adjustment;
109 adj->step_increment = 1;
80e8b5de 110 adj->page_increment =
f6814d01
PC
111 adj->page_size = page_size;
112 gtk_range_set_range(range, 0, upper);
ae4c09a8
PC
113
114 // ensure that the scroll position is always in valid range
115 if (*pos > *lines)
116 *pos = *lines;
30954328
RR
117}
118
29e1398f 119void wxScrollHelper::AdjustScrollbars()
566d84a7 120{
9ec927f8 121 int vw, vh;
3d9ecb87
VZ
122 m_targetWindow->GetVirtualSize(&vw, &vh);
123
124 int w, h;
125 const wxSize availSize = GetSizeAvailableForScrollTarget(
126 m_win->GetSize() - m_win->GetWindowBorderSize());
127 if ( availSize.x >= vw && availSize.y >= vh )
128 {
129 w = availSize.x;
130 h = availSize.y;
131
132 // we know that the scrollbars will be removed
133 DoAdjustHScrollbar(w, vw);
134 DoAdjustVScrollbar(h, vh);
135
136 return;
137 }
830ed6d9 138
cd38dd5b 139 m_targetWindow->GetClientSize(&w, NULL);
9ec927f8
VZ
140 DoAdjustHScrollbar(w, vw);
141
cd38dd5b 142 m_targetWindow->GetClientSize(NULL, &h);
9ec927f8 143 DoAdjustVScrollbar(h, vh);
cd38dd5b
PC
144
145 const int w_old = w;
146 m_targetWindow->GetClientSize(&w, NULL);
9ec927f8 147 if ( w != w_old )
cd38dd5b
PC
148 {
149 // It is necessary to repeat the calculations in this case to avoid an
150 // observed infinite series of size events, involving alternating
151 // changes in visibility of the scrollbars.
152 // At this point, GTK+ has already queued a resize, which will cause
153 // AdjustScrollbars() to be called again. If the scrollbar visibility
154 // is not correct before then, yet another resize will occur, possibly
155 // leading to an unending series if the sizes are just right.
9ec927f8
VZ
156 DoAdjustHScrollbar(w, vw);
157
cd38dd5b 158 m_targetWindow->GetClientSize(NULL, &h);
9ec927f8 159 DoAdjustVScrollbar(h, vh);
cd38dd5b 160 }
830ed6d9
RR
161}
162
29e1398f
VZ
163void wxScrollHelper::DoScrollOneDir(int orient,
164 int pos,
165 int pixelsPerLine,
166 int *posOld)
30486297 167{
d32e78bd 168 if ( pos != -1 && pos != *posOld && pixelsPerLine )
30486297 169 {
add7cadd
PC
170 m_win->SetScrollPos(orient, pos);
171 pos = m_win->GetScrollPos(orient);
30486297 172
d32e78bd
VZ
173 int diff = (*posOld - pos)*pixelsPerLine;
174 m_targetWindow->ScrollWindow(orient == wxHORIZONTAL ? diff : 0,
175 orient == wxHORIZONTAL ? 0 : diff);
30954328 176
d32e78bd 177 *posOld = pos;
2b5f62a0 178 }
30954328
RR
179}
180
29e1398f 181void wxScrollHelper::DoScroll( int x_pos, int y_pos )
30954328 182{
d32e78bd 183 wxCHECK_RET( m_targetWindow != 0, _T("No target window") );
30954328 184
0b0f6f87
VZ
185 DoScrollOneDir(wxHORIZONTAL, x_pos, m_xScrollPixelsPerLine, &m_xScrollPosition);
186 DoScrollOneDir(wxVERTICAL, y_pos, m_yScrollPixelsPerLine, &m_yScrollPosition);
30954328 187}
6362d82b
VZ
188
189// ----------------------------------------------------------------------------
190// scrollbars visibility
191// ----------------------------------------------------------------------------
192
193namespace
194{
195
196GtkPolicyType GtkPolicyFromWX(wxScrollbarVisibility visibility)
197{
198 GtkPolicyType policy;
199 switch ( visibility )
200 {
201 case wxSHOW_SB_NEVER:
202 policy = GTK_POLICY_NEVER;
203 break;
204
205 case wxSHOW_SB_DEFAULT:
206 policy = GTK_POLICY_AUTOMATIC;
207 break;
208
209 case wxSHOW_SB_ALWAYS:
210 policy = GTK_POLICY_ALWAYS;
211 break;
212 }
213
214 return policy;
215}
216
217} // anonymous namespace
218
29e1398f
VZ
219void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz,
220 wxScrollbarVisibility vert)
6362d82b
VZ
221{
222 GtkScrolledWindow * const scrolled = GTK_SCROLLED_WINDOW(m_win->m_widget);
223 wxCHECK_RET( scrolled, "window must be created" );
224
225 gtk_scrolled_window_set_policy(scrolled,
226 GtkPolicyFromWX(horz),
227 GtkPolicyFromWX(vert));
228}
229