2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2000 Stefan Schimanski (1Stein@gmx.de)
7 * Copyright (C) 2004 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
24 #include "html/html_objectimpl.h"
26 #include "khtml_part.h"
27 #include "dom/dom_string.h"
28 #include "misc/htmlhashes.h"
29 #include "khtmlview.h"
34 #include "xml/dom_docimpl.h"
35 #include "css/cssstyleselector.h"
36 #include "css/csshelper.h"
37 #include "css/cssproperties.h"
38 #include "css/cssvalues.h"
39 #include "rendering/render_applet.h"
40 #include "rendering/render_frames.h"
41 #include "rendering/render_image.h"
42 #include "xml/dom2_eventsimpl.h"
44 #ifndef Q_WS_QWS // We don't have Java in Qt Embedded
45 #include "java/kjavaappletwidget.h"
46 #include "java/kjavaappletcontext.h"
50 #include "KWQKHTMLPart.h"
54 using namespace khtml;
56 // -------------------------------------------------------------------------
58 HTMLAppletElementImpl::HTMLAppletElementImpl(DocumentPtr *doc)
59 : HTMLElementImpl(doc)
64 HTMLAppletElementImpl::~HTMLAppletElementImpl()
66 delete appletInstance;
69 NodeImpl::Id HTMLAppletElementImpl::id() const
74 bool HTMLAppletElementImpl::mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const
82 result = eReplaced; // Share with <img> since the alignment behavior is the same.
88 return HTMLElementImpl::mapToEntry(attr, result);
91 void HTMLAppletElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
103 addCSSLength(attr, CSS_PROP_WIDTH, attr->value());
106 addCSSLength(attr, CSS_PROP_HEIGHT, attr->value());
109 addHTMLAlignment(attr);
112 HTMLElementImpl::parseHTMLAttribute(attr);
116 bool HTMLAppletElementImpl::rendererIsNeeded(RenderStyle *style)
118 return !getAttribute(ATTR_CODE).isNull();
121 RenderObject *HTMLAppletElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
123 #ifndef Q_WS_QWS // FIXME(E)? I don't think this is possible with Qt Embedded...
124 KHTMLPart *part = getDocument()->part();
126 if( part && part->javaEnabled() )
128 QMap<QString, QString> args;
130 args.insert( "code", getAttribute(ATTR_CODE).string());
131 DOMString codeBase = getAttribute(ATTR_CODEBASE);
132 if(!codeBase.isNull())
133 args.insert( "codeBase", codeBase.string() );
134 DOMString name = getDocument()->htmlMode() != DocumentImpl::XHtml ?
135 getAttribute(ATTR_NAME) : getAttribute(ATTR_ID);
137 args.insert( "name", name.string() );
138 DOMString archive = getAttribute(ATTR_ARCHIVE);
139 if(!archive.isNull())
140 args.insert( "archive", archive.string() );
142 args.insert( "baseURL", getDocument()->baseURL() );
144 DOMString mayScript = getAttribute(ATTR_MAYSCRIPT);
145 if (!mayScript.isNull())
146 args.insert("mayScript", mayScript.string());
148 // Other arguments (from <PARAM> tags) are added later.
150 return new (getDocument()->renderArena()) RenderApplet(this, args);
153 // ### remove me. we should never show an empty applet, instead
154 // render the alternative content given by the webpage
155 return new (getDocument()->renderArena()) RenderEmptyApplet(this);
161 bool HTMLAppletElementImpl::getMember(const QString & name, JType & type, QString & val) {
165 #ifndef Q_WS_QWS // We don't have Java in Qt Embedded
166 if ( !m_render || !m_render->isApplet() )
168 KJavaAppletWidget *w = static_cast<KJavaAppletWidget*>(static_cast<RenderApplet*>(m_render)->widget());
169 return (w && w->applet() && w->applet()->getMember(name, type, val));
176 bool HTMLAppletElementImpl::callMember(const QString & name, const QStringList & args, JType & type, QString & val) {
180 #ifndef Q_WS_QWS // We don't have Java in Qt Embedded
181 if ( !m_render || !m_render->isApplet() )
183 KJavaAppletWidget *w = static_cast<KJavaAppletWidget*>(static_cast<RenderApplet*>(m_render)->widget());
184 return (w && w->applet() && w->applet()->callMember(name, args, type, val));
192 KJS::Bindings::Instance *HTMLAppletElementImpl::getAppletInstance() const
194 KHTMLPart* part = getDocument()->part();
195 if (!part || !part->javaEnabled())
199 return appletInstance;
201 RenderApplet *r = static_cast<RenderApplet*>(m_render);
203 r->createWidgetIfNecessary();
205 // Call into the part (and over the bridge) to pull the Bindings::Instance
206 // from the guts of the plugin.
207 void *_view = r->widget()->getView();
208 appletInstance = KWQ(part)->getAppletInstanceForView((NSView *)_view);
211 return appletInstance;
215 // -------------------------------------------------------------------------
217 HTMLEmbedElementImpl::HTMLEmbedElementImpl(DocumentPtr *doc)
218 : HTMLElementImpl(doc), embedInstance(0)
221 HTMLEmbedElementImpl::~HTMLEmbedElementImpl()
225 NodeImpl::Id HTMLEmbedElementImpl::id() const
231 KJS::Bindings::Instance *HTMLEmbedElementImpl::getEmbedInstance() const
233 KHTMLPart* part = getDocument()->part();
238 return embedInstance;
240 RenderPartObject *r = static_cast<RenderPartObject*>(m_render);
243 // Call into the part (and over the bridge) to pull the Bindings::Instance
244 // from the guts of the Java VM.
245 void *_view = r->widget()->getView();
246 embedInstance = KWQ(part)->getEmbedInstanceForView((NSView *)_view);
249 return embedInstance;
253 bool HTMLEmbedElementImpl::mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const
266 result = eReplaced; // Share with <img> since the alignment behavior is the same.
272 return HTMLElementImpl::mapToEntry(attr, result);
275 void HTMLEmbedElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
277 QString val = attr->value().string();
280 switch ( attr->id() )
283 serviceType = val.lower();
284 pos = serviceType.find( ";" );
286 serviceType = serviceType.left( pos );
290 url = khtml::parseURL(attr->value()).string();
293 addCSSLength( attr, CSS_PROP_WIDTH, attr->value() );
296 addCSSLength( attr, CSS_PROP_HEIGHT, attr->value());
299 addCSSLength(attr, CSS_PROP_BORDER_WIDTH, attr->value());
300 addCSSProperty( attr, CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_SOLID );
301 addCSSProperty( attr, CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_SOLID );
302 addCSSProperty( attr, CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_SOLID );
303 addCSSProperty( attr, CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_SOLID );
306 addCSSLength(attr, CSS_PROP_MARGIN_TOP, attr->value());
307 addCSSLength(attr, CSS_PROP_MARGIN_BOTTOM, attr->value());
310 addCSSLength(attr, CSS_PROP_MARGIN_LEFT, attr->value());
311 addCSSLength(attr, CSS_PROP_MARGIN_RIGHT, attr->value());
314 addHTMLAlignment(attr);
317 addCSSProperty(attr, CSS_PROP_VERTICAL_ALIGN, attr->value());
319 case ATTR_PLUGINPAGE:
320 case ATTR_PLUGINSPAGE:
324 if (val.lower()=="yes" || val.lower()=="true") {
325 // FIXME: Not dynamic, but it's not really important that such a rarely-used
326 // feature work dynamically.
327 addCSSLength( attr, CSS_PROP_WIDTH, "0" );
328 addCSSLength( attr, CSS_PROP_HEIGHT, "0" );
332 HTMLElementImpl::parseHTMLAttribute( attr );
336 bool HTMLEmbedElementImpl::rendererIsNeeded(RenderStyle *style)
338 KHTMLPart *part = getDocument()->part();
341 return part->pluginsEnabled() && parentNode()->id() != ID_OBJECT;
344 RenderObject *HTMLEmbedElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
346 return new (arena) RenderPartObject(this);
349 void HTMLEmbedElementImpl::attach()
351 HTMLElementImpl::attach();
353 static_cast<RenderPartObject*>(m_render)->updateWidget();
357 bool HTMLEmbedElementImpl::isURLAttribute(AttributeImpl *attr) const
359 return attr->id() == ATTR_SRC;
362 // -------------------------------------------------------------------------
364 HTMLObjectElementImpl::HTMLObjectElementImpl(DocumentPtr *doc)
366 : HTMLElementImpl(doc), m_imageLoader(0), objectInstance(0)
368 : HTMLElementImpl(doc), m_imageLoader(0)
371 needWidgetUpdate = false;
374 HTMLObjectElementImpl::~HTMLObjectElementImpl()
376 delete m_imageLoader;
379 NodeImpl::Id HTMLObjectElementImpl::id() const
385 KJS::Bindings::Instance *HTMLObjectElementImpl::getObjectInstance() const
387 KHTMLPart* part = getDocument()->part();
392 return objectInstance;
394 RenderPartObject *r = static_cast<RenderPartObject*>(m_render);
397 // Call into the part (and over the bridge) to pull the Bindings::Instance
398 // from the guts of the plugin.
399 void *_view = r->widget()->getView();
400 objectInstance = KWQ(part)->getObjectInstanceForView((NSView *)_view);
403 return objectInstance;
407 HTMLFormElementImpl *HTMLObjectElementImpl::form() const
412 bool HTMLObjectElementImpl::mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const
423 return HTMLElementImpl::mapToEntry(attr, result);
426 void HTMLObjectElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
428 QString val = attr->value().string();
430 switch ( attr->id() )
433 serviceType = val.lower();
434 pos = serviceType.find( ";" );
436 serviceType = serviceType.left( pos );
438 needWidgetUpdate = true;
439 if (!canRenderImageType(serviceType) && m_imageLoader) {
440 delete m_imageLoader;
445 url = khtml::parseURL( val ).string();
447 needWidgetUpdate = true;
448 if (m_render && canRenderImageType(serviceType)) {
450 m_imageLoader = new HTMLImageLoader(this);
451 m_imageLoader->updateFromElement();
455 addCSSLength( attr, CSS_PROP_WIDTH, attr->value());
458 addCSSLength( attr, CSS_PROP_HEIGHT, attr->value());
463 needWidgetUpdate = true;
465 case ATTR_ONLOAD: // ### support load/unload on object elements
466 setHTMLEventListener(EventImpl::LOAD_EVENT,
467 getDocument()->createHTMLEventListener(attr->value().string()));
470 setHTMLEventListener(EventImpl::UNLOAD_EVENT,
471 getDocument()->createHTMLEventListener(attr->value().string()));
474 HTMLElementImpl::parseHTMLAttribute( attr );
478 DocumentImpl* HTMLObjectElementImpl::contentDocument() const
484 bool HTMLObjectElementImpl::rendererIsNeeded(RenderStyle *style)
486 if (canRenderImageType(serviceType)) {
487 return HTMLElementImpl::rendererIsNeeded(style);
490 KHTMLPart* part = getDocument()->part();
491 if (!part || !part->pluginsEnabled()) {
495 // Eventually we will merge with the better version of this check on the tip of tree.
496 // Until then, just leave it out.
498 KURL u = getDocument()->completeURL(url);
499 for (KHTMLPart* part = w->part()->parentPart(); part; part = part->parentPart())
500 if (part->url() == u) {
507 RenderObject *HTMLObjectElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
509 if (canRenderImageType(serviceType)) {
510 return new (arena) RenderImage(this);
512 return new (arena) RenderPartObject(this);
515 void HTMLObjectElementImpl::attach()
517 HTMLElementImpl::attach();
520 if (canRenderImageType(serviceType)) {
522 m_imageLoader = new HTMLImageLoader(this);
523 m_imageLoader->updateFromElement();
525 RenderImage* imageObj = static_cast<RenderImage*>(renderer());
526 imageObj->setImage(m_imageLoader->image());
529 if (needWidgetUpdate) {
530 static_cast<RenderPartObject*>(m_render)->updateWidget();
531 dispatchHTMLEvent(EventImpl::LOAD_EVENT,false,false);
532 needWidgetUpdate = false;
534 needWidgetUpdate = true;
541 void HTMLObjectElementImpl::detach()
543 // Only bother with an unload event if we had a render object. - dwh
544 if (attached() && m_render)
545 // ### do this when we are actualy removed from document instead
546 dispatchHTMLEvent(EventImpl::UNLOAD_EVENT,false,false);
548 HTMLElementImpl::detach();
551 void HTMLObjectElementImpl::recalcStyle(StyleChange ch)
553 if (needWidgetUpdate && m_render && !canRenderImageType(serviceType)) {
554 static_cast<RenderPartObject*>(m_render)->updateWidget();
555 dispatchHTMLEvent(EventImpl::LOAD_EVENT,false,false);
556 needWidgetUpdate = false;
558 HTMLElementImpl::recalcStyle(ch);
561 void HTMLObjectElementImpl::childrenChanged()
564 needWidgetUpdate = true;
569 bool HTMLObjectElementImpl::isURLAttribute(AttributeImpl *attr) const
571 return (attr->id() == ATTR_DATA || (attr->id() == ATTR_USEMAP && attr->value().domString()[0] != '#'));
574 // -------------------------------------------------------------------------
576 HTMLParamElementImpl::HTMLParamElementImpl(DocumentPtr *doc)
577 : HTMLElementImpl(doc)
581 HTMLParamElementImpl::~HTMLParamElementImpl()
585 NodeImpl::Id HTMLParamElementImpl::id() const
590 void HTMLParamElementImpl::parseHTMLAttribute(HTMLAttributeImpl *attr)
595 // Must call base class so that hasID bit gets set.
596 HTMLElementImpl::parseHTMLAttribute(attr);
597 if (getDocument()->htmlMode() != DocumentImpl::XHtml) break;
600 m_name = attr->value();
603 m_value = attr->value();
608 bool HTMLParamElementImpl::isURLAttribute(AttributeImpl *attr) const
610 if (attr->id() == ATTR_VALUE) {
611 AttributeImpl *attr = attributes()->getAttributeItem(ATTR_NAME);
613 DOMString value = attr->value().string().lower();
614 if (value == "src" || value == "movie" || value == "data") {