• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
create(const AtomicString & eventType,PassRefPtr<AbstractView> view,const PlatformMouseEvent & event,int detail,PassRefPtr<Node> relatedTarget)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 
MouseEvent()46 MouseEvent::MouseEvent()
47     : m_button(0)
48     , m_buttonDown(false)
49 {
50 }
51 
MouseEvent(const AtomicString & eventType,bool canBubble,bool cancelable,PassRefPtr<AbstractView> view,int detail,int screenX,int screenY,int pageX,int pageY,bool ctrlKey,bool altKey,bool shiftKey,bool metaKey,unsigned short button,PassRefPtr<EventTarget> relatedTarget,PassRefPtr<Clipboard> clipboard,bool isSimulated)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, screenX, screenY,
58                         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 
~MouseEvent()66 MouseEvent::~MouseEvent()
67 {
68 }
69 
initMouseEvent(const AtomicString & type,bool canBubble,bool cancelable,PassRefPtr<AbstractView> view,int detail,int screenX,int screenY,int clientX,int clientY,bool ctrlKey,bool altKey,bool shiftKey,bool metaKey,unsigned short button,PassRefPtr<EventTarget> relatedTarget)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_screenX = screenX;
81     m_screenY = screenY;
82     m_ctrlKey = ctrlKey;
83     m_altKey = altKey;
84     m_shiftKey = shiftKey;
85     m_metaKey = metaKey;
86     m_button = button == (unsigned short)-1 ? 0 : button;
87     m_buttonDown = button != (unsigned short)-1;
88     m_relatedTarget = relatedTarget;
89 
90     initCoordinates(clientX, clientY);
91 
92     // FIXME: m_isSimulated is not set to false here.
93     // FIXME: m_clipboard is not set to 0 here.
94 }
95 
isMouseEvent() const96 bool MouseEvent::isMouseEvent() const
97 {
98     return true;
99 }
100 
isDragEvent() const101 bool MouseEvent::isDragEvent() const
102 {
103     const AtomicString& t = type();
104     return t == eventNames().dragenterEvent || t == eventNames().dragoverEvent || t == eventNames().dragleaveEvent || t == eventNames().dropEvent
105                || t == eventNames().dragstartEvent|| t == eventNames().dragEvent || t == eventNames().dragendEvent;
106 }
107 
which() const108 int MouseEvent::which() const
109 {
110     // For the DOM, the return values for left, middle and right mouse buttons are 0, 1, 2, respectively.
111     // For the Netscape "which" property, the return values for left, middle and right mouse buttons are 1, 2, 3, respectively.
112     // So we must add 1.
113     return m_button + 1;
114 }
115 
toElement() const116 Node* MouseEvent::toElement() const
117 {
118     // MSIE extension - "the object toward which the user is moving the mouse pointer"
119     if (type() == eventNames().mouseoutEvent)
120         return relatedTarget() ? relatedTarget()->toNode() : 0;
121 
122     return target() ? target()->toNode() : 0;
123 }
124 
fromElement() const125 Node* MouseEvent::fromElement() const
126 {
127     // MSIE extension - "object from which activation or the mouse pointer is exiting during the event" (huh?)
128     if (type() != eventNames().mouseoutEvent)
129         return relatedTarget() ? relatedTarget()->toNode() : 0;
130 
131     return target() ? target()->toNode() : 0;
132 }
133 
create(const AtomicString & eventType,PassRefPtr<AbstractView> view,PassRefPtr<Event> underlyingEvent)134 PassRefPtr<SimulatedMouseEvent> SimulatedMouseEvent::create(const AtomicString& eventType, PassRefPtr<AbstractView> view, PassRefPtr<Event> underlyingEvent)
135 {
136     return adoptRef(new SimulatedMouseEvent(eventType, view, underlyingEvent));
137 }
138 
~SimulatedMouseEvent()139 SimulatedMouseEvent::~SimulatedMouseEvent()
140 {
141 }
142 
SimulatedMouseEvent(const AtomicString & eventType,PassRefPtr<AbstractView> view,PassRefPtr<Event> underlyingEvent)143 SimulatedMouseEvent::SimulatedMouseEvent(const AtomicString& eventType, PassRefPtr<AbstractView> view, PassRefPtr<Event> underlyingEvent)
144     : MouseEvent(eventType, true, true, view, 0, 0, 0, 0, 0, false, false, false, false, 0, 0, 0, true)
145 {
146     if (UIEventWithKeyState* keyStateEvent = findEventWithKeyState(underlyingEvent.get())) {
147         m_ctrlKey = keyStateEvent->ctrlKey();
148         m_altKey = keyStateEvent->altKey();
149         m_shiftKey = keyStateEvent->shiftKey();
150         m_metaKey = keyStateEvent->metaKey();
151     }
152     setUnderlyingEvent(underlyingEvent);
153 }
154 
MouseEventDispatchMediator(PassRefPtr<MouseEvent> mouseEvent)155 MouseEventDispatchMediator::MouseEventDispatchMediator(PassRefPtr<MouseEvent> mouseEvent)
156     : EventDispatchMediator(mouseEvent)
157 {
158 }
159 
event() const160 MouseEvent* MouseEventDispatchMediator::event() const
161 {
162     return static_cast<MouseEvent*>(EventDispatchMediator::event());
163 }
164 
dispatchEvent(EventDispatcher * dispatcher) const165 bool MouseEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
166 {
167     if (dispatcher->node()->disabled()) // Don't even send DOM events for disabled controls..
168         return true;
169 
170     if (event()->type().isEmpty())
171         return false; // Shouldn't happen.
172 
173     RefPtr<EventTarget> relatedTarget = dispatcher->adjustRelatedTarget(event(), event()->relatedTarget());
174     event()->setRelatedTarget(relatedTarget);
175 
176     dispatcher->dispatchEvent(event());
177     bool swallowEvent = event()->defaultHandled() || event()->defaultPrevented();
178 
179     // Special case: If it's a double click event, we also send the dblclick event. This is not part
180     // of the DOM specs, but is used for compatibility with the ondblclick="" attribute. This is treated
181     // as a separate event in other DOM-compliant browsers like Firefox, and so we do the same.
182     if (event()->type() == eventNames().clickEvent && event()->detail() == 2) {
183         RefPtr<MouseEvent> doubleClickEvent = MouseEvent::create();
184         doubleClickEvent->initMouseEvent(eventNames().dblclickEvent, event()->bubbles(), event()->cancelable(), event()->view(),
185                 event()->detail(), event()->screenX(), event()->screenY(), event()->clientX(), event()->clientY(),
186                 event()->ctrlKey(), event()->altKey(), event()->shiftKey(), event()->metaKey(),
187                 event()->button(), relatedTarget);
188         if (event()->defaultHandled())
189             doubleClickEvent->setDefaultHandled();
190         dispatcher->dispatchEvent(doubleClickEvent);
191         if (doubleClickEvent->defaultHandled() || doubleClickEvent->defaultPrevented())
192             swallowEvent = true;
193     }
194 
195     return swallowEvent;
196 }
197 
198 } // namespace WebCore
199