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