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 "Clipboard.h"
     28 #include "DOMTimeStamp.h"
     29 #include <wtf/RefCounted.h>
     30 #include <wtf/text/AtomicString.h>
     31 
     32 namespace WebCore {
     33 
     34     class EventTarget;
     35     class EventDispatcher;
     36 
     37     class Event : public RefCounted<Event> {
     38     public:
     39         enum PhaseType {
     40             CAPTURING_PHASE     = 1,
     41             AT_TARGET           = 2,
     42             BUBBLING_PHASE      = 3
     43         };
     44 
     45         enum EventType {
     46             MOUSEDOWN           = 1,
     47             MOUSEUP             = 2,
     48             MOUSEOVER           = 4,
     49             MOUSEOUT            = 8,
     50             MOUSEMOVE           = 16,
     51             MOUSEDRAG           = 32,
     52             CLICK               = 64,
     53             DBLCLICK            = 128,
     54             KEYDOWN             = 256,
     55             KEYUP               = 512,
     56             KEYPRESS            = 1024,
     57             DRAGDROP            = 2048,
     58             FOCUS               = 4096,
     59             BLUR                = 8192,
     60             SELECT              = 16384,
     61             CHANGE              = 32768
     62         };
     63 
     64         static PassRefPtr<Event> create()
     65         {
     66             return adoptRef(new Event);
     67         }
     68         static PassRefPtr<Event> create(const AtomicString& type, bool canBubble, bool cancelable)
     69         {
     70             return adoptRef(new Event(type, canBubble, cancelable));
     71         }
     72         virtual ~Event();
     73 
     74         void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
     75 
     76         const AtomicString& type() const { return m_type; }
     77 
     78         EventTarget* target() const { return m_target.get(); }
     79         void setTarget(PassRefPtr<EventTarget>);
     80 
     81         EventTarget* currentTarget() const { return m_currentTarget; }
     82         void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }
     83 
     84         unsigned short eventPhase() const { return m_eventPhase; }
     85         void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }
     86 
     87         bool bubbles() const { return m_canBubble; }
     88         bool cancelable() const { return m_cancelable; }
     89         DOMTimeStamp timeStamp() const { return m_createTime; }
     90 
     91         void stopPropagation() { m_propagationStopped = true; }
     92         void stopImmediatePropagation() { m_immediatePropagationStopped = true; }
     93 
     94         // IE Extensions
     95         EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"
     96 
     97         bool returnValue() const { return !defaultPrevented(); }
     98         void setReturnValue(bool returnValue) { setDefaultPrevented(!returnValue); }
     99 
    100         Clipboard* clipboardData() const { return isClipboardEvent() ? clipboard() : 0; }
    101 
    102         virtual bool isCustomEvent() const;
    103         virtual bool isUIEvent() const;
    104         virtual bool isMouseEvent() const;
    105         virtual bool isMutationEvent() const;
    106         virtual bool isKeyboardEvent() const;
    107         virtual bool isTextEvent() const;
    108         virtual bool isCompositionEvent() const;
    109         virtual bool isDragEvent() const; // a subset of mouse events
    110         virtual bool isClipboardEvent() const;
    111         virtual bool isMessageEvent() const;
    112         virtual bool isWheelEvent() const;
    113         virtual bool isBeforeTextInsertedEvent() const;
    114         virtual bool isOverflowEvent() const;
    115         virtual bool isPageTransitionEvent() const;
    116         virtual bool isPopStateEvent() const;
    117         virtual bool isProgressEvent() const;
    118         virtual bool isXMLHttpRequestProgressEvent() const;
    119         virtual bool isWebKitAnimationEvent() const;
    120         virtual bool isWebKitTransitionEvent() const;
    121         virtual bool isBeforeLoadEvent() const;
    122         virtual bool isHashChangeEvent() const;
    123 #if ENABLE(SVG)
    124         virtual bool isSVGZoomEvent() const;
    125 #endif
    126 #if ENABLE(DOM_STORAGE)
    127         virtual bool isStorageEvent() const;
    128 #endif
    129 #if ENABLE(INDEXED_DATABASE)
    130         virtual bool isIDBVersionChangeEvent() const;
    131 #endif
    132 #if ENABLE(WEB_AUDIO)
    133         virtual bool isAudioProcessingEvent() const;
    134         virtual bool isOfflineAudioCompletionEvent() const;
    135 #endif
    136         virtual bool isErrorEvent() const;
    137 #if ENABLE(TOUCH_EVENTS)
    138         virtual bool isTouchEvent() const;
    139 #endif
    140 #if ENABLE(DEVICE_ORIENTATION)
    141         virtual bool isDeviceMotionEvent() const;
    142         virtual bool isDeviceOrientationEvent() const;
    143 #endif
    144 #if ENABLE(INPUT_SPEECH)
    145         virtual bool isSpeechInputEvent() const;
    146 #endif
    147         bool fromUserGesture();
    148 
    149         bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
    150         bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }
    151 
    152         bool defaultPrevented() const { return m_defaultPrevented; }
    153         void preventDefault() { if (m_cancelable) m_defaultPrevented = true; }
    154         void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }
    155 
    156         bool defaultHandled() const { return m_defaultHandled; }
    157         void setDefaultHandled() { m_defaultHandled = true; }
    158 
    159         bool cancelBubble() const { return m_cancelBubble; }
    160         void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }
    161 
    162         Event* underlyingEvent() const { return m_underlyingEvent.get(); }
    163         void setUnderlyingEvent(PassRefPtr<Event>);
    164 
    165         virtual bool storesResultAsString() const;
    166         virtual void storeResult(const String&);
    167 
    168         virtual Clipboard* clipboard() const { return 0; }
    169 
    170 
    171     protected:
    172         Event();
    173         Event(const AtomicString& type, bool canBubble, bool cancelable);
    174 
    175         virtual void receivedTarget();
    176         bool dispatched() const { return m_target; }
    177 
    178     private:
    179         AtomicString m_type;
    180         bool m_canBubble;
    181         bool m_cancelable;
    182 
    183         bool m_propagationStopped;
    184         bool m_immediatePropagationStopped;
    185         bool m_defaultPrevented;
    186         bool m_defaultHandled;
    187         bool m_cancelBubble;
    188 
    189         unsigned short m_eventPhase;
    190         EventTarget* m_currentTarget;
    191         RefPtr<EventTarget> m_target;
    192         DOMTimeStamp m_createTime;
    193 
    194         RefPtr<Event> m_underlyingEvent;
    195     };
    196 
    197 class EventDispatchMediator {
    198 public:
    199     explicit EventDispatchMediator(PassRefPtr<Event>);
    200     virtual ~EventDispatchMediator();
    201 
    202     virtual bool dispatchEvent(EventDispatcher*) const;
    203 
    204 protected:
    205     EventDispatchMediator();
    206 
    207     Event* event() const;
    208     void setEvent(PassRefPtr<Event>);
    209 
    210 private:
    211     RefPtr<Event> m_event;
    212 };
    213 
    214 inline EventDispatchMediator::EventDispatchMediator()
    215 {
    216 }
    217 
    218 inline Event* EventDispatchMediator::event() const
    219 {
    220     return m_event.get();
    221 }
    222 
    223 inline void EventDispatchMediator::setEvent(PassRefPtr<Event> event)
    224 {
    225     m_event = event;
    226 }
    227 
    228 } // namespace WebCore
    229 
    230 #endif // Event_h
    231