1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * Portions are Copyright (C) 1998 Netscape Communications Corporation.
5 *
6 * Other contributors:
7 * Robert O'Callahan <roc+@cs.cmu.edu>
8 * David Baron <dbaron@fas.harvard.edu>
9 * Christian Biesinger <cbiesinger@web.de>
10 * Randall Jesup <rjesup@wgate.com>
11 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
12 * Josh Soref <timeless@mac.com>
13 * Boris Zbarsky <bzbarsky@mit.edu>
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Alternatively, the contents of this file may be used under the terms
30 * of either the Mozilla Public License Version 1.1, found at
31 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
32 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
33 * (the "GPL"), in which case the provisions of the MPL or the GPL are
34 * applicable instead of those above. If you wish to allow use of your
35 * version of this file only under the terms of one of those two
36 * licenses (the MPL or the GPL) and not to allow others to use your
37 * version of this file under the LGPL, indicate your decision by
38 * deletingthe provisions above and replace them with the notice and
39 * other provisions required by the MPL or the GPL, as the case may be.
40 * If you do not delete the provisions above, a recipient may use your
41 * version of this file under any of the LGPL, the MPL or the GPL.
42 */
43
44 #include "config.h"
45 #include "core/rendering/ScrollAlignment.h"
46
47 #include "platform/geometry/LayoutRect.h"
48
49 namespace WebCore {
50
51 const ScrollAlignment ScrollAlignment::alignCenterIfNeeded = { ScrollAlignmentNoScroll, ScrollAlignmentCenter, ScrollAlignmentClosestEdge };
52 const ScrollAlignment ScrollAlignment::alignToEdgeIfNeeded = { ScrollAlignmentNoScroll, ScrollAlignmentClosestEdge, ScrollAlignmentClosestEdge };
53 const ScrollAlignment ScrollAlignment::alignCenterAlways = { ScrollAlignmentCenter, ScrollAlignmentCenter, ScrollAlignmentCenter };
54 const ScrollAlignment ScrollAlignment::alignTopAlways = { ScrollAlignmentTop, ScrollAlignmentTop, ScrollAlignmentTop };
55 const ScrollAlignment ScrollAlignment::alignBottomAlways = { ScrollAlignmentBottom, ScrollAlignmentBottom, ScrollAlignmentBottom };
56
57 #define MIN_INTERSECT_FOR_REVEAL 32
58
getRectToExpose(const LayoutRect & visibleRect,const LayoutRect & exposeRect,const ScrollAlignment & alignX,const ScrollAlignment & alignY)59 LayoutRect ScrollAlignment::getRectToExpose(const LayoutRect& visibleRect, const LayoutRect& exposeRect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
60 {
61 // Determine the appropriate X behavior.
62 ScrollAlignmentBehavior scrollX;
63 LayoutRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
64 LayoutUnit intersectWidth = intersection(visibleRect, exposeRectX).width();
65 if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL) {
66 // If the rectangle is fully visible, use the specified visible behavior.
67 // If the rectangle is partially visible, but over a certain threshold,
68 // then treat it as fully visible to avoid unnecessary horizontal scrolling
69 scrollX = getVisibleBehavior(alignX);
70 } else if (intersectWidth == visibleRect.width()) {
71 // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
72 scrollX = getVisibleBehavior(alignX);
73 if (scrollX == ScrollAlignmentCenter)
74 scrollX = ScrollAlignmentNoScroll;
75 } else if (intersectWidth > 0) {
76 // If the rectangle is partially visible, but not above the minimum threshold, use the specified partial behavior
77 scrollX = getPartialBehavior(alignX);
78 } else {
79 scrollX = getHiddenBehavior(alignX);
80 }
81
82 if (scrollX == ScrollAlignmentClosestEdge) {
83 // Closest edge is the right in two cases:
84 // (1) exposeRect to the right of and smaller than visibleRect
85 // (2) exposeRect to the left of and larger than visibleRect
86 if ((exposeRect.maxX() > visibleRect.maxX() && exposeRect.width() < visibleRect.width())
87 || (exposeRect.maxX() < visibleRect.maxX() && exposeRect.width() > visibleRect.width())) {
88 scrollX = ScrollAlignmentRight;
89 }
90 }
91
92 // Given the X behavior, compute the X coordinate.
93 LayoutUnit x;
94 if (scrollX == ScrollAlignmentNoScroll)
95 x = visibleRect.x();
96 else if (scrollX == ScrollAlignmentRight)
97 x = exposeRect.maxX() - visibleRect.width();
98 else if (scrollX == ScrollAlignmentCenter)
99 x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
100 else
101 x = exposeRect.x();
102
103 // Determine the appropriate Y behavior.
104 ScrollAlignmentBehavior scrollY;
105 LayoutRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
106 LayoutUnit intersectHeight = intersection(visibleRect, exposeRectY).height();
107 if (intersectHeight == exposeRect.height()) {
108 // If the rectangle is fully visible, use the specified visible behavior.
109 scrollY = getVisibleBehavior(alignY);
110 } else if (intersectHeight == visibleRect.height()) {
111 // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
112 scrollY = getVisibleBehavior(alignY);
113 if (scrollY == ScrollAlignmentCenter)
114 scrollY = ScrollAlignmentNoScroll;
115 } else if (intersectHeight > 0) {
116 // If the rectangle is partially visible, use the specified partial behavior
117 scrollY = getPartialBehavior(alignY);
118 } else {
119 scrollY = getHiddenBehavior(alignY);
120 }
121
122 if (scrollY == ScrollAlignmentClosestEdge) {
123 // Closest edge is the bottom in two cases:
124 // (1) exposeRect below and smaller than visibleRect
125 // (2) exposeRect above and larger than visibleRect
126 if ((exposeRect.maxY() > visibleRect.maxY() && exposeRect.height() < visibleRect.height())
127 || (exposeRect.maxY() < visibleRect.maxY() && exposeRect.height() > visibleRect.height())) {
128 scrollY = ScrollAlignmentBottom;
129 }
130 }
131
132 // Given the Y behavior, compute the Y coordinate.
133 LayoutUnit y;
134 if (scrollY == ScrollAlignmentNoScroll)
135 y = visibleRect.y();
136 else if (scrollY == ScrollAlignmentBottom)
137 y = exposeRect.maxY() - visibleRect.height();
138 else if (scrollY == ScrollAlignmentCenter)
139 y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
140 else
141 y = exposeRect.y();
142
143 return LayoutRect(LayoutPoint(x, y), visibleRect.size());
144 }
145
146 }; // namespace WebCore
147