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/core/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 blink {
     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     DEFINE_WRAPPERTYPEINFO();
     51 public:
     52     enum PhaseType {
     53         NONE                = 0,
     54         CAPTURING_PHASE     = 1,
     55         AT_TARGET           = 2,
     56         BUBBLING_PHASE      = 3
     57     };
     58 
     59     enum EventType {
     60         MOUSEDOWN           = 1,
     61         MOUSEUP             = 2,
     62         MOUSEOVER           = 4,
     63         MOUSEOUT            = 8,
     64         MOUSEMOVE           = 16,
     65         MOUSEDRAG           = 32,
     66         CLICK               = 64,
     67         DBLCLICK            = 128,
     68         KEYDOWN             = 256,
     69         KEYUP               = 512,
     70         KEYPRESS            = 1024,
     71         DRAGDROP            = 2048,
     72         FOCUS               = 4096,
     73         BLUR                = 8192,
     74         SELECT              = 16384,
     75         CHANGE              = 32768
     76     };
     77 
     78     static PassRefPtrWillBeRawPtr<Event> create()
     79     {
     80         return adoptRefWillBeNoop(new Event);
     81     }
     82 
     83     // A factory for a simple event. The event doesn't bubble, and isn't
     84     // cancelable.
     85     // http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#fire-a-simple-event
     86     static PassRefPtrWillBeRawPtr<Event> create(const AtomicString& type)
     87     {
     88         return adoptRefWillBeNoop(new Event(type, false, false));
     89     }
     90     static PassRefPtrWillBeRawPtr<Event> createCancelable(const AtomicString& type)
     91     {
     92         return adoptRefWillBeNoop(new Event(type, false, true));
     93     }
     94     static PassRefPtrWillBeRawPtr<Event> createBubble(const AtomicString& type)
     95     {
     96         return adoptRefWillBeNoop(new Event(type, true, false));
     97     }
     98     static PassRefPtrWillBeRawPtr<Event> createCancelableBubble(const AtomicString& type)
     99     {
    100         return adoptRefWillBeNoop(new Event(type, true, true));
    101     }
    102 
    103     static PassRefPtrWillBeRawPtr<Event> create(const AtomicString& type, const EventInit& initializer)
    104     {
    105         return adoptRefWillBeNoop(new Event(type, initializer));
    106     }
    107 
    108     virtual ~Event();
    109 
    110     void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
    111 
    112     const AtomicString& type() const { return m_type; }
    113     void setType(const AtomicString& type) { m_type = type; }
    114 
    115     EventTarget* target() const { return m_target.get(); }
    116     void setTarget(PassRefPtrWillBeRawPtr<EventTarget>);
    117 
    118     EventTarget* currentTarget() const;
    119     void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }
    120 
    121     unsigned short eventPhase() const { return m_eventPhase; }
    122     void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
    123 
    124     bool bubbles() const { return m_canBubble; }
    125     bool cancelable() const { return m_cancelable; }
    126     DOMTimeStamp timeStamp() const { return m_createTime; }
    127 
    128     void stopPropagation() { m_propagationStopped = true; }
    129     void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
    130 
    131     // IE Extensions
    132     EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
    133 
    134     bool legacyReturnValue(ExecutionContext*) const;
    135     void setLegacyReturnValue(ExecutionContext*, bool returnValue);
    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     virtual bool isRelatedEvent() const;
    149 
    150     // Drag events are a subset of mouse events.
    151     virtual bool isDragEvent() const;
    152 
    153     // These events lack a DOM interface.
    154     virtual bool isClipboardEvent() const;
    155     virtual bool isBeforeTextInsertedEvent() const;
    156 
    157     virtual bool isBeforeUnloadEvent() const;
    158 
    159     bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
    160     bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
    161 
    162     bool defaultPrevented() const { return m_defaultPrevented; }
    163     virtual void preventDefault()
    164     {
    165         if (m_cancelable)
    166             m_defaultPrevented = true;
    167     }
    168     void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
    169 
    170     bool defaultHandled() const { return m_defaultHandled; }
    171     void setDefaultHandled() { m_defaultHandled = true; }
    172 
    173     bool cancelBubble() const { return m_cancelBubble; }
    174     void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
    175 
    176     Event* underlyingEvent() const { return m_underlyingEvent.get(); }
    177     void setUnderlyingEvent(PassRefPtrWillBeRawPtr<Event>);
    178 
    179     EventPath& eventPath() { ASSERT(m_eventPath); return *m_eventPath; }
    180     EventPath& ensureEventPath();
    181 
    182     PassRefPtrWillBeRawPtr<StaticNodeList> path() const;
    183 
    184     bool isBeingDispatched() const { return eventPhase(); }
    185 
    186     virtual void trace(Visitor*);
    187 
    188 protected:
    189     Event();
    190     Event(const AtomicString& type, bool canBubble, bool cancelable);
    191     Event(const AtomicString& type, const EventInit&);
    192 
    193     virtual void receivedTarget();
    194     bool dispatched() const { return m_target; }
    195 
    196 private:
    197     AtomicString m_type;
    198     bool m_canBubble;
    199     bool m_cancelable;
    200 
    201     bool m_propagationStopped;
    202     bool m_immediatePropagationStopped;
    203     bool m_defaultPrevented;
    204     bool m_defaultHandled;
    205     bool m_cancelBubble;
    206 
    207     unsigned short m_eventPhase;
    208     RefPtrWillBeMember<EventTarget> m_currentTarget;
    209     RefPtrWillBeMember<EventTarget> m_target;
    210     DOMTimeStamp m_createTime;
    211     RefPtrWillBeMember<Event> m_underlyingEvent;
    212     OwnPtrWillBeMember<EventPath> m_eventPath;
    213 };
    214 
    215 #define DEFINE_EVENT_TYPE_CASTS(typeName) \
    216     DEFINE_TYPE_CASTS(typeName, Event, event, event->is##typeName(), event.is##typeName())
    217 
    218 } // namespace blink
    219 
    220 #endif // Event_h
    221