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, 2007, 2008, 2013 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 Event_h
25#define Event_h
26
27#include "DOMTimeStamp.h"
28#include "EventInterfaces.h"
29#include "ScriptWrappable.h"
30#include <wtf/HashMap.h>
31#include <wtf/ListHashSet.h>
32#include <wtf/RefCounted.h>
33#include <wtf/text/AtomicString.h>
34
35namespace WebCore {
36
37class DataTransfer;
38class EventTarget;
39class HTMLIFrameElement;
40
41struct EventInit {
42    EventInit();
43    EventInit(bool bubbles, bool cancelable);
44
45    bool bubbles;
46    bool cancelable;
47};
48
49enum EventInterface {
50
51#define DOM_EVENT_INTERFACE_DECLARE(name) name##InterfaceType,
52DOM_EVENT_INTERFACES_FOR_EACH(DOM_EVENT_INTERFACE_DECLARE)
53#undef DOM_EVENT_INTERFACE_DECLARE
54
55};
56
57class Event : public ScriptWrappable, public RefCounted<Event> {
58public:
59    enum PhaseType {
60        NONE                = 0,
61        CAPTURING_PHASE     = 1,
62        AT_TARGET           = 2,
63        BUBBLING_PHASE      = 3
64    };
65
66    enum EventType {
67        MOUSEDOWN           = 1,
68        MOUSEUP             = 2,
69        MOUSEOVER           = 4,
70        MOUSEOUT            = 8,
71        MOUSEMOVE           = 16,
72        MOUSEDRAG           = 32,
73        CLICK               = 64,
74        DBLCLICK            = 128,
75        KEYDOWN             = 256,
76        KEYUP               = 512,
77        KEYPRESS            = 1024,
78        DRAGDROP            = 2048,
79        FOCUS               = 4096,
80        BLUR                = 8192,
81        SELECT              = 16384,
82        CHANGE              = 32768
83    };
84
85    static PassRefPtr<Event> create()
86    {
87        return adoptRef(new Event);
88    }
89    static PassRefPtr<Event> create(const AtomicString& type, bool canBubble, bool cancelable)
90    {
91        return adoptRef(new Event(type, canBubble, cancelable));
92    }
93
94    static PassRefPtr<Event> create(const AtomicString& type, const EventInit& initializer)
95    {
96        return adoptRef(new Event(type, initializer));
97    }
98
99    virtual ~Event();
100
101    void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
102
103    const AtomicString& type() const { return m_type; }
104    void setType(const AtomicString& type) { m_type = type; }
105
106    EventTarget* target() const { return m_target.get(); }
107    void setTarget(PassRefPtr<EventTarget>);
108
109    EventTarget* currentTarget() const { return m_currentTarget; }
110    void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }
111
112    unsigned short eventPhase() const { return m_eventPhase; }
113    void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
114
115    bool bubbles() const { return m_canBubble; }
116    bool cancelable() const { return m_cancelable; }
117    DOMTimeStamp timeStamp() const { return m_createTime; }
118
119    void stopPropagation() { m_propagationStopped = true; }
120    void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
121
122    // IE Extensions
123    EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
124
125    bool legacyReturnValue() const { return !defaultPrevented(); }
126    void setLegacyReturnValue(bool returnValue) { setDefaultPrevented(!returnValue); }
127
128    DataTransfer* clipboardData() const { return isClipboardEvent() ? internalDataTransfer() : 0; }
129
130    virtual EventInterface eventInterface() const;
131
132    // These events are general classes of events.
133    virtual bool isUIEvent() const;
134    virtual bool isMouseEvent() const;
135    virtual bool isFocusEvent() const;
136    virtual bool isKeyboardEvent() const;
137    virtual bool isTouchEvent() const;
138
139    // Drag events are a subset of mouse events.
140    virtual bool isDragEvent() const;
141
142    // These events lack a DOM interface.
143    virtual bool isClipboardEvent() const;
144    virtual bool isBeforeTextInsertedEvent() const;
145
146    virtual bool isBeforeUnloadEvent() const;
147
148    virtual bool isErrorEvent() const;
149    virtual bool isTextEvent() const;
150    virtual bool isWheelEvent() const;
151
152    bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
153    bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
154
155    bool defaultPrevented() const { return m_defaultPrevented; }
156    void preventDefault()
157    {
158        if (m_cancelable)
159            m_defaultPrevented = true;
160    }
161    void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
162
163    bool defaultHandled() const { return m_defaultHandled; }
164    void setDefaultHandled() { m_defaultHandled = true; }
165
166    bool cancelBubble() const { return m_cancelBubble; }
167    void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
168
169    Event* underlyingEvent() const { return m_underlyingEvent.get(); }
170    void setUnderlyingEvent(PassRefPtr<Event>);
171
172    virtual DataTransfer* internalDataTransfer() const { return 0; }
173
174    bool isBeingDispatched() const { return eventPhase(); }
175
176    virtual PassRefPtr<Event> cloneFor(HTMLIFrameElement*) const;
177
178    virtual EventTarget* relatedTarget() const { return nullptr; }
179
180protected:
181    Event();
182    Event(const AtomicString& type, bool canBubble, bool cancelable);
183    Event(const AtomicString& type, bool canBubble, bool cancelable, double timestamp);
184    Event(const AtomicString& type, const EventInit&);
185
186    virtual void receivedTarget();
187    bool dispatched() const { return m_target; }
188
189private:
190    AtomicString m_type;
191    bool m_canBubble;
192    bool m_cancelable;
193
194    bool m_propagationStopped;
195    bool m_immediatePropagationStopped;
196    bool m_defaultPrevented;
197    bool m_defaultHandled;
198    bool m_cancelBubble;
199
200    unsigned short m_eventPhase;
201    EventTarget* m_currentTarget;
202    RefPtr<EventTarget> m_target;
203    DOMTimeStamp m_createTime;
204
205    RefPtr<Event> m_underlyingEvent;
206};
207
208#define EVENT_TYPE_CASTS(ToValueTypeName) \
209    TYPE_CASTS_BASE(ToValueTypeName, Event, event, event->is##ToValueTypeName(), event.is##ToValueTypeName())
210
211
212} // namespace WebCore
213
214#endif // Event_h
215