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