fix logic
[personal-kdelibs.git] / khtml / xml / dom2_eventsimpl.h
blob7e38c2cbebe27ca79ecdae42a2a891d1ed10c7e8
1 /*
2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
6 * (C) 2002 Apple Computer, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef _DOM_EventsImpl_h_
26 #define _DOM_EventsImpl_h_
28 #include "dom/dom2_events.h"
29 #include "xml/dom2_viewsimpl.h"
30 #include "misc/idstring.h"
32 #undef FOCUS_EVENT //for win32
34 class QMouseEvent;
36 namespace DOM {
38 class AbstractViewImpl;
39 class DOMStringImpl;
40 class NodeImpl;
42 class EventImpl : public khtml::Shared<EventImpl>
44 public:
45 enum EventId {
46 // UI events
47 DOMFOCUSIN_EVENT,
48 DOMFOCUSOUT_EVENT,
49 DOMACTIVATE_EVENT,
50 // Mouse events
51 CLICK_EVENT,
52 MOUSEDOWN_EVENT,
53 MOUSEUP_EVENT,
54 MOUSEOVER_EVENT,
55 MOUSEMOVE_EVENT,
56 MOUSEOUT_EVENT,
57 // Mutation events
58 DOMSUBTREEMODIFIED_EVENT,
59 DOMNODEINSERTED_EVENT,
60 DOMNODEREMOVED_EVENT,
61 DOMNODEREMOVEDFROMDOCUMENT_EVENT,
62 DOMNODEINSERTEDINTODOCUMENT_EVENT,
63 DOMATTRMODIFIED_EVENT,
64 DOMCHARACTERDATAMODIFIED_EVENT,
65 // HTML events
66 LOAD_EVENT,
67 UNLOAD_EVENT,
68 ABORT_EVENT,
69 ERROR_EVENT,
70 SELECT_EVENT,
71 CHANGE_EVENT,
72 SUBMIT_EVENT,
73 RESET_EVENT,
74 FOCUS_EVENT,
75 BLUR_EVENT,
76 RESIZE_EVENT,
77 SCROLL_EVENT,
78 // keyboard events
79 KEYDOWN_EVENT,
80 KEYUP_EVENT,
81 KEYPRESS_EVENT, //Mostly corresponds to DOM3 textInput event.
82 // khtml events (not part of DOM)
83 KHTML_ECMA_DBLCLICK_EVENT, // for html ondblclick
84 KHTML_ECMA_CLICK_EVENT, // for html onclick
85 KHTML_DRAGDROP_EVENT,
86 KHTML_MOVE_EVENT,
87 KHTML_MOUSEWHEEL_EVENT,
88 KHTML_CONTENTLOADED_EVENT,
89 // XMLHttpRequest events
90 KHTML_READYSTATECHANGE_EVENT
93 EventImpl();
94 EventImpl(EventId id, bool canBubbleArg, bool cancelableArg);
95 virtual ~EventImpl();
97 EventId id() const { return EventId(m_eventName.id()); }
98 DOMString type() const { return m_eventName.toString(); }
99 EventName name() const { return m_eventName; }
101 NodeImpl *target() const { return m_target; }
102 void setTarget(NodeImpl *_target);
103 NodeImpl *currentTarget() const { return m_currentTarget; }
104 void setCurrentTarget(NodeImpl *_currentTarget) { m_currentTarget = _currentTarget; }
105 unsigned short eventPhase() const { return m_eventPhase; }
106 void setEventPhase(unsigned short _eventPhase) { m_eventPhase = _eventPhase; }
107 bool bubbles() const { return m_canBubble; }
108 bool cancelable() const { return m_cancelable; }
109 DOMTimeStamp timeStamp();
110 void stopPropagation(bool stop) { m_propagationStopped = stop; }
111 void preventDefault(bool prevent) { if ( m_cancelable ) m_defaultPrevented = prevent; }
113 void initEvent(const DOMString &eventTypeArg, bool canBubbleArg, bool cancelableArg);
115 virtual bool isUIEvent() const;
116 virtual bool isMouseEvent() const;
117 virtual bool isMutationEvent() const;
118 virtual bool isTextInputEvent() const;
119 virtual bool isKeyboardEvent() const;
120 bool isKeyRelatedEvent() const { return isTextInputEvent() || isKeyboardEvent(); }
122 bool propagationStopped() const { return m_propagationStopped; }
123 bool defaultPrevented() const { return m_defaultPrevented; }
125 void setDefaultHandled() { m_defaultHandled = true; }
126 bool defaultHandled() const { return m_defaultHandled; }
128 DOMString message() const { return m_message; }
129 void setMessage(const DOMString &_message) { m_message = _message; }
131 static khtml::IDTable<EventImpl>* idTable() {
132 if (s_idTable)
133 return s_idTable;
134 else
135 return initIdTable();
137 protected:
138 static khtml::IDTable<EventImpl>* s_idTable;
139 static khtml::IDTable<EventImpl>* initIdTable();
140 EventName m_eventName;
141 bool m_canBubble : 1;
142 bool m_cancelable : 1;
144 bool m_propagationStopped : 1;
145 bool m_defaultPrevented : 1;
146 bool m_defaultHandled : 1;
147 unsigned short m_eventPhase : 2;
148 NodeImpl *m_currentTarget; // ref > 0 maintained externally
149 NodeImpl *m_target;
150 QDateTime m_createTime;
151 DOMString m_message;
156 class UIEventImpl : public EventImpl
158 public:
159 UIEventImpl() : m_view(0), m_detail(0) {}
160 UIEventImpl(EventId _id,
161 bool canBubbleArg,
162 bool cancelableArg,
163 AbstractViewImpl *viewArg,
164 long detailArg);
165 virtual ~UIEventImpl();
166 AbstractViewImpl *view() const { return m_view; }
167 long detail() const { return m_detail; }
168 void initUIEvent(const DOMString &typeArg,
169 bool canBubbleArg,
170 bool cancelableArg,
171 AbstractViewImpl* viewArg,
172 long detailArg);
173 virtual bool isUIEvent() const;
175 //Compat stuff
176 virtual int keyCode() const { return 0; }
177 virtual int charCode() const { return 0; }
179 virtual long pageX() const { return 0; }
180 virtual long pageY() const { return 0; }
181 virtual long layerX() const { return 0; }
182 virtual long layerY() const { return 0; }
183 virtual int which() const { return 0; }
184 protected:
185 AbstractViewImpl *m_view;
186 long m_detail;
190 // Introduced in DOM Level 2: - internal
191 class MouseEventImpl : public UIEventImpl {
192 public:
193 enum Orientation {
194 ONone = 0,
195 OHorizontal,
196 OVertical
199 MouseEventImpl();
200 MouseEventImpl(EventId _id,
201 bool canBubbleArg,
202 bool cancelableArg,
203 AbstractViewImpl *viewArg,
204 long detailArg,
205 long screenXArg,
206 long screenYArg,
207 long clientXArg,
208 long clientYArg,
209 long pageXArg,
210 long pageYArg,
211 bool ctrlKeyArg,
212 bool altKeyArg,
213 bool shiftKeyArg,
214 bool metaKeyArg,
215 unsigned short buttonArg,
216 NodeImpl *relatedTargetArg,
217 QMouseEvent *qe = 0,
218 bool isDoubleClick = false,
219 Orientation orient = ONone);
220 virtual ~MouseEventImpl();
221 long screenX() const { return m_screenX; }
222 long screenY() const { return m_screenY; }
223 long clientX() const { return m_clientX; }
224 long clientY() const { return m_clientY; }
225 long layerX() const { return m_layerX; } // non-DOM extension
226 long layerY() const { return m_layerY; } // non-DOM extension
227 long pageX() const { return m_pageX; } // non-DOM extension
228 long pageY() const { return m_pageY; } // non-DOM extension
229 virtual int which() const { return button() + 1; } // non-DOM extension
230 bool isDoubleClick() const { return m_isDoubleClick; } // non-DOM extension
231 Orientation orientation() const { return KDE_CAST_BF_ENUM(Orientation, m_orientation); } // non-DOM extension
232 bool ctrlKey() const { return m_ctrlKey; }
233 bool shiftKey() const { return m_shiftKey; }
234 bool altKey() const { return m_altKey; }
235 bool metaKey() const { return m_metaKey; }
236 unsigned short button() const { return m_button; }
237 NodeImpl *relatedTarget() const { return m_relatedTarget; }
239 void computeLayerPos();
241 void initMouseEvent(const DOMString &typeArg,
242 bool canBubbleArg,
243 bool cancelableArg,
244 AbstractViewImpl* viewArg,
245 long detailArg,
246 long screenXArg,
247 long screenYArg,
248 long clientXArg,
249 long clientYArg,
250 bool ctrlKeyArg,
251 bool altKeyArg,
252 bool shiftKeyArg,
253 bool metaKeyArg,
254 unsigned short buttonArg,
255 const Node &relatedTargetArg,
256 Orientation orient = ONone);
257 virtual bool isMouseEvent() const;
259 QMouseEvent *qEvent() const { return m_qevent; }
260 protected:
261 long m_screenX;
262 long m_screenY;
263 long m_clientX;
264 long m_clientY;
265 long m_layerX;
266 long m_layerY;
267 long m_pageX;
268 long m_pageY;
269 bool m_ctrlKey : 1;
270 bool m_altKey : 1;
271 bool m_shiftKey : 1;
272 bool m_metaKey : 1;
273 bool m_isDoubleClick : 1;
274 KDE_BF_ENUM(Orientation) m_orientation : 2;
275 unsigned short m_button;
276 NodeImpl *m_relatedTarget;
277 QMouseEvent *m_qevent;
280 class KeyEventBaseImpl : public UIEventImpl {
281 public:
282 // VirtualKeyCode
283 enum KeyCodes {
284 DOM_VK_UNDEFINED = 0x0,
285 DOM_VK_RIGHT_ALT = 0x12,
286 DOM_VK_LEFT_ALT = 0x12,
287 DOM_VK_LEFT_CONTROL = 0x11,
288 DOM_VK_RIGHT_CONTROL = 0x11,
289 DOM_VK_LEFT_SHIFT = 0x10,
290 DOM_VK_RIGHT_SHIFT = 0x10,
291 DOM_VK_META = 0x9D,
292 DOM_VK_BACK_SPACE = 0x08,
293 DOM_VK_CAPS_LOCK = 0x14,
294 DOM_VK_DELETE = 0x7F,
295 DOM_VK_END = 0x23,
296 DOM_VK_ENTER = 0x0D,
297 DOM_VK_ESCAPE = 0x1B,
298 DOM_VK_HOME = 0x24,
299 DOM_VK_NUM_LOCK = 0x90,
300 DOM_VK_PAUSE = 0x13,
301 DOM_VK_PRINTSCREEN = 0x9A,
302 DOM_VK_SCROLL_LOCK = 0x91,
303 DOM_VK_SPACE = 0x20,
304 DOM_VK_TAB = 0x09,
305 DOM_VK_LEFT = 0x25,
306 DOM_VK_RIGHT = 0x27,
307 DOM_VK_UP = 0x26,
308 DOM_VK_DOWN = 0x28,
309 DOM_VK_PAGE_DOWN = 0x22,
310 DOM_VK_PAGE_UP = 0x21,
311 DOM_VK_F1 = 0x70,
312 DOM_VK_F2 = 0x71,
313 DOM_VK_F3 = 0x72,
314 DOM_VK_F4 = 0x73,
315 DOM_VK_F5 = 0x74,
316 DOM_VK_F6 = 0x75,
317 DOM_VK_F7 = 0x76,
318 DOM_VK_F8 = 0x77,
319 DOM_VK_F9 = 0x78,
320 DOM_VK_F10 = 0x79,
321 DOM_VK_F11 = 0x7A,
322 DOM_VK_F12 = 0x7B,
323 DOM_VK_F13 = 0xF000,
324 DOM_VK_F14 = 0xF001,
325 DOM_VK_F15 = 0xF002,
326 DOM_VK_F16 = 0xF003,
327 DOM_VK_F17 = 0xF004,
328 DOM_VK_F18 = 0xF005,
329 DOM_VK_F19 = 0xF006,
330 DOM_VK_F20 = 0xF007,
331 DOM_VK_F21 = 0xF008,
332 DOM_VK_F22 = 0xF009,
333 DOM_VK_F23 = 0xF00A,
334 DOM_VK_F24 = 0xF00B
337 void initKeyBaseEvent(const DOMString &typeArg,
338 bool canBubbleArg,
339 bool cancelableArg,
340 AbstractViewImpl* viewArg,
341 unsigned long keyVal,
342 unsigned long virtKeyVal,
343 unsigned long modifiers);
345 bool ctrlKey() const { return m_modifier & Qt::ControlModifier; }
346 bool shiftKey() const { return m_modifier & Qt::ShiftModifier; }
347 bool altKey() const { return m_modifier & Qt::AltModifier; }
348 bool metaKey() const { return m_modifier & Qt::MetaModifier; }
350 bool inputGenerated() const { return m_virtKeyVal == 0; }
351 unsigned long keyVal() const { return m_keyVal; }
352 unsigned long virtKeyVal() const { return m_virtKeyVal; }
354 QKeyEvent *qKeyEvent() const { if (!m_keyEvent) buildQKeyEvent(); return m_keyEvent; }
356 ~KeyEventBaseImpl();
358 bool checkModifier(unsigned long modifierArg);
360 virtual int which() const { return keyCode(); } // non-DOM extension
362 //Returns true if the event was synthesized by client use of DOM
363 bool isSynthetic() const { return m_synthetic; }
364 protected:
365 KeyEventBaseImpl(): m_keyEvent(0), m_keyVal(0), m_virtKeyVal(0), m_modifier(0), m_synthetic(false)
366 { m_detail = 0; }
368 KeyEventBaseImpl(EventId id,
369 bool canBubbleArg,
370 bool cancelableArg,
371 AbstractViewImpl *viewArg,
372 QKeyEvent *key);
375 mutable QKeyEvent *m_keyEvent;
376 unsigned long m_keyVal; //Unicode key value
377 unsigned long m_virtKeyVal; //Virtual key value for keys like arrows, Fn, etc.
379 // bitfield containing state of modifiers. not part of the dom.
380 unsigned long m_modifier;
382 bool m_synthetic;
384 void buildQKeyEvent() const; //Construct a Qt key event from m_keyVal/m_virtKeyVal
387 class TextEventImpl : public KeyEventBaseImpl {
388 public:
389 TextEventImpl();
391 TextEventImpl(QKeyEvent* key, DOM::AbstractViewImpl* view);
393 void initTextEvent(const DOMString &typeArg,
394 bool canBubbleArg,
395 bool cancelableArg,
396 AbstractViewImpl* viewArg,
397 const DOMString& text);
399 virtual bool isTextInputEvent() const;
401 //Legacy key stuff...
402 virtual int keyCode() const;
403 virtual int charCode() const;
405 DOMString data() const { return m_outputString; }
406 private:
407 DOMString m_outputString;
410 class KeyboardEventImpl : public KeyEventBaseImpl {
411 public:
412 KeyboardEventImpl();
413 KeyboardEventImpl(QKeyEvent* key, DOM::AbstractViewImpl* view);
415 virtual bool isKeyboardEvent() const;
417 //Legacy key stuff...
418 int keyCode() const;
419 int charCode() const;
421 DOMString keyIdentifier() const;
422 unsigned long keyLocation() const { return m_keyLocation; }
424 bool getModifierState(const DOMString& keyIdentifierArg) const;
426 void initKeyboardEvent(const DOMString &typeArg,
427 bool canBubbleArg,
428 bool cancelableArg,
429 AbstractViewImpl* viewArg,
430 const DOMString &keyIdentifierArg,
431 unsigned long keyLocationArg,
432 const DOMString& modifiersList);
434 private:
435 unsigned long m_keyLocation;
439 class MutationEventImpl : public EventImpl {
440 // ### fire these during parsing (if necessary)
441 public:
442 MutationEventImpl();
443 MutationEventImpl(EventId _id, /* for convenience */
444 bool canBubbleArg,
445 bool cancelableArg,
446 const Node &relatedNodeArg,
447 const DOMString &prevValueArg,
448 const DOMString &newValueArg,
449 const DOMString &attrNameArg,
450 unsigned short attrChangeArg);
451 ~MutationEventImpl();
453 Node relatedNode() const { return m_relatedNode; }
454 DOMString prevValue() const { return m_prevValue; }
455 DOMString newValue() const { return m_newValue; }
456 DOMString attrName() const { return m_attrName; }
457 unsigned short attrChange() const { return m_attrChange; }
458 void initMutationEvent(const DOMString &typeArg,
459 bool canBubbleArg,
460 bool cancelableArg,
461 const Node &relatedNodeArg,
462 const DOMString &prevValueArg,
463 const DOMString &newValueArg,
464 const DOMString &attrNameArg,
465 unsigned short attrChangeArg);
466 virtual bool isMutationEvent() const;
467 protected:
468 NodeImpl *m_relatedNode;
469 DOMStringImpl *m_prevValue;
470 DOMStringImpl *m_newValue;
471 DOMStringImpl *m_attrName;
472 unsigned short m_attrChange;
476 class RegisteredEventListener {
477 public:
478 RegisteredEventListener() : useCapture(false), listener(0) {}
480 RegisteredEventListener(EventName _id, EventListener *_listener, bool _useCapture)
481 : eventName(_id), useCapture(_useCapture), listener(_listener) { if (listener) listener->ref(); }
483 ~RegisteredEventListener() { if (listener) listener->deref(); listener = 0; }
485 bool operator==(const RegisteredEventListener &other) const
486 { return eventName == other.eventName && listener == other.listener
487 && useCapture == other.useCapture; }
490 EventName eventName;
491 bool useCapture;
492 EventListener *listener;
494 RegisteredEventListener( const RegisteredEventListener &other ) :
495 eventName(other.eventName), useCapture(other.useCapture), listener(other.listener)
496 { if (listener) listener->ref(); }
498 RegisteredEventListener & operator=( const RegisteredEventListener &other ) {
499 eventName = other.eventName;
500 useCapture = other.useCapture;
501 if (other.listener)
502 other.listener->ref();
503 if (listener)
504 listener->deref();
505 listener = other.listener;
506 return *this;
512 } //namespace
513 #endif