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 
00029 namespace KJS {
00030 
00031   class Window;
00032 
00033   class JSEventListener : public DOM::EventListener {
00034   public:
00040     JSEventListener(Object _listener, ObjectImp *_compareListenerImp, const Object &_win, bool _html = false);
00041     virtual ~JSEventListener();
00042     virtual void handleEvent(DOM::Event &evt);
00043     virtual DOM::DOMString eventListenerType();
00044     // Return the KJS function object executed when this event is emitted
00045     virtual Object listenerObj() const;
00046     ObjectImp *listenerObjImp() const { return listenerObj().imp(); }
00047     // for Window::clear(). This is a bad hack though. The JSEventListener might not get deleted
00048     // if it was added to a DOM node in another frame (#61467). But calling removeEventListener on
00049     // all nodes we're listening to is quite difficult.
00050     void clear() { listener = Object(); }
00051     bool isHTMLEventListener() const { return html; }
00052 
00053   protected:
00054     mutable Object listener;
00055     // Storing a different ObjectImp ptr is needed to support addEventListener(.. [Object] ..) calls
00056     // In the real-life case (where a 'function' is passed to addEventListener) we can directly call
00057     // the 'listener' object and can cache the 'listener.imp()'. If the event listener should be removed
00058     // the implementation will call removeEventListener(.. [Function] ..), and we can lookup the event
00059     // listener by the passed function's imp() ptr.
00060     // In the only dom-approved way (passing an Object to add/removeEventListener), the 'listener'
00061     // variable stores the function object 'passedListener.handleEvent'. But we need to cache
00062     // the imp() ptr of the 'passedListener' function _object_, as the implementation will
00063     // call removeEventListener(.. [Object ..] on removal, and now we can successfully lookup
00064     // the correct event listener, as well as the 'listener.handleEvent' function, we need to call.
00065     mutable ObjectImp *compareListenerImp;
00066     bool html;
00067     Object win;
00068   };
00069 
00070   class JSLazyEventListener : public JSEventListener {
00071   public:
00072     JSLazyEventListener(const QString &_code, const QString &_name, const Object &_win, DOM::NodeImpl* node);
00073     ~JSLazyEventListener();
00074     virtual void handleEvent(DOM::Event &evt);
00075     Object listenerObj() const;
00076   private:
00077     void parseCode() const;
00078 
00079     mutable QString code;
00080     mutable QString name;
00081     mutable bool parsed;
00082     DOM::NodeImpl *originalNode;
00083   };
00084 
00085   // Constructor for Event - currently only used for some global vars
00086   class EventConstructor : public DOMObject {
00087   public:
00088     EventConstructor(ExecState *);
00089     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00090     Value getValueProperty(ExecState *, int token) const;
00091     // no put - all read-only
00092     virtual const ClassInfo* classInfo() const { return &info; }
00093     static const ClassInfo info;
00094   };
00095 
00096   Value getEventConstructor(ExecState *exec);
00097 
00098   class DOMEvent : public DOMObject {
00099   public:
00100     // Build a DOMEvent
00101     DOMEvent(ExecState *exec, DOM::Event e);
00102     // Constructor for inherited classes
00103     DOMEvent(const Object &proto, DOM::Event e);
00104     ~DOMEvent();
00105     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00106     Value getValueProperty(ExecState *, int token) const;
00107     virtual void tryPut(ExecState *exec, const Identifier &propertyName,
00108             const Value& value, int attr = None);
00109     virtual Value defaultValue(ExecState *exec, KJS::Type hint) const;
00110     void putValueProperty(ExecState *exec, int token, const Value& value, int);
00111     virtual const ClassInfo* classInfo() const { return &info; }
00112     static const ClassInfo info;
00113     enum { Type, Target, CurrentTarget, EventPhase, Bubbles,
00114            Cancelable, TimeStamp, StopPropagation, PreventDefault, InitEvent,
00115        // MS IE equivalents
00116        SrcElement, ReturnValue, CancelBubble };
00117     DOM::Event toEvent() const { return event; }
00118   protected:
00119     DOM::Event event;
00120   };
00121 
00122   Value getDOMEvent(ExecState *exec, DOM::Event e);
00123 
00127   DOM::Event toEvent(const Value&);
00128 
00129   // Constructor object EventException
00130   class EventExceptionConstructor : public DOMObject {
00131   public:
00132     EventExceptionConstructor(ExecState *);
00133     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00134     Value getValueProperty(ExecState *, int token) const;
00135     // no put - all read-only
00136     virtual const ClassInfo* classInfo() const { return &info; }
00137     static const ClassInfo info;
00138   };
00139 
00140   Value getEventExceptionConstructor(ExecState *exec);
00141 
00142   class DOMUIEvent : public DOMEvent {
00143   public:
00144     // Build a DOMUIEvent
00145     DOMUIEvent(ExecState *exec, DOM::UIEvent ue);
00146     // Constructor for inherited classes
00147     DOMUIEvent(const Object &proto, DOM::UIEvent ue);
00148     ~DOMUIEvent();
00149     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00150     Value getValueProperty(ExecState *, int token) const;
00151     // no put - all read-only
00152     virtual const ClassInfo* classInfo() const { return &info; }
00153     static const ClassInfo info;
00154     enum { View, Detail, KeyCode, CharCode, LayerX, LayerY, PageX, PageY, Which, InitUIEvent };
00155     DOM::UIEvent toUIEvent() const { return static_cast<DOM::UIEvent>(event); }
00156   };
00157 
00158   class DOMMouseEvent : public DOMUIEvent {
00159   public:
00160     DOMMouseEvent(ExecState *exec, DOM::MouseEvent me);
00161     ~DOMMouseEvent();
00162     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00163     Value getValueProperty(ExecState *, int token) const;
00164     // no put - all read-only
00165     virtual const ClassInfo* classInfo() const { return &info; }
00166     static const ClassInfo info;
00167     enum { ScreenX, ScreenY, ClientX, X, ClientY, Y, OffsetX, OffsetY,
00168            CtrlKey, ShiftKey, AltKey,
00169            MetaKey, Button, RelatedTarget, FromElement, ToElement,
00170            InitMouseEvent
00171     };
00172     DOM::MouseEvent toMouseEvent() const { return static_cast<DOM::MouseEvent>(event); }
00173   };
00174 
00175   class DOMTextEvent : public DOMUIEvent {
00176   public:
00177     DOMTextEvent(ExecState *exec, DOM::TextEvent ke);
00178     ~DOMTextEvent();
00179     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00180     Value getValueProperty(ExecState *, int token) const;
00181     // no put - all read-only
00182     virtual const ClassInfo* classInfo() const { return &info; }
00183     static const ClassInfo info;
00184     enum { Key, VirtKey, OutputString, InitTextEvent, InputGenerated, NumPad,
00185            CtrlKey, ShiftKey, AltKey, MetaKey };
00186     DOM::TextEvent toTextEvent() const { return static_cast<DOM::TextEvent>(event); }
00187   };
00188 
00189   // Constructor object MutationEvent
00190   class MutationEventConstructor : public DOMObject {
00191   public:
00192     MutationEventConstructor(ExecState *);
00193     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00194     Value getValueProperty(ExecState *, int token) const;
00195     // no put - all read-only
00196     virtual const ClassInfo* classInfo() const { return &info; }
00197     static const ClassInfo info;
00198   };
00199 
00200   Value getMutationEventConstructor(ExecState *exec);
00201 
00202   class DOMMutationEvent : public DOMEvent {
00203   public:
00204     DOMMutationEvent(ExecState *exec, DOM::MutationEvent me);
00205     ~DOMMutationEvent();
00206     virtual Value tryGet(ExecState *exec,const Identifier &p) const;
00207     Value getValueProperty(ExecState *, int token) const;
00208     // no put - all read-only
00209     virtual const ClassInfo* classInfo() const { return &info; }
00210     static const ClassInfo info;
00211     enum { AttrChange, RelatedNode, AttrName, PrevValue, NewValue,
00212            InitMutationEvent };
00213     DOM::MutationEvent toMutationEvent() const { return static_cast<DOM::MutationEvent>(event); }
00214   };
00215 
00216 } // namespace
00217 
00218 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys