OSDN Git Service

0acbd740332b40beef45e4e50a2ca7c10eeec333
[android-x86/external-webkit.git] / 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 "Frame.h"
27 #include "FrameView.h"
28 #include "EventNames.h"
29 #include "PlatformMouseEvent.h"
30
31 namespace WebCore {
32
33 PassRefPtr<MouseEvent> MouseEvent::create(const AtomicString& eventType, PassRefPtr<AbstractView> view, const PlatformMouseEvent& event, const IntPoint& position, int detail, PassRefPtr<Node> relatedTarget)
34 {
35     bool isCancelable = eventType != eventNames().mousemoveEvent;
36
37     return MouseEvent::create(eventType, true, isCancelable, view,
38         detail, event.globalX(), event.globalY(), position.x(), position.y(),
39         event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(), event.button(),
40         relatedTarget, 0, false);
41 }
42
43 MouseEvent::MouseEvent()
44     : m_button(0)
45     , m_buttonDown(false)
46 {
47 }
48
49 MouseEvent::MouseEvent(const AtomicString& eventType, bool canBubble, bool cancelable, PassRefPtr<AbstractView> view,
50                        int detail, int screenX, int screenY, int pageX, int pageY,
51                        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
52                        unsigned short button, PassRefPtr<EventTarget> relatedTarget,
53                        PassRefPtr<Clipboard> clipboard, bool isSimulated)
54     : MouseRelatedEvent(eventType, canBubble, cancelable, view, detail, screenX, screenY,
55                         pageX, pageY, ctrlKey, altKey, shiftKey, metaKey, isSimulated)
56     , m_button(button == (unsigned short)-1 ? 0 : button)
57     , m_buttonDown(button != (unsigned short)-1)
58     , m_relatedTarget(relatedTarget)
59     , m_clipboard(clipboard)
60 {
61 }
62
63 MouseEvent::~MouseEvent()
64 {
65 }
66
67 void MouseEvent::initMouseEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView> view,
68                                 int detail, int screenX, int screenY, int clientX, int clientY,
69                                 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
70                                 unsigned short button, PassRefPtr<EventTarget> relatedTarget)
71 {
72     if (dispatched())
73         return;
74
75     initUIEvent(type, canBubble, cancelable, view, detail);
76
77     m_screenX = screenX;
78     m_screenY = screenY;
79     m_ctrlKey = ctrlKey;
80     m_altKey = altKey;
81     m_shiftKey = shiftKey;
82     m_metaKey = metaKey;
83     m_button = button == (unsigned short)-1 ? 0 : button;
84     m_buttonDown = button != (unsigned short)-1;
85     m_relatedTarget = relatedTarget;
86
87     initCoordinates(clientX, clientY);
88
89     // FIXME: m_isSimulated is not set to false here.
90     // FIXME: m_clipboard is not set to 0 here.
91 }
92
93 bool MouseEvent::isMouseEvent() const
94 {
95     return true;
96 }
97
98 bool MouseEvent::isDragEvent() const
99 {
100     const AtomicString& t = type();
101     return t == eventNames().dragenterEvent || t == eventNames().dragoverEvent || t == eventNames().dragleaveEvent || t == eventNames().dropEvent
102                || t == eventNames().dragstartEvent|| t == eventNames().dragEvent || t == eventNames().dragendEvent;
103 }
104
105 int MouseEvent::which() const
106 {
107     // For the DOM, the return values for left, middle and right mouse buttons are 0, 1, 2, respectively.
108     // For the Netscape "which" property, the return values for left, middle and right mouse buttons are 1, 2, 3, respectively. 
109     // So we must add 1.
110     return m_button + 1;
111 }
112
113 Node* MouseEvent::toElement() const
114 {
115     // MSIE extension - "the object toward which the user is moving the mouse pointer"
116     if (type() == eventNames().mouseoutEvent) 
117         return relatedTarget() ? relatedTarget()->toNode() : 0;
118     
119     return target() ? target()->toNode() : 0;
120 }
121
122 Node* MouseEvent::fromElement() const
123 {
124     // MSIE extension - "object from which activation or the mouse pointer is exiting during the event" (huh?)
125     if (type() != eventNames().mouseoutEvent)
126         return relatedTarget() ? relatedTarget()->toNode() : 0;
127     
128     return target() ? target()->toNode() : 0;
129 }
130
131 PassRefPtr<SimulatedMouseEvent> SimulatedMouseEvent::create(const AtomicString& eventType, PassRefPtr<AbstractView> view, PassRefPtr<Event> underlyingEvent)
132 {
133     return adoptRef(new SimulatedMouseEvent(eventType, view, underlyingEvent));
134 }
135
136 SimulatedMouseEvent::~SimulatedMouseEvent()
137 {
138 }
139
140 SimulatedMouseEvent::SimulatedMouseEvent(const AtomicString& eventType, PassRefPtr<AbstractView> view, PassRefPtr<Event> underlyingEvent)
141     : MouseEvent(eventType, true, true, view, 0, 0, 0, 0, 0, false, false, false, false, 0, 0, 0, true)
142 {
143     if (UIEventWithKeyState* keyStateEvent = findEventWithKeyState(underlyingEvent.get())) {
144         m_ctrlKey = keyStateEvent->ctrlKey();
145         m_altKey = keyStateEvent->altKey();
146         m_shiftKey = keyStateEvent->shiftKey();
147         m_metaKey = keyStateEvent->metaKey();
148     }
149     setUnderlyingEvent(underlyingEvent);
150 }
151
152 } // namespace WebCore