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
39inline long long maxDistance()
40{
41    return std::numeric_limits<long long>::max();
42}
43
44inline int fudgeFactor()
45{
46    return 2;
47}
48
49bool isSpatialNavigationEnabled(const Frame*);
50
51// Spatially speaking, two given elements in a web page can be:
52// 1) Fully aligned: There is a full intersection between the rects, either
53//    vertically or horizontally.
54//
55// * Horizontally       * Vertically
56//    _
57//   |_|                   _ _ _ _ _ _
58//   |_|...... _          |_|_|_|_|_|_|
59//   |_|      |_|         .       .
60//   |_|......|_|   OR    .       .
61//   |_|      |_|         .       .
62//   |_|......|_|          _ _ _ _
63//   |_|                  |_|_|_|_|
64//
65//
66// 2) Partially aligned: There is a partial intersection between the rects, either
67//    vertically or horizontally.
68//
69// * Horizontally       * Vertically
70//    _                   _ _ _ _ _
71//   |_|                 |_|_|_|_|_|
72//   |_|.... _      OR           . .
73//   |_|    |_|                  . .
74//   |_|....|_|                  ._._ _
75//          |_|                  |_|_|_|
76//          |_|
77//
78// 3) Or, otherwise, not aligned at all.
79//
80// * Horizontally       * Vertically
81//         _              _ _ _ _
82//        |_|            |_|_|_|_|
83//        |_|                    .
84//        |_|                     .
85//       .          OR             .
86//    _ .                           ._ _ _ _ _
87//   |_|                            |_|_|_|_|_|
88//   |_|
89//   |_|
90//
91// "Totally Aligned" elements are preferable candidates to move
92// focus to over "Partially Aligned" ones, that on its turns are
93// more preferable than "Not Aligned".
94enum RectsAlignment {
95    None = 0,
96    Partial,
97    Full
98};
99
100struct FocusCandidate {
101    FocusCandidate()
102        : visibleNode(0)
103        , focusableNode(0)
104        , enclosingScrollableBox(0)
105        , distance(maxDistance())
106        , alignment(None)
107        , isOffscreen(true)
108        , isOffscreenAfterScrolling(true)
109    {
110    }
111
112    FocusCandidate(Node* n, FocusDirection);
113    explicit FocusCandidate(HTMLAreaElement* area, FocusDirection);
114    bool isNull() const { return !visibleNode; }
115    bool inScrollableContainer() const { return visibleNode && enclosingScrollableBox; }
116    bool isFrameOwnerElement() const { return visibleNode && visibleNode->isFrameOwnerElement(); }
117    Document* document() const { return visibleNode ? &visibleNode->document() : 0; }
118
119    // We handle differently visibleNode and FocusableNode to properly handle the areas of imagemaps,
120    // where visibleNode would represent the image element and focusableNode would represent the area element.
121    // In all other cases, visibleNode and focusableNode are one and the same.
122    Node* visibleNode;
123    Node* focusableNode;
124    Node* enclosingScrollableBox;
125    long long distance;
126    RectsAlignment alignment;
127    LayoutRect rect;
128    bool isOffscreen;
129    bool isOffscreenAfterScrolling;
130};
131
132bool hasOffscreenRect(Node*, FocusDirection direction = FocusDirectionNone);
133bool scrollInDirection(Frame*, FocusDirection);
134bool scrollInDirection(Node* container, FocusDirection);
135bool canScrollInDirection(const Node* container, FocusDirection);
136bool canScrollInDirection(const Frame*, FocusDirection);
137bool canBeScrolledIntoView(FocusDirection, const FocusCandidate&);
138bool areElementsOnSameLine(const FocusCandidate& firstCandidate, const FocusCandidate& secondCandidate);
139bool isValidCandidate(FocusDirection, const FocusCandidate&, FocusCandidate&);
140void distanceDataForNode(FocusDirection, const FocusCandidate& current, FocusCandidate& candidate);
141Node* scrollableEnclosingBoxOrParentFrameForNodeInDirection(FocusDirection, Node*);
142LayoutRect nodeRectInAbsoluteCoordinates(Node*, bool ignoreBorder = false);
143LayoutRect frameRectInAbsoluteCoordinates(Frame*);
144LayoutRect virtualRectForDirection(FocusDirection, const LayoutRect& startingRect, LayoutUnit width = 0);
145LayoutRect virtualRectForAreaElementAndDirection(HTMLAreaElement*, FocusDirection);
146HTMLFrameOwnerElement* frameOwnerElement(FocusCandidate&);
147
148} // namspace WebCore
149
150#endif // SpatialNavigation_h
151