File: FatFingers.h

package info (click to toggle)
qtwebkit-opensource-src 5.3.2%2Bdfsg-2~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 291,472 kB
  • sloc: cpp: 1,358,084; python: 70,286; ansic: 42,964; perl: 35,474; ruby: 12,229; objc: 9,465; xml: 8,396; asm: 3,866; yacc: 2,397; sh: 1,647; makefile: 644; lex: 644; java: 110
file content (197 lines) | stat: -rw-r--r-- 6,609 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef FatFingers_h
#define FatFingers_h

#include "HitTestResult.h"
#include "RenderLayer.h"

#include <BlackBerryPlatformIntRectRegion.h>

#include <utility>

#include <wtf/HashSet.h>
#include <wtf/ListHashSet.h>
#include <wtf/Vector.h>

namespace WebCore {
class Document;
class Element;
class IntPoint;
class IntRect;
class IntSize;
class Node;
}

#define DEBUG_FAT_FINGERS 0

namespace BlackBerry {
namespace WebKit {

class WebPagePrivate;
class FatFingersResult;
class TouchEventHandler;


class FatFingers {
public:
    enum TargetType { ClickableElement, Text };

    FatFingers(WebPagePrivate* webpage, const WebCore::IntPoint& contentPos, TargetType);
    ~FatFingers();

    const FatFingersResult findBestPoint();

#if DEBUG_FAT_FINGERS
    // These debug vars are all in content coordinates. They are public so
    // they can be read from BackingStore, which will draw a visible rect
    // around the fat fingers area.
    static WebCore::IntRect m_debugFatFingerRect;
    static WebCore::IntPoint m_debugFatFingerClickPosition;
    static WebCore::IntPoint m_debugFatFingerAdjustedPosition;
#endif

private:
    enum MatchingApproachForClickable { ClickableByDefault = 0, MadeClickableByTheWebpage, Done };

    typedef std::pair<WebCore::Node*, Platform::IntRectRegion> IntersectingRegion;

    bool checkFingerIntersection(const Platform::IntRectRegion&,
        const Platform::IntRectRegion& remainingFingerRegion,
        WebCore::Node*,
        Vector<IntersectingRegion>& intersectingRegions);

    bool findIntersectingRegions(WebCore::Document*,
        Vector<IntersectingRegion>& intersectingRegions,
        Platform::IntRectRegion& remainingFingerRegion);

    bool checkForClickableElement(WebCore::Element*,
        Vector<IntersectingRegion>& intersectingRegions,
        Platform::IntRectRegion& remainingFingerRegion,
        WebCore::RenderLayer*& lowestPositionedEnclosingLayerSoFar);

    bool checkForText(WebCore::Node*,
        Vector<IntersectingRegion>& intersectingRegions,
        Platform::IntRectRegion& fingerRegion);

    void setSuccessfulFatFingersResult(FatFingersResult&, WebCore::Node*, const WebCore::IntPoint&);

    void getNodesFromRect(WebCore::Document*, const WebCore::IntPoint&, ListHashSet<RefPtr<WebCore::Node> >&);

    bool isElementClickable(WebCore::Element*) const;

    inline WebCore::IntRect fingerRectForPoint(const WebCore::IntPoint&) const;
    void getAdjustedPaddings(const WebCore::IntPoint&, unsigned& top, unsigned& right, unsigned& bottom, unsigned& left) const;

    WebPagePrivate* m_webPage;
    WebCore::IntPoint m_contentPos;
    TargetType m_targetType;
};

class FatFingersResult {
public:

    FatFingersResult(const WebCore::IntPoint& p = WebCore::IntPoint::zero(), const FatFingers::TargetType targetType = FatFingers::ClickableElement)
        : m_originalPosition(p)
        , m_adjustedPosition(p)
        , m_targetType(targetType)
        , m_positionWasAdjusted(false)
        , m_isTextInput(false)
        , m_isValid(false)
        , m_nodeUnderFatFinger(0)
    {
    }

    void reset()
    {
        m_originalPosition = m_adjustedPosition = WebCore::IntPoint::zero();
        m_positionWasAdjusted = false;
        m_isTextInput = false;
        m_isValid = false;
        m_nodeUnderFatFinger = 0;
    }

    bool resultMatches(const WebCore::IntPoint& p, const FatFingers::TargetType targetType) const
    {
        return m_isValid && p == m_originalPosition && targetType == m_targetType;
    }

    WebCore::IntPoint originPosition() const { return m_originalPosition; }
    WebCore::IntPoint adjustedPosition() const { return m_adjustedPosition; }
    bool positionWasAdjusted() const { return m_isValid && m_positionWasAdjusted; }
    bool isTextInput() const { return m_isValid && !!m_nodeUnderFatFinger && m_isTextInput; }
    bool isValid() const { return m_isValid; }

    enum ContentType { ShadowContentAllowed, ShadowContentNotAllowed };

    WebCore::Node* node(ContentType type = ShadowContentAllowed, bool shouldUseRootEditableElement = false) const
    {
        if (!m_nodeUnderFatFinger || !m_nodeUnderFatFinger->inDocument())
            return 0;

        WebCore::Node* result = m_nodeUnderFatFinger.get();

        if (type == ShadowContentAllowed)
            return result;

        // Shadow trees can be nested.
        while (result->isInShadowTree())
            result = toElement(result->deprecatedShadowAncestorNode());

        if (!shouldUseRootEditableElement || !result->isElementNode())
            return result;

        // Retrieve the top level editable node
        while (!result->isRootEditableElement()) {
            WebCore::Element* parentElement = result->parentElement();
            if (!parentElement)
                break;
            result = parentElement;
        }

        return result;
    }

    WebCore::Element* nodeAsElementIfApplicable(ContentType type = ShadowContentAllowed, bool shouldUseRootEditableElement = false) const
    {
        WebCore::Node* result = node(type, shouldUseRootEditableElement);
        if (!result || !result->isElementNode())
            return 0;

        return static_cast<WebCore::Element*>(result);
    }

private:
    friend class WebKit::FatFingers;
    friend class WebKit::TouchEventHandler;

    WebCore::IntPoint m_originalPosition; // Main frame contents coordinates.
    WebCore::IntPoint m_adjustedPosition; // Main frame contents coordinates.
    FatFingers::TargetType m_targetType;
    bool m_positionWasAdjusted;
    bool m_isTextInput; // Check if the element under the touch point will require a VKB be displayed so that the touch down can be suppressed.
    bool m_isValid;
    RefPtr<WebCore::Node> m_nodeUnderFatFinger;
};

}
}

#endif // FatFingers_h