#include <config.h>
#include <klocale.h>
#include <kdebug.h>
#include "rendering/render_applet.h"
#include "rendering/render_canvas.h"
#include "xml/dom_docimpl.h"
#include "khtmlview.h"
#include "khtml_part.h"
#include <qlabel.h>
#ifndef Q_WS_QWS // We don't have Java in Qt Embedded
#include "java/kjavaappletwidget.h"
#include "misc/htmltags.h"
#include "html/html_objectimpl.h"
using namespace khtml;
using namespace DOM;
RenderApplet::RenderApplet(HTMLElementImpl *applet, const QMap<QString, QString> &args )
: RenderWidget(applet)
{
setInline(true);
KJavaAppletContext *context = 0;
KHTMLPart *part = applet->getDocument()->part();
if ( part ) {
context = part->createJavaContext();
}
#if APPLE_CHANGES
m_context = context;
m_args = args;
#else
if ( context ) {
setQWidget( new KJavaAppletWidget(context, _view->viewport()) );
processArguments(args);
}
#endif
}
RenderApplet::~RenderApplet()
{
}
int RenderApplet::intrinsicWidth() const
{
int rval = 150;
if( m_widget )
rval = ((KJavaAppletWidget*)(m_widget))->sizeHint().width();
return rval > 10 ? rval : 50;
}
int RenderApplet::intrinsicHeight() const
{
int rval = 150;
if( m_widget )
rval = m_widget->sizeHint().height();
return rval > 10 ? rval : 50;
}
void RenderApplet::createWidgetIfNecessary()
{
if (!m_widget) {
if (static_cast<HTMLAppletElementImpl*>(element())->allParamsAvailable()) {
int width = style()->width().isFixed() ? style()->width().value :
m_width - borderLeft() - borderRight() - paddingLeft() - paddingRight();
int height = style()->height().isFixed() ? style()->height().value :
m_height - borderTop() - borderBottom() - paddingTop() - paddingBottom();
NodeImpl *child = element()->firstChild();
while (child) {
if (child->id() == ID_PARAM) {
HTMLParamElementImpl *p = static_cast<HTMLParamElementImpl *>(child);
m_args.insert(p->name(), p->value());
}
child = child->nextSibling();
}
setQWidget(new KJavaAppletWidget(QSize(width, height), m_context, m_args));
}
}
}
void RenderApplet::layout()
{
KHTMLAssert( needsLayout() );
KHTMLAssert( minMaxKnown() );
calcWidth();
calcHeight();
KJavaAppletWidget *tmp = static_cast<KJavaAppletWidget*>(m_widget);
#if APPLE_CHANGES
if (!tmp)
createWidgetIfNecessary();
#else
if ( tmp ) {
NodeImpl *child = element()->firstChild();
while(child) {
if(child->id() == ID_PARAM) {
HTMLParamElementImpl *p = static_cast<HTMLParamElementImpl *>(child);
if(tmp->applet())
tmp->applet()->setParameter( p->name(), p->value());
}
child = child->nextSibling();
}
m_widget->resize(m_width - borderLeft() - borderRight() - paddingLeft() - paddingRight(),
m_height - borderTop() - borderBottom() - paddingTop() - paddingBottom());
tmp->showApplet();
}
#endif
setNeedsLayout(false);
}
#if !APPLE_CHANGES
void RenderApplet::processArguments(const QMap<QString, QString> &args)
{
KJavaAppletWidget *w = static_cast<KJavaAppletWidget*>(m_widget);
KJavaApplet* applet = w ? w->applet() : 0;
if ( applet ) {
applet->setBaseURL( args[QString::fromLatin1("baseURL") ] );
applet->setAppletClass( args[QString::fromLatin1("code") ] );
QString str = args[QString::fromLatin1("codeBase") ];
if( !str.isEmpty() )
applet->setCodeBase( str );
str = args[QString::fromLatin1("name") ];
if( !str.isNull() )
applet->setAppletName( str );
else
applet->setAppletName( args[QString::fromLatin1("code") ] );
str = args[QString::fromLatin1("archive") ];
if( !str.isEmpty() )
applet->setArchives( args[QString::fromLatin1("archive") ] );
}
}
#endif
RenderEmptyApplet::RenderEmptyApplet(DOM::NodeImpl* node)
: RenderWidget(node)
{
setInline(true);
#if APPLE_CHANGES
#else
QLabel* label = new QLabel(i18n("Java Applet is not loaded. (Java interpreter disabled)"), node->getDocument()->view()->viewport());
label->setAlignment( Qt::AlignCenter | Qt::WordBreak );
setQWidget(label);
#endif
}
int RenderEmptyApplet::intrinsicWidth() const
{
return (m_widget ? m_widget->sizeHint().width() : 150);
}
int RenderEmptyApplet::intrinsicHeight() const
{
return (m_widget ? m_widget->sizeHint().height() : 150);
}
void RenderEmptyApplet::layout()
{
KHTMLAssert( needsLayout() );
KHTMLAssert( minMaxKnown() );
calcWidth();
calcHeight();
#if !APPLE_CHANGES
if(m_widget)
{
m_widget->resize(m_width-borderLeft()-borderRight()-paddingLeft()-paddingRight(),
m_height-borderTop()-borderBottom()-paddingTop()-paddingBottom());
}
#endif
setNeedsLayout(false);
}
#endif