1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19 */
20 #ifndef HitTestResult_h
21 #define HitTestResult_h
22 
23 #include "FloatRect.h"
24 #include "IntPoint.h"
25 #include "IntRect.h"
26 #include "IntSize.h"
27 #include "TextDirection.h"
28 #include <wtf/Forward.h>
29 #include <wtf/ListHashSet.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/RefPtr.h>
32 
33 namespace WebCore {
34 
35 class Element;
36 class Frame;
37 #if ENABLE(VIDEO)
38 class HTMLMediaElement;
39 #endif
40 class Image;
41 class IntRect;
42 class KURL;
43 class Node;
44 class Scrollbar;
45 
46 class HitTestResult {
47 public:
48     typedef ListHashSet<RefPtr<Node> > NodeSet;
49 
50     HitTestResult();
51     HitTestResult(const IntPoint&);
52     // Pass non-negative padding values to perform a rect-based hit test.
53     HitTestResult(const IntPoint& centerPoint, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding);
54     HitTestResult(const HitTestResult&);
55     ~HitTestResult();
56     HitTestResult& operator=(const HitTestResult&);
57 
innerNode()58     Node* innerNode() const { return m_innerNode.get(); }
innerNonSharedNode()59     Node* innerNonSharedNode() const { return m_innerNonSharedNode.get(); }
point()60     IntPoint point() const { return m_point; }
localPoint()61     IntPoint localPoint() const { return m_localPoint; }
URLElement()62     Element* URLElement() const { return m_innerURLElement.get(); }
scrollbar()63     Scrollbar* scrollbar() const { return m_scrollbar.get(); }
isOverWidget()64     bool isOverWidget() const { return m_isOverWidget; }
65 
66     void setToNonShadowAncestor();
67 
68     void setInnerNode(Node*);
69     void setInnerNonSharedNode(Node*);
setPoint(const IntPoint & p)70     void setPoint(const IntPoint& p) { m_point = p; }
setLocalPoint(const IntPoint & p)71     void setLocalPoint(const IntPoint& p) { m_localPoint = p; }
72     void setURLElement(Element*);
73     void setScrollbar(Scrollbar*);
setIsOverWidget(bool b)74     void setIsOverWidget(bool b) { m_isOverWidget = b; }
75 
76     Frame* targetFrame() const;
77     bool isSelected() const;
78     String spellingToolTip(TextDirection&) const;
79     String replacedString() const;
80     String title(TextDirection&) const;
81     String altDisplayString() const;
82     String titleDisplayString() const;
83     Image* image() const;
84     IntRect imageRect() const;
85     KURL absoluteImageURL() const;
86     KURL absoluteMediaURL() const;
87     KURL absoluteLinkURL() const;
88     String textContent() const;
89     bool isLiveLink() const;
90     bool isContentEditable() const;
91     void toggleMediaControlsDisplay() const;
92     void toggleMediaLoopPlayback() const;
93     void enterFullscreenForVideo() const;
94     bool mediaControlsEnabled() const;
95     bool mediaLoopEnabled() const;
96     bool mediaPlaying() const;
97     bool mediaSupportsFullscreen() const;
98     void toggleMediaPlayState() const;
99     bool mediaHasAudio() const;
100     bool mediaIsVideo() const;
101     bool mediaMuted() const;
102     void toggleMediaMuteState() const;
103 
104     // Rect-based hit test related methods.
isRectBasedTest()105     bool isRectBasedTest() const { return m_isRectBased; }
106     IntRect rectForPoint(int x, int y) const;
107     IntRect rectForPoint(const IntPoint&) const;
108     static IntRect rectForPoint(const IntPoint&, unsigned topPadding, unsigned rightPadding, unsigned bottomPadding, unsigned leftPadding);
topPadding()109     int topPadding() const { return m_topPadding; }
rightPadding()110     int rightPadding() const { return m_rightPadding; }
bottomPadding()111     int bottomPadding() const { return m_bottomPadding; }
leftPadding()112     int leftPadding() const { return m_leftPadding; }
113 
114     // Returns true if it is rect-based hit test and needs to continue until the rect is fully
115     // enclosed by the boundaries of a node.
116     bool addNodeToRectBasedTestResult(Node*, int x, int y, const IntRect& = IntRect());
117     bool addNodeToRectBasedTestResult(Node*, int x, int y, const FloatRect&);
118     void append(const HitTestResult&);
119 
120     // If m_rectBasedTestResult is 0 then set it to a new NodeSet. Return *m_rectBasedTestResult. Lazy allocation makes
121     // sense because the NodeSet is seldom necessary, and it's somewhat expensive to allocate and initialize. This method does
122     // the same thing as mutableRectBasedTestResult(), but here the return value is const.
123     const NodeSet& rectBasedTestResult() const;
124 
125 private:
126     NodeSet& mutableRectBasedTestResult(); // See above.
127 
128 #if ENABLE(VIDEO)
129     HTMLMediaElement* mediaElement() const;
130 #endif
131 
132     RefPtr<Node> m_innerNode;
133     RefPtr<Node> m_innerNonSharedNode;
134     IntPoint m_point;
135     IntPoint m_localPoint; // A point in the local coordinate space of m_innerNonSharedNode's renderer.  Allows us to efficiently
136                            // determine where inside the renderer we hit on subsequent operations.
137     RefPtr<Element> m_innerURLElement;
138     RefPtr<Scrollbar> m_scrollbar;
139     bool m_isOverWidget; // Returns true if we are over a widget (and not in the border/padding area of a RenderWidget for example).
140     bool m_isRectBased;
141     int m_topPadding;
142     int m_rightPadding;
143     int m_bottomPadding;
144     int m_leftPadding;
145     mutable OwnPtr<NodeSet> m_rectBasedTestResult;
146 };
147 
rectForPoint(int x,int y)148 inline IntRect HitTestResult::rectForPoint(int x, int y) const
149 {
150     return rectForPoint(IntPoint(x, y), m_topPadding, m_rightPadding, m_bottomPadding, m_leftPadding);
151 }
152 
153 // Formula:
154 // x = p.x() - rightPadding
155 // y = p.y() - topPadding
156 // width = leftPadding + rightPadding + 1
157 // height = topPadding + bottomPadding + 1
rectForPoint(const IntPoint & point)158 inline IntRect HitTestResult::rectForPoint(const IntPoint& point) const
159 {
160     return rectForPoint(point, m_topPadding, m_rightPadding, m_bottomPadding, m_leftPadding);
161 }
162 
163 String displayString(const String&, const Node*);
164 
165 } // namespace WebCore
166 
167 #endif // HitTestResult_h
168