html_imageimpl.cpp [plain text]
#include "html/html_imageimpl.h"
#include "html/html_documentimpl.h"
#include "misc/htmlhashes.h"
#include "khtmlview.h"
#include "khtml_part.h"
#include <kstringhandler.h>
#include <kglobal.h>
#include <kdebug.h>
#include "rendering/render_image.h"
#include "rendering/render_flow.h"
#include "css/cssstyleselector.h"
#include "css/cssproperties.h"
#include "css/cssvalues.h"
#include "css/csshelper.h"
#include "xml/dom2_eventsimpl.h"
#include <qstring.h>
#include <qpoint.h>
#include <qregion.h>
#include <qptrstack.h>
#include <qimage.h>
#include <qpointarray.h>
using namespace DOM;
using namespace khtml;
HTMLImageElementImpl::HTMLImageElementImpl(DocumentPtr *doc)
: HTMLElementImpl(doc)
{
ismap = false;
}
HTMLImageElementImpl::~HTMLImageElementImpl()
{
}
NodeImpl::Id HTMLImageElementImpl::id() const
{
return ID_IMG;
}
void HTMLImageElementImpl::parseAttribute(AttributeImpl *attr)
{
switch (attr->id())
{
case ATTR_ALT:
case ATTR_SRC:
if (m_render) m_render->updateFromElement();
break;
case ATTR_WIDTH:
addCSSLength(CSS_PROP_WIDTH, attr->value());
break;
case ATTR_HEIGHT:
addCSSLength(CSS_PROP_HEIGHT, attr->value());
break;
case ATTR_BORDER:
if(attr->value().toInt()) {
addCSSLength(CSS_PROP_BORDER_WIDTH, attr->value());
addCSSProperty( CSS_PROP_BORDER_TOP_STYLE, CSS_VAL_SOLID );
addCSSProperty( CSS_PROP_BORDER_RIGHT_STYLE, CSS_VAL_SOLID );
addCSSProperty( CSS_PROP_BORDER_BOTTOM_STYLE, CSS_VAL_SOLID );
addCSSProperty( CSS_PROP_BORDER_LEFT_STYLE, CSS_VAL_SOLID );
}
break;
case ATTR_VSPACE:
addCSSLength(CSS_PROP_MARGIN_TOP, attr->value());
addCSSLength(CSS_PROP_MARGIN_BOTTOM, attr->value());
break;
case ATTR_HSPACE:
addCSSLength(CSS_PROP_MARGIN_LEFT, attr->value());
addCSSLength(CSS_PROP_MARGIN_RIGHT, attr->value());
break;
case ATTR_ALIGN:
addHTMLAlignment( attr->value() );
break;
case ATTR_VALIGN:
addCSSProperty(CSS_PROP_VERTICAL_ALIGN, attr->value());
break;
case ATTR_USEMAP:
if ( attr->value()[0] == '#' )
usemap = attr->value();
else {
QString url = getDocument()->completeURL( khtml::parseURL( attr->value() ).string() );
usemap = url;
}
m_hasAnchor = attr->val() != 0;
case ATTR_ISMAP:
ismap = true;
break;
case ATTR_ONABORT: setHTMLEventListener(EventImpl::ABORT_EVENT,
getDocument()->createHTMLEventListener(attr->value().string()));
break;
case ATTR_ONERROR:
setHTMLEventListener(EventImpl::ERROR_EVENT,
getDocument()->createHTMLEventListener(attr->value().string()));
break;
case ATTR_ONLOAD:
setHTMLEventListener(EventImpl::LOAD_EVENT,
getDocument()->createHTMLEventListener(attr->value().string()));
break;
case ATTR_NOSAVE:
break;
case ATTR_NAME:
{
QString newNameAttr = attr->value().string();
if (attached() && getDocument()->isHTMLDocument()) {
HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
document->removeNamedImageOrForm(oldNameAttr);
document->addNamedImageOrForm(newNameAttr);
}
oldNameAttr = newNameAttr;
}
break;
case ATTR_ID:
{
QString newIdAttr = attr->value().string();
if (attached() && getDocument()->isHTMLDocument()) {
HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
document->removeNamedImageOrForm(oldIdAttr);
document->addNamedImageOrForm(newIdAttr);
}
oldIdAttr = newIdAttr;
}
default:
HTMLElementImpl::parseAttribute(attr);
}
}
DOMString HTMLImageElementImpl::altText() const
{
DOMString alt( getAttribute( ATTR_ALT ) );
if ( alt.isNull() )
alt = getAttribute( ATTR_TITLE );
#if 0
if ( alt.isNull() ) {
QString p = KURL( getDocument()->completeURL( getAttribute(ATTR_SRC).string() ) ).prettyURL();
int pos;
if ( ( pos = p.findRev( '.' ) ) > 0 )
p.truncate( pos );
alt = DOMString( KStringHandler::csqueeze( p ) );
}
#endif
return alt;
}
RenderObject *HTMLImageElementImpl::createRenderer(RenderArena *arena, RenderStyle *style)
{
return new (arena) RenderImage(this);
}
void HTMLImageElementImpl::attach()
{
createRendererIfNeeded();
if (m_render) {
m_render->updateFromElement();
}
if (getDocument()->isHTMLDocument()) {
HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
document->addNamedImageOrForm(oldIdAttr);
document->addNamedImageOrForm(oldNameAttr);
}
NodeBaseImpl::attach();
}
void HTMLImageElementImpl::detach()
{
if (getDocument()->isHTMLDocument()) {
HTMLDocumentImpl *document = static_cast<HTMLDocumentImpl *>(getDocument());
document->removeNamedImageOrForm(oldIdAttr);
document->removeNamedImageOrForm(oldNameAttr);
}
NodeBaseImpl::detach();
}
long HTMLImageElementImpl::width() const
{
if (!m_render) return getAttribute(ATTR_WIDTH).toInt();
if (changed() || m_render->needsLayout()) {
getDocument()->updateRendering();
if (getDocument()->view())
getDocument()->view()->layout();
}
return m_render->contentWidth();
}
long HTMLImageElementImpl::height() const
{
if (!m_render) return getAttribute(ATTR_HEIGHT).toInt();
if (changed() || m_render->needsLayout()) {
getDocument()->updateRendering();
if (getDocument()->view())
getDocument()->view()->layout();
}
return m_render->contentHeight();
}
QImage HTMLImageElementImpl::currentImage() const
{
RenderImage *r = static_cast<RenderImage*>(renderer());
if(r)
return r->pixmap().convertToImage();
return QImage();
}
HTMLMapElementImpl::HTMLMapElementImpl(DocumentPtr *doc)
: HTMLElementImpl(doc)
{
}
HTMLMapElementImpl::~HTMLMapElementImpl()
{
if(getDocument() && getDocument()->isHTMLDocument())
static_cast<HTMLDocumentImpl*>(getDocument())->mapMap.remove(name);
}
NodeImpl::Id HTMLMapElementImpl::id() const
{
return ID_MAP;
}
bool
HTMLMapElementImpl::mapMouseEvent(int x_, int y_, int width_, int height_,
RenderObject::NodeInfo& info)
{
QPtrStack<NodeImpl> nodeStack;
NodeImpl *current = firstChild();
while(1)
{
if(!current)
{
if(nodeStack.isEmpty()) break;
current = nodeStack.pop();
current = current->nextSibling();
continue;
}
if(current->id()==ID_AREA)
{
HTMLAreaElementImpl* area=static_cast<HTMLAreaElementImpl*>(current);
if (area->mapMouseEvent(x_,y_,width_,height_, info))
return true;
}
NodeImpl *child = current->firstChild();
if(child)
{
nodeStack.push(current);
current = child;
}
else
{
current = current->nextSibling();
}
}
return false;
}
void HTMLMapElementImpl::parseAttribute(AttributeImpl *attr)
{
switch (attr->id())
{
case ATTR_ID:
if (getDocument()->htmlMode() != DocumentImpl::XHtml) break;
case ATTR_NAME:
{
DOMString s = attr->value();
if(*s.unicode() == '#')
name = QString(s.unicode()+1, s.length()-1);
else
name = s.string();
if(getDocument()->isHTMLDocument())
static_cast<HTMLDocumentImpl*>(getDocument())->mapMap[name] = this;
break;
}
default:
HTMLElementImpl::parseAttribute(attr);
}
}
HTMLAreaElementImpl::HTMLAreaElementImpl(DocumentPtr *doc)
: HTMLAnchorElementImpl(doc)
{
m_coords=0;
m_coordsLen = 0;
nohref = false;
shape = Unknown;
lasth = lastw = -1;
}
HTMLAreaElementImpl::~HTMLAreaElementImpl()
{
if (m_coords) delete [] m_coords;
}
NodeImpl::Id HTMLAreaElementImpl::id() const
{
return ID_AREA;
}
void HTMLAreaElementImpl::parseAttribute(AttributeImpl *attr)
{
switch (attr->id())
{
case ATTR_SHAPE:
if ( strcasecmp( attr->value(), "default" ) == 0 )
shape = Default;
else if ( strcasecmp( attr->value(), "circle" ) == 0 )
shape = Circle;
else if ( strcasecmp( attr->value(), "poly" ) == 0 )
shape = Poly;
else if ( strcasecmp( attr->value(), "rect" ) == 0 )
shape = Rect;
break;
case ATTR_COORDS:
if (m_coords) delete [] m_coords;
m_coords = attr->val()->toLengthArray(m_coordsLen);
break;
case ATTR_NOHREF:
nohref = attr->val() != 0;
break;
case ATTR_TARGET:
m_hasTarget = attr->val() != 0;
break;
case ATTR_ALT:
break;
case ATTR_ACCESSKEY:
break;
default:
HTMLAnchorElementImpl::parseAttribute(attr);
}
}
bool HTMLAreaElementImpl::mapMouseEvent(int x_, int y_, int width_, int height_,
RenderObject::NodeInfo& info)
{
bool inside = false;
if (width_ != lastw || height_ != lasth)
{
region=getRegion(width_, height_);
lastw=width_; lasth=height_;
}
if (region.contains(QPoint(x_,y_)))
{
inside = true;
info.setInnerNode(this);
info.setURLElement(this);
}
return inside;
}
QRect HTMLAreaElementImpl::getRect() const
{
if (parentNode()->renderer()==0)
return QRect();
int dx, dy;
if (!parentNode()->renderer()->absolutePosition(dx, dy))
return QRect();
QRegion region = getRegion(lastw,lasth);
region.translate(dx, dy);
return region.boundingRect();
}
QRegion HTMLAreaElementImpl::getRegion(int width_, int height_) const
{
QRegion region;
if (!m_coords)
return region;
if ((shape==Poly || shape==Unknown) && m_coordsLen > 5) {
int len = m_coordsLen >> 1;
QPointArray points(len);
for (int i = 0; i < len; ++i)
points.setPoint(i, m_coords[(i<<1)].minWidth(width_),
m_coords[(i<<1)+1].minWidth(height_));
region = QRegion(points);
}
else if (shape==Circle && m_coordsLen>=3 || shape==Unknown && m_coordsLen == 3) {
int r = kMin(m_coords[2].minWidth(width_), m_coords[2].minWidth(height_));
region = QRegion(m_coords[0].minWidth(width_)-r,
m_coords[1].minWidth(height_)-r, 2*r, 2*r,QRegion::Ellipse);
}
else if (shape==Rect && m_coordsLen>=4 || shape==Unknown && m_coordsLen == 4) {
int x0 = m_coords[0].minWidth(width_);
int y0 = m_coords[1].minWidth(height_);
int x1 = m_coords[2].minWidth(width_);
int y1 = m_coords[3].minWidth(height_);
region = QRegion(x0,y0,x1-x0,y1-y0);
}
else if (shape==Default)
region = QRegion(0,0,width_,height_);
return region;
}