1 | /* |
2 | * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
3 | * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de) |
4 | * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) |
5 | * Copyright (C) 2003, 2005, 2006, 2008, 2013 Apple Inc. All rights reserved. |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Library General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public License |
18 | * along with this library; see the file COPYING.LIB. If not, write to |
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "config.h" |
24 | #include "MouseRelatedEvent.h" |
25 | |
26 | #include "DOMWindow.h" |
27 | #include "Document.h" |
28 | #include "Frame.h" |
29 | #include "FrameView.h" |
30 | #include "LayoutPoint.h" |
31 | #include "RenderLayer.h" |
32 | #include "RenderObject.h" |
33 | |
34 | namespace WebCore { |
35 | |
36 | MouseRelatedEvent::MouseRelatedEvent(const AtomicString& eventType, CanBubble canBubble, IsCancelable isCancelable, IsComposed isComposed, |
37 | MonotonicTime timestamp, RefPtr<WindowProxy>&& view, int detail, |
38 | const IntPoint& screenLocation, const IntPoint& windowLocation, const IntPoint& movementDelta, OptionSet<Modifier> modifiers, IsSimulated isSimulated, IsTrusted isTrusted) |
39 | : UIEventWithKeyState(eventType, canBubble, isCancelable, isComposed, timestamp, WTFMove(view), detail, modifiers, isTrusted) |
40 | , m_screenLocation(screenLocation) |
41 | #if ENABLE(POINTER_LOCK) |
42 | , m_movementDelta(movementDelta) |
43 | #endif |
44 | , m_isSimulated(isSimulated == IsSimulated::Yes) |
45 | { |
46 | #if !ENABLE(POINTER_LOCK) |
47 | UNUSED_PARAM(movementDelta); |
48 | #endif |
49 | init(m_isSimulated, windowLocation); |
50 | } |
51 | |
52 | MouseRelatedEvent::MouseRelatedEvent(const AtomicString& type, IsCancelable isCancelable, MonotonicTime timestamp, RefPtr<WindowProxy>&& view, const IntPoint& globalLocation, OptionSet<Modifier> modifiers) |
53 | : MouseRelatedEvent(type, CanBubble::Yes, isCancelable, IsComposed::Yes, timestamp, |
54 | WTFMove(view), 0, globalLocation, globalLocation /* Converted in init */, { }, modifiers, IsSimulated::No) |
55 | { |
56 | } |
57 | |
58 | MouseRelatedEvent::MouseRelatedEvent(const AtomicString& eventType, const MouseRelatedEventInit& initializer, IsTrusted isTrusted) |
59 | : UIEventWithKeyState(eventType, initializer) |
60 | , m_screenLocation(IntPoint(initializer.screenX, initializer.screenY)) |
61 | #if ENABLE(POINTER_LOCK) |
62 | , m_movementDelta(IntPoint(0, 0)) |
63 | #endif |
64 | { |
65 | ASSERT_UNUSED(isTrusted, isTrusted == IsTrusted::No); |
66 | init(false, IntPoint(0, 0)); |
67 | } |
68 | |
69 | void MouseRelatedEvent::init(bool isSimulated, const IntPoint& windowLocation) |
70 | { |
71 | if (!isSimulated) { |
72 | if (auto* frameView = frameViewFromWindowProxy(view())) { |
73 | FloatPoint absolutePoint = frameView->windowToContents(windowLocation); |
74 | FloatPoint documentPoint = frameView->absoluteToDocumentPoint(absolutePoint); |
75 | m_pageLocation = flooredLayoutPoint(documentPoint); |
76 | m_clientLocation = pagePointToClientPoint(m_pageLocation, frameView); |
77 | } |
78 | } |
79 | |
80 | initCoordinates(); |
81 | } |
82 | |
83 | void MouseRelatedEvent::initCoordinates() |
84 | { |
85 | // Set up initial values for coordinates. |
86 | // Correct values are computed lazily, see computeRelativePosition. |
87 | m_layerLocation = m_pageLocation; |
88 | m_offsetLocation = m_pageLocation; |
89 | |
90 | computePageLocation(); |
91 | m_hasCachedRelativePosition = false; |
92 | } |
93 | |
94 | FrameView* MouseRelatedEvent::frameViewFromWindowProxy(WindowProxy* windowProxy) |
95 | { |
96 | if (!windowProxy || !is<DOMWindow>(windowProxy->window())) |
97 | return nullptr; |
98 | |
99 | auto* frame = downcast<DOMWindow>(*windowProxy->window()).frame(); |
100 | return frame ? frame->view() : nullptr; |
101 | } |
102 | |
103 | LayoutPoint MouseRelatedEvent::pagePointToClientPoint(LayoutPoint pagePoint, FrameView* frameView) |
104 | { |
105 | if (!frameView) |
106 | return pagePoint; |
107 | |
108 | return flooredLayoutPoint(frameView->documentToClientPoint(pagePoint)); |
109 | } |
110 | |
111 | LayoutPoint MouseRelatedEvent::pagePointToAbsolutePoint(LayoutPoint pagePoint, FrameView* frameView) |
112 | { |
113 | if (!frameView) |
114 | return pagePoint; |
115 | |
116 | return pagePoint.scaled(frameView->documentToAbsoluteScaleFactor()); |
117 | } |
118 | |
119 | void MouseRelatedEvent::initCoordinates(const LayoutPoint& clientLocation) |
120 | { |
121 | // Set up initial values for coordinates. |
122 | // Correct values are computed lazily, see computeRelativePosition. |
123 | FloatSize documentToClientOffset; |
124 | if (auto* frameView = frameViewFromWindowProxy(view())) |
125 | documentToClientOffset = frameView->documentToClientOffset(); |
126 | |
127 | m_clientLocation = clientLocation; |
128 | m_pageLocation = clientLocation - LayoutSize(documentToClientOffset); |
129 | |
130 | m_layerLocation = m_pageLocation; |
131 | m_offsetLocation = m_pageLocation; |
132 | |
133 | computePageLocation(); |
134 | m_hasCachedRelativePosition = false; |
135 | } |
136 | |
137 | float MouseRelatedEvent::documentToAbsoluteScaleFactor() const |
138 | { |
139 | if (auto* frameView = frameViewFromWindowProxy(view())) |
140 | return frameView->documentToAbsoluteScaleFactor(); |
141 | |
142 | return 1; |
143 | } |
144 | |
145 | void MouseRelatedEvent::computePageLocation() |
146 | { |
147 | m_absoluteLocation = pagePointToAbsolutePoint(m_pageLocation, frameViewFromWindowProxy(view())); |
148 | } |
149 | |
150 | void MouseRelatedEvent::receivedTarget() |
151 | { |
152 | m_hasCachedRelativePosition = false; |
153 | } |
154 | |
155 | void MouseRelatedEvent::computeRelativePosition() |
156 | { |
157 | if (!is<Node>(target())) |
158 | return; |
159 | auto& targetNode = downcast<Node>(*target()); |
160 | |
161 | // Compute coordinates that are based on the target. |
162 | m_layerLocation = m_pageLocation; |
163 | m_offsetLocation = m_pageLocation; |
164 | |
165 | // Must have an updated render tree for this math to work correctly. |
166 | targetNode.document().updateLayoutIgnorePendingStylesheets(); |
167 | |
168 | // Adjust offsetLocation to be relative to the target's position. |
169 | if (RenderObject* r = targetNode.renderer()) { |
170 | m_offsetLocation = LayoutPoint(r->absoluteToLocal(absoluteLocation(), UseTransforms)); |
171 | float scaleFactor = 1 / documentToAbsoluteScaleFactor(); |
172 | if (scaleFactor != 1.0f) |
173 | m_offsetLocation.scale(scaleFactor); |
174 | } |
175 | |
176 | // Adjust layerLocation to be relative to the layer. |
177 | // FIXME: event.layerX and event.layerY are poorly defined, |
178 | // and probably don't always correspond to RenderLayer offsets. |
179 | // https://bugs.webkit.org/show_bug.cgi?id=21868 |
180 | Node* n = &targetNode; |
181 | while (n && !n->renderer()) |
182 | n = n->parentNode(); |
183 | |
184 | RenderLayer* layer; |
185 | if (n && (layer = n->renderer()->enclosingLayer())) { |
186 | for (; layer; layer = layer->parent()) { |
187 | m_layerLocation -= toLayoutSize(layer->location()); |
188 | } |
189 | } |
190 | |
191 | m_hasCachedRelativePosition = true; |
192 | } |
193 | |
194 | FloatPoint MouseRelatedEvent::locationInRootViewCoordinates() const |
195 | { |
196 | if (auto* frameView = frameViewFromWindowProxy(view())) |
197 | return frameView->contentsToRootView(roundedIntPoint(m_absoluteLocation)); |
198 | |
199 | return m_absoluteLocation; |
200 | } |
201 | |
202 | int MouseRelatedEvent::layerX() |
203 | { |
204 | if (!m_hasCachedRelativePosition) |
205 | computeRelativePosition(); |
206 | return m_layerLocation.x(); |
207 | } |
208 | |
209 | int MouseRelatedEvent::layerY() |
210 | { |
211 | if (!m_hasCachedRelativePosition) |
212 | computeRelativePosition(); |
213 | return m_layerLocation.y(); |
214 | } |
215 | |
216 | int MouseRelatedEvent::offsetX() |
217 | { |
218 | if (isSimulated()) |
219 | return 0; |
220 | if (!m_hasCachedRelativePosition) |
221 | computeRelativePosition(); |
222 | return roundToInt(m_offsetLocation.x()); |
223 | } |
224 | |
225 | int MouseRelatedEvent::offsetY() |
226 | { |
227 | if (isSimulated()) |
228 | return 0; |
229 | if (!m_hasCachedRelativePosition) |
230 | computeRelativePosition(); |
231 | return roundToInt(m_offsetLocation.y()); |
232 | } |
233 | |
234 | int MouseRelatedEvent::pageX() const |
235 | { |
236 | return m_pageLocation.x(); |
237 | } |
238 | |
239 | int MouseRelatedEvent::pageY() const |
240 | { |
241 | return m_pageLocation.y(); |
242 | } |
243 | |
244 | const LayoutPoint& MouseRelatedEvent::pageLocation() const |
245 | { |
246 | return m_pageLocation; |
247 | } |
248 | |
249 | int MouseRelatedEvent::x() const |
250 | { |
251 | // FIXME: This is not correct. |
252 | // See Microsoft documentation and <http://www.quirksmode.org/dom/w3c_events.html>. |
253 | return m_clientLocation.x(); |
254 | } |
255 | |
256 | int MouseRelatedEvent::y() const |
257 | { |
258 | // FIXME: This is not correct. |
259 | // See Microsoft documentation and <http://www.quirksmode.org/dom/w3c_events.html>. |
260 | return m_clientLocation.y(); |
261 | } |
262 | |
263 | } // namespace WebCore |
264 | |