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, 2004, 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
24#ifndef MouseEvent_h
25#define MouseEvent_h
26
27#include "EventDispatchMediator.h"
28#include "MouseRelatedEvent.h"
29
30namespace WebCore {
31
32class Clipboard;
33class EventDispatcher;
34class PlatformMouseEvent;
35
36struct MouseEventInit : public UIEventInit {
37    MouseEventInit();
38
39    int screenX;
40    int screenY;
41    int clientX;
42    int clientY;
43    bool ctrlKey;
44    bool altKey;
45    bool shiftKey;
46    bool metaKey;
47    unsigned short button;
48    RefPtr<EventTarget> relatedTarget;
49};
50
51class MouseEvent : public MouseRelatedEvent {
52public:
53    static PassRefPtr<MouseEvent> create()
54    {
55        return adoptRef(new MouseEvent);
56    }
57
58    static PassRefPtr<MouseEvent> create(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView>,
59        int detail, int screenX, int screenY, int pageX, int pageY,
60#if ENABLE(POINTER_LOCK)
61        int movementX, int movementY,
62#endif
63        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, unsigned short button,
64        PassRefPtr<EventTarget> relatedTarget);
65
66    static PassRefPtr<MouseEvent> create(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView>,
67        int detail, int screenX, int screenY, int pageX, int pageY,
68#if ENABLE(POINTER_LOCK)
69        int movementX, int movementY,
70#endif
71        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, unsigned short button,
72        PassRefPtr<EventTarget> relatedTarget, PassRefPtr<Clipboard>, bool isSimulated = false);
73
74    static PassRefPtr<MouseEvent> create(const AtomicString& eventType, PassRefPtr<AbstractView>, const PlatformMouseEvent&, int detail, PassRefPtr<Node> relatedTarget);
75
76    static PassRefPtr<MouseEvent> create(const AtomicString& eventType, const MouseEventInit&);
77
78    virtual ~MouseEvent();
79
80    void initMouseEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView>,
81        int detail, int screenX, int screenY, int clientX, int clientY,
82        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey,
83        unsigned short button, PassRefPtr<EventTarget> relatedTarget);
84
85    // WinIE uses 1,4,2 for left/middle/right but not for click (just for mousedown/up, maybe others),
86    // but we will match the standard DOM.
87    unsigned short button() const { return m_button; }
88    bool buttonDown() const { return m_buttonDown; }
89    EventTarget* relatedTarget() const { return m_relatedTarget.get(); }
90    void setRelatedTarget(PassRefPtr<EventTarget> relatedTarget) { m_relatedTarget = relatedTarget; }
91
92    Clipboard* clipboard() const { return m_clipboard.get(); }
93
94    Node* toElement() const;
95    Node* fromElement() const;
96
97    Clipboard* dataTransfer() const { return isDragEvent() ? m_clipboard.get() : 0; }
98
99    virtual const AtomicString& interfaceName() const;
100
101    virtual bool isMouseEvent() const;
102    virtual bool isDragEvent() const;
103    virtual int which() const;
104
105    virtual PassRefPtr<Event> cloneFor(HTMLIFrameElement*) const OVERRIDE;
106
107protected:
108    MouseEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<AbstractView>,
109        int detail, int screenX, int screenY, int pageX, int pageY,
110#if ENABLE(POINTER_LOCK)
111        int movementX, int movementY,
112#endif
113        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, unsigned short button,
114        PassRefPtr<EventTarget> relatedTarget, PassRefPtr<Clipboard>, bool isSimulated);
115
116    MouseEvent(const AtomicString& type, const MouseEventInit&);
117
118    MouseEvent();
119
120private:
121    unsigned short m_button;
122    bool m_buttonDown;
123    RefPtr<EventTarget> m_relatedTarget;
124    RefPtr<Clipboard> m_clipboard;
125};
126
127class SimulatedMouseEvent : public MouseEvent {
128public:
129    static PassRefPtr<SimulatedMouseEvent> create(const AtomicString& eventType, PassRefPtr<AbstractView>, PassRefPtr<Event> underlyingEvent);
130    virtual ~SimulatedMouseEvent();
131
132private:
133    SimulatedMouseEvent(const AtomicString& eventType, PassRefPtr<AbstractView>, PassRefPtr<Event> underlyingEvent);
134};
135
136class MouseEventDispatchMediator : public EventDispatchMediator {
137public:
138    enum MouseEventType { SyntheticMouseEvent, NonSyntheticMouseEvent};
139    static PassRefPtr<MouseEventDispatchMediator> create(PassRefPtr<MouseEvent>, MouseEventType = NonSyntheticMouseEvent);
140
141private:
142    explicit MouseEventDispatchMediator(PassRefPtr<MouseEvent>, MouseEventType);
143    MouseEvent* event() const;
144
145    virtual bool dispatchEvent(EventDispatcher*) const OVERRIDE;
146    bool isSyntheticMouseEvent() const { return m_mouseEventType == SyntheticMouseEvent; }
147    MouseEventType m_mouseEventType;
148};
149
150inline MouseEvent* toMouseEvent(Event* event)
151{
152    ASSERT(event && event->isMouseEvent());
153    return static_cast<MouseEvent*>(event);
154}
155
156} // namespace WebCore
157
158#endif // MouseEvent_h
159