initial import
[vuplus_webkit] / Source / WebCore / dom / MouseRelatedEvent.cpp
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 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 "RenderLayer.h"
31 #include "RenderObject.h"
32
33 namespace WebCore {
34
35 MouseRelatedEvent::MouseRelatedEvent()
36     : m_isSimulated(false)
37     , m_hasCachedRelativePosition(false)
38 {
39 }
40
41 static LayoutSize contentsScrollOffset(AbstractView* abstractView)
42 {
43     if (!abstractView)
44         return LayoutSize();
45     Frame* frame = abstractView->frame();
46     if (!frame)
47         return LayoutSize();
48     FrameView* frameView = frame->view();
49     if (!frameView)
50         return LayoutSize();
51     return LayoutSize(frameView->scrollX() / frame->pageZoomFactor(),
52         frameView->scrollY() / frame->pageZoomFactor());
53 }
54
55 MouseRelatedEvent::MouseRelatedEvent(const AtomicString& eventType, bool canBubble, bool cancelable, PassRefPtr<AbstractView> abstractView,
56                                      int detail, const LayoutPoint& screenLocation, const LayoutPoint& windowLocation,
57                                      bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool isSimulated)
58     : UIEventWithKeyState(eventType, canBubble, cancelable, abstractView, detail, ctrlKey, altKey, shiftKey, metaKey)
59     , m_screenLocation(screenLocation)
60     , m_isSimulated(isSimulated)
61 {
62     LayoutPoint adjustedPageLocation;
63     LayoutPoint scrollPosition;
64
65     Frame* frame = view() ? view()->frame() : 0;
66     if (frame && !isSimulated) {
67         if (FrameView* frameView = frame->view()) {
68             scrollPosition = frameView->scrollPosition();
69             adjustedPageLocation = frameView->windowToContents(windowLocation);
70             float pageZoom = frame->pageZoomFactor();
71             if (pageZoom != 1.0f) {
72                 // Adjust our pageX and pageY to account for the page zoom.
73                 adjustedPageLocation.scale(1 / pageZoom, 1 / pageZoom);
74
75                 // FIXME: Change this to use float math and proper rounding (or
76                 // better yet, use LayoutPoint::scale).
77                 scrollPosition.setX(scrollPosition.x() / pageZoom);
78                 scrollPosition.setY(scrollPosition.y() / pageZoom);
79             }
80         }
81     }
82
83     m_clientLocation = adjustedPageLocation - toSize(scrollPosition);
84     m_pageLocation = adjustedPageLocation;
85
86     initCoordinates();
87 }
88
89 void MouseRelatedEvent::initCoordinates()
90 {
91     // Set up initial values for coordinates.
92     // Correct values are computed lazily, see computeRelativePosition.
93     m_layerLocation = m_pageLocation;
94     m_offsetLocation = m_pageLocation;
95
96     computePageLocation();
97     m_hasCachedRelativePosition = false;
98 }
99
100 void MouseRelatedEvent::initCoordinates(const LayoutPoint& clientLocation)
101 {
102     // Set up initial values for coordinates.
103     // Correct values are computed lazily, see computeRelativePosition.
104     m_clientLocation = clientLocation;
105     m_pageLocation = clientLocation + contentsScrollOffset(view());
106
107     m_layerLocation = m_pageLocation;
108     m_offsetLocation = m_pageLocation;
109
110     computePageLocation();
111     m_hasCachedRelativePosition = false;
112 }
113
114 static float pageZoomFactor(const UIEvent* event)
115 {
116     DOMWindow* window = event->view();
117     if (!window)
118         return 1;
119     Frame* frame = window->frame();
120     if (!frame)
121         return 1;
122     return frame->pageZoomFactor();
123 }
124
125 void MouseRelatedEvent::computePageLocation()
126 {
127     float zoomFactor = pageZoomFactor(this);
128     setAbsoluteLocation(roundedLayoutPoint(FloatPoint(pageX() * zoomFactor, pageY() * zoomFactor)));
129 }
130
131 void MouseRelatedEvent::receivedTarget()
132 {
133     m_hasCachedRelativePosition = false;
134 }
135
136 void MouseRelatedEvent::computeRelativePosition()
137 {
138     Node* targetNode = target() ? target()->toNode() : 0;
139     if (!targetNode)
140         return;
141
142     // Compute coordinates that are based on the target.
143     m_layerLocation = m_pageLocation;
144     m_offsetLocation = m_pageLocation;
145
146     // Must have an updated render tree for this math to work correctly.
147     targetNode->document()->updateStyleIfNeeded();
148
149     // Adjust offsetLocation to be relative to the target's position.
150     if (!isSimulated()) {
151         if (RenderObject* r = targetNode->renderer()) {
152             FloatPoint localPos = r->absoluteToLocal(absoluteLocation(), false, true);
153             m_offsetLocation = roundedLayoutPoint(localPos);
154             float scaleFactor = 1 / pageZoomFactor(this);
155             if (scaleFactor != 1.0f)
156                 m_offsetLocation.scale(scaleFactor, scaleFactor);
157         }
158     }
159
160     // Adjust layerLocation to be relative to the layer.
161     // FIXME: We're pretty sure this is the wrong definition of "layer."
162     // Our RenderLayer is a more modern concept, and layerX/Y is some
163     // other notion about groups of elements (left over from the Netscape 4 days?);
164     // we should test and fix this.
165     Node* n = targetNode;
166     while (n && !n->renderer())
167         n = n->parentNode();
168
169     RenderLayer* layer;
170     if (n && (layer = n->renderer()->enclosingLayer())) {
171         layer->updateLayerPosition();
172         for (; layer; layer = layer->parent()) {
173             m_layerLocation -= toSize(layer->location());
174         }
175     }
176
177     m_hasCachedRelativePosition = true;
178 }
179
180 int MouseRelatedEvent::layerX()
181 {
182     if (!m_hasCachedRelativePosition)
183         computeRelativePosition();
184     return m_layerLocation.x();
185 }
186
187 int MouseRelatedEvent::layerY()
188 {
189     if (!m_hasCachedRelativePosition)
190         computeRelativePosition();
191     return m_layerLocation.y();
192 }
193
194 int MouseRelatedEvent::offsetX()
195 {
196     if (!m_hasCachedRelativePosition)
197         computeRelativePosition();
198     return m_offsetLocation.x();
199 }
200
201 int MouseRelatedEvent::offsetY()
202 {
203     if (!m_hasCachedRelativePosition)
204         computeRelativePosition();
205     return m_offsetLocation.y();
206 }
207
208 int MouseRelatedEvent::pageX() const
209 {
210     return m_pageLocation.x();
211 }
212
213 int MouseRelatedEvent::pageY() const
214 {
215     return m_pageLocation.y();
216 }
217
218 const LayoutPoint& MouseRelatedEvent::pageLocation() const
219 {
220     return m_pageLocation;
221 }
222
223 int MouseRelatedEvent::x() const
224 {
225     // FIXME: This is not correct.
226     // See Microsoft documentation and <http://www.quirksmode.org/dom/w3c_events.html>.
227     return m_clientLocation.x();
228 }
229
230 int MouseRelatedEvent::y() const
231 {
232     // FIXME: This is not correct.
233     // See Microsoft documentation and <http://www.quirksmode.org/dom/w3c_events.html>.
234     return m_clientLocation.y();
235 }
236
237 } // namespace WebCore