fix logic
[personal-kdelibs.git] / khtml / ecma / kjs_events.h
blob45ccbecd990c230751bd5492384b725f13410856
1 // -*- c-basic-offset: 2 -*-
2 /*
3 * This file is part of the KDE libraries
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5 * Copyright (C) 2003 Apple Computer, Inc.
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.
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.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _KJS_EVENTS_H_
23 #define _KJS_EVENTS_H_
25 #include "ecma/kjs_dom.h"
26 #include "dom/dom2_events.h"
27 #include "dom/dom_misc.h"
28 #include "xml/dom2_eventsimpl.h"
30 namespace KJS {
32 class Window;
34 class JSEventListener : public DOM::EventListener {
35 public:
36 /**
37 * @param _listener the function object, that will be called when the event is emitted
38 * @param _compareListenerImp Compare Listener implementation.
39 * @param _win Window object, for memory management and caching.
40 * @param _html \c true if it is HTML.
41 * Never create a JSEventListener directly, use Window::getJSEventListener.
43 JSEventListener(JSObject* _listener, JSObject *_compareListenerImp, JSObject* _win, bool _html = false);
44 virtual ~JSEventListener();
45 virtual void handleEvent(DOM::Event &evt);
46 virtual DOM::DOMString eventListenerType();
47 // Return the KJS function object executed when this event is emitted
48 virtual JSObject* listenerObj() const;
49 // for Window::clear(). This is a bad hack though. The JSEventListener might not get deleted
50 // if it was added to a DOM node in another frame (#61467). But calling removeEventListener on
51 // all nodes we're listening to is quite difficult.
52 void clear() { listener = 0; compareListenerImp = 0; }
53 bool isHTMLEventListener() const { return html; }
55 protected:
56 mutable ProtectedPtr<JSObject> listener;
57 // Storing a different JSObject ptr is needed to support addEventListener(.. [Object] ..) calls
58 // In the real-life case (where a 'function' is passed to addEventListener) we can directly call
59 // the 'listener' object and can cache the 'listener.imp()'. If the event listener should be removed
60 // the implementation will call removeEventListener(.. [Function] ..), and we can lookup the event
61 // listener by the passed function's imp() ptr.
62 // In the only dom-approved way (passing an Object to add/removeEventListener), the 'listener'
63 // variable stores the function object 'passedListener.handleEvent'. But we need to cache
64 // the imp() ptr of the 'passedListener' function _object_, as the implementation will
65 // call removeEventListener(.. [Object ..] on removal, and now we can successfully lookup
66 // the correct event listener, as well as the 'listener.handleEvent' function, we need to call.
67 mutable ProtectedPtr<JSObject> compareListenerImp;
68 bool html;
69 mutable ProtectedPtr<JSObject> win;
72 class JSLazyEventListener : public JSEventListener {
73 public:
74 JSLazyEventListener(const QString &_code, const QString &_url, int _lineNum,
75 const QString &_name, JSObject* _win, DOM::NodeImpl* node, bool _svg = false);
76 ~JSLazyEventListener();
77 virtual void handleEvent(DOM::Event &evt);
78 JSObject* listenerObj() const;
79 private:
80 void parseCode() const;
82 mutable QString code;
83 mutable QString url;
84 int lineNum;
86 mutable QString name;
87 mutable bool parsed;
88 DOM::NodeImpl *originalNode;
89 bool svg;
92 // Constructor for Event - currently only used for some global vars
93 DEFINE_PSEUDO_CONSTRUCTOR(EventConstructor)
95 class DOMEvent : public DOMObject {
96 public:
97 // Build a DOMEvent
98 DOMEvent(ExecState *exec, DOM::EventImpl* e);
99 DOMEvent(JSObject *proto, DOM::EventImpl* e);
100 ~DOMEvent();
102 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
103 JSValue* getValueProperty(ExecState *, int token) const;
104 virtual void put(ExecState *exec, const Identifier &propertyName,
105 JSValue* value, int attr = None);
106 virtual JSValue* defaultValue(ExecState *exec, KJS::JSType hint) const;
107 void putValueProperty(ExecState *exec, int token, JSValue* value, int);
108 virtual const ClassInfo* classInfo() const { return &info; }
109 static const ClassInfo info;
110 enum { Type, Target, CurrentTarget, EventPhase, Bubbles,
111 Cancelable, TimeStamp, StopPropagation, PreventDefault, InitEvent,
112 // MS IE equivalents
113 SrcElement, ReturnValue, CancelBubble };
114 DOM::EventImpl* impl() const { return m_impl.get(); }
115 protected:
116 SharedPtr<DOM::EventImpl> m_impl;
119 JSValue* getDOMEvent(ExecState *exec, DOM::EventImpl* e);
122 * Convert an object to an Event. Returns a null Event if not possible.
124 DOM::EventImpl* toEvent(JSValue*);
126 // Constructor object EventException
127 class EventExceptionConstructor : public DOMObject {
128 public:
129 EventExceptionConstructor(ExecState *);
130 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
131 JSValue* getValueProperty(ExecState *, int token) const;
132 // no put - all read-only
133 virtual const ClassInfo* classInfo() const { return &info; }
134 static const ClassInfo info;
137 JSValue* getEventExceptionConstructor(ExecState *exec);
139 class DOMUIEvent : public DOMEvent {
140 public:
141 // Build a DOMUIEvent
142 DOMUIEvent(ExecState *exec, DOM::UIEventImpl* ue);
143 DOMUIEvent(JSObject *proto, DOM::UIEventImpl* ue);
144 ~DOMUIEvent();
145 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
146 JSValue* getValueProperty(ExecState *, int token) const;
147 // no put - all read-only
148 virtual const ClassInfo* classInfo() const { return &info; }
149 static const ClassInfo info;
150 enum { View, Detail, KeyCode, CharCode, LayerX, LayerY, PageX, PageY, Which, InitUIEvent };
151 DOM::UIEventImpl* impl() const { return static_cast<DOM::UIEventImpl*>(m_impl.get()); }
154 class DOMMouseEvent : public DOMUIEvent {
155 public:
156 DOMMouseEvent(ExecState *exec, DOM::MouseEventImpl* me);
157 ~DOMMouseEvent();
158 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
159 JSValue* getValueProperty(ExecState *, int token) const;
160 // no put - all read-only
161 virtual const ClassInfo* classInfo() const { return &info; }
162 static const ClassInfo info;
163 enum { ScreenX, ScreenY, ClientX, X, ClientY, Y, OffsetX, OffsetY,
164 CtrlKey, ShiftKey, AltKey,
165 MetaKey, Button, RelatedTarget, FromElement, ToElement,
166 InitMouseEvent
168 DOM::MouseEventImpl* impl() const { return static_cast<DOM::MouseEventImpl*>(m_impl.get()); }
171 class DOMKeyEventBase : public DOMUIEvent {
172 public:
173 DOMKeyEventBase(JSObject* proto, DOM::KeyEventBaseImpl* ke);
174 ~DOMKeyEventBase();
176 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
177 JSValue* getValueProperty(ExecState *, int token) const;
178 // no put - all read-only
179 virtual const ClassInfo* classInfo() const { return &info; }
180 static const ClassInfo info;
181 enum { Key, VirtKey, CtrlKey, ShiftKey, AltKey, MetaKey };
182 DOM::KeyEventBaseImpl* impl() const { return static_cast<DOM::KeyEventBaseImpl*>(m_impl.get()); }
185 class DOMTextEvent : public DOMKeyEventBase {
186 public:
187 DOMTextEvent(ExecState *exec, DOM::TextEventImpl* ke);
188 ~DOMTextEvent();
189 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
190 JSValue* getValueProperty(ExecState *, int token) const;
191 // no put - all read-only
192 virtual const ClassInfo* classInfo() const { return &info; }
193 static const ClassInfo info;
194 enum {Data, InitTextEvent};
195 DOM::TextEventImpl* impl() const { return static_cast<DOM::TextEventImpl*>(m_impl.get()); }
198 class DOMKeyboardEvent : public DOMKeyEventBase {
199 public:
200 DOMKeyboardEvent(ExecState *exec, DOM::KeyboardEventImpl* ke);
201 ~DOMKeyboardEvent();
202 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
203 JSValue* getValueProperty(ExecState *, int token) const;
204 // no put - all read-only
205 virtual const ClassInfo* classInfo() const { return &info; }
206 static const ClassInfo info;
207 enum {KeyIdentifier, KeyLocation, GetModifierState, InitKeyboardEvent};
208 DOM::KeyboardEventImpl* impl() const { return static_cast<DOM::KeyboardEventImpl*>(m_impl.get()); }
211 // Constructor object KeyboardEvent
212 class KeyboardEventConstructor : public DOMObject {
213 public:
214 KeyboardEventConstructor(ExecState *);
215 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
216 JSValue* getValueProperty(ExecState *, int token) const;
217 // no put - all read-only
218 virtual const ClassInfo* classInfo() const { return &info; }
219 static const ClassInfo info;
222 JSValue* getKeyboardEventConstructor(ExecState *exec);
224 // Constructor object MutationEvent
225 class MutationEventConstructor : public DOMObject {
226 public:
227 MutationEventConstructor(ExecState *);
228 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
229 JSValue* getValueProperty(ExecState *, int token) const;
230 // no put - all read-only
231 virtual const ClassInfo* classInfo() const { return &info; }
232 static const ClassInfo info;
235 JSValue* getMutationEventConstructor(ExecState *exec);
237 class DOMMutationEvent : public DOMEvent {
238 public:
239 DOMMutationEvent(ExecState *exec, DOM::MutationEventImpl* me);
240 ~DOMMutationEvent();
241 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot);
242 JSValue* getValueProperty(ExecState *, int token) const;
243 // no put - all read-only
244 virtual const ClassInfo* classInfo() const { return &info; }
245 static const ClassInfo info;
246 enum { AttrChange, RelatedNode, AttrName, PrevValue, NewValue,
247 InitMutationEvent };
248 DOM::MutationEventImpl* impl() const { return static_cast<DOM::MutationEventImpl*>(m_impl.get()); }
251 } // namespace
253 #endif