initial import
[vuplus_webkit] / Source / WebCore / dom / MouseEvent.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 "MouseEvent.h"
25
26 #include "EventDispatcher.h"
27 #include "EventNames.h"
28 #include "Frame.h"
29 #include "FrameView.h"
30 #include "PlatformMouseEvent.h"
31
32 namespace WebCore {
33
34 PassRefPtr<MouseEvent> MouseEvent::create(const AtomicString& eventType, PassRefPtr<AbstractView> view, const PlatformMouseEvent& event, int detail, PassRefPtr<Node> relatedTarget)
35 {
36     ASSERT(event.eventType() == MouseEventMoved || event.button() != NoButton);
37
38     bool isCancelable = eventType != eventNames().mousemoveEvent;
39
40     return MouseEvent::create(eventType, true, isCancelable, view,
41         detail, event.globalX(), event.globalY(), event.x(), event.y(),
42         event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(), event.button(),
43         relatedTarget, 0, false);
44 }
45
46 MouseEvent::MouseEvent()
47     : m_button(0)
48     , m_buttonDown(false)
49 {
50 }
51
52 MouseEvent::MouseEvent(const AtomicString& eventType, bool canBubble, bool cancelable, PassRefPtr<AbstractView> view,
53                        int detail, int screenX, int screenY, int pageX, int pageY,
54                        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
55                        unsigned short button, PassRefPtr<EventTarget> relatedTarget,
56                        PassRefPtr<Clipboard> clipboard, bool isSimulated)
57     : MouseRelatedEvent(eventType, canBubble, cancelable, view, detail, IntPoint(screenX, screenY),
58                         IntPoint(pageX, pageY), ctrlKey, altKey, shiftKey, metaKey, isSimulated)
59     , m_button(button == (unsigned short)-1 ? 0 : button)
60     , m_buttonDown(button != (unsigned short)-1)
61     , m_relatedTarget(relatedTarget)
62     , m_clipboard(clipboard)
63 {
64 }
65
66 MouseEvent::~MouseEvent()
67 {
68 }
69
70 void MouseEvent::initMouseEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView> view,
71                                 int detail, int screenX, int screenY, int clientX, int clientY,
72                                 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
73                                 unsigned short button, PassRefPtr<EventTarget> relatedTarget)
74 {
75     if (dispatched())
76         return;
77
78     initUIEvent(type, canBubble, cancelable, view, detail);
79
80     m_screenLocation = IntPoint(screenX, screenY);
81     m_ctrlKey = ctrlKey;
82     m_altKey = altKey;
83     m_shiftKey = shiftKey;
84     m_metaKey = metaKey;
85     m_button = button == (unsigned short)-1 ? 0 : button;
86     m_buttonDown = button != (unsigned short)-1;
87     m_relatedTarget = relatedTarget;
88
89     initCoordinates(IntPoint(clientX, clientY));
90
91     // FIXME: m_isSimulated is not set to false here.
92     // FIXME: m_clipboard is not set to 0 here.
93 }
94
95 bool MouseEvent::isMouseEvent() const
96 {
97     return true;
98 }
99
100 bool MouseEvent::isDragEvent() const
101 {
102     const AtomicString& t = type();
103     return t == eventNames().dragenterEvent || t == eventNames().dragoverEvent || t == eventNames().dragleaveEvent || t == eventNames().dropEvent
104                || t == eventNames().dragstartEvent|| t == eventNames().dragEvent || t == eventNames().dragendEvent;
105 }
106
107 int MouseEvent::which() const
108 {
109     // For the DOM, the return values for left, middle and right mouse buttons are 0, 1, 2, respectively.
110     // For the Netscape "which" property, the return values for left, middle and right mouse buttons are 1, 2, 3, respectively. 
111     // So we must add 1.
112     if (!m_buttonDown)
113         return 0;
114     return m_button + 1;
115 }
116
117 Node* MouseEvent::toElement() const
118 {
119     // MSIE extension - "the object toward which the user is moving the mouse pointer"
120     if (type() == eventNames().mouseoutEvent) 
121         return relatedTarget() ? relatedTarget()->toNode() : 0;
122     
123     return target() ? target()->toNode() : 0;
124 }
125
126 Node* MouseEvent::fromElement() const
127 {
128     // MSIE extension - "object from which activation or the mouse pointer is exiting during the event" (huh?)
129     if (type() != eventNames().mouseoutEvent)
130         return relatedTarget() ? relatedTarget()->toNode() : 0;
131     
132     return target() ? target()->toNode() : 0;
133 }
134
135 PassRefPtr<SimulatedMouseEvent> SimulatedMouseEvent::create(const AtomicString& eventType, PassRefPtr<AbstractView> view, PassRefPtr<Event> underlyingEvent)
136 {
137     return adoptRef(new SimulatedMouseEvent(eventType, view, underlyingEvent));
138 }
139
140 SimulatedMouseEvent::~SimulatedMouseEvent()
141 {
142 }
143
144 SimulatedMouseEvent::SimulatedMouseEvent(const AtomicString& eventType, PassRefPtr<AbstractView> view, PassRefPtr<Event> underlyingEvent)
145     : MouseEvent(eventType, true, true, view, 0, 0, 0, 0, 0, false, false, false, false, 0, 0, 0, true)
146 {
147     if (UIEventWithKeyState* keyStateEvent = findEventWithKeyState(underlyingEvent.get())) {
148         m_ctrlKey = keyStateEvent->ctrlKey();
149         m_altKey = keyStateEvent->altKey();
150         m_shiftKey = keyStateEvent->shiftKey();
151         m_metaKey = keyStateEvent->metaKey();
152     }
153     setUnderlyingEvent(underlyingEvent);
154
155     if (this->underlyingEvent() && this->underlyingEvent()->isMouseEvent()) {
156         MouseEvent* mouseEvent = static_cast<MouseEvent*>(this->underlyingEvent());
157         m_screenLocation = mouseEvent->screenLocation();
158         initCoordinates(mouseEvent->clientLocation());
159     }
160 }
161
162 PassRefPtr<MouseEventDispatchMediator> MouseEventDispatchMediator::create(PassRefPtr<MouseEvent> mouseEvent)
163 {
164     return adoptRef(new MouseEventDispatchMediator(mouseEvent));
165 }
166
167 MouseEventDispatchMediator::MouseEventDispatchMediator(PassRefPtr<MouseEvent> mouseEvent)
168     : EventDispatchMediator(mouseEvent)
169 {
170 }
171
172 MouseEvent* MouseEventDispatchMediator::event() const
173 {
174     return static_cast<MouseEvent*>(EventDispatchMediator::event());
175 }
176
177 bool MouseEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
178 {
179     if (dispatcher->node()->disabled()) // Don't even send DOM events for disabled controls..
180         return true;
181
182     if (event()->type().isEmpty())
183         return false; // Shouldn't happen.
184
185     RefPtr<EventTarget> relatedTarget = dispatcher->adjustRelatedTarget(event(), event()->relatedTarget());
186     event()->setRelatedTarget(relatedTarget);
187
188     dispatcher->dispatchEvent(event());
189     bool swallowEvent = event()->defaultHandled() || event()->defaultPrevented();
190
191     // Special case: If it's a double click event, we also send the dblclick event. This is not part
192     // of the DOM specs, but is used for compatibility with the ondblclick="" attribute. This is treated
193     // as a separate event in other DOM-compliant browsers like Firefox, and so we do the same.
194     if (event()->type() == eventNames().clickEvent && event()->detail() == 2) {
195         RefPtr<MouseEvent> doubleClickEvent = MouseEvent::create();
196         doubleClickEvent->initMouseEvent(eventNames().dblclickEvent, event()->bubbles(), event()->cancelable(), event()->view(),
197                 event()->detail(), event()->screenX(), event()->screenY(), event()->clientX(), event()->clientY(),
198                 event()->ctrlKey(), event()->altKey(), event()->shiftKey(), event()->metaKey(),
199                 event()->button(), relatedTarget);
200         if (event()->defaultHandled())
201             doubleClickEvent->setDefaultHandled();
202         dispatcher->dispatchEvent(doubleClickEvent);
203         if (doubleClickEvent->defaultHandled() || doubleClickEvent->defaultPrevented())
204             swallowEvent = true;
205     }
206
207     return swallowEvent;
208 }
209
210 } // namespace WebCore