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 "Clipboard.h"
28#include "DOMTimeStamp.h"
29#include <wtf/RefCounted.h>
30#include <wtf/text/AtomicString.h>
31
32namespace WebCore {
33
34    class EventTarget;
35    class EventDispatcher;
36
37    class Event : public RefCounted<Event> {
38    public:
39        enum PhaseType {
40            CAPTURING_PHASE     = 1,
41            AT_TARGET           = 2,
42            BUBBLING_PHASE      = 3
43        };
44
45        enum EventType {
46            MOUSEDOWN           = 1,
47            MOUSEUP             = 2,
48            MOUSEOVER           = 4,
49            MOUSEOUT            = 8,
50            MOUSEMOVE           = 16,
51            MOUSEDRAG           = 32,
52            CLICK               = 64,
53            DBLCLICK            = 128,
54            KEYDOWN             = 256,
55            KEYUP               = 512,
56            KEYPRESS            = 1024,
57            DRAGDROP            = 2048,
58            FOCUS               = 4096,
59            BLUR                = 8192,
60            SELECT              = 16384,
61            CHANGE              = 32768
62        };
63
64        static PassRefPtr<Event> create()
65        {
66            return adoptRef(new Event);
67        }
68        static PassRefPtr<Event> create(const AtomicString& type, bool canBubble, bool cancelable)
69        {
70            return adoptRef(new Event(type, canBubble, cancelable));
71        }
72        virtual ~Event();
73
74        void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
75
76        const AtomicString& type() const { return m_type; }
77
78        EventTarget* target() const { return m_target.get(); }
79        void setTarget(PassRefPtr<EventTarget>);
80
81        EventTarget* currentTarget() const { return m_currentTarget; }
82        void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }
83
84        unsigned short eventPhase() const { return m_eventPhase; }
85        void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
86
87        bool bubbles() const { return m_canBubble; }
88        bool cancelable() const { return m_cancelable; }
89        DOMTimeStamp timeStamp() const { return m_createTime; }
90
91        void stopPropagation() { m_propagationStopped = true; }
92        void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
93
94        // IE Extensions
95        EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
96
97        bool returnValue() const { return !defaultPrevented(); }
98        void setReturnValue(bool returnValue) { setDefaultPrevented(!returnValue); }
99
100        Clipboard* clipboardData() const { return isClipboardEvent() ? clipboard() : 0; }
101
102        virtual bool isCustomEvent() const;
103        virtual bool isUIEvent() const;
104        virtual bool isMouseEvent() const;
105        virtual bool isMutationEvent() const;
106        virtual bool isKeyboardEvent() const;
107        virtual bool isTextEvent() const;
108        virtual bool isCompositionEvent() const;
109        virtual bool isDragEvent() const; // a subset of mouse events
110        virtual bool isClipboardEvent() const;
111        virtual bool isMessageEvent() const;
112        virtual bool isWheelEvent() const;
113        virtual bool isBeforeTextInsertedEvent() const;
114        virtual bool isOverflowEvent() const;
115        virtual bool isPageTransitionEvent() const;
116        virtual bool isPopStateEvent() const;
117        virtual bool isProgressEvent() const;
118        virtual bool isXMLHttpRequestProgressEvent() const;
119        virtual bool isWebKitAnimationEvent() const;
120        virtual bool isWebKitTransitionEvent() const;
121        virtual bool isBeforeLoadEvent() const;
122        virtual bool isHashChangeEvent() const;
123#if ENABLE(SVG)
124        virtual bool isSVGZoomEvent() const;
125#endif
126#if ENABLE(DOM_STORAGE)
127        virtual bool isStorageEvent() const;
128#endif
129#if ENABLE(INDEXED_DATABASE)
130        virtual bool isIDBVersionChangeEvent() const;
131#endif
132#if ENABLE(WEB_AUDIO)
133        virtual bool isAudioProcessingEvent() const;
134        virtual bool isOfflineAudioCompletionEvent() const;
135#endif
136        virtual bool isErrorEvent() const;
137#if ENABLE(TOUCH_EVENTS)
138        virtual bool isTouchEvent() const;
139#if PLATFORM(ANDROID)
140        virtual bool hitTouchHandler() const { return false; }
141        virtual void setHitTouchHandler() { }
142#endif
143#endif
144#if ENABLE(DEVICE_ORIENTATION)
145        virtual bool isDeviceMotionEvent() const;
146        virtual bool isDeviceOrientationEvent() const;
147#endif
148#if ENABLE(INPUT_SPEECH)
149        virtual bool isSpeechInputEvent() const;
150#endif
151        bool fromUserGesture();
152
153        bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
154        bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
155
156        bool defaultPrevented() const { return m_defaultPrevented; }
157        void preventDefault() { if (m_cancelable) m_defaultPrevented = true; }
158        void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
159
160        bool defaultHandled() const { return m_defaultHandled; }
161        void setDefaultHandled() { m_defaultHandled = true; }
162
163        bool cancelBubble() const { return m_cancelBubble; }
164        void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
165
166        Event* underlyingEvent() const { return m_underlyingEvent.get(); }
167        void setUnderlyingEvent(PassRefPtr<Event>);
168
169        virtual bool storesResultAsString() const;
170        virtual void storeResult(const String&);
171
172        virtual Clipboard* clipboard() const { return 0; }
173
174
175    protected:
176        Event();
177        Event(const AtomicString& type, bool canBubble, bool cancelable);
178
179        virtual void receivedTarget();
180        bool dispatched() const { return m_target; }
181
182    private:
183        AtomicString m_type;
184        bool m_canBubble;
185        bool m_cancelable;
186
187        bool m_propagationStopped;
188        bool m_immediatePropagationStopped;
189        bool m_defaultPrevented;
190        bool m_defaultHandled;
191        bool m_cancelBubble;
192
193        unsigned short m_eventPhase;
194        EventTarget* m_currentTarget;
195        RefPtr<EventTarget> m_target;
196        DOMTimeStamp m_createTime;
197
198        RefPtr<Event> m_underlyingEvent;
199    };
200
201class EventDispatchMediator {
202public:
203    explicit EventDispatchMediator(PassRefPtr<Event>);
204    virtual ~EventDispatchMediator();
205
206    virtual bool dispatchEvent(EventDispatcher*) const;
207
208protected:
209    EventDispatchMediator();
210
211    Event* event() const;
212    void setEvent(PassRefPtr<Event>);
213
214private:
215    RefPtr<Event> m_event;
216};
217
218inline EventDispatchMediator::EventDispatchMediator()
219{
220}
221
222inline Event* EventDispatchMediator::event() const
223{
224    return m_event.get();
225}
226
227inline void EventDispatchMediator::setEvent(PassRefPtr<Event> event)
228{
229    m_event = event;
230}
231
232} // namespace WebCore
233
234#endif // Event_h
235