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 "EventNames.h"
27
28namespace WebCore {
29
30MouseEvent::MouseEvent()
31    : m_button(0)
32    , m_buttonDown(false)
33{
34}
35
36MouseEvent::MouseEvent(const AtomicString& eventType, bool canBubble, bool cancelable, PassRefPtr<AbstractView> view,
37                       int detail, int screenX, int screenY, int pageX, int pageY,
38                       bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
39                       unsigned short button, PassRefPtr<EventTarget> relatedTarget,
40                       PassRefPtr<Clipboard> clipboard, bool isSimulated)
41    : MouseRelatedEvent(eventType, canBubble, cancelable, view, detail, screenX, screenY,
42                        pageX, pageY, ctrlKey, altKey, shiftKey, metaKey, isSimulated)
43    , m_button(button == (unsigned short)-1 ? 0 : button)
44    , m_buttonDown(button != (unsigned short)-1)
45    , m_relatedTarget(relatedTarget)
46    , m_clipboard(clipboard)
47{
48}
49
50MouseEvent::~MouseEvent()
51{
52}
53
54void MouseEvent::initMouseEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView> view,
55                                int detail, int screenX, int screenY, int clientX, int clientY,
56                                bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
57                                unsigned short button, PassRefPtr<EventTarget> relatedTarget)
58{
59    if (dispatched())
60        return;
61
62    initUIEvent(type, canBubble, cancelable, view, detail);
63
64    m_screenX = screenX;
65    m_screenY = screenY;
66    m_ctrlKey = ctrlKey;
67    m_altKey = altKey;
68    m_shiftKey = shiftKey;
69    m_metaKey = metaKey;
70    m_button = button == (unsigned short)-1 ? 0 : button;
71    m_buttonDown = button != (unsigned short)-1;
72    m_relatedTarget = relatedTarget;
73
74    initCoordinates(clientX, clientY);
75
76    // FIXME: m_isSimulated is not set to false here.
77    // FIXME: m_clipboard is not set to 0 here.
78}
79
80bool MouseEvent::isMouseEvent() const
81{
82    return true;
83}
84
85bool MouseEvent::isDragEvent() const
86{
87    const AtomicString& t = type();
88    return t == eventNames().dragenterEvent || t == eventNames().dragoverEvent || t == eventNames().dragleaveEvent || t == eventNames().dropEvent
89               || t == eventNames().dragstartEvent|| t == eventNames().dragEvent || t == eventNames().dragendEvent;
90}
91
92int MouseEvent::which() const
93{
94    // For the DOM, the return values for left, middle and right mouse buttons are 0, 1, 2, respectively.
95    // For the Netscape "which" property, the return values for left, middle and right mouse buttons are 1, 2, 3, respectively.
96    // So we must add 1.
97    return m_button + 1;
98}
99
100Node* MouseEvent::toElement() const
101{
102    // MSIE extension - "the object toward which the user is moving the mouse pointer"
103    if (type() == eventNames().mouseoutEvent)
104        return relatedTarget() ? relatedTarget()->toNode() : 0;
105
106    return target() ? target()->toNode() : 0;
107}
108
109Node* MouseEvent::fromElement() const
110{
111    // MSIE extension - "object from which activation or the mouse pointer is exiting during the event" (huh?)
112    if (type() != eventNames().mouseoutEvent)
113        return relatedTarget() ? relatedTarget()->toNode() : 0;
114
115    return target() ? target()->toNode() : 0;
116}
117
118} // namespace WebCore
119