1/*
2 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
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 program 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 program; 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
21#include "config.h"
22#include "TapHighlightController.h"
23
24#if ENABLE(TOUCH_EVENTS)
25
26#include "ShareableBitmap.h"
27#include "WKPage.h"
28#include "WebCoreArgumentCoders.h"
29#include "WebPage.h"
30#include "WebPageProxyMessages.h"
31#include "WebProcess.h"
32#include <WebCore/FocusController.h>
33#include <WebCore/Frame.h>
34#include <WebCore/FrameView.h>
35#include <WebCore/GestureTapHighlighter.h>
36#include <WebCore/GraphicsContext.h>
37#include <WebCore/Page.h>
38
39#include <WebCore/RenderObject.h>
40
41using namespace std;
42using namespace WebCore;
43
44namespace WebKit {
45
46TapHighlightController::TapHighlightController(WebPage* webPage)
47    : m_webPage(webPage)
48    , m_overlay(0)
49{
50}
51
52TapHighlightController::~TapHighlightController()
53{
54}
55
56void TapHighlightController::highlight(Node* node)
57{
58    ASSERT(node);
59
60    m_path = GestureTapHighlighter::pathForNodeHighlight(node);
61    m_color = node->renderer()->style()->tapHighlightColor();
62
63    if (!m_overlay) {
64        RefPtr<PageOverlay> overlay = PageOverlay::create(this);
65        m_overlay = overlay.get();
66        m_webPage->installPageOverlay(overlay.release());
67    } else
68        m_overlay->setNeedsDisplay();
69}
70
71void TapHighlightController::hideHighlight()
72{
73    if (m_overlay)
74        m_webPage->uninstallPageOverlay(m_overlay, /* fadeout */ true);
75}
76
77void TapHighlightController::pageOverlayDestroyed(PageOverlay*)
78{
79}
80
81void TapHighlightController::willMoveToWebPage(PageOverlay*, WebPage* webPage)
82{
83    if (webPage)
84        return;
85
86    // The page overlay is moving away from the web page, reset it.
87    ASSERT(m_overlay);
88    m_overlay = 0;
89}
90
91void TapHighlightController::didMoveToWebPage(PageOverlay*, WebPage*)
92{
93}
94
95static Color highlightColor(Color baseColor, float fractionFadedIn)
96{
97    return Color(baseColor.red(), baseColor.green(), baseColor.blue(), int(baseColor.alpha() * fractionFadedIn));
98}
99
100void TapHighlightController::drawRect(PageOverlay* pageOverlay, GraphicsContext& context, const IntRect& /*dirtyRect*/)
101{
102    if (m_path.isEmpty())
103        return;
104
105    {
106        GraphicsContextStateSaver stateSaver(context);
107        if (m_webPage->drawingArea()->pageOverlayShouldApplyFadeWhenPainting())
108            context.setFillColor(highlightColor(m_color, pageOverlay->fractionFadedIn() * 0.5f), ColorSpaceSRGB);
109        else
110            context.setFillColor(highlightColor(m_color, 0.5f), ColorSpaceSRGB);
111        context.fillPath(m_path);
112    }
113}
114
115bool TapHighlightController::mouseEvent(PageOverlay*, const WebMouseEvent&)
116{
117    return false;
118}
119
120} // namespace WebKit
121
122#endif // ENABLE(TOUCH_EVENTS)
123