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   class EventConstructor : public DOMObject {
00088   public:
00089     EventConstructor(ExecState *);
00090     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00091     Value getValueProperty(ExecState *, int token) const;
00092     // no put - all read-only
00093     virtual const ClassInfo* classInfo() const { return &info; }
00094     static const ClassInfo info;
00095   };
00096 
00097   Value getEventConstructor(ExecState *exec);
00098 
00099   class DOMEvent : public DOMObject {
00100   public:
00101     // Build a DOMEvent
00102     DOMEvent(ExecState *exec, DOM::Event e);
00103     // Constructor for inherited classes
00104     DOMEvent(const Object &proto, DOM::Event e);
00105     ~DOMEvent();
00106     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00107     Value getValueProperty(ExecState *, int token) const;
00108     virtual void tryPut(ExecState *exec, const Identifier &propertyName,
00109             const Value& value, int attr = None);
00110     virtual Value defaultValue(ExecState *exec, KJS::Type hint) const;
00111     void putValueProperty(ExecState *exec, int token, const Value& value, int);
00112     virtual const ClassInfo* classInfo() const { return &info; }
00113     static const ClassInfo info;
00114     enum { Type, Target, CurrentTarget, EventPhase, Bubbles,
00115            Cancelable, TimeStamp, StopPropagation, PreventDefault, InitEvent,
00116        // MS IE equivalents
00117        SrcElement, ReturnValue, CancelBubble };
00118     DOM::Event toEvent() const { return event; }
00119   protected:
00120     DOM::Event event;
00121   };
00122 
00123   Value getDOMEvent(ExecState *exec, DOM::Event e);
00124 
00128   DOM::Event toEvent(const Value&);
00129 
00130   // Constructor object EventException
00131   class EventExceptionConstructor : public DOMObject {
00132   public:
00133     EventExceptionConstructor(ExecState *);
00134     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00135     Value getValueProperty(ExecState *, int token) const;
00136     // no put - all read-only
00137     virtual const ClassInfo* classInfo() const { return &info; }
00138     static const ClassInfo info;
00139   };
00140 
00141   Value getEventExceptionConstructor(ExecState *exec);
00142 
00143   class DOMUIEvent : public DOMEvent {
00144   public:
00145     // Build a DOMUIEvent
00146     DOMUIEvent(ExecState *exec, DOM::UIEvent ue);
00147     // Constructor for inherited classes
00148     DOMUIEvent(const Object &proto, DOM::UIEvent ue);
00149     ~DOMUIEvent();
00150     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00151     Value getValueProperty(ExecState *, int token) const;
00152     // no put - all read-only
00153     virtual const ClassInfo* classInfo() const { return &info; }
00154     static const ClassInfo info;
00155     enum { View, Detail, KeyCode, CharCode, LayerX, LayerY, PageX, PageY, Which, InitUIEvent };
00156     DOM::UIEvent toUIEvent() const { return static_cast<DOM::UIEvent>(event); }
00157   };
00158 
00159   class DOMMouseEvent : public DOMUIEvent {
00160   public:
00161     DOMMouseEvent(ExecState *exec, DOM::MouseEvent me);
00162     ~DOMMouseEvent();
00163     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00164     Value getValueProperty(ExecState *, int token) const;
00165     // no put - all read-only
00166     virtual const ClassInfo* classInfo() const { return &info; }
00167     static const ClassInfo info;
00168     enum { ScreenX, ScreenY, ClientX, X, ClientY, Y, OffsetX, OffsetY,
00169            CtrlKey, ShiftKey, AltKey,
00170            MetaKey, Button, RelatedTarget, FromElement, ToElement,
00171            InitMouseEvent
00172     };
00173     DOM::MouseEvent toMouseEvent() const { return static_cast<DOM::MouseEvent>(event); }
00174   };
00175 
00176   class DOMKeyEventBase : public DOMUIEvent {
00177   public:
00178     DOMKeyEventBase(const Object &proto, DOM::TextEvent ke);
00179     ~DOMKeyEventBase();
00180 
00181     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00182     Value getValueProperty(ExecState *, int token) const;
00183     // no put - all read-only
00184     virtual const ClassInfo* classInfo() const { return &info; }
00185     static const ClassInfo info;
00186     enum { Key, VirtKey, CtrlKey, ShiftKey, AltKey, MetaKey };
00187     DOM::KeyEventBaseImpl* impl() const { return static_cast<DOM::KeyEventBaseImpl*>(event.handle()); }
00188   };
00189 
00190   class DOMTextEvent : public DOMKeyEventBase {
00191   public:
00192     DOMTextEvent(ExecState *exec, DOM::TextEvent ke);
00193     ~DOMTextEvent();
00194     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00195     Value getValueProperty(ExecState *, int token) const;
00196     // no put - all read-only
00197     virtual const ClassInfo* classInfo() const { return &info; }
00198     static const ClassInfo info;
00199     enum {Data, InitTextEvent};
00200     DOM::TextEventImpl* impl() const { return static_cast<DOM::TextEventImpl*>(event.handle()); }
00201   };
00202 
00203   class DOMKeyboardEvent : public DOMKeyEventBase {
00204   public:
00205     DOMKeyboardEvent(ExecState *exec, DOM::TextEvent ke);
00206     ~DOMKeyboardEvent();
00207     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00208     Value getValueProperty(ExecState *, int token) const;
00209     // no put - all read-only
00210     virtual const ClassInfo* classInfo() const { return &info; }
00211     static const ClassInfo info;
00212     enum {KeyIdentifier, KeyLocation, GetModifierState, InitKeyboardEvent};
00213     DOM::KeyboardEventImpl* impl() const { return static_cast<DOM::KeyboardEventImpl*>(event.handle()); }
00214   };
00215 
00216   // Constructor object KeyboardEvent
00217   class KeyboardEventConstructor : public DOMObject {
00218   public:
00219     KeyboardEventConstructor(ExecState *);
00220     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00221     Value getValueProperty(ExecState *, int token) const;
00222     // no put - all read-only
00223     virtual const ClassInfo* classInfo() const { return &info; }
00224     static const ClassInfo info;
00225   };
00226 
00227   Value getKeyboardEventConstructor(ExecState *exec);
00228 
00229   // Constructor object MutationEvent
00230   class MutationEventConstructor : public DOMObject {
00231   public:
00232     MutationEventConstructor(ExecState *);
00233     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00234     Value getValueProperty(ExecState *, int token) const;
00235     // no put - all read-only
00236     virtual const ClassInfo* classInfo() const { return &info; }
00237     static const ClassInfo info;
00238   };
00239 
00240   Value getMutationEventConstructor(ExecState *exec);
00241 
00242   class DOMMutationEvent : public DOMEvent {
00243   public:
00244     DOMMutationEvent(ExecState *exec, DOM::MutationEvent me);
00245     ~DOMMutationEvent();
00246     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00247     Value getValueProperty(ExecState *, int token) const;
00248     // no put - all read-only
00249     virtual const ClassInfo* classInfo() const { return &info; }
00250     static const ClassInfo info;
00251     enum { AttrChange, RelatedNode, AttrName, PrevValue, NewValue,
00252            InitMutationEvent };
00253     DOM::MutationEvent toMutationEvent() const { return static_cast<DOM::MutationEvent>(event); }
00254   };
00255 
00256 } // namespace
00257 
00258 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys