1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WKBundlePageOverlay.h"
28
29#include "PageOverlay.h"
30#include "WKAPICast.h"
31#include "WKBundleAPICast.h"
32#include <WebCore/GraphicsContext.h>
33#include <wtf/PassOwnPtr.h>
34
35using namespace WebCore;
36using namespace WebKit;
37
38class PageOverlayClientImpl : public PageOverlay::Client {
39public:
40    static PassOwnPtr<PageOverlayClientImpl> create(WKBundlePageOverlayClient* client)
41    {
42        return adoptPtr(new PageOverlayClientImpl(client));
43    }
44
45private:
46    explicit PageOverlayClientImpl(WKBundlePageOverlayClient* client)
47        : m_client()
48    {
49        if (client)
50            m_client = *client;
51    }
52
53    // PageOverlay::Client.
54    virtual void pageOverlayDestroyed(PageOverlay*)
55    {
56        delete this;
57    }
58
59    virtual void willMoveToWebPage(PageOverlay* pageOverlay, WebPage* page)
60    {
61        if (!m_client.willMoveToPage)
62            return;
63
64        m_client.willMoveToPage(toAPI(pageOverlay), toAPI(page), m_client.clientInfo);
65    }
66
67    virtual void didMoveToWebPage(PageOverlay* pageOverlay, WebPage* page)
68    {
69        if (!m_client.didMoveToPage)
70            return;
71
72        m_client.didMoveToPage(toAPI(pageOverlay), toAPI(page), m_client.clientInfo);
73    }
74
75    virtual void drawRect(PageOverlay* pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
76    {
77        if (!m_client.drawRect)
78            return;
79
80        m_client.drawRect(toAPI(pageOverlay), graphicsContext.platformContext(), toAPI(dirtyRect), m_client.clientInfo);
81    }
82
83    virtual bool mouseEvent(PageOverlay* pageOverlay, const WebMouseEvent& event)
84    {
85        switch (event.type()) {
86        case WebEvent::MouseDown: {
87            if (!m_client.mouseDown)
88                return false;
89
90            return m_client.mouseDown(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
91        }
92        case WebEvent::MouseUp: {
93            if (!m_client.mouseUp)
94                return false;
95
96            return m_client.mouseUp(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
97        }
98        case WebEvent::MouseMove: {
99            if (event.button() == WebMouseEvent::NoButton) {
100                if (!m_client.mouseMoved)
101                    return false;
102
103                return m_client.mouseMoved(toAPI(pageOverlay), toAPI(event.position()), m_client.clientInfo);
104            }
105
106            // This is a MouseMove event with a mouse button pressed. Call mouseDragged.
107            if (!m_client.mouseDragged)
108                return false;
109
110            return m_client.mouseDragged(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
111        }
112
113        default:
114            return false;
115        }
116    }
117
118    WKBundlePageOverlayClient m_client;
119};
120
121WKTypeID WKBundlePageOverlayGetTypeID()
122{
123    return toAPI(PageOverlay::APIType);
124}
125
126WKBundlePageOverlayRef WKBundlePageOverlayCreate(WKBundlePageOverlayClient* wkClient)
127{
128    if (wkClient && wkClient->version)
129        return 0;
130
131    OwnPtr<PageOverlayClientImpl> clientImpl = PageOverlayClientImpl::create(wkClient);
132
133    return toAPI(PageOverlay::create(clientImpl.leakPtr()).leakRef());
134}
135
136void WKBundlePageOverlaySetNeedsDisplay(WKBundlePageOverlayRef bundlePageOverlayRef, WKRect rect)
137{
138    toImpl(bundlePageOverlayRef)->setNeedsDisplay(enclosingIntRect(toFloatRect(rect)));
139}
140
141float WKBundlePageOverlayFractionFadedIn(WKBundlePageOverlayRef bundlePageOverlayRef)
142{
143    return toImpl(bundlePageOverlayRef)->fractionFadedIn();
144}
145