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/EventPath.h"
     30 #include "platform/heap/Handle.h"
     31 #include "wtf/RefCounted.h"
     32 #include "wtf/text/AtomicString.h"
     33 
     34 namespace WebCore {
     35 
     36 class EventTarget;
     37 class EventDispatcher;
     38 class ExecutionContext;
     39 
     40 struct EventInit {
     41     STACK_ALLOCATED();
     42 public:
     43     EventInit();
     44 
     45     bool bubbles;
     46     bool cancelable;
     47 };
     48 
     49 class Event : public RefCountedWillBeGarbageCollectedFinalized<Event>,  public ScriptWrappable {
     50 public:
     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 PassRefPtrWillBeRawPtr<Event> create()
     78     {
     79         return adoptRefWillBeNoop(new Event);
     80     }
     81 
     82     // A factory for a simple event. The event doesn't bubble, and isn't
     83     // cancelable.
     84     // http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#fire-a-simple-event
     85     static PassRefPtrWillBeRawPtr<Event> create(const AtomicString& type)
     86     {
     87         return adoptRefWillBeNoop(new Event(type, false, false));
     88     }
     89     static PassRefPtrWillBeRawPtr<Event> createCancelable(const AtomicString& type)
     90     {
     91         return adoptRefWillBeNoop(new Event(type, false, true));
     92     }
     93     static PassRefPtrWillBeRawPtr<Event> createBubble(const AtomicString& type)
     94     {
     95         return adoptRefWillBeNoop(new Event(type, true, false));
     96     }
     97     static PassRefPtrWillBeRawPtr<Event> createCancelableBubble(const AtomicString& type)
     98     {
     99         return adoptRefWillBeNoop(new Event(type, true, true));
    100     }
    101 
    102     static PassRefPtrWillBeRawPtr<Event> create(const AtomicString& type, const EventInit& initializer)
    103     {
    104         return adoptRefWillBeNoop(new Event(type, initializer));
    105     }
    106 
    107     virtual ~Event();
    108 
    109     void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
    110 
    111     const AtomicString& type() const { return m_type; }
    112     void setType(const AtomicString& type) { m_type = type; }
    113 
    114     EventTarget* target() const { return m_target.get(); }
    115     void setTarget(PassRefPtrWillBeRawPtr<EventTarget>);
    116 
    117     EventTarget* currentTarget() const;
    118     void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }
    119 
    120     unsigned short eventPhase() const { return m_eventPhase; }
    121     void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
    122 
    123     bool bubbles() const { return m_canBubble; }
    124     bool cancelable() const { return m_cancelable; }
    125     DOMTimeStamp timeStamp() const { return m_createTime; }
    126 
    127     void stopPropagation() { m_propagationStopped = true; }
    128     void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
    129 
    130     // IE Extensions
    131     EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
    132 
    133     bool legacyReturnValue(ExecutionContext*) const;
    134     void setLegacyReturnValue(ExecutionContext*, bool returnValue);
    135 
    136     virtual const AtomicString& interfaceName() const;
    137     bool hasInterface(const AtomicString&) const;
    138 
    139     // These events are general classes of events.
    140     virtual bool isUIEvent() const;
    141     virtual bool isMouseEvent() const;
    142     virtual bool isFocusEvent() const;
    143     virtual bool isKeyboardEvent() const;
    144     virtual bool isTouchEvent() const;
    145     virtual bool isGestureEvent() const;
    146     virtual bool isWheelEvent() const;
    147 
    148     // Drag events are a subset of mouse events.
    149     virtual bool isDragEvent() const;
    150 
    151     // These events lack a DOM interface.
    152     virtual bool isClipboardEvent() const;
    153     virtual bool isBeforeTextInsertedEvent() const;
    154 
    155     virtual bool isBeforeUnloadEvent() const;
    156 
    157     bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
    158     bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
    159 
    160     bool defaultPrevented() const { return m_defaultPrevented; }
    161     virtual void preventDefault()
    162     {
    163         if (m_cancelable)
    164             m_defaultPrevented = true;
    165     }
    166     void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
    167 
    168     bool defaultHandled() const { return m_defaultHandled; }
    169     void setDefaultHandled() { m_defaultHandled = true; }
    170 
    171     bool cancelBubble() const { return m_cancelBubble; }
    172     void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
    173 
    174     Event* underlyingEvent() const { return m_underlyingEvent.get(); }
    175     void setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event>);
    176 
    177     EventPath& eventPath() { ASSERT(m_eventPath); return *m_eventPath; }
    178     EventPath& ensureEventPath();
    179 
    180     PassRefPtrWillBeRawPtr<StaticNodeList> path() const;
    181 
    182     bool isBeingDispatched() const { return eventPhase(); }
    183 
    184     virtual void trace(Visitor*);
    185 
    186 protected:
    187     Event();
    188     Event(const AtomicString& type, bool canBubble, bool cancelable);
    189     Event(const AtomicString& type, const EventInit&);
    190 
    191     virtual void receivedTarget();
    192     bool dispatched() const { return m_target; }
    193 
    194 private:
    195     AtomicString m_type;
    196     bool m_canBubble;
    197     bool m_cancelable;
    198 
    199     bool m_propagationStopped;
    200     bool m_immediatePropagationStopped;
    201     bool m_defaultPrevented;
    202     bool m_defaultHandled;
    203     bool m_cancelBubble;
    204 
    205     unsigned short m_eventPhase;
    206     RawPtrWillBeMember<EventTarget> m_currentTarget;
    207     RefPtrWillBeMember<EventTarget> m_target;
    208     DOMTimeStamp m_createTime;
    209     RefPtrWillBeMember<Event> m_underlyingEvent;
    210     OwnPtrWillBeMember<EventPath> m_eventPath;
    211 };
    212 
    213 #define DEFINE_EVENT_TYPE_CASTS(typeName) \
    214     DEFINE_TYPE_CASTS(typeName, Event, event, event->is##typeName(), event.is##typeName())
    215 
    216 } // namespace WebCore
    217 
    218 #endif // Event_h
    219