1/*
2 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2009 Antonio Gomes <tonikitoo@webkit.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef SpatialNavigation_h
22#define SpatialNavigation_h
23
24#include "FocusDirection.h"
25#include "HTMLFrameOwnerElement.h"
26#include "LayoutRect.h"
27#include "Node.h"
28
29#include <limits>
30
31namespace WebCore {
32
33class Element;
34class Frame;
35class HTMLAreaElement;
36class IntRect;
37class RenderObject;
38
39using namespace std;
40
41inline long long maxDistance()
42{
43    return numeric_limits<long long>::max();
44}
45
46inline int fudgeFactor()
47{
48    return 2;
49}
50
51bool isSpatialNavigationEnabled(const Frame*);
52
53// Spatially speaking, two given elements in a web page can be:
54// 1) Fully aligned: There is a full intersection between the rects, either
55//    vertically or horizontally.
56//
57// * Horizontally       * Vertically
58//    _
59//   |_|                   _ _ _ _ _ _
60//   |_|...... _          |_|_|_|_|_|_|
61//   |_|      |_|         .       .
62//   |_|......|_|   OR    .       .
63//   |_|      |_|         .       .
64//   |_|......|_|          _ _ _ _
65//   |_|                  |_|_|_|_|
66//
67//
68// 2) Partially aligned: There is a partial intersection between the rects, either
69//    vertically or horizontally.
70//
71// * Horizontally       * Vertically
72//    _                   _ _ _ _ _
73//   |_|                 |_|_|_|_|_|
74//   |_|.... _      OR           . .
75//   |_|    |_|                  . .
76//   |_|....|_|                  ._._ _
77//          |_|                  |_|_|_|
78//          |_|
79//
80// 3) Or, otherwise, not aligned at all.
81//
82// * Horizontally       * Vertically
83//         _              _ _ _ _
84//        |_|            |_|_|_|_|
85//        |_|                    .
86//        |_|                     .
87//       .          OR             .
88//    _ .                           ._ _ _ _ _
89//   |_|                            |_|_|_|_|_|
90//   |_|
91//   |_|
92//
93// "Totally Aligned" elements are preferable candidates to move
94// focus to over "Partially Aligned" ones, that on its turns are
95// more preferable than "Not Aligned".
96enum RectsAlignment {
97    None = 0,
98    Partial,
99    Full
100};
101
102struct FocusCandidate {
103    FocusCandidate()
104        : visibleNode(0)
105        , focusableNode(0)
106        , enclosingScrollableBox(0)
107        , distance(maxDistance())
108        , parentDistance(maxDistance())
109        , alignment(None)
110        , parentAlignment(None)
111        , isOffscreen(true)
112        , isOffscreenAfterScrolling(true)
113    {
114    }
115
116    FocusCandidate(Node* n, FocusDirection);
117    explicit FocusCandidate(HTMLAreaElement* area, FocusDirection);
118    bool isNull() const { return !visibleNode; }
119    bool inScrollableContainer() const { return visibleNode && enclosingScrollableBox; }
120    bool isFrameOwnerElement() const { return visibleNode && visibleNode->isFrameOwnerElement(); }
121    Document* document() const { return visibleNode ? visibleNode->document() : 0; }
122
123    // We handle differently visibleNode and FocusableNode to properly handle the areas of imagemaps,
124    // where visibleNode would represent the image element and focusableNode would represent the area element.
125    // In all other cases, visibleNode and focusableNode are one and the same.
126    Node* visibleNode;
127    Node* focusableNode;
128    Node* enclosingScrollableBox;
129    long long distance;
130    long long parentDistance;
131    RectsAlignment alignment;
132    RectsAlignment parentAlignment;
133    LayoutRect rect;
134    bool isOffscreen;
135    bool isOffscreenAfterScrolling;
136};
137
138bool hasOffscreenRect(Node*, FocusDirection direction = FocusDirectionNone);
139bool scrollInDirection(Frame*, FocusDirection);
140bool scrollInDirection(Node* container, FocusDirection);
141bool canScrollInDirection(const Node* container, FocusDirection);
142bool canScrollInDirection(const Frame*, FocusDirection);
143bool canBeScrolledIntoView(FocusDirection, const FocusCandidate&);
144bool areElementsOnSameLine(const FocusCandidate& firstCandidate, const FocusCandidate& secondCandidate);
145void distanceDataForNode(FocusDirection, const FocusCandidate& current, FocusCandidate& candidate);
146Node* scrollableEnclosingBoxOrParentFrameForNodeInDirection(FocusDirection, Node*);
147LayoutRect nodeRectInAbsoluteCoordinates(Node*, bool ignoreBorder = false);
148LayoutRect frameRectInAbsoluteCoordinates(Frame*);
149LayoutRect virtualRectForDirection(FocusDirection, const LayoutRect& startingRect, LayoutUnit width = 0);
150LayoutRect virtualRectForAreaElementAndDirection(HTMLAreaElement*, FocusDirection);
151HTMLFrameOwnerElement* frameOwnerElement(FocusCandidate&);
152
153} // namspace WebCore
154
155#endif // SpatialNavigation_h
156