File indexing completed on 2024-04-28 15:22:55

0001 /**
0002  * This file is part of the DOM implementation for KDE.
0003  *
0004  * Copyright 2001 Peter Kelly (pmk@post.com)
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 
0023 #include "dom/dom2_views.h"
0024 #include "dom/dom_exception.h"
0025 #include "xml/dom2_eventsimpl.h"
0026 #include "xml/dom_nodeimpl.h"
0027 
0028 using namespace DOM;
0029 
0030 EventListener::EventListener()
0031 {
0032 }
0033 
0034 EventListener::~EventListener()
0035 {
0036 }
0037 
0038 void EventListener::handleEvent(Event &/*evt*/)
0039 {
0040 }
0041 
0042 DOMString EventListener::eventListenerType()
0043 {
0044     return "";
0045 }
0046 
0047 // -----------------------------------------------------------------------------
0048 
0049 Event::Event()
0050 {
0051     impl = nullptr;
0052 }
0053 
0054 Event::Event(const Event &other)
0055 {
0056     impl = other.impl;
0057     if (impl) {
0058         impl->ref();
0059     }
0060 }
0061 
0062 Event::Event(EventImpl *i)
0063 {
0064     impl = i;
0065     if (impl) {
0066         impl->ref();
0067     }
0068 }
0069 
0070 Event::~Event()
0071 {
0072     if (impl) {
0073         impl->deref();
0074     }
0075 }
0076 
0077 Event &Event::operator = (const Event &other)
0078 {
0079     if (impl != other.impl) {
0080         if (impl) {
0081             impl->deref();
0082         }
0083         impl = other.impl;
0084         if (impl) {
0085             impl->ref();
0086         }
0087     }
0088     return *this;
0089 }
0090 
0091 DOMString Event::type() const
0092 {
0093     if (!impl) {
0094         throw DOMException(DOMException::INVALID_STATE_ERR);
0095     }
0096 
0097     return impl->type();
0098 }
0099 
0100 Node Event::target() const
0101 {
0102     if (!impl) {
0103         throw DOMException(DOMException::INVALID_STATE_ERR);
0104     }
0105 
0106     if (impl->target()->eventTargetType() == EventTargetImpl::DOM_NODE) {
0107         return static_cast<DOM::NodeImpl *>(impl->target());
0108     }
0109     return nullptr;
0110 }
0111 
0112 Node Event::currentTarget() const
0113 {
0114     if (!impl) {
0115         throw DOMException(DOMException::INVALID_STATE_ERR);
0116     }
0117 
0118     if (impl->currentTarget()->eventTargetType() == EventTargetImpl::DOM_NODE) {
0119         return static_cast<DOM::NodeImpl *>(impl->currentTarget());
0120     }
0121     return nullptr;
0122 }
0123 
0124 unsigned short Event::eventPhase() const
0125 {
0126     if (!impl) {
0127         throw DOMException(DOMException::INVALID_STATE_ERR);
0128     }
0129 
0130     return impl->eventPhase();
0131 }
0132 
0133 bool Event::bubbles() const
0134 {
0135     if (!impl) {
0136         throw DOMException(DOMException::INVALID_STATE_ERR);
0137     }
0138 
0139     return impl->bubbles();
0140 }
0141 
0142 bool Event::cancelable() const
0143 {
0144     if (!impl) {
0145         throw DOMException(DOMException::INVALID_STATE_ERR);
0146     }
0147 
0148     return impl->cancelable();
0149 }
0150 
0151 DOMTimeStamp Event::timeStamp() const
0152 {
0153     if (!impl) {
0154         throw DOMException(DOMException::INVALID_STATE_ERR);
0155     }
0156 
0157     return impl->timeStamp();
0158 }
0159 
0160 void Event::stopPropagation()
0161 {
0162     if (!impl) {
0163         throw DOMException(DOMException::INVALID_STATE_ERR);
0164     }
0165 
0166     impl->stopPropagation(true);
0167 }
0168 
0169 void Event::preventDefault()
0170 {
0171     if (!impl) {
0172         throw DOMException(DOMException::INVALID_STATE_ERR);
0173     }
0174 
0175     impl->preventDefault(true);
0176 }
0177 
0178 void Event::initEvent(const DOMString &eventTypeArg, bool canBubbleArg, bool cancelableArg)
0179 {
0180     if (!impl) {
0181         throw DOMException(DOMException::INVALID_STATE_ERR);
0182     }
0183 
0184     impl->initEvent(eventTypeArg, canBubbleArg, cancelableArg);
0185 }
0186 
0187 EventImpl *Event::handle() const
0188 {
0189     return impl;
0190 }
0191 
0192 bool Event::isNull() const
0193 {
0194     return (impl == nullptr);
0195 }
0196 
0197 // -----------------------------------------------------------------------------
0198 
0199 #ifndef SAVE_SPACE
0200 
0201 EventException::EventException(unsigned short _code)
0202 {
0203     code = _code;
0204 }
0205 
0206 EventException::EventException(const EventException &other)
0207 {
0208     code = other.code;
0209 }
0210 
0211 EventException &EventException::operator = (const EventException &other)
0212 {
0213     code = other.code;
0214     return *this;
0215 }
0216 
0217 #endif
0218 
0219 DOMString EventException::codeAsString() const
0220 {
0221     return codeAsString(code);
0222 }
0223 
0224 bool EventException::isEventExceptionCode(int exceptioncode)
0225 {
0226     return exceptioncode >= _EXCEPTION_OFFSET && exceptioncode < _EXCEPTION_MAX;
0227 }
0228 
0229 DOMString EventException::codeAsString(int code)
0230 {
0231     switch (code) {
0232     case UNSPECIFIED_EVENT_TYPE_ERR:
0233         return DOMString("UNSPECIFIED_EVENT_TYPE_ERR");
0234     default:
0235         return DOMString("(unknown exception code)");
0236     }
0237 }
0238 
0239 // -----------------------------------------------------------------------------
0240 
0241 UIEvent::UIEvent() : Event()
0242 {
0243 }
0244 
0245 UIEvent::UIEvent(const UIEvent &other) : Event(other)
0246 {
0247 }
0248 
0249 UIEvent::UIEvent(const Event &other) : Event()
0250 {
0251     (*this) = other;
0252 }
0253 
0254 UIEvent::UIEvent(UIEventImpl *impl) : Event(impl)
0255 {
0256 }
0257 
0258 UIEvent &UIEvent::operator = (const UIEvent &other)
0259 {
0260     Event::operator = (other);
0261     return *this;
0262 }
0263 
0264 UIEvent &UIEvent::operator = (const Event &other)
0265 {
0266     Event e;
0267     e = other;
0268     if (!e.isNull() && !e.handle()->isUIEvent()) {
0269         if (impl) {
0270             impl->deref();
0271         }
0272         impl = nullptr;
0273     } else {
0274         Event::operator = (other);
0275     }
0276     return *this;
0277 }
0278 
0279 UIEvent::~UIEvent()
0280 {
0281 }
0282 
0283 AbstractView UIEvent::view() const
0284 {
0285     if (!impl) {
0286         throw DOMException(DOMException::INVALID_STATE_ERR);
0287     }
0288 
0289     return static_cast<UIEventImpl *>(impl)->view();
0290 }
0291 
0292 long UIEvent::detail() const
0293 {
0294     if (!impl) {
0295         throw DOMException(DOMException::INVALID_STATE_ERR);
0296     }
0297 
0298     return static_cast<UIEventImpl *>(impl)->detail();
0299 }
0300 
0301 int UIEvent::keyCode() const
0302 {
0303     if (!impl) {
0304         throw DOMException(DOMException::INVALID_STATE_ERR);
0305     }
0306 
0307     return static_cast<UIEventImpl *>(impl)->keyCode();
0308 }
0309 
0310 int UIEvent::charCode() const
0311 {
0312     if (!impl) {
0313         throw DOMException(DOMException::INVALID_STATE_ERR);
0314     }
0315 
0316     return static_cast<UIEventImpl *>(impl)->charCode();
0317 }
0318 
0319 int UIEvent::pageX() const
0320 {
0321     if (!impl) {
0322         throw DOMException(DOMException::INVALID_STATE_ERR);
0323     }
0324 
0325     return static_cast<UIEventImpl *>(impl)->pageX();
0326 }
0327 
0328 int UIEvent::pageY() const
0329 {
0330     if (!impl) {
0331         throw DOMException(DOMException::INVALID_STATE_ERR);
0332     }
0333 
0334     return static_cast<UIEventImpl *>(impl)->pageY();
0335 }
0336 
0337 int UIEvent::layerX() const
0338 {
0339     if (!impl) {
0340         throw DOMException(DOMException::INVALID_STATE_ERR);
0341     }
0342 
0343     return static_cast<UIEventImpl *>(impl)->layerX();
0344 }
0345 
0346 int UIEvent::layerY() const
0347 {
0348     if (!impl) {
0349         throw DOMException(DOMException::INVALID_STATE_ERR);
0350     }
0351 
0352     return static_cast<UIEventImpl *>(impl)->layerY();
0353 }
0354 
0355 int UIEvent::which() const
0356 {
0357     if (!impl) {
0358         throw DOMException(DOMException::INVALID_STATE_ERR);
0359     }
0360     return static_cast<UIEventImpl *>(impl)->which();
0361 }
0362 
0363 void UIEvent::initUIEvent(const DOMString &typeArg,
0364                           bool canBubbleArg,
0365                           bool cancelableArg,
0366                           const AbstractView &viewArg,
0367                           long detailArg)
0368 {
0369     if (!impl) {
0370         throw DOMException(DOMException::INVALID_STATE_ERR);
0371     }
0372 
0373     static_cast<UIEventImpl *>(impl)->initUIEvent(typeArg, canBubbleArg, cancelableArg,
0374             viewArg.handle(), detailArg);
0375 }
0376 
0377 // -----------------------------------------------------------------------------
0378 
0379 MouseEvent::MouseEvent() : UIEvent()
0380 {
0381 }
0382 
0383 MouseEvent::MouseEvent(const MouseEvent &other) : UIEvent(other)
0384 {
0385 }
0386 
0387 MouseEvent::MouseEvent(const Event &other) : UIEvent()
0388 {
0389     (*this) = other;
0390 }
0391 
0392 MouseEvent::MouseEvent(MouseEventImpl *impl) : UIEvent(impl)
0393 {
0394 }
0395 
0396 MouseEvent &MouseEvent::operator = (const MouseEvent &other)
0397 {
0398     UIEvent::operator = (other);
0399     return *this;
0400 }
0401 
0402 MouseEvent &MouseEvent::operator = (const Event &other)
0403 {
0404     Event e;
0405     e = other;
0406     if (!e.isNull() && !e.handle()->isMouseEvent()) {
0407         if (impl) {
0408             impl->deref();
0409         }
0410         impl = nullptr;
0411     } else {
0412         UIEvent::operator = (other);
0413     }
0414     return *this;
0415 }
0416 
0417 MouseEvent::~MouseEvent()
0418 {
0419 }
0420 
0421 long MouseEvent::screenX() const
0422 {
0423     if (!impl) {
0424         throw DOMException(DOMException::INVALID_STATE_ERR);
0425     }
0426 
0427     return static_cast<MouseEventImpl *>(impl)->screenX();
0428 }
0429 
0430 long MouseEvent::screenY() const
0431 {
0432     if (!impl) {
0433         throw DOMException(DOMException::INVALID_STATE_ERR);
0434     }
0435 
0436     return static_cast<MouseEventImpl *>(impl)->screenY();
0437 }
0438 
0439 long MouseEvent::clientX() const
0440 {
0441     if (!impl) {
0442         throw DOMException(DOMException::INVALID_STATE_ERR);
0443     }
0444 
0445     return static_cast<MouseEventImpl *>(impl)->clientX();
0446 }
0447 
0448 long MouseEvent::clientY() const
0449 {
0450     if (!impl) {
0451         throw DOMException(DOMException::INVALID_STATE_ERR);
0452     }
0453 
0454     return static_cast<MouseEventImpl *>(impl)->clientY();
0455 }
0456 
0457 bool MouseEvent::ctrlKey() const
0458 {
0459     if (!impl) {
0460         throw DOMException(DOMException::INVALID_STATE_ERR);
0461     }
0462 
0463     return static_cast<MouseEventImpl *>(impl)->ctrlKey();
0464 }
0465 
0466 bool MouseEvent::shiftKey() const
0467 {
0468     if (!impl) {
0469         throw DOMException(DOMException::INVALID_STATE_ERR);
0470     }
0471 
0472     return static_cast<MouseEventImpl *>(impl)->shiftKey();
0473 }
0474 
0475 bool MouseEvent::altKey() const
0476 {
0477     if (!impl) {
0478         throw DOMException(DOMException::INVALID_STATE_ERR);
0479     }
0480 
0481     return static_cast<MouseEventImpl *>(impl)->altKey();
0482 }
0483 
0484 bool MouseEvent::metaKey() const
0485 {
0486     if (!impl) {
0487         throw DOMException(DOMException::INVALID_STATE_ERR);
0488     }
0489 
0490     return static_cast<MouseEventImpl *>(impl)->metaKey();
0491 }
0492 
0493 unsigned short MouseEvent::button() const
0494 {
0495     if (!impl) {
0496         throw DOMException(DOMException::INVALID_STATE_ERR);
0497     }
0498 
0499     return static_cast<MouseEventImpl *>(impl)->button();
0500 }
0501 
0502 Node MouseEvent::relatedTarget() const
0503 {
0504     if (!impl) {
0505         throw DOMException(DOMException::INVALID_STATE_ERR);
0506     }
0507 
0508     return static_cast<MouseEventImpl *>(impl)->relatedTarget();
0509 }
0510 
0511 void MouseEvent::initMouseEvent(const DOMString &typeArg,
0512                                 bool canBubbleArg,
0513                                 bool cancelableArg,
0514                                 const AbstractView &viewArg,
0515                                 long detailArg,
0516                                 long screenXArg,
0517                                 long screenYArg,
0518                                 long clientXArg,
0519                                 long clientYArg,
0520                                 bool ctrlKeyArg,
0521                                 bool altKeyArg,
0522                                 bool shiftKeyArg,
0523                                 bool metaKeyArg,
0524                                 unsigned short buttonArg,
0525                                 const Node &relatedTargetArg)
0526 {
0527     if (!impl) {
0528         throw DOMException(DOMException::INVALID_STATE_ERR);
0529     }
0530 
0531     static_cast<MouseEventImpl *>(impl)->initMouseEvent(typeArg, canBubbleArg,
0532             cancelableArg, viewArg.handle(), detailArg, screenXArg, screenYArg, clientXArg,
0533             clientYArg, ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg, buttonArg,
0534             relatedTargetArg);
0535 }
0536 
0537 // -----------------------------------------------------------------------------
0538 
0539 TextEvent::TextEvent() : UIEvent()
0540 {
0541 }
0542 
0543 TextEvent::TextEvent(const TextEvent &other) : UIEvent(other)
0544 {
0545 }
0546 
0547 TextEvent::TextEvent(const Event &other) : UIEvent()
0548 {
0549     (*this) = other;
0550 }
0551 
0552 TextEvent &TextEvent::operator = (const TextEvent &other)
0553 {
0554     UIEvent::operator = (other);
0555     return *this;
0556 }
0557 
0558 TextEvent &TextEvent::operator = (const Event &other)
0559 {
0560     Event e;
0561     e = other;
0562     if (!e.isNull() && !e.handle()->isTextInputEvent()) {
0563         if (impl) {
0564             impl->deref();
0565         }
0566         impl = nullptr;
0567     } else {
0568         UIEvent::operator = (other);
0569     }
0570     return *this;
0571 }
0572 
0573 TextEvent::~TextEvent()
0574 {
0575 }
0576 
0577 void TextEvent::initTextEvent(const DOMString &typeArg,
0578                               bool canBubbleArg,
0579                               bool cancelableArg,
0580                               const AbstractView &viewArg,
0581                               const DOMString &dataArg)
0582 {
0583     static_cast<TextEventImpl *>(impl)->initTextEvent(
0584         typeArg, canBubbleArg, cancelableArg, viewArg.handle(), dataArg);
0585 }
0586 // -----------------------------------------------------------------------------
0587 
0588 KeyboardEvent::KeyboardEvent() : UIEvent()
0589 {
0590 }
0591 
0592 KeyboardEvent::KeyboardEvent(const KeyboardEvent &other) : UIEvent(other)
0593 {
0594 }
0595 
0596 KeyboardEvent::KeyboardEvent(const Event &other) : UIEvent()
0597 {
0598     (*this) = other;
0599 }
0600 
0601 KeyboardEvent &KeyboardEvent::operator = (const KeyboardEvent &other)
0602 {
0603     UIEvent::operator = (other);
0604     return *this;
0605 }
0606 
0607 KeyboardEvent &KeyboardEvent::operator = (const Event &other)
0608 {
0609     Event e;
0610     e = other;
0611     if (!e.isNull() && !e.handle()->isKeyboardEvent()) {
0612         if (impl) {
0613             impl->deref();
0614         }
0615         impl = nullptr;
0616     } else {
0617         UIEvent::operator = (other);
0618     }
0619     return *this;
0620 }
0621 
0622 KeyboardEvent::~KeyboardEvent()
0623 {
0624 }
0625 
0626 DOMString KeyboardEvent::keyIdentifier() const
0627 {
0628     return static_cast<const KeyboardEventImpl *>(impl)->keyIdentifier();
0629 }
0630 
0631 unsigned long KeyboardEvent::keyLocation() const
0632 {
0633     return static_cast<const KeyboardEventImpl *>(impl)->keyLocation();
0634 }
0635 
0636 bool KeyboardEvent::ctrlKey() const
0637 {
0638     return static_cast<const KeyboardEventImpl *>(impl)->ctrlKey();
0639 }
0640 
0641 bool KeyboardEvent::shiftKey() const
0642 {
0643     return static_cast<const KeyboardEventImpl *>(impl)->shiftKey();
0644 }
0645 
0646 bool KeyboardEvent::altKey() const
0647 {
0648     return static_cast<const KeyboardEventImpl *>(impl)->altKey();
0649 }
0650 
0651 bool KeyboardEvent::metaKey() const
0652 {
0653     return static_cast<const KeyboardEventImpl *>(impl)->metaKey();
0654 }
0655 
0656 bool KeyboardEvent::getModifierState(DOMString keyIdentifierArg) const
0657 {
0658     return static_cast<const KeyboardEventImpl *>(impl)->getModifierState(keyIdentifierArg);
0659 }
0660 
0661 void KeyboardEvent::initKeyboardEvent(DOMString typeArg,
0662                                       bool canBubbleArg,
0663                                       bool cancelableArg,
0664                                       AbstractView viewArg,
0665                                       DOMString keyIdentifierArg,
0666                                       unsigned long keyLocationArg,
0667                                       DOMString modifiersList)
0668 {
0669     static_cast<KeyboardEventImpl *>(impl)->initKeyboardEvent(typeArg,
0670             canBubbleArg, cancelableArg, viewArg.handle(), keyIdentifierArg, keyLocationArg, modifiersList);
0671 }
0672 
0673 // -----------------------------------------------------------------------------
0674 
0675 MutationEvent::MutationEvent() : Event()
0676 {
0677 }
0678 
0679 MutationEvent::MutationEvent(const MutationEvent &other) : Event(other)
0680 {
0681 }
0682 
0683 MutationEvent::MutationEvent(const Event &other) : Event()
0684 {
0685     (*this) = other;
0686 }
0687 
0688 MutationEvent::MutationEvent(MutationEventImpl *impl) : Event(impl)
0689 {
0690 }
0691 
0692 MutationEvent &MutationEvent::operator = (const MutationEvent &other)
0693 {
0694     Event::operator = (other);
0695     return *this;
0696 }
0697 
0698 MutationEvent &MutationEvent::operator = (const Event &other)
0699 {
0700     Event e;
0701     e = other;
0702     if (!e.isNull() && !e.handle()->isMutationEvent()) {
0703         if (impl) {
0704             impl->deref();
0705         }
0706         impl = nullptr;
0707     } else {
0708         Event::operator = (other);
0709     }
0710     return *this;
0711 }
0712 
0713 MutationEvent::~MutationEvent()
0714 {
0715 }
0716 
0717 Node MutationEvent::relatedNode() const
0718 {
0719     if (!impl) {
0720         throw DOMException(DOMException::INVALID_STATE_ERR);
0721     }
0722 
0723     return static_cast<MutationEventImpl *>(impl)->relatedNode();
0724 }
0725 
0726 DOMString MutationEvent::prevValue() const
0727 {
0728     if (!impl) {
0729         throw DOMException(DOMException::INVALID_STATE_ERR);
0730     }
0731 
0732     return static_cast<MutationEventImpl *>(impl)->prevValue();
0733 }
0734 
0735 DOMString MutationEvent::newValue() const
0736 {
0737     if (!impl) {
0738         throw DOMException(DOMException::INVALID_STATE_ERR);
0739     }
0740 
0741     return static_cast<MutationEventImpl *>(impl)->newValue();
0742 }
0743 
0744 DOMString MutationEvent::attrName() const
0745 {
0746     if (!impl) {
0747         throw DOMException(DOMException::INVALID_STATE_ERR);
0748     }
0749 
0750     return static_cast<MutationEventImpl *>(impl)->attrName();
0751 }
0752 
0753 unsigned short MutationEvent::attrChange() const
0754 {
0755     if (!impl) {
0756         throw DOMException(DOMException::INVALID_STATE_ERR);
0757     }
0758 
0759     return static_cast<MutationEventImpl *>(impl)->attrChange();
0760 }
0761 
0762 void MutationEvent::initMutationEvent(const DOMString &typeArg,
0763                                       bool canBubbleArg,
0764                                       bool cancelableArg,
0765                                       const Node &relatedNodeArg,
0766                                       const DOMString &prevValueArg,
0767                                       const DOMString &newValueArg,
0768                                       const DOMString &attrNameArg,
0769                                       unsigned short attrChangeArg)
0770 {
0771     if (!impl) {
0772         throw DOMException(DOMException::INVALID_STATE_ERR);
0773     }
0774 
0775     static_cast<MutationEventImpl *>(impl)->initMutationEvent(typeArg,
0776             canBubbleArg, cancelableArg, relatedNodeArg, prevValueArg,
0777             newValueArg, attrNameArg, attrChangeArg);
0778 }
0779