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