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 
    130     // Drag events are a subset of mouse events.
    131     virtual bool isDragEvent() const;
    132 
    133     // These events lack a DOM interface.
    134     virtual bool isClipboardEvent() const;
    135     virtual bool isBeforeTextInsertedEvent() const;
    136 
    137     bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
    138     bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
    139 
    140     bool defaultPrevented() const { return m_defaultPrevented; }
    141     void preventDefault()
    142     {
    143         if (m_cancelable)
    144             m_defaultPrevented = true;
    145     }
    146     void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
    147 
    148     bool defaultHandled() const { return m_defaultHandled; }
    149     void setDefaultHandled() { m_defaultHandled = true; }
    150 
    151     bool cancelBubble() const { return m_cancelBubble; }
    152     void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
    153 
    154     Event* underlyingEvent() const { return m_underlyingEvent.get(); }
    155     void setUnderlyingEvent(PassRefPtr<Event>);
    156 
    157     EventPath& eventPath() { return m_eventPath; }
    158     PassRefPtr<NodeList> path() const;
    159 
    160     virtual bool storesResultAsString() const;
    161     virtual void storeResult(const String&);
    162 
    163     virtual Clipboard* clipboard() const { return 0; }
    164 
    165     bool isBeingDispatched() const { return eventPhase(); }
    166 
    167 protected:
    168     Event();
    169     Event(const AtomicString& type, bool canBubble, bool cancelable);
    170     Event(const AtomicString& type, const EventInit&);
    171 
    172     virtual void receivedTarget();
    173     bool dispatched() const { return m_target; }
    174 
    175 private:
    176     AtomicString m_type;
    177     bool m_canBubble;
    178     bool m_cancelable;
    179 
    180     bool m_propagationStopped;
    181     bool m_immediatePropagationStopped;
    182     bool m_defaultPrevented;
    183     bool m_defaultHandled;
    184     bool m_cancelBubble;
    185 
    186     unsigned short m_eventPhase;
    187     EventTarget* m_currentTarget;
    188     RefPtr<EventTarget> m_target;
    189     DOMTimeStamp m_createTime;
    190     RefPtr<Event> m_underlyingEvent;
    191     EventPath m_eventPath;
    192 };
    193 
    194 } // namespace WebCore
    195 
    196 #endif // Event_h
    197