blob: 59a4df03517e3c75f4e5310c88fbe003d37ee431 [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
2 * Copyright (C) 2012 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "PageWidgetDelegate.h"
33
34#include "PageOverlayList.h"
35#include "WebInputEvent.h"
36#include "WebInputEventConversion.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000037#include "core/frame/FrameView.h"
38#include "core/frame/LocalFrame.h"
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000039#include "core/page/AutoscrollController.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010040#include "core/page/EventHandler.h"
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000041#include "core/rendering/RenderView.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000042#include "core/rendering/compositing/RenderLayerCompositor.h"
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000043#include "platform/graphics/GraphicsContext.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010044#include "wtf/CurrentTime.h"
45
46using namespace WebCore;
47
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000048namespace blink {
Ben Murdoche69819b2013-07-17 14:56:49 +010049
50static inline FrameView* mainFrameView(Page* page)
51{
52 if (!page)
53 return 0;
54 // FIXME: Can we remove this check?
55 if (!page->mainFrame())
56 return 0;
57 return page->mainFrame()->view();
58}
59
60void PageWidgetDelegate::animate(Page* page, double monotonicFrameBeginTime)
61{
Torne (Richard Coles)09380292014-02-21 12:17:33 +000062 RefPtr<FrameView> view = mainFrameView(page);
Ben Murdoche69819b2013-07-17 14:56:49 +010063 if (!view)
64 return;
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000065 page->autoscrollController().animate(monotonicFrameBeginTime);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000066 page->animator().serviceScriptedAnimations(monotonicFrameBeginTime);
Ben Murdoche69819b2013-07-17 14:56:49 +010067}
68
69void PageWidgetDelegate::layout(Page* page)
70{
Ben Murdoch07a852d2014-03-31 11:51:52 +010071 if (!page || !page->mainFrame())
Ben Murdoche69819b2013-07-17 14:56:49 +010072 return;
Ben Murdoch07a852d2014-03-31 11:51:52 +010073 page->animator().updateLayoutAndStyleForPainting();
Ben Murdoche69819b2013-07-17 14:56:49 +010074}
75
76void PageWidgetDelegate::paint(Page* page, PageOverlayList* overlays, WebCanvas* canvas, const WebRect& rect, CanvasBackground background)
77{
78 if (rect.isEmpty())
79 return;
80 GraphicsContext gc(canvas);
81 gc.setCertainlyOpaque(background == Opaque);
82 gc.applyDeviceScaleFactor(page->deviceScaleFactor());
83 gc.setUseHighResMarkers(page->deviceScaleFactor() > 1.5f);
84 IntRect dirtyRect(rect);
Torne (Richard Coles)09380292014-02-21 12:17:33 +000085 gc.save(); // Needed to save the canvas, not the GraphicsContext.
Ben Murdoche69819b2013-07-17 14:56:49 +010086 FrameView* view = mainFrameView(page);
87 // FIXME: Can we remove the mainFrame()->document() check?
88 if (view && page->mainFrame()->document()) {
89 gc.clip(dirtyRect);
90 view->paint(&gc, dirtyRect);
91 if (overlays)
92 overlays->paintWebFrame(gc);
93 } else {
94 gc.fillRect(dirtyRect, Color::white);
95 }
96 gc.restore();
97}
98
99bool PageWidgetDelegate::handleInputEvent(Page* page, PageWidgetEventHandler& handler, const WebInputEvent& event)
100{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000101 LocalFrame* frame = page ? page->mainFrame() : 0;
Ben Murdoche69819b2013-07-17 14:56:49 +0100102 switch (event.type) {
103
104 // FIXME: WebKit seems to always return false on mouse events processing
105 // methods. For now we'll assume it has processed them (as we are only
106 // interested in whether keyboard events are processed).
107 case WebInputEvent::MouseMove:
108 if (!frame || !frame->view())
109 return true;
110 handler.handleMouseMove(*frame, *static_cast<const WebMouseEvent*>(&event));
111 return true;
112 case WebInputEvent::MouseLeave:
113 if (!frame || !frame->view())
114 return true;
115 handler.handleMouseLeave(*frame, *static_cast<const WebMouseEvent*>(&event));
116 return true;
117 case WebInputEvent::MouseDown:
118 if (!frame || !frame->view())
119 return true;
120 handler.handleMouseDown(*frame, *static_cast<const WebMouseEvent*>(&event));
121 return true;
122 case WebInputEvent::MouseUp:
123 if (!frame || !frame->view())
124 return true;
125 handler.handleMouseUp(*frame, *static_cast<const WebMouseEvent*>(&event));
126 return true;
127
128 case WebInputEvent::MouseWheel:
129 if (!frame || !frame->view())
130 return false;
131 return handler.handleMouseWheel(*frame, *static_cast<const WebMouseWheelEvent*>(&event));
132
133 case WebInputEvent::RawKeyDown:
134 case WebInputEvent::KeyDown:
135 case WebInputEvent::KeyUp:
136 return handler.handleKeyEvent(*static_cast<const WebKeyboardEvent*>(&event));
137
138 case WebInputEvent::Char:
139 return handler.handleCharEvent(*static_cast<const WebKeyboardEvent*>(&event));
140 case WebInputEvent::GestureScrollBegin:
141 case WebInputEvent::GestureScrollEnd:
142 case WebInputEvent::GestureScrollUpdate:
143 case WebInputEvent::GestureScrollUpdateWithoutPropagation:
144 case WebInputEvent::GestureFlingStart:
145 case WebInputEvent::GestureFlingCancel:
146 case WebInputEvent::GestureTap:
147 case WebInputEvent::GestureTapUnconfirmed:
148 case WebInputEvent::GestureTapDown:
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100149 case WebInputEvent::GestureShowPress:
Ben Murdoche69819b2013-07-17 14:56:49 +0100150 case WebInputEvent::GestureTapCancel:
151 case WebInputEvent::GestureDoubleTap:
152 case WebInputEvent::GestureTwoFingerTap:
153 case WebInputEvent::GestureLongPress:
154 case WebInputEvent::GestureLongTap:
155 return handler.handleGestureEvent(*static_cast<const WebGestureEvent*>(&event));
156
157 case WebInputEvent::TouchStart:
158 case WebInputEvent::TouchMove:
159 case WebInputEvent::TouchEnd:
160 case WebInputEvent::TouchCancel:
161 if (!frame || !frame->view())
162 return false;
163 return handler.handleTouchEvent(*frame, *static_cast<const WebTouchEvent*>(&event));
164
165 case WebInputEvent::GesturePinchBegin:
166 case WebInputEvent::GesturePinchEnd:
167 case WebInputEvent::GesturePinchUpdate:
168 // FIXME: Once PlatformGestureEvent is updated to support pinch, this
169 // should call handleGestureEvent, just like it currently does for
170 // gesture scroll.
171 return false;
172
173 default:
174 return false;
175 }
176}
177
178// ----------------------------------------------------------------
179// Default handlers for PageWidgetEventHandler
180
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000181void PageWidgetEventHandler::handleMouseMove(LocalFrame& mainFrame, const WebMouseEvent& event)
Ben Murdoche69819b2013-07-17 14:56:49 +0100182{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000183 mainFrame.eventHandler().handleMouseMoveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
Ben Murdoche69819b2013-07-17 14:56:49 +0100184}
185
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000186void PageWidgetEventHandler::handleMouseLeave(LocalFrame& mainFrame, const WebMouseEvent& event)
Ben Murdoche69819b2013-07-17 14:56:49 +0100187{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000188 mainFrame.eventHandler().handleMouseLeaveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
Ben Murdoche69819b2013-07-17 14:56:49 +0100189}
190
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000191void PageWidgetEventHandler::handleMouseDown(LocalFrame& mainFrame, const WebMouseEvent& event)
Ben Murdoche69819b2013-07-17 14:56:49 +0100192{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000193 mainFrame.eventHandler().handleMousePressEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
Ben Murdoche69819b2013-07-17 14:56:49 +0100194}
195
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000196void PageWidgetEventHandler::handleMouseUp(LocalFrame& mainFrame, const WebMouseEvent& event)
Ben Murdoche69819b2013-07-17 14:56:49 +0100197{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000198 mainFrame.eventHandler().handleMouseReleaseEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
Ben Murdoche69819b2013-07-17 14:56:49 +0100199}
200
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000201bool PageWidgetEventHandler::handleMouseWheel(LocalFrame& mainFrame, const WebMouseWheelEvent& event)
Ben Murdoche69819b2013-07-17 14:56:49 +0100202{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000203 return mainFrame.eventHandler().handleWheelEvent(PlatformWheelEventBuilder(mainFrame.view(), event));
Ben Murdoche69819b2013-07-17 14:56:49 +0100204}
205
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000206bool PageWidgetEventHandler::handleTouchEvent(LocalFrame& mainFrame, const WebTouchEvent& event)
Ben Murdoche69819b2013-07-17 14:56:49 +0100207{
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000208 return mainFrame.eventHandler().handleTouchEvent(PlatformTouchEventBuilder(mainFrame.view(), event));
Ben Murdoche69819b2013-07-17 14:56:49 +0100209}
210
211}