Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / events / Event.h
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 "bindings/v8/ScriptWrappable.h"
28 #include "core/dom/DOMTimeStamp.h"
29 #include "core/events/EventPath.h"
30 #include "wtf/RefCounted.h"
31 #include "wtf/text/AtomicString.h"
32
33 namespace WebCore {
34
35 class EventTarget;
36 class EventDispatcher;
37 class HTMLIFrameElement;
38
39 struct EventInit {
40     EventInit();
41
42     bool bubbles;
43     bool cancelable;
44 };
45
46 class Event : public ScriptWrappable, public RefCounted<Event> {
47 public:
48     enum PhaseType {
49         NONE                = 0,
50         CAPTURING_PHASE     = 1,
51         AT_TARGET           = 2,
52         BUBBLING_PHASE      = 3
53     };
54
55     enum EventType {
56         MOUSEDOWN           = 1,
57         MOUSEUP             = 2,
58         MOUSEOVER           = 4,
59         MOUSEOUT            = 8,
60         MOUSEMOVE           = 16,
61         MOUSEDRAG           = 32,
62         CLICK               = 64,
63         DBLCLICK            = 128,
64         KEYDOWN             = 256,
65         KEYUP               = 512,
66         KEYPRESS            = 1024,
67         DRAGDROP            = 2048,
68         FOCUS               = 4096,
69         BLUR                = 8192,
70         SELECT              = 16384,
71         CHANGE              = 32768
72     };
73
74     static PassRefPtr<Event> create()
75     {
76         return adoptRef(new Event);
77     }
78
79     // A factory for a simple event. The event doesn't bubble, and isn't
80     // cancelable.
81     // http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#fire-a-simple-event
82     static PassRefPtr<Event> create(const AtomicString& type)
83     {
84         return adoptRef(new Event(type, false, false));
85     }
86     static PassRefPtr<Event> createCancelable(const AtomicString& type)
87     {
88         return adoptRef(new Event(type, false, true));
89     }
90     static PassRefPtr<Event> createBubble(const AtomicString& type)
91     {
92         return adoptRef(new Event(type, true, false));
93     }
94     static PassRefPtr<Event> createCancelableBubble(const AtomicString& type)
95     {
96         return adoptRef(new Event(type, true, true));
97     }
98
99     static PassRefPtr<Event> create(const AtomicString& type, const EventInit& initializer)
100     {
101         return adoptRef(new Event(type, initializer));
102     }
103
104     virtual ~Event();
105
106     void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
107
108     const AtomicString& type() const { return m_type; }
109     void setType(const AtomicString& type) { m_type = type; }
110
111     EventTarget* target() const { return m_target.get(); }
112     void setTarget(PassRefPtr<EventTarget>);
113
114     EventTarget* currentTarget() const { return m_currentTarget; }
115     void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }
116
117     unsigned short eventPhase() const { return m_eventPhase; }
118     void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
119
120     bool bubbles() const { return m_canBubble; }
121     bool cancelable() const { return m_cancelable; }
122     DOMTimeStamp timeStamp() const { return m_createTime; }
123
124     void stopPropagation() { m_propagationStopped = true; }
125     void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
126
127     // IE Extensions
128     EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
129
130     bool legacyReturnValue() const { return !defaultPrevented(); }
131     void setLegacyReturnValue(bool returnValue) { setDefaultPrevented(!returnValue); }
132
133     virtual const AtomicString& interfaceName() const;
134     bool hasInterface(const AtomicString&) const;
135
136     // These events are general classes of events.
137     virtual bool isUIEvent() const;
138     virtual bool isMouseEvent() const;
139     virtual bool isFocusEvent() const;
140     virtual bool isKeyboardEvent() const;
141     virtual bool isTouchEvent() const;
142     virtual bool isGestureEvent() const;
143     virtual bool isWheelEvent() const;
144
145     // Drag events are a subset of mouse events.
146     virtual bool isDragEvent() const;
147
148     // These events lack a DOM interface.
149     virtual bool isClipboardEvent() const;
150     virtual bool isBeforeTextInsertedEvent() const;
151
152     virtual bool isBeforeUnloadEvent() const;
153
154     bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
155     bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
156
157     bool defaultPrevented() const { return m_defaultPrevented; }
158     void preventDefault()
159     {
160         if (m_cancelable)
161             m_defaultPrevented = true;
162     }
163     void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
164
165     bool defaultHandled() const { return m_defaultHandled; }
166     void setDefaultHandled() { m_defaultHandled = true; }
167
168     bool cancelBubble() const { return m_cancelBubble; }
169     void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
170
171     Event* underlyingEvent() const { return m_underlyingEvent.get(); }
172     void setUnderlyingEvent(PassRefPtr<Event>);
173
174     EventPath& eventPath() { ASSERT(m_eventPath); return *m_eventPath; }
175     EventPath& ensureEventPath();
176
177     PassRefPtr<NodeList> path() const;
178
179     bool isBeingDispatched() const { return eventPhase(); }
180
181 protected:
182     Event();
183     Event(const AtomicString& type, bool canBubble, bool cancelable);
184     Event(const AtomicString& type, const EventInit&);
185
186     virtual void receivedTarget();
187     bool dispatched() const { return m_target; }
188
189 private:
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     RefPtr<Event> m_underlyingEvent;
205     OwnPtr<EventPath> m_eventPath;
206 };
207
208 #define DEFINE_EVENT_TYPE_CASTS(typeName) \
209     DEFINE_TYPE_CASTS(typeName, Event, event, event->is##typeName(), event.is##typeName())
210
211 } // namespace WebCore
212
213 #endif // Event_h