kjs_events.h

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
00005  *  Copyright (C) 2003 Apple Computer, Inc.
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Library General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public
00018  *  License along with this library; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _KJS_EVENTS_H_
00023 #define _KJS_EVENTS_H_
00024 
00025 #include "ecma/kjs_dom.h"
00026 #include "dom/dom2_events.h"
00027 #include "dom/dom_misc.h"
00028 #include "xml/dom2_eventsimpl.h"
00029 
00030 namespace KJS {
00031 
00032   class Window;
00033 
00034   class JSEventListener : public DOM::EventListener {
00035   public:
00041     JSEventListener(Object _listener, ObjectImp *_compareListenerImp, const Object &_win, bool _html = false);
00042     virtual ~JSEventListener();
00043     virtual void handleEvent(DOM::Event &evt);
00044     virtual DOM::DOMString eventListenerType();
00045     // Return the KJS function object executed when this event is emitted
00046     virtual Object listenerObj() const;
00047     ObjectImp *listenerObjImp() const { return listenerObj().imp(); }
00048     // for Window::clear(). This is a bad hack though. The JSEventListener might not get deleted
00049     // if it was added to a DOM node in another frame (#61467). But calling removeEventListener on
00050     // all nodes we're listening to is quite difficult.
00051     void clear() { listener = Object(); }
00052     bool isHTMLEventListener() const { return html; }
00053 
00054   protected:
00055     mutable Object listener;
00056     // Storing a different ObjectImp ptr is needed to support addEventListener(.. [Object] ..) calls
00057     // In the real-life case (where a 'function' is passed to addEventListener) we can directly call
00058     // the 'listener' object and can cache the 'listener.imp()'. If the event listener should be removed
00059     // the implementation will call removeEventListener(.. [Function] ..), and we can lookup the event
00060     // listener by the passed function's imp() ptr.
00061     // In the only dom-approved way (passing an Object to add/removeEventListener), the 'listener'
00062     // variable stores the function object 'passedListener.handleEvent'. But we need to cache
00063     // the imp() ptr of the 'passedListener' function _object_, as the implementation will
00064     // call removeEventListener(.. [Object ..] on removal, and now we can successfully lookup
00065     // the correct event listener, as well as the 'listener.handleEvent' function, we need to call.
00066     mutable ObjectImp *compareListenerImp;
00067     bool html;
00068     Object win;
00069   };
00070 
00071   class JSLazyEventListener : public JSEventListener {
00072   public:
00073     JSLazyEventListener(const QString &_code, const QString &_name, const Object &_win, DOM::NodeImpl* node);
00074     ~JSLazyEventListener();
00075     virtual void handleEvent(DOM::Event &evt);
00076     Object listenerObj() const;
00077   private:
00078     void parseCode() const;
00079 
00080     mutable QString code;
00081     mutable QString name;
00082     mutable bool parsed;
00083     DOM::NodeImpl *originalNode;
00084   };
00085 
00086   // Constructor for Event - currently only used for some global vars
00087   DEFINE_PSEUDO_CONSTRUCTOR(EventConstructor)
00088 
00089   class DOMEvent : public DOMObject {
00090   public:
00091     // Build a DOMEvent
00092     DOMEvent(ExecState *exec, DOM::Event e);
00093     // Constructor for inherited classes
00094     DOMEvent(const Object &proto, DOM::Event e);
00095     ~DOMEvent();
00096     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00097     Value getValueProperty(ExecState *, int token) const;
00098     virtual void tryPut(ExecState *exec, const Identifier &propertyName,
00099             const Value& value, int attr = None);
00100     virtual Value defaultValue(ExecState *exec, KJS::Type hint) const;
00101     void putValueProperty(ExecState *exec, int token, const Value& value, int);
00102     virtual const ClassInfo* classInfo() const { return &info; }
00103     static const ClassInfo info;
00104     enum { Type, Target, CurrentTarget, EventPhase, Bubbles,
00105            Cancelable, TimeStamp, StopPropagation, PreventDefault, InitEvent,
00106        // MS IE equivalents
00107        SrcElement, ReturnValue, CancelBubble };
00108     DOM::Event toEvent() const { return event; }
00109   protected:
00110     DOM::Event event;
00111   };
00112 
00113   Value getDOMEvent(ExecState *exec, DOM::Event e);
00114 
00118   DOM::Event toEvent(const Value&);
00119 
00120   // Constructor object EventException
00121   class EventExceptionConstructor : public DOMObject {
00122   public:
00123     EventExceptionConstructor(ExecState *);
00124     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00125     Value getValueProperty(ExecState *, int token) const;
00126     // no put - all read-only
00127     virtual const ClassInfo* classInfo() const { return &info; }
00128     static const ClassInfo info;
00129   };
00130 
00131   Value getEventExceptionConstructor(ExecState *exec);
00132 
00133   class DOMUIEvent : public DOMEvent {
00134   public:
00135     // Build a DOMUIEvent
00136     DOMUIEvent(ExecState *exec, DOM::UIEvent ue);
00137     // Constructor for inherited classes
00138     DOMUIEvent(const Object &proto, DOM::UIEvent ue);
00139     ~DOMUIEvent();
00140     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00141     Value getValueProperty(ExecState *, int token) const;
00142     // no put - all read-only
00143     virtual const ClassInfo* classInfo() const { return &info; }
00144     static const ClassInfo info;
00145     enum { View, Detail, KeyCode, CharCode, LayerX, LayerY, PageX, PageY, Which, InitUIEvent };
00146     DOM::UIEvent toUIEvent() const { return static_cast<DOM::UIEvent>(event); }
00147   };
00148 
00149   class DOMMouseEvent : public DOMUIEvent {
00150   public:
00151     DOMMouseEvent(ExecState *exec, DOM::MouseEvent me);
00152     ~DOMMouseEvent();
00153     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00154     Value getValueProperty(ExecState *, int token) const;
00155     // no put - all read-only
00156     virtual const ClassInfo* classInfo() const { return &info; }
00157     static const ClassInfo info;
00158     enum { ScreenX, ScreenY, ClientX, X, ClientY, Y, OffsetX, OffsetY,
00159            CtrlKey, ShiftKey, AltKey,
00160            MetaKey, Button, RelatedTarget, FromElement, ToElement,
00161            InitMouseEvent
00162     };
00163     DOM::MouseEvent toMouseEvent() const { return static_cast<DOM::MouseEvent>(event); }
00164   };
00165 
00166   class DOMKeyEventBase : public DOMUIEvent {
00167   public:
00168     DOMKeyEventBase(const Object &proto, DOM::TextEvent ke);
00169     ~DOMKeyEventBase();
00170 
00171     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00172     Value getValueProperty(ExecState *, int token) const;
00173     // no put - all read-only
00174     virtual const ClassInfo* classInfo() const { return &info; }
00175     static const ClassInfo info;
00176     enum { Key, VirtKey, CtrlKey, ShiftKey, AltKey, MetaKey };
00177     DOM::KeyEventBaseImpl* impl() const { return static_cast<DOM::KeyEventBaseImpl*>(event.handle()); }
00178   };
00179 
00180   class DOMTextEvent : public DOMKeyEventBase {
00181   public:
00182     DOMTextEvent(ExecState *exec, DOM::TextEvent ke);
00183     ~DOMTextEvent();
00184     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00185     Value getValueProperty(ExecState *, int token) const;
00186     // no put - all read-only
00187     virtual const ClassInfo* classInfo() const { return &info; }
00188     static const ClassInfo info;
00189     enum {Data, InitTextEvent};
00190     DOM::TextEventImpl* impl() const { return static_cast<DOM::TextEventImpl*>(event.handle()); }
00191   };
00192 
00193   class DOMKeyboardEvent : public DOMKeyEventBase {
00194   public:
00195     DOMKeyboardEvent(ExecState *exec, DOM::TextEvent ke);
00196     ~DOMKeyboardEvent();
00197     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00198     Value getValueProperty(ExecState *, int token) const;
00199     // no put - all read-only
00200     virtual const ClassInfo* classInfo() const { return &info; }
00201     static const ClassInfo info;
00202     enum {KeyIdentifier, KeyLocation, GetModifierState, InitKeyboardEvent};
00203     DOM::KeyboardEventImpl* impl() const { return static_cast<DOM::KeyboardEventImpl*>(event.handle()); }
00204   };
00205 
00206   // Constructor object KeyboardEvent
00207   class KeyboardEventConstructor : public DOMObject {
00208   public:
00209     KeyboardEventConstructor(ExecState *);
00210     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00211     Value getValueProperty(ExecState *, int token) const;
00212     // no put - all read-only
00213     virtual const ClassInfo* classInfo() const { return &info; }
00214     static const ClassInfo info;
00215   };
00216 
00217   Value getKeyboardEventConstructor(ExecState *exec);
00218 
00219   // Constructor object MutationEvent
00220   class MutationEventConstructor : public DOMObject {
00221   public:
00222     MutationEventConstructor(ExecState *);
00223     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00224     Value getValueProperty(ExecState *, int token) const;
00225     // no put - all read-only
00226     virtual const ClassInfo* classInfo() const { return &info; }
00227     static const ClassInfo info;
00228   };
00229 
00230   Value getMutationEventConstructor(ExecState *exec);
00231 
00232   class DOMMutationEvent : public DOMEvent {
00233   public:
00234     DOMMutationEvent(ExecState *exec, DOM::MutationEvent me);
00235     ~DOMMutationEvent();
00236     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00237     Value getValueProperty(ExecState *, int token) const;
00238     // no put - all read-only
00239     virtual const ClassInfo* classInfo() const { return &info; }
00240     static const ClassInfo info;
00241     enum { AttrChange, RelatedNode, AttrName, PrevValue, NewValue,
00242            InitMutationEvent };
00243     DOM::MutationEvent toMutationEvent() const { return static_cast<DOM::MutationEvent>(event); }
00244   };
00245 
00246 } // namespace
00247 
00248 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys