khtml Library API Documentation

html_objectimpl.cpp

00001 
00023 #include "html/html_objectimpl.h"
00024 
00025 #include "khtml_part.h"
00026 #include "dom/dom_string.h"
00027 #include "misc/htmlhashes.h"
00028 #include "khtmlview.h"
00029 #include <qstring.h>
00030 #include <qvariant.h>
00031 #include <qmap.h>
00032 #include <qtimer.h>
00033 #include <kdebug.h>
00034 
00035 #include "xml/dom_docimpl.h"
00036 #include "css/cssstyleselector.h"
00037 #include "css/csshelper.h"
00038 #include "css/cssproperties.h"
00039 #include "css/cssvalues.h"
00040 #include "rendering/render_applet.h"
00041 #include "rendering/render_frames.h"
00042 #include "rendering/render_image.h"
00043 #include "xml/dom2_eventsimpl.h"
00044 
00045 #ifndef Q_WS_QWS // We don't have Java in Qt Embedded
00046 #include "java/kjavaappletwidget.h"
00047 #include "java/kjavaappletcontext.h"
00048 #endif
00049 
00050 using namespace DOM;
00051 using namespace khtml;
00052 
00053 // -------------------------------------------------------------------------
00054 LiveConnectElementImpl::LiveConnectElementImpl(DocumentPtr *doc)
00055   : HTMLElementImpl(doc), liveconnect(0L), timer (new QTimer(this)) {
00056     connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
00057 }
00058 
00059 bool LiveConnectElementImpl::get(const unsigned long objid, const QString & field, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value) {
00060     if (!liveconnect)
00061         return false;
00062     return liveconnect->get(objid, field, type, retobjid, value);
00063 }
00064 
00065 bool LiveConnectElementImpl::put(const unsigned long objid, const QString & field, const QString & value) {
00066     if (!liveconnect)
00067         return false;
00068     return liveconnect->put(objid, field, value);
00069 }
00070 
00071 bool LiveConnectElementImpl::call(const unsigned long objid, const QString & func, const QStringList & args, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value) {
00072     if (!liveconnect)
00073         return false;
00074     return liveconnect->call(objid, func, args, type, retobjid, value);
00075 }
00076 
00077 void LiveConnectElementImpl::unregister(const unsigned long objid) {
00078     if (!liveconnect)
00079         return;
00080     liveconnect->unregister(objid);
00081 }
00082 
00083 void LiveConnectElementImpl::setLiveConnect(KParts::LiveConnectExtension * lc) {
00084     liveconnect = lc;
00085     if (lc)
00086         connect(lc, SIGNAL(partEvent(const unsigned long, const QString &, const KParts::LiveConnectExtension::ArgList &)), static_cast<LiveConnectElementImpl*>(this), SLOT(liveConnectEvent( const unsigned long, const QString&, const KParts::LiveConnectExtension::ArgList &)));
00087 }
00088 
00089 void LiveConnectElementImpl::liveConnectEvent(const unsigned long, const QString & event, const KParts::LiveConnectExtension::ArgList & args)
00090 {
00091     if (!liveconnect)
00092         // not attached
00093         return;
00094 
00095     if (timer->isActive()) {
00096         timerDone();
00097         timer->stop();
00098     }
00099     script.sprintf("%s(", event.latin1());
00100     KParts::LiveConnectExtension::ArgList::const_iterator i = args.begin();
00101     for ( ; i != args.end(); i++) {
00102         if (i != args.begin())
00103             script += ",";
00104         if ((*i).first == KParts::LiveConnectExtension::TypeString) {
00105             script += "\"";
00106             script += (*i).second;
00107             script += "\"";
00108         } else
00109             script += (*i).second;
00110     }
00111     script += ")";
00112 
00113     kdDebug(6036) << "LiveConnectElementImpl::liveConnectEvent " << script << endl;
00114     KHTMLView* w = getDocument()->view();
00115     w->part()->executeScript(this, script);
00116     //timer->start(0, true);
00117 }
00118 
00119 void LiveConnectElementImpl::timerDone() {
00120     KHTMLView* w = getDocument()->view();
00121     w->part()->executeScript(this, script);
00122 }
00123 
00124 void LiveConnectElementImpl::detach() {
00125     if (timer->isActive())
00126         timer->stop();
00127     setLiveConnect(0L);
00128 
00129     HTMLElementImpl::detach();
00130 }
00131 
00132 // -------------------------------------------------------------------------
00133 
00134 HTMLAppletElementImpl::HTMLAppletElementImpl(DocumentPtr *doc)
00135   : LiveConnectElementImpl(doc)
00136 {
00137 }
00138 
00139 HTMLAppletElementImpl::~HTMLAppletElementImpl()
00140 {
00141 }
00142 
00143 NodeImpl::Id HTMLAppletElementImpl::id() const
00144 {
00145     return ID_APPLET;
00146 }
00147 
00148 KJavaApplet* HTMLAppletElementImpl::applet() const
00149 {
00150     if (!m_render || !m_render->isApplet())
00151         return 0L;
00152 
00153     return static_cast<KJavaAppletWidget*>(static_cast<RenderApplet*>(m_render)->widget())->applet();
00154 }
00155 
00156 void HTMLAppletElementImpl::parseAttribute(AttributeImpl *attr)
00157 {
00158     switch( attr->id() )
00159     {
00160     case ATTR_CODEBASE:
00161     case ATTR_ARCHIVE:
00162     case ATTR_CODE:
00163     case ATTR_OBJECT:
00164     case ATTR_ALT:
00165     case ATTR_ID:
00166     case ATTR_NAME:
00167         break;
00168     case ATTR_WIDTH:
00169         addCSSLength(CSS_PROP_WIDTH, attr->value());
00170         break;
00171     case ATTR_HEIGHT:
00172         addCSSLength(CSS_PROP_HEIGHT, attr->value());
00173         break;
00174     case ATTR_ALIGN:
00175     addHTMLAlignment( attr->value() );
00176     break;
00177     default:
00178         HTMLElementImpl::parseAttribute(attr);
00179     }
00180 }
00181 
00182 void HTMLAppletElementImpl::attach()
00183 {
00184     if (!parentNode()->renderer() || getAttribute(ATTR_CODE).isNull()) {
00185         NodeBaseImpl::attach();
00186         return;
00187     }
00188 
00189     KHTMLView *view = getDocument()->view();
00190 
00191 #ifndef Q_WS_QWS // FIXME?
00192     KURL url = getDocument()->baseURL();
00193     DOMString codeBase = getAttribute( ATTR_CODEBASE );
00194     DOMString code = getAttribute( ATTR_CODE );
00195     if ( !codeBase.isEmpty() )
00196         url = KURL( url, codeBase.string() );
00197     if ( !code.isEmpty() )
00198         url = KURL( url, code.string() );
00199 
00200     if( view->part()->javaEnabled() && isURLAllowed( url.url() ) )
00201     {
00202     QMap<QString, QString> args;
00203 
00204     args.insert( "code", code.string());
00205     if(!codeBase.isNull())
00206         args.insert( "codeBase", codeBase.string() );
00207     DOMString name = getDocument()->htmlMode() != DocumentImpl::XHtml ?
00208              getAttribute(ATTR_NAME) : getAttribute(ATTR_ID);
00209         if (name.isNull())
00210             setAttribute(ATTR_ID, code.string());
00211         else
00212         args.insert( "name", name.string() );
00213     DOMString archive = getAttribute(ATTR_ARCHIVE);
00214     if(!archive.isNull())
00215         args.insert( "archive", archive.string() );
00216 
00217     args.insert( "baseURL", getDocument()->baseURL() );
00218         m_render = new RenderApplet(this, args);
00219         setLiveConnect(applet()->getLiveConnectExtension());
00220 
00221         m_render->setStyle(getDocument()->styleSelector()->styleForElement(this));
00222         parentNode()->renderer()->addChild(m_render, nextRenderer());
00223         NodeBaseImpl::attach();
00224     }
00225     else
00226 #endif
00227         ElementImpl::attach();
00228 }
00229 
00230 // -------------------------------------------------------------------------
00231 
00232 HTMLEmbedElementImpl::HTMLEmbedElementImpl(DocumentPtr *doc)
00233     : LiveConnectElementImpl(doc)
00234 {
00235 }
00236 
00237 HTMLEmbedElementImpl::~HTMLEmbedElementImpl()
00238 {
00239 }
00240 
00241 NodeImpl::Id HTMLEmbedElementImpl::id() const
00242 {
00243     return ID_EMBED;
00244 }
00245 
00246 void HTMLEmbedElementImpl::parseAttribute(AttributeImpl *attr)
00247 {
00248   DOM::DOMStringImpl *stringImpl = attr->val();
00249   QConstString cv( stringImpl->s, stringImpl->l );
00250   QString val = cv.string();
00251 
00252   int pos;
00253   switch ( attr->id() )
00254   {
00255      case ATTR_TYPE:
00256         serviceType = val.lower();
00257         pos = serviceType.find( ";" );
00258         if ( pos!=-1 )
00259             serviceType = serviceType.left( pos );
00260         serviceType.truncate( serviceType.find( "-plugin" ) );
00261         break;
00262      case ATTR_CODE:
00263      case ATTR_SRC:
00264          url = khtml::parseURL(attr->val()).string();
00265          break;
00266      case ATTR_WIDTH:
00267         addCSSLength( CSS_PROP_WIDTH, attr->value() );
00268         break;
00269      case ATTR_HEIGHT:
00270         addCSSLength( CSS_PROP_HEIGHT, attr->value());
00271         break;
00272      case ATTR_BORDER:
00273         addCSSLength(CSS_PROP_BORDER_WIDTH, attr->value());
00274         addCSSProperty( CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_SOLID );
00275         addCSSProperty( CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_SOLID );
00276         addCSSProperty( CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_SOLID );
00277         addCSSProperty( CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_SOLID );
00278         break;
00279      case ATTR_VSPACE:
00280         addCSSLength(CSS_PROP_MARGIN_TOP, attr->value());
00281         addCSSLength(CSS_PROP_MARGIN_BOTTOM, attr->value());
00282         break;
00283      case ATTR_HSPACE:
00284         addCSSLength(CSS_PROP_MARGIN_LEFT, attr->value());
00285         addCSSLength(CSS_PROP_MARGIN_RIGHT, attr->value());
00286         break;
00287      case ATTR_ALIGN:
00288     addHTMLAlignment( attr->value() );
00289     break;
00290      case ATTR_VALIGN:
00291         addCSSProperty(CSS_PROP_VERTICAL_ALIGN, attr->value());
00292         break;
00293      case ATTR_PLUGINPAGE:
00294      case ATTR_PLUGINSPAGE:
00295         pluginPage = val;
00296         break;
00297      case ATTR_HIDDEN:
00298         if (val.lower()=="yes" || val.lower()=="true")
00299            hidden = true;
00300         else
00301            hidden = false;
00302         break;
00303      default:
00304         HTMLElementImpl::parseAttribute( attr );
00305   }
00306 }
00307 
00308 void HTMLEmbedElementImpl::attach()
00309 {
00310     assert(!attached());
00311     assert(!m_render);
00312 
00313     if (parentNode()->renderer()) {
00314         KHTMLView* w = getDocument()->view();
00315         RenderStyle* _style = getDocument()->styleSelector()->styleForElement( this );
00316         _style->ref();
00317 
00318         if (w->part()->pluginsEnabled() && isURLAllowed( url ) &&
00319             parentNode()->id() != ID_OBJECT && _style->display() != NONE ) {
00320             m_render = new RenderPartObject(this);
00321             m_render->setStyle(_style );
00322             parentNode()->renderer()->addChild(m_render, nextRenderer());
00323             static_cast<RenderPartObject*>(m_render)->updateWidget();
00324             setLiveConnect(w->part()->liveConnectExtension(static_cast<RenderPartObject*>(m_render)));
00325         }
00326         _style->deref();
00327     }
00328 
00329     NodeBaseImpl::attach();
00330 }
00331 
00332 // -------------------------------------------------------------------------
00333 
00334 HTMLObjectElementImpl::HTMLObjectElementImpl(DocumentPtr *doc)
00335     : LiveConnectElementImpl(doc)
00336 {
00337     needWidgetUpdate = false;
00338     m_renderAlternative = false;
00339 }
00340 
00341 HTMLObjectElementImpl::~HTMLObjectElementImpl()
00342 {
00343 }
00344 
00345 NodeImpl::Id HTMLObjectElementImpl::id() const
00346 {
00347     return ID_OBJECT;
00348 }
00349 
00350 HTMLFormElementImpl *HTMLObjectElementImpl::form() const
00351 {
00352   return 0;
00353 }
00354 
00355 void HTMLObjectElementImpl::parseAttribute(AttributeImpl *attr)
00356 {
00357   DOM::DOMStringImpl *stringImpl = attr->val();
00358   QString val = QConstString( stringImpl->s, stringImpl->l ).string();
00359   int pos;
00360   switch ( attr->id() )
00361   {
00362     case ATTR_TYPE:
00363     case ATTR_CODETYPE:
00364       serviceType = val.lower();
00365       pos = serviceType.find( ";" );
00366       if ( pos!=-1 )
00367           serviceType = serviceType.left( pos );
00368       serviceType.truncate( serviceType.find( "-plugin" ) );
00369       needWidgetUpdate = true;
00370       break;
00371     case ATTR_DATA:
00372       url = khtml::parseURL( val ).string();
00373       needWidgetUpdate = true;
00374       break;
00375     case ATTR_WIDTH:
00376       addCSSLength( CSS_PROP_WIDTH, attr->value());
00377       break;
00378     case ATTR_HEIGHT:
00379       addCSSLength( CSS_PROP_HEIGHT, attr->value());
00380       break;
00381     case ATTR_CLASSID:
00382       classId = val;
00383       needWidgetUpdate = true;
00384       break;
00385     case ATTR_ONLOAD: // ### support load/unload on object elements
00386         setHTMLEventListener(EventImpl::LOAD_EVENT,
00387         getDocument()->createHTMLEventListener(attr->value().string()));
00388         break;
00389     case ATTR_ONUNLOAD:
00390         setHTMLEventListener(EventImpl::UNLOAD_EVENT,
00391         getDocument()->createHTMLEventListener(attr->value().string()));
00392         break;
00393     default:
00394       HTMLElementImpl::parseAttribute( attr );
00395   }
00396 }
00397 
00398 DocumentImpl* HTMLObjectElementImpl::contentDocument() const
00399 {
00400     if ( !m_render ) return 0;
00401     if ( !m_render->isWidget() ) return 0;
00402     QWidget* widget = static_cast<RenderWidget*>( m_render )->widget();
00403     if( widget && widget->inherits("KHTMLView") )
00404         return static_cast<KHTMLView*>( widget )->part()->xmlDocImpl();
00405     return 0;
00406 }
00407 
00408 void HTMLObjectElementImpl::attach()
00409 {
00410     assert(!attached());
00411     assert(!m_render);
00412 
00413     KHTMLView* w = getDocument()->view();
00414     if ( !w->part()->pluginsEnabled() ||
00415          ( url.isEmpty() && classId.isEmpty() ) ||
00416          m_renderAlternative || !isURLAllowed( url ) ) {
00417         // render alternative content
00418         ElementImpl::attach();
00419         return;
00420     }
00421 
00422     RenderStyle* _style = getDocument()->styleSelector()->styleForElement(this);
00423     _style->ref();
00424 
00425     if (parentNode()->renderer() && _style->display() != NONE) {
00426         needWidgetUpdate=false;
00427         bool imagelike = serviceType.startsWith("image/");
00428         if (imagelike)
00429             m_render = new RenderImage(this);
00430         else
00431             m_render = new RenderPartObject(this);
00432 
00433         m_render->setStyle(_style);
00434         parentNode()->renderer()->addChild(m_render, nextRenderer());
00435         if (imagelike)
00436             m_render->updateFromElement();
00437     }
00438 
00439     _style->deref();
00440 
00441     NodeBaseImpl::attach();
00442 
00443     // ### do this when we are actually finished loading instead
00444     if (m_render)  dispatchHTMLEvent(EventImpl::LOAD_EVENT,false,false);
00445 }
00446 
00447 void HTMLObjectElementImpl::detach()
00448 {
00449     if (attached())
00450         // ### do this when we are actualy removed from document instead
00451         dispatchHTMLEvent(EventImpl::UNLOAD_EVENT,false,false);
00452 
00453   HTMLElementImpl::detach();
00454 }
00455 
00456 void HTMLObjectElementImpl::renderAlternative()
00457 {
00458     // an unbelievable hack. FIXME!!
00459 
00460     if ( m_renderAlternative ) return;
00461 
00462     if ( attached() )
00463         detach();
00464 
00465     m_renderAlternative = true;
00466 
00467     attach();
00468 }
00469 
00470 void HTMLObjectElementImpl::recalcStyle( StyleChange ch )
00471 {
00472     if (needWidgetUpdate) {
00473         if(m_render && strcmp( m_render->renderName(),  "RenderPartObject" ) == 0 )
00474             static_cast<RenderPartObject*>(m_render)->updateWidget();
00475         needWidgetUpdate = false;
00476     }
00477     HTMLElementImpl::recalcStyle( ch );
00478 }
00479 
00480 // -------------------------------------------------------------------------
00481 
00482 NodeImpl::Id HTMLParamElementImpl::id() const
00483 {
00484     return ID_PARAM;
00485 }
00486 
00487 void HTMLParamElementImpl::parseAttribute(AttributeImpl *attr)
00488 {
00489     switch( attr->id() )
00490     {
00491     case ATTR_ID:
00492         if (getDocument()->htmlMode() != DocumentImpl::XHtml) break;
00493         // fall through
00494     case ATTR_NAME:
00495         m_name = attr->value().string();
00496         break;
00497     case ATTR_VALUE:
00498         m_value = attr->value().string();
00499         break;
00500     }
00501 }
00502 
00503 #include "html_objectimpl.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:16:35 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001