/*
 * This file is part of the WebKit project.
 *
 * Copyright (C) 2006 Apple Computer, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"
#if SVG_SUPPORT
#include "RenderForeignObject.h"

#include "GraphicsContext.h"
#include "KCanvasMatrix.h"
#include "KRenderingDevice.h"
#include "SVGAnimatedLength.h"
#include "SVGForeignObjectElement.h"

namespace WebCore {

RenderForeignObject::RenderForeignObject(SVGForeignObjectElement *node) 
    : RenderBlock(node)
{
}

AffineTransform RenderForeignObject::translationForAttributes()
{
    SVGForeignObjectElement *foreign = static_cast<SVGForeignObjectElement *>(element());
    return AffineTransform().translate(foreign->x()->baseVal()->value(), foreign->y()->baseVal()->value());
}

void RenderForeignObject::paint(PaintInfo& paintInfo, int parentX, int parentY)
{
    if (paintInfo.p->paintingDisabled())
        return;

    KRenderingDevice *device = renderingDevice();
    KRenderingDeviceContext *context = device->currentContext();
    bool shouldPopContext = false;
    if (!context) {
        // Only need to setup for KCanvas rendering if it hasn't already been done.
        context = paintInfo.p->createRenderingDeviceContext();
        device->pushContext(context);
        shouldPopContext = true;
    }

    paintInfo.p->save();

    context->concatCTM(AffineTransform().translate(parentX, parentY));
    context->concatCTM(localTransform());
    context->concatCTM(translationForAttributes());

    paintInfo.p->addClip(getClipRect(parentX, parentY));

    float opacity = style()->opacity();
    if (opacity < 1.0f)
        paintInfo.p->beginTransparencyLayer(opacity);

    PaintInfo pi(paintInfo);
    pi.r = absoluteTransform().invert().mapRect(paintInfo.r);
    RenderBlock::paint(pi, 0, 0);

    if (opacity < 1.0f)
        paintInfo.p->endTransparencyLayer();
    
    if (shouldPopContext) {
        device->popContext();
        delete context;
    }

    paintInfo.p->restore();
}

void RenderForeignObject::computeAbsoluteRepaintRect(IntRect& r, bool f)
{
    AffineTransform transform = translationForAttributes() * localTransform();
    r = transform.mapRect(r);
    
    RenderBlock::computeAbsoluteRepaintRect(r, f);
}

bool RenderForeignObject::requiresLayer()
{
    return false;
}

void RenderForeignObject::layout()
{
    ASSERT(needsLayout());
    ASSERT(minMaxKnown());

    IntRect oldBounds;
    bool checkForRepaint = checkForRepaintDuringLayout();
    if (checkForRepaint)
        oldBounds = m_absoluteBounds;

    RenderBlock::layout();

    m_absoluteBounds = getAbsoluteRepaintRect();

    if (checkForRepaint)
        repaintAfterLayoutIfNeeded(oldBounds, oldBounds);
    
    setNeedsLayout(false);
}

bool RenderForeignObject::nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty, HitTestAction hitTestAction)
{
    AffineTransform totalTransform = absoluteTransform();
    totalTransform *= translationForAttributes();
    double localX, localY;
    totalTransform.invert().map(x, y, &localX, &localY);
    return RenderBlock::nodeAtPoint(info, (int)localX, (int)localY, tx, ty, hitTestAction);
}

}

#endif // SVG_SUPPORT
