kjs_events.cpp

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 #include "kjs_window.h"
00023 #include "kjs_events.h"
00024 #include "kjs_events.lut.h"
00025 #include "kjs_views.h"
00026 #include "kjs_proxy.h"
00027 #include "xml/dom_nodeimpl.h"
00028 #include "xml/dom_docimpl.h"
00029 #include "xml/dom2_eventsimpl.h"
00030 #include "rendering/render_object.h"
00031 #include "rendering/render_canvas.h"
00032 #include "xml/dom2_eventsimpl.h"
00033 #include "khtml_part.h"
00034 
00035 #include <kdebug.h>
00036 
00037 using namespace KJS;
00038 using namespace DOM;
00039 
00040 // -------------------------------------------------------------------------
00041 
00042 JSEventListener::JSEventListener(Object _listener, ObjectImp *_compareListenerImp, const Object &_win, bool _html)
00043   : listener( _listener ), compareListenerImp( _compareListenerImp ), html( _html ), win( _win )
00044 {
00045     //fprintf(stderr,"JSEventListener::JSEventListener this=%p listener=%p\n",this,listener.imp());
00046   if (compareListenerImp) {
00047     static_cast<Window*>(win.imp())->jsEventListeners.insert(compareListenerImp, this);
00048   }
00049 }
00050 
00051 JSEventListener::~JSEventListener()
00052 {
00053   if (compareListenerImp) {
00054     static_cast<Window*>(win.imp())->jsEventListeners.remove(compareListenerImp);
00055   }
00056   //fprintf(stderr,"JSEventListener::~JSEventListener this=%p listener=%p\n",this,listener.imp());
00057 }
00058 
00059 void JSEventListener::handleEvent(DOM::Event &evt)
00060 {
00061 #ifdef KJS_DEBUGGER
00062   if (KJSDebugWin::debugWindow() && KJSDebugWin::debugWindow()->inSession())
00063     return;
00064 #endif
00065   KHTMLPart *part = ::qt_cast<KHTMLPart *>(static_cast<Window*>(win.imp())->part());
00066   KJSProxy *proxy = 0L;
00067   if (part)
00068     proxy = part->jScript();
00069 
00070   if (proxy && listener.isValid() && listener.implementsCall()) {
00071     ref();
00072 
00073     KJS::ScriptInterpreter *interpreter = static_cast<KJS::ScriptInterpreter *>(proxy->interpreter());
00074     ExecState *exec = interpreter->globalExec();
00075 
00076     List args;
00077     args.append(getDOMEvent(exec,evt));
00078 
00079     // Set "this" to the event's current target
00080     Object thisObj = Object::dynamicCast(getDOMNode(exec,evt.currentTarget()));
00081     if ( !thisObj.isValid() ) {
00082       // Window events (window.onload/window.onresize etc.) must have 'this' set to the window.
00083       // DocumentImpl::defaultEventHandler sets currentTarget to 0 to mean 'window'.
00084       thisObj = win;
00085     }
00086 
00087     Window *window = static_cast<Window*>(win.imp());
00088     // Set the event we're handling in the Window object
00089     window->setCurrentEvent( &evt );
00090     // ... and in the interpreter
00091     interpreter->setCurrentEvent( &evt );
00092 
00093     KJSCPUGuard guard;
00094     guard.start();
00095     Value retval = listener.call(exec, thisObj, args);
00096     guard.stop();
00097 
00098     window->setCurrentEvent( 0 );
00099     interpreter->setCurrentEvent( 0 );
00100     if ( exec->hadException() )
00101       exec->clearException();
00102     else if (html)
00103     {
00104       QVariant ret = ValueToVariant(exec, retval);
00105       if (ret.type() == QVariant::Bool && ret.toBool() == false)
00106         evt.preventDefault();
00107     }
00108     window->afterScriptExecution();
00109     deref();
00110   }
00111 }
00112 
00113 DOM::DOMString JSEventListener::eventListenerType()
00114 {
00115     if (html)
00116     return "_khtml_HTMLEventListener";
00117     else
00118     return "_khtml_JSEventListener";
00119 }
00120 
00121 Object JSEventListener::listenerObj() const
00122 {
00123   return listener;
00124 }
00125 
00126 JSLazyEventListener::JSLazyEventListener(const QString &_code, const QString &_name, const Object &_win, DOM::NodeImpl* _originalNode)
00127   : JSEventListener(Object(), 0, _win, true),
00128     code(_code), name(_name),
00129     parsed(false)
00130 {
00131   // We don't retain the original node, because we assume it
00132   // will stay alive as long as this handler object is around
00133   // and we need to avoid a reference cycle. If JS transfers
00134   // this handler to another node, parseCode will be called and
00135   // then originalNode is no longer needed.
00136 
00137   originalNode = _originalNode;
00138 }
00139 
00140 JSLazyEventListener::~JSLazyEventListener()
00141 {
00142   if (listener.isValid()) {
00143     static_cast<Window*>(win.imp())->jsEventListeners.remove(listener.imp());
00144   }
00145 }
00146 
00147 void JSLazyEventListener::handleEvent(DOM::Event &evt)
00148 {
00149   parseCode();
00150   if (listener.isValid()) {
00151     JSEventListener::handleEvent(evt);
00152   }
00153 }
00154 
00155 
00156 Object JSLazyEventListener::listenerObj() const
00157 {
00158   parseCode();
00159   return listener;
00160 }
00161 
00162 void JSLazyEventListener::parseCode() const
00163 {
00164   if (!parsed) {
00165     KHTMLPart *part = ::qt_cast<KHTMLPart *>(static_cast<Window*>(win.imp())->part());
00166     KJSProxy *proxy = 0L;
00167     if (part)
00168       proxy = part->jScript();
00169 
00170     if (proxy) {
00171       KJS::ScriptInterpreter *interpreter = static_cast<KJS::ScriptInterpreter *>(proxy->interpreter());
00172       ExecState *exec = interpreter->globalExec();
00173 
00174       //KJS::Constructor constr(KJS::Global::current().get("Function").imp());
00175       KJS::Object constr = interpreter->builtinFunction();
00176       KJS::List args;
00177 
00178       static KJS::String eventString("event");
00179 
00180       args.append(eventString);
00181       args.append(KJS::String(code));
00182       listener = constr.construct(exec, args); // ### is globalExec ok ?
00183 
00184       if (exec->hadException()) {
00185         exec->clearException();
00186 
00187         // failed to parse, so let's just make this listener a no-op
00188         listener = Object();
00189       } else if (!listener.inherits(&DeclaredFunctionImp::info)) {
00190         listener = Object();// Error creating function
00191       } else {
00192         DeclaredFunctionImp *declFunc = static_cast<DeclaredFunctionImp*>(listener.imp());
00193         declFunc->setName(Identifier(name));
00194 
00195         if (originalNode)
00196         {
00197           // Add the event's home element to the scope
00198           // (and the document, and the form - see KJS::HTMLElement::eventHandlerScope)
00199           ScopeChain scope = listener.scope();
00200 
00201           Object thisObj = Object::dynamicCast(getDOMNode(exec, originalNode));
00202 
00203           if (thisObj.isValid()) {
00204             static_cast<DOMNode*>(thisObj.imp())->pushEventHandlerScope(exec, scope);
00205 
00206             listener.setScope(scope);
00207           }
00208         }
00209       }
00210     }
00211 
00212     // no more need to keep the unparsed code around
00213     code = QString();
00214 
00215     if (listener.isValid()) {
00216       static_cast<Window*>(win.imp())->jsEventListeners.insert(listener.imp(),
00217                                                                (KJS::JSEventListener *)(this));
00218     }
00219 
00220     parsed = true;
00221   }
00222 }
00223 
00224 // -------------------------------------------------------------------------
00225 
00226 const ClassInfo EventConstructor::info = { "EventConstructor", 0, &EventConstructorTable, 0 };
00227 /*
00228 @begin EventConstructorTable 3
00229   CAPTURING_PHASE   DOM::Event::CAPTURING_PHASE DontDelete|ReadOnly
00230   AT_TARGET     DOM::Event::AT_TARGET       DontDelete|ReadOnly
00231   BUBBLING_PHASE    DOM::Event::BUBBLING_PHASE  DontDelete|ReadOnly
00232 # Reverse-engineered from Netscape
00233   MOUSEDOWN     1               DontDelete|ReadOnly
00234   MOUSEUP       2               DontDelete|ReadOnly
00235   MOUSEOVER     4               DontDelete|ReadOnly
00236   MOUSEOUT      8               DontDelete|ReadOnly
00237   MOUSEMOVE     16              DontDelete|ReadOnly
00238   MOUSEDRAG     32              DontDelete|ReadOnly
00239   CLICK         64              DontDelete|ReadOnly
00240   DBLCLICK      128             DontDelete|ReadOnly
00241   KEYDOWN       256             DontDelete|ReadOnly
00242   KEYUP         512             DontDelete|ReadOnly
00243   KEYPRESS      1024                DontDelete|ReadOnly
00244   DRAGDROP      2048                DontDelete|ReadOnly
00245   FOCUS         4096                DontDelete|ReadOnly
00246   BLUR          8192                DontDelete|ReadOnly
00247   SELECT        16384               DontDelete|ReadOnly
00248   CHANGE        32768               DontDelete|ReadOnly
00249 @end
00250 */
00251 
00252 EventConstructor::EventConstructor(ExecState *exec)
00253   : DOMObject(exec->interpreter()->builtinObjectPrototype())
00254 {
00255 }
00256 
00257 Value EventConstructor::tryGet(ExecState *exec, const Identifier &p) const
00258 {
00259   return DOMObjectLookupGetValue<EventConstructor, DOMObject>(exec,p,&EventConstructorTable,this);
00260 }
00261 
00262 Value EventConstructor::getValueProperty(ExecState *, int token) const
00263 {
00264   // We use the token as the value to return directly
00265   return Number(token);
00266 }
00267 
00268 Value KJS::getEventConstructor(ExecState *exec)
00269 {
00270   return cacheGlobalObject<EventConstructor>(exec, "[[event.constructor]]");
00271 }
00272 
00273 // -------------------------------------------------------------------------
00274 
00275 const ClassInfo DOMEvent::info = { "Event", 0, &DOMEventTable, 0 };
00276 /*
00277 @begin DOMEventTable 7
00278   type      DOMEvent::Type      DontDelete|ReadOnly
00279   target    DOMEvent::Target    DontDelete|ReadOnly
00280   currentTarget DOMEvent::CurrentTarget DontDelete|ReadOnly
00281   srcElement    DOMEvent::SrcElement    DontDelete|ReadOnly
00282   eventPhase    DOMEvent::EventPhase    DontDelete|ReadOnly
00283   bubbles   DOMEvent::Bubbles   DontDelete|ReadOnly
00284   cancelable    DOMEvent::Cancelable    DontDelete|ReadOnly
00285   timeStamp DOMEvent::TimeStamp DontDelete|ReadOnly
00286   returnValue   DOMEvent::ReturnValue   DontDelete
00287   cancelBubble  DOMEvent::CancelBubble  DontDelete
00288 @end
00289 @begin DOMEventProtoTable 3
00290   stopPropagation   DOMEvent::StopPropagation   DontDelete|Function 0
00291   preventDefault    DOMEvent::PreventDefault    DontDelete|Function 0
00292   initEvent     DOMEvent::InitEvent     DontDelete|Function 3
00293 @end
00294 */
00295 DEFINE_PROTOTYPE("DOMEvent", DOMEventProto)
00296 IMPLEMENT_PROTOFUNC_DOM(DOMEventProtoFunc)
00297 IMPLEMENT_PROTOTYPE(DOMEventProto, DOMEventProtoFunc)
00298 
00299 DOMEvent::DOMEvent(ExecState *exec, DOM::Event e)
00300   : DOMObject(DOMEventProto::self(exec)), event(e) { }
00301 
00302 DOMEvent::DOMEvent(const Object &proto, DOM::Event e)
00303   : DOMObject(proto), event(e) { }
00304 
00305 DOMEvent::~DOMEvent()
00306 {
00307   ScriptInterpreter::forgetDOMObject(event.handle());
00308 }
00309 
00310 Value DOMEvent::tryGet(ExecState *exec, const Identifier &p) const
00311 {
00312 #ifdef KJS_VERBOSE
00313   kdDebug() << "KJS::DOMEvent::tryGet " << p.qstring() << endl;
00314 #endif
00315   return DOMObjectLookupGetValue<DOMEvent,DOMObject>(exec, p, &DOMEventTable, this );
00316 }
00317 
00318 Value DOMEvent::getValueProperty(ExecState *exec, int token) const
00319 {
00320   switch (token) {
00321   case Type:
00322     return String(event.type());
00323   case Target:
00324   case SrcElement: /*MSIE extension - "the object that fired the event"*/
00325     return getDOMNode(exec,event.target());
00326   case CurrentTarget:
00327     return getDOMNode(exec,event.currentTarget());
00328   case EventPhase:
00329     return Number((unsigned int)event.eventPhase());
00330   case Bubbles:
00331     return Boolean(event.bubbles());
00332   case Cancelable:
00333     return Boolean(event.cancelable());
00334   case TimeStamp:
00335     return Number((long unsigned int)event.timeStamp()); // ### long long ?
00336   case ReturnValue: // MSIE extension
00337     return Boolean(event.handle()->defaultPrevented());
00338   case CancelBubble: // MSIE extension
00339     return Boolean(event.handle()->propagationStopped());
00340   default:
00341     kdDebug(6070) << "WARNING: Unhandled token in DOMEvent::getValueProperty : " << token << endl;
00342     return Value();
00343   }
00344 }
00345 
00346 Value DOMEvent::defaultValue(ExecState *exec, KJS::Type hint) const
00347 {
00348   if (event.handle()->id() == EventImpl::ERROR_EVENT && !event.handle()->message().isNull()) {
00349     return String(event.handle()->message());
00350   }
00351   else
00352     return DOMObject::defaultValue(exec,hint);
00353 }
00354 
00355 void DOMEvent::tryPut(ExecState *exec, const Identifier &propertyName,
00356                       const Value& value, int attr)
00357 {
00358   DOMObjectLookupPut<DOMEvent, DOMObject>(exec, propertyName, value, attr,
00359                                           &DOMEventTable, this);
00360 }
00361 
00362 void DOMEvent::putValueProperty(ExecState *exec, int token, const Value& value, int)
00363 {
00364   switch (token) {
00365   case ReturnValue: // MSIE equivalent for "preventDefault" (but with a way to reset it)
00366     // returnValue=false means "default action of the event on the source object is canceled",
00367     // which means preventDefault(true). Hence the '!'.
00368     event.handle()->preventDefault(!value.toBoolean(exec));
00369     break;
00370   case CancelBubble: // MSIE equivalent for "stopPropagation" (but with a way to reset it)
00371     event.handle()->stopPropagation(value.toBoolean(exec));
00372     break;
00373   default:
00374     break;
00375   }
00376 }
00377 
00378 Value DOMEventProtoFunc::tryCall(ExecState *exec, Object & thisObj, const List &args)
00379 {
00380   KJS_CHECK_THIS( KJS::DOMEvent, thisObj );
00381   DOM::Event event = static_cast<DOMEvent *>( thisObj.imp() )->toEvent();
00382   switch (id) {
00383     case DOMEvent::StopPropagation:
00384       event.stopPropagation();
00385       return Undefined();
00386     case DOMEvent::PreventDefault:
00387       event.preventDefault();
00388       return Undefined();
00389     case DOMEvent::InitEvent:
00390       event.initEvent(args[0].toString(exec).string(),args[1].toBoolean(exec),args[2].toBoolean(exec));
00391       return Undefined();
00392   };
00393   return Undefined();
00394 }
00395 
00396 Value KJS::getDOMEvent(ExecState *exec, DOM::Event e)
00397 {
00398   DOM::EventImpl *ei = e.handle();
00399   if (!ei)
00400     return Null();
00401   ScriptInterpreter* interp = static_cast<ScriptInterpreter *>(exec->interpreter());
00402   DOMObject *ret = interp->getDOMObject(ei);
00403   if (!ret) {
00404     if (ei->isTextEvent())
00405       ret = new DOMTextEvent(exec, e);
00406     else if (ei->isMouseEvent())
00407       ret = new DOMMouseEvent(exec, e);
00408     else if (ei->isUIEvent())
00409       ret = new DOMUIEvent(exec, e);
00410     else if (ei->isMutationEvent())
00411       ret = new DOMMutationEvent(exec, e);
00412     else
00413       ret = new DOMEvent(exec, e);
00414 
00415     interp->putDOMObject(ei, ret);
00416   }
00417 
00418   return Value(ret);
00419 }
00420 
00421 DOM::Event KJS::toEvent(const Value& val)
00422 {
00423   Object obj = Object::dynamicCast(val);
00424   if (!obj.isValid() || !obj.inherits(&DOMEvent::info))
00425     return DOM::Event();
00426 
00427   const DOMEvent *dobj = static_cast<const DOMEvent*>(obj.imp());
00428   return dobj->toEvent();
00429 }
00430 
00431 // -------------------------------------------------------------------------
00432 
00433 
00434 const ClassInfo EventExceptionConstructor::info = { "EventExceptionConstructor", 0, &EventExceptionConstructorTable, 0 };
00435 /*
00436 @begin EventExceptionConstructorTable 1
00437   UNSPECIFIED_EVENT_TYPE_ERR    DOM::EventException::UNSPECIFIED_EVENT_TYPE_ERR DontDelete|ReadOnly
00438 @end
00439 */
00440 EventExceptionConstructor::EventExceptionConstructor(ExecState *exec)
00441   : DOMObject(exec->interpreter()->builtinObjectPrototype())
00442 {
00443 }
00444 
00445 Value EventExceptionConstructor::tryGet(ExecState *exec, const Identifier &p) const
00446 {
00447   return DOMObjectLookupGetValue<EventExceptionConstructor, DOMObject>(exec,p,&EventExceptionConstructorTable,this);
00448 }
00449 
00450 Value EventExceptionConstructor::getValueProperty(ExecState *, int token) const
00451 {
00452   // We use the token as the value to return directly
00453   return Number(token);
00454 }
00455 
00456 Value KJS::getEventExceptionConstructor(ExecState *exec)
00457 {
00458   return cacheGlobalObject<EventExceptionConstructor>(exec, "[[eventException.constructor]]");
00459 }
00460 
00461 // -------------------------------------------------------------------------
00462 
00463 const ClassInfo DOMUIEvent::info = { "UIEvent", &DOMEvent::info, &DOMUIEventTable, 0 };
00464 /*
00465 @begin DOMUIEventTable 7
00466   view      DOMUIEvent::View    DontDelete|ReadOnly
00467   detail    DOMUIEvent::Detail  DontDelete|ReadOnly
00468   keyCode   DOMUIEvent::KeyCode DontDelete|ReadOnly
00469   charCode  DOMUIEvent::CharCode    DontDelete|ReadOnly
00470   layerX    DOMUIEvent::LayerX  DontDelete|ReadOnly
00471   layerY    DOMUIEvent::LayerY  DontDelete|ReadOnly
00472   pageX     DOMUIEvent::PageX   DontDelete|ReadOnly
00473   pageY     DOMUIEvent::PageY   DontDelete|ReadOnly
00474   which     DOMUIEvent::Which   DontDelete|ReadOnly
00475 @end
00476 @begin DOMUIEventProtoTable 1
00477   initUIEvent   DOMUIEvent::InitUIEvent DontDelete|Function 5
00478 @end
00479 */
00480 DEFINE_PROTOTYPE("DOMUIEvent",DOMUIEventProto)
00481 IMPLEMENT_PROTOFUNC_DOM(DOMUIEventProtoFunc)
00482 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMUIEventProto,DOMUIEventProtoFunc,DOMEventProto)
00483 
00484 DOMUIEvent::DOMUIEvent(ExecState *exec, DOM::UIEvent ue) :
00485   DOMEvent(DOMUIEventProto::self(exec), ue) {}
00486 
00487 DOMUIEvent::DOMUIEvent(const Object &proto, DOM::UIEvent ue) :
00488   DOMEvent(proto, ue) {}
00489 
00490 DOMUIEvent::~DOMUIEvent()
00491 {
00492 }
00493 
00494 Value DOMUIEvent::tryGet(ExecState *exec, const Identifier &p) const
00495 {
00496   return DOMObjectLookupGetValue<DOMUIEvent,DOMEvent>(exec,p,&DOMUIEventTable,this);
00497 }
00498 
00499 Value DOMUIEvent::getValueProperty(ExecState *exec, int token) const
00500 {
00501   switch (token) {
00502   case View:
00503     return getDOMAbstractView(exec,static_cast<DOM::UIEvent>(event).view());
00504   case Detail:
00505     return Number(static_cast<DOM::UIEvent>(event).detail());
00506   case KeyCode:
00507     // IE-compatibility
00508     return Number(static_cast<DOM::UIEvent>(event).keyCode());
00509   case CharCode:
00510     // IE-compatibility
00511     return Number(static_cast<DOM::UIEvent>(event).charCode());
00512   case LayerX:
00513     // NS-compatibility
00514     return Number(static_cast<DOM::UIEvent>(event).layerX());
00515   case LayerY:
00516     // NS-compatibility
00517     return Number(static_cast<DOM::UIEvent>(event).layerY());
00518   case PageX:
00519     // NS-compatibility
00520     return Number(static_cast<DOM::UIEvent>(event).pageX());
00521   case PageY:
00522     // NS-compatibility
00523     return Number(static_cast<DOM::UIEvent>(event).pageY());
00524   case Which:
00525     // NS-compatibility
00526     return Number(static_cast<DOM::UIEvent>(event).which());
00527   default:
00528     kdDebug(6070) << "WARNING: Unhandled token in DOMUIEvent::getValueProperty : " << token << endl;
00529     return Undefined();
00530   }
00531 }
00532 
00533 Value DOMUIEventProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00534 {
00535   KJS_CHECK_THIS( KJS::DOMUIEvent, thisObj );
00536   DOM::UIEvent uiEvent = static_cast<DOMUIEvent *>(thisObj.imp())->toUIEvent();
00537   switch (id) {
00538     case DOMUIEvent::InitUIEvent: {
00539       DOM::AbstractView v = toAbstractView(args[3]);
00540       static_cast<DOM::UIEvent>(uiEvent).initUIEvent(args[0].toString(exec).string(),
00541                                                      args[1].toBoolean(exec),
00542                                                      args[2].toBoolean(exec),
00543                                                      v,
00544                                                      args[4].toInteger(exec));
00545       }
00546       return Undefined();
00547   }
00548   return Undefined();
00549 }
00550 
00551 // -------------------------------------------------------------------------
00552 
00553 const ClassInfo DOMMouseEvent::info = { "MouseEvent", &DOMUIEvent::info, &DOMMouseEventTable, 0 };
00554 
00555 /*
00556 @begin DOMMouseEventTable 2
00557   screenX   DOMMouseEvent::ScreenX  DontDelete|ReadOnly
00558   screenY   DOMMouseEvent::ScreenY  DontDelete|ReadOnly
00559   clientX   DOMMouseEvent::ClientX  DontDelete|ReadOnly
00560   x     DOMMouseEvent::X    DontDelete|ReadOnly
00561   clientY   DOMMouseEvent::ClientY  DontDelete|ReadOnly
00562   y     DOMMouseEvent::Y    DontDelete|ReadOnly
00563   offsetX   DOMMouseEvent::OffsetX  DontDelete|ReadOnly
00564   offsetY   DOMMouseEvent::OffsetY  DontDelete|ReadOnly
00565   ctrlKey   DOMMouseEvent::CtrlKey  DontDelete|ReadOnly
00566   shiftKey  DOMMouseEvent::ShiftKey DontDelete|ReadOnly
00567   altKey    DOMMouseEvent::AltKey   DontDelete|ReadOnly
00568   metaKey   DOMMouseEvent::MetaKey  DontDelete|ReadOnly
00569   button    DOMMouseEvent::Button   DontDelete|ReadOnly
00570   relatedTarget DOMMouseEvent::RelatedTarget DontDelete|ReadOnly
00571   fromElement   DOMMouseEvent::FromElement DontDelete|ReadOnly
00572   toElement DOMMouseEvent::ToElement    DontDelete|ReadOnly
00573 @end
00574 @begin DOMMouseEventProtoTable 1
00575   initMouseEvent    DOMMouseEvent::InitMouseEvent   DontDelete|Function 15
00576 @end
00577 */
00578 DEFINE_PROTOTYPE("DOMMouseEvent",DOMMouseEventProto)
00579 IMPLEMENT_PROTOFUNC_DOM(DOMMouseEventProtoFunc)
00580 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMMouseEventProto,DOMMouseEventProtoFunc,DOMUIEventProto)
00581 
00582 DOMMouseEvent::DOMMouseEvent(ExecState *exec, DOM::MouseEvent me) :
00583   DOMUIEvent(DOMMouseEventProto::self(exec), me) {}
00584 
00585 DOMMouseEvent::~DOMMouseEvent()
00586 {
00587 }
00588 
00589 Value DOMMouseEvent::tryGet(ExecState *exec, const Identifier &p) const
00590 {
00591 #ifdef KJS_VERBOSE
00592   kdDebug(6070) << "DOMMouseEvent::tryGet " << p.qstring() << endl;
00593 #endif
00594   return DOMObjectLookupGetValue<DOMMouseEvent,DOMUIEvent>(exec,p,&DOMMouseEventTable,this);
00595 }
00596 
00597 Value DOMMouseEvent::getValueProperty(ExecState *exec, int token) const
00598 {
00599   switch (token) {
00600   case ScreenX:
00601     return Number(static_cast<DOM::MouseEvent>(event).screenX());
00602   case ScreenY:
00603     return Number(static_cast<DOM::MouseEvent>(event).screenY());
00604   case ClientX:
00605   case X:
00606     return Number(static_cast<DOM::MouseEvent>(event).clientX());
00607   case ClientY:
00608   case Y:
00609     return Number(static_cast<DOM::MouseEvent>(event).clientY());
00610   case OffsetX:
00611   case OffsetY: // MSIE extension
00612   {
00613     DOM::Node node = event.target();
00614     node.handle()->getDocument()->updateRendering();
00615     khtml::RenderObject *rend = node.handle() ? node.handle()->renderer() : 0L;
00616     int x = static_cast<DOM::MouseEvent>(event).clientX();
00617     int y = static_cast<DOM::MouseEvent>(event).clientY();
00618     if ( rend ) {
00619       int xPos, yPos;
00620       if ( rend->absolutePosition( xPos, yPos ) ) {
00621         kdDebug() << "DOMMouseEvent::getValueProperty rend=" << rend << "  xPos=" << xPos << "  yPos=" << yPos << endl;
00622         x -= xPos;
00623         y -= yPos;
00624       }
00625       if ( rend->canvas() ) {
00626         int cYPos, cXPos;
00627         rend->canvas()->absolutePosition( cXPos,  cYPos,  true );
00628         x += cXPos;
00629         y += cYPos;
00630       }
00631     }
00632     return Number( token == OffsetX ? x : y );
00633   }
00634   case CtrlKey:
00635     return Boolean(static_cast<DOM::MouseEvent>(event).ctrlKey());
00636   case ShiftKey:
00637     return Boolean(static_cast<DOM::MouseEvent>(event).shiftKey());
00638   case AltKey:
00639     return Boolean(static_cast<DOM::MouseEvent>(event).altKey());
00640   case MetaKey:
00641     return Boolean(static_cast<DOM::MouseEvent>(event).metaKey());
00642   case Button:
00643   {
00644     if ( exec->interpreter()->compatMode() == Interpreter::NetscapeCompat ) {
00645         return Number(static_cast<DOM::MouseEvent>(event).button());
00646     }
00647     // Tricky. The DOM (and khtml) use 0 for LMB, 1 for MMB and 2 for RMB
00648     // but MSIE uses 1=LMB, 2=RMB, 4=MMB, as a bitfield
00649     int domButton = static_cast<DOM::MouseEvent>(event).button();
00650     int button = domButton==0 ? 1 : domButton==1 ? 4 : domButton==2 ? 2 : 0;
00651     return Number( (unsigned int)button );
00652   }
00653   case ToElement:
00654     // MSIE extension - "the object toward which the user is moving the mouse pointer"
00655     if (event.handle()->id() == DOM::EventImpl::MOUSEOUT_EVENT)
00656       return getDOMNode(exec,static_cast<DOM::MouseEvent>(event).relatedTarget());
00657     return getDOMNode(exec,static_cast<DOM::MouseEvent>(event).target());
00658   case FromElement:
00659     // MSIE extension - "object from which activation
00660     // or the mouse pointer is exiting during the event" (huh?)
00661     if (event.handle()->id() == DOM::EventImpl::MOUSEOUT_EVENT)
00662       return getDOMNode(exec,static_cast<DOM::MouseEvent>(event).target());
00663     /* fall through */
00664   case RelatedTarget:
00665     return getDOMNode(exec,static_cast<DOM::MouseEvent>(event).relatedTarget());
00666   default:
00667     kdDebug(6070) << "WARNING: Unhandled token in DOMMouseEvent::getValueProperty : " << token << endl;
00668     return Value();
00669   }
00670 }
00671 
00672 Value DOMMouseEventProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00673 {
00674   KJS_CHECK_THIS( KJS::DOMMouseEvent, thisObj );
00675   DOM::MouseEvent mouseEvent = static_cast<DOMMouseEvent *>(thisObj.imp())->toMouseEvent();
00676   switch (id) {
00677     case DOMMouseEvent::InitMouseEvent:
00678       mouseEvent.initMouseEvent(args[0].toString(exec).string(), // typeArg
00679                                 args[1].toBoolean(exec), // canBubbleArg
00680                                 args[2].toBoolean(exec), // cancelableArg
00681                                 toAbstractView(args[3]), // viewArg
00682                                 args[4].toInteger(exec), // detailArg
00683                                 args[5].toInteger(exec), // screenXArg
00684                                 args[6].toInteger(exec), // screenYArg
00685                                 args[7].toInteger(exec), // clientXArg
00686                                 args[8].toInteger(exec), // clientYArg
00687                                 args[9].toBoolean(exec), // ctrlKeyArg
00688                                 args[10].toBoolean(exec), // altKeyArg
00689                                 args[11].toBoolean(exec), // shiftKeyArg
00690                                 args[12].toBoolean(exec), // metaKeyArg
00691                                 args[13].toInteger(exec), // buttonArg
00692                                 toNode(args[14])); // relatedTargetArg
00693       return Undefined();
00694   }
00695   return Undefined();
00696 }
00697 
00698 // -------------------------------------------------------------------------
00699 
00700 const ClassInfo DOMTextEvent::info = { "TextEvent", &DOMUIEvent::info, &DOMTextEventTable, 0 };
00701 
00702 /*
00703 @begin DOMTextEventTable 5
00704   keyVal     DOMTextEvent::Key       DontDelete|ReadOnly
00705   virtKeyVal     DOMTextEvent::VirtKey        DontDelete|ReadOnly
00706   outputString   DOMTextEvent::OutputString   DontDelete|ReadOnly
00707   inputGenerated DOMTextEvent::InputGenerated DontDelete|ReadOnly
00708   numPad         DOMTextEvent::NumPad         DontDelete|ReadOnly
00709   # actually belonging to KeyboardEvent
00710   ctrlKey        DOMTextEvent::CtrlKey     DontDelete|ReadOnly
00711   altKey         DOMTextEvent::AltKey      DontDelete|ReadOnly
00712   shiftKey       DOMTextEvent::ShiftKey    DontDelete|ReadOnly
00713   altKey         DOMTextEvent::AltKey      DontDelete|ReadOnly
00714 @end
00715 @begin DOMTextEventProtoTable 1
00716   initTextEvent DOMTextEvent::InitTextEvent DontDelete|Function 10
00717   # Missing: initTextEventNS, initModifier
00718 @end
00719 */
00720 DEFINE_PROTOTYPE("DOMTextEvent",DOMTextEventProto)
00721 IMPLEMENT_PROTOFUNC_DOM(DOMTextEventProtoFunc)
00722 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMTextEventProto,DOMTextEventProtoFunc,DOMUIEventProto)
00723 
00724 DOMTextEvent::DOMTextEvent(ExecState *exec, DOM::TextEvent ke) :
00725   DOMUIEvent(DOMTextEventProto::self(exec), ke) {}
00726 
00727 DOMTextEvent::~DOMTextEvent()
00728 {
00729 }
00730 
00731 Value DOMTextEvent::tryGet(ExecState *exec, const Identifier &p) const
00732 {
00733 #ifdef KJS_VERBOSE
00734   kdDebug(6070) << "DOMTextEvent::tryGet " << p.qstring() << endl;
00735 #endif
00736   return DOMObjectLookupGetValue<DOMTextEvent,DOMUIEvent>(exec,p,&DOMTextEventTable,this);
00737 }
00738 
00739 Value DOMTextEvent::getValueProperty(ExecState *, int token) const
00740 {
00741   // ### KDE 4: use const reference
00742   DOM::TextEvent tevent = static_cast<DOM::TextEvent>(event);
00743   switch (token) {
00744   case Key:
00745     return Number(tevent.keyVal());
00746   case VirtKey:
00747     return Number(tevent.virtKeyVal());
00748   case OutputString:
00749     return String(tevent.outputString());
00750   case InputGenerated:
00751     return Boolean(tevent.inputGenerated());
00752   case NumPad:
00753     return Boolean(tevent.numPad());
00754   // these modifier attributes actually belong into a KeyboardEvent interface
00755   case CtrlKey:
00756     return Boolean(tevent.checkModifier(Qt::ControlButton));
00757   case ShiftKey:
00758     return Boolean(tevent.checkModifier(Qt::ShiftButton));
00759   case AltKey:
00760     return Boolean(tevent.checkModifier(Qt::AltButton));
00761   case MetaKey:
00762     return Boolean(tevent.checkModifier(Qt::MetaButton));
00763   default:
00764     kdDebug(6070) << "WARNING: Unhandled token in DOMTextEvent::getValueProperty : " << token << endl;
00765     return KJS::Undefined();
00766   }
00767 }
00768 
00769 Value DOMTextEventProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00770 {
00771   KJS_CHECK_THIS( KJS::DOMTextEvent, thisObj );
00772   DOM::TextEvent keyEvent = static_cast<DOMTextEvent *>(thisObj.imp())->toTextEvent();
00773   switch (id) {
00774     case DOMTextEvent::InitTextEvent:
00775       keyEvent.initTextEvent(args[0].toString(exec).string(), // typeArg
00776                             args[1].toBoolean(exec), // canBubbleArg
00777                             args[2].toBoolean(exec), // cancelableArg
00778                             toAbstractView(args[3]), // viewArg
00779                             args[4].toInteger(exec), // detailArg
00780                             args[5].toString(exec).string(),  // outputStringArg
00781                             args[6].toInteger(exec), // keyValArg
00782                             args[7].toInteger(exec), // virtKeyValArg
00783                             args[8].toBoolean(exec), // inputGeneratedArg
00784                             args[9].toBoolean(exec));// numPadArg
00785 
00786       return Undefined();
00787   }
00788   return Undefined();
00789 }
00790 
00791 // -------------------------------------------------------------------------
00792 
00793 const ClassInfo MutationEventConstructor::info = { "MutationEventConstructor", 0, &MutationEventConstructorTable, 0 };
00794 /*
00795 @begin MutationEventConstructorTable 3
00796   MODIFICATION  DOM::MutationEvent::MODIFICATION    DontDelete|ReadOnly
00797   ADDITION  DOM::MutationEvent::ADDITION        DontDelete|ReadOnly
00798   REMOVAL   DOM::MutationEvent::REMOVAL     DontDelete|ReadOnly
00799 @end
00800 */
00801 MutationEventConstructor::MutationEventConstructor(ExecState* exec)
00802   : DOMObject(exec->interpreter()->builtinObjectPrototype())
00803 {
00804 }
00805 
00806 Value MutationEventConstructor::tryGet(ExecState *exec, const Identifier &p) const
00807 {
00808   return DOMObjectLookupGetValue<MutationEventConstructor,DOMObject>(exec,p,&MutationEventConstructorTable,this);
00809 }
00810 
00811 Value MutationEventConstructor::getValueProperty(ExecState *, int token) const
00812 {
00813   // We use the token as the value to return directly
00814   return Number(token);
00815 }
00816 
00817 Value KJS::getMutationEventConstructor(ExecState *exec)
00818 {
00819   return cacheGlobalObject<MutationEventConstructor>(exec, "[[mutationEvent.constructor]]");
00820 }
00821 
00822 // -------------------------------------------------------------------------
00823 
00824 const ClassInfo DOMMutationEvent::info = { "MutationEvent", &DOMEvent::info, &DOMMutationEventTable, 0 };
00825 /*
00826 @begin DOMMutationEventTable 5
00827   relatedNode   DOMMutationEvent::RelatedNode   DontDelete|ReadOnly
00828   prevValue DOMMutationEvent::PrevValue DontDelete|ReadOnly
00829   newValue  DOMMutationEvent::NewValue  DontDelete|ReadOnly
00830   attrName  DOMMutationEvent::AttrName  DontDelete|ReadOnly
00831   attrChange    DOMMutationEvent::AttrChange    DontDelete|ReadOnly
00832 @end
00833 @begin DOMMutationEventProtoTable 1
00834   initMutationEvent DOMMutationEvent::InitMutationEvent DontDelete|Function 8
00835 @end
00836 */
00837 DEFINE_PROTOTYPE("DOMMutationEvent",DOMMutationEventProto)
00838 IMPLEMENT_PROTOFUNC_DOM(DOMMutationEventProtoFunc)
00839 IMPLEMENT_PROTOTYPE_WITH_PARENT(DOMMutationEventProto,DOMMutationEventProtoFunc,DOMEventProto)
00840 
00841 DOMMutationEvent::DOMMutationEvent(ExecState *exec, DOM::MutationEvent me) :
00842   DOMEvent(DOMMutationEventProto::self(exec), me) {}
00843 
00844 DOMMutationEvent::~DOMMutationEvent()
00845 {
00846 }
00847 
00848 Value DOMMutationEvent::tryGet(ExecState *exec, const Identifier &p) const
00849 {
00850   return DOMObjectLookupGetValue<DOMMutationEvent,DOMEvent>(exec,p,&DOMMutationEventTable,this);
00851 }
00852 
00853 Value DOMMutationEvent::getValueProperty(ExecState *exec, int token) const
00854 {
00855   switch (token) {
00856   case RelatedNode:
00857     return getDOMNode(exec,static_cast<DOM::MutationEvent>(event).relatedNode());
00858   case PrevValue:
00859     return String(static_cast<DOM::MutationEvent>(event).prevValue());
00860   case NewValue:
00861     return String(static_cast<DOM::MutationEvent>(event).newValue());
00862   case AttrName:
00863     return String(static_cast<DOM::MutationEvent>(event).attrName());
00864   case AttrChange:
00865     return Number((unsigned int)static_cast<DOM::MutationEvent>(event).attrChange());
00866   default:
00867     kdDebug(6070) << "WARNING: Unhandled token in DOMMutationEvent::getValueProperty : " << token << endl;
00868     return Value();
00869   }
00870 }
00871 
00872 Value DOMMutationEventProtoFunc::tryCall(ExecState *exec, Object &thisObj, const List &args)
00873 {
00874   KJS_CHECK_THIS( KJS::DOMMutationEvent, thisObj );
00875   DOM::MutationEvent mutationEvent = static_cast<DOMMutationEvent *>(thisObj.imp())->toMutationEvent();
00876   switch (id) {
00877     case DOMMutationEvent::InitMutationEvent:
00878       mutationEvent.initMutationEvent(args[0].toString(exec).string(), // typeArg,
00879                                       args[1].toBoolean(exec), // canBubbleArg
00880                                       args[2].toBoolean(exec), // cancelableArg
00881                                       toNode(args[3]), // relatedNodeArg
00882                                       args[4].toString(exec).string(), // prevValueArg
00883                                       args[5].toString(exec).string(), // newValueArg
00884                                       args[6].toString(exec).string(), // attrNameArg
00885                                       args[7].toInteger(exec)); // attrChangeArg
00886       return Undefined();
00887   }
00888   return Undefined();
00889 }
KDE Home | KDE Accessibility Home | Description of Access Keys