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
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
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 break;
00261 case ATTR_CODE:
00262 case ATTR_SRC:
00263 url = khtml::parseURL(attr->val()).string();
00264 break;
00265 case ATTR_WIDTH:
00266 addCSSLength( CSS_PROP_WIDTH, attr->value() );
00267 break;
00268 case ATTR_HEIGHT:
00269 addCSSLength( CSS_PROP_HEIGHT, attr->value());
00270 break;
00271 case ATTR_BORDER:
00272 addCSSLength(CSS_PROP_BORDER_WIDTH, attr->value());
00273 addCSSProperty( CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_SOLID );
00274 addCSSProperty( CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_SOLID );
00275 addCSSProperty( CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_SOLID );
00276 addCSSProperty( CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_SOLID );
00277 break;
00278 case ATTR_VSPACE:
00279 addCSSLength(CSS_PROP_MARGIN_TOP, attr->value());
00280 addCSSLength(CSS_PROP_MARGIN_BOTTOM, attr->value());
00281 break;
00282 case ATTR_HSPACE:
00283 addCSSLength(CSS_PROP_MARGIN_LEFT, attr->value());
00284 addCSSLength(CSS_PROP_MARGIN_RIGHT, attr->value());
00285 break;
00286 case ATTR_ALIGN:
00287 addHTMLAlignment( attr->value() );
00288 break;
00289 case ATTR_VALIGN:
00290 addCSSProperty(CSS_PROP_VERTICAL_ALIGN, attr->value());
00291 break;
00292 case ATTR_PLUGINPAGE:
00293 case ATTR_PLUGINSPAGE:
00294 pluginPage = val;
00295 break;
00296 case ATTR_HIDDEN:
00297 if (val.lower()=="yes" || val.lower()=="true")
00298 hidden = true;
00299 else
00300 hidden = false;
00301 break;
00302 default:
00303 HTMLElementImpl::parseAttribute( attr );
00304 }
00305 }
00306
00307 void HTMLEmbedElementImpl::attach()
00308 {
00309 assert(!attached());
00310 assert(!m_render);
00311
00312 if (parentNode()->renderer()) {
00313 KHTMLView* w = getDocument()->view();
00314 RenderStyle* _style = getDocument()->styleSelector()->styleForElement( this );
00315 _style->ref();
00316
00317 if (w->part()->pluginsEnabled() && isURLAllowed( url ) &&
00318 parentNode()->id() != ID_OBJECT && _style->display() != NONE ) {
00319 m_render = new RenderPartObject(this);
00320 m_render->setStyle(_style );
00321 parentNode()->renderer()->addChild(m_render, nextRenderer());
00322 static_cast<RenderPartObject*>(m_render)->updateWidget();
00323 setLiveConnect(w->part()->liveConnectExtension(static_cast<RenderPartObject*>(m_render)));
00324 }
00325 _style->deref();
00326 }
00327
00328 NodeBaseImpl::attach();
00329 }
00330
00331
00332
00333 HTMLObjectElementImpl::HTMLObjectElementImpl(DocumentPtr *doc) : HTMLElementImpl(doc)
00334 {
00335 needWidgetUpdate = false;
00336 m_renderAlternative = false;
00337 }
00338
00339 HTMLObjectElementImpl::~HTMLObjectElementImpl()
00340 {
00341 }
00342
00343 NodeImpl::Id HTMLObjectElementImpl::id() const
00344 {
00345 return ID_OBJECT;
00346 }
00347
00348 HTMLFormElementImpl *HTMLObjectElementImpl::form() const
00349 {
00350 return 0;
00351 }
00352
00353 void HTMLObjectElementImpl::parseAttribute(AttributeImpl *attr)
00354 {
00355 DOM::DOMStringImpl *stringImpl = attr->val();
00356 QString val = QConstString( stringImpl->s, stringImpl->l ).string();
00357 int pos;
00358 switch ( attr->id() )
00359 {
00360 case ATTR_TYPE:
00361 case ATTR_CODETYPE:
00362 serviceType = val.lower();
00363 pos = serviceType.find( ";" );
00364 if ( pos!=-1 )
00365 serviceType = serviceType.left( pos );
00366 needWidgetUpdate = true;
00367 break;
00368 case ATTR_DATA:
00369 url = khtml::parseURL( val ).string();
00370 needWidgetUpdate = true;
00371 break;
00372 case ATTR_WIDTH:
00373 addCSSLength( CSS_PROP_WIDTH, attr->value());
00374 break;
00375 case ATTR_HEIGHT:
00376 addCSSLength( CSS_PROP_HEIGHT, attr->value());
00377 break;
00378 case ATTR_CLASSID:
00379 classId = val;
00380 needWidgetUpdate = true;
00381 break;
00382 case ATTR_ONLOAD:
00383 setHTMLEventListener(EventImpl::LOAD_EVENT,
00384 getDocument()->createHTMLEventListener(attr->value().string()));
00385 break;
00386 case ATTR_ONUNLOAD:
00387 setHTMLEventListener(EventImpl::UNLOAD_EVENT,
00388 getDocument()->createHTMLEventListener(attr->value().string()));
00389 break;
00390 default:
00391 HTMLElementImpl::parseAttribute( attr );
00392 }
00393 }
00394
00395 DocumentImpl* HTMLObjectElementImpl::contentDocument() const
00396 {
00397 if ( !m_render ) return 0;
00398 if ( !m_render->isWidget() ) return 0;
00399 QWidget* widget = static_cast<RenderWidget*>( m_render )->widget();
00400 if( widget && widget->inherits("KHTMLView") )
00401 return static_cast<KHTMLView*>( widget )->part()->xmlDocImpl();
00402 return 0;
00403 }
00404
00405 void HTMLObjectElementImpl::attach()
00406 {
00407 assert(!attached());
00408 assert(!m_render);
00409
00410 KHTMLView* w = getDocument()->view();
00411 if ( !w->part()->pluginsEnabled() ||
00412 ( url.isEmpty() && classId.isEmpty() ) ||
00413 m_renderAlternative || !isURLAllowed( url ) ) {
00414
00415 ElementImpl::attach();
00416 return;
00417 }
00418
00419 RenderStyle* _style = getDocument()->styleSelector()->styleForElement(this);
00420 _style->ref();
00421
00422 if (parentNode()->renderer() && _style->display() != NONE) {
00423 needWidgetUpdate=false;
00424 bool imagelike = serviceType.startsWith("image/");
00425 if (imagelike)
00426 m_render = new RenderImage(this);
00427 else
00428 m_render = new RenderPartObject(this);
00429
00430 m_render->setStyle(_style);
00431 parentNode()->renderer()->addChild(m_render, nextRenderer());
00432 if (imagelike)
00433 m_render->updateFromElement();
00434 }
00435
00436 _style->deref();
00437
00438 NodeBaseImpl::attach();
00439
00440
00441 if (m_render) dispatchHTMLEvent(EventImpl::LOAD_EVENT,false,false);
00442 }
00443
00444 void HTMLObjectElementImpl::detach()
00445 {
00446 if (attached())
00447
00448 dispatchHTMLEvent(EventImpl::UNLOAD_EVENT,false,false);
00449
00450 HTMLElementImpl::detach();
00451 }
00452
00453 void HTMLObjectElementImpl::renderAlternative()
00454 {
00455
00456
00457 if ( m_renderAlternative ) return;
00458
00459 if ( attached() )
00460 detach();
00461
00462 m_renderAlternative = true;
00463
00464 attach();
00465 }
00466
00467 void HTMLObjectElementImpl::recalcStyle( StyleChange ch )
00468 {
00469 if (needWidgetUpdate) {
00470 if(m_render && strcmp( m_render->renderName(), "RenderPartObject" ) == 0 )
00471 static_cast<RenderPartObject*>(m_render)->updateWidget();
00472 needWidgetUpdate = false;
00473 }
00474 HTMLElementImpl::recalcStyle( ch );
00475 }
00476
00477
00478
00479 NodeImpl::Id HTMLParamElementImpl::id() const
00480 {
00481 return ID_PARAM;
00482 }
00483
00484 void HTMLParamElementImpl::parseAttribute(AttributeImpl *attr)
00485 {
00486 switch( attr->id() )
00487 {
00488 case ATTR_ID:
00489 if (getDocument()->htmlMode() != DocumentImpl::XHtml) break;
00490
00491 case ATTR_NAME:
00492 m_name = attr->value().string();
00493 break;
00494 case ATTR_VALUE:
00495 m_value = attr->value().string();
00496 break;
00497 }
00498 }
00499
00500 #include "html_objectimpl.moc"