#include "config.h"
#include "RenderWidget.h"
#include "AXObjectCache.h"
#include "AnimationController.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "RenderCounter.h"
#include "RenderView.h"
#include "RenderWidgetProtector.h"
#if USE(ACCELERATED_COMPOSITING)
#include "RenderLayerBacking.h"
#endif
using namespace std;
namespace WebCore {
static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
{
static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
return *staticWidgetRendererMap;
}
static size_t widgetHierarchyUpdateSuspendCount;
typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
static WidgetToParentMap& widgetNewParentMap()
{
DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
return map;
}
void RenderWidget::suspendWidgetHierarchyUpdates()
{
widgetHierarchyUpdateSuspendCount++;
}
void RenderWidget::resumeWidgetHierarchyUpdates()
{
ASSERT(widgetHierarchyUpdateSuspendCount);
if (widgetHierarchyUpdateSuspendCount == 1) {
WidgetToParentMap map = widgetNewParentMap();
widgetNewParentMap().clear();
WidgetToParentMap::iterator end = map.end();
for (WidgetToParentMap::iterator it = map.begin(); it != end; ++it) {
Widget* child = it->first.get();
ScrollView* currentParent = child->parent();
FrameView* newParent = it->second;
if (newParent != currentParent) {
if (currentParent)
currentParent->removeChild(child);
if (newParent)
newParent->addChild(child);
}
}
}
widgetHierarchyUpdateSuspendCount--;
}
static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
{
if (!widgetHierarchyUpdateSuspendCount) {
if (parent)
parent->addChild(child);
else
child->removeFromParent();
return;
}
widgetNewParentMap().set(child, parent);
}
RenderWidget::RenderWidget(Node* node)
: RenderReplaced(node)
, m_widget(0)
, m_frameView(node->document()->view())
, m_refCount(1)
{
view()->addWidget(this);
}
void RenderWidget::destroy()
{
animation()->cancelAnimations(this);
if (RenderView* v = view())
v->removeWidget(this);
if (m_hasCounterNodeMap)
RenderCounter::destroyCounterNodes(this);
if (AXObjectCache::accessibilityEnabled()) {
document()->axObjectCache()->childrenChanged(this->parent());
document()->axObjectCache()->remove(this);
}
remove();
setWidget(0);
if (hasOverrideSize())
setOverrideSize(-1);
if (style() && (style()->height().isPercent() || style()->minHeight().isPercent() || style()->maxHeight().isPercent()))
RenderBlock::removePercentHeightDescendant(this);
if (hasLayer()) {
layer()->clearClipRects();
setHasLayer(false);
destroyLayer();
}
RenderArena* arena = renderArena();
setNode(0);
deref(arena);
}
RenderWidget::~RenderWidget()
{
ASSERT(m_refCount <= 0);
clearWidget();
}
bool RenderWidget::setWidgetGeometry(const IntRect& frame)
{
ASSERT(!widgetHierarchyUpdateSuspendCount);
if (!node())
return false;
IntRect clipRect = enclosingLayer()->childrenClipRect();
bool clipChanged = m_clipRect != clipRect;
bool boundsChanged = m_widget->frameRect() != frame;
if (!boundsChanged && !clipChanged)
return false;
m_clipRect = clipRect;
RenderWidgetProtector protector(this);
RefPtr<Node> protectedNode(node());
m_widget->setFrameRect(frame);
#if USE(ACCELERATED_COMPOSITING)
if (hasLayer() && layer()->isComposited())
layer()->backing()->updateAfterWidgetResize();
#endif
return boundsChanged;
}
void RenderWidget::setWidget(PassRefPtr<Widget> widget)
{
if (widget == m_widget)
return;
if (m_widget) {
moveWidgetToParentSoon(m_widget.get(), 0);
widgetRendererMap().remove(m_widget.get());
clearWidget();
}
m_widget = widget;
if (m_widget) {
widgetRendererMap().add(m_widget.get(), this);
if (style()) {
if (!needsLayout())
setWidgetGeometry(absoluteContentBox());
if (style()->visibility() != VISIBLE)
m_widget->hide();
else
m_widget->show();
}
moveWidgetToParentSoon(m_widget.get(), m_frameView);
}
}
void RenderWidget::layout()
{
ASSERT(needsLayout());
setNeedsLayout(false);
}
void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderReplaced::styleDidChange(diff, oldStyle);
if (m_widget) {
if (style()->visibility() != VISIBLE)
m_widget->hide();
else
m_widget->show();
}
}
void RenderWidget::showSubstituteImage(PassRefPtr<Image> prpImage)
{
m_substituteImage = prpImage;
repaint();
}
void RenderWidget::paint(PaintInfo& paintInfo, int tx, int ty)
{
if (!shouldPaint(paintInfo, tx, ty))
return;
tx += x();
ty += y();
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, tx, ty);
if (paintInfo.phase == PaintPhaseMask) {
paintMask(paintInfo, tx, ty);
return;
}
if (!m_frameView || paintInfo.phase != PaintPhaseForeground || style()->visibility() != VISIBLE)
return;
#if PLATFORM(MAC)
if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
paintCustomHighlight(tx - x(), ty - y(), style()->highlight(), true);
#endif
if (style()->hasBorderRadius()) {
IntRect borderRect = IntRect(tx, ty, width(), height());
if (borderRect.isEmpty())
return;
paintInfo.context->save();
IntSize topLeft, topRight, bottomLeft, bottomRight;
style()->getBorderRadiiForRect(borderRect, topLeft, topRight, bottomLeft, bottomRight);
paintInfo.context->addRoundedRectClip(borderRect, topLeft, topRight, bottomLeft, bottomRight);
}
if (m_widget) {
if (m_substituteImage)
paintInfo.context->drawImage(m_substituteImage.get(), style()->colorSpace(), m_widget->frameRect());
else {
IntPoint widgetLocation = m_widget->frameRect().location();
IntPoint paintLocation(tx + borderLeft() + paddingLeft(), ty + borderTop() + paddingTop());
IntRect paintRect = paintInfo.rect;
IntSize paintOffset = paintLocation - widgetLocation;
if (!paintOffset.isZero()) {
paintInfo.context->translate(paintOffset);
paintRect.move(-paintOffset);
}
m_widget->paint(paintInfo.context, paintRect);
if (!paintOffset.isZero())
paintInfo.context->translate(-paintOffset);
}
if (m_widget->isFrameView()) {
FrameView* frameView = static_cast<FrameView*>(m_widget.get());
bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContent();
if (paintInfo.overlapTestRequests && runOverlapTests) {
ASSERT(!paintInfo.overlapTestRequests->contains(this));
paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
}
}
}
if (style()->hasBorderRadius())
paintInfo.context->restore();
if (isSelected() && !document()->printing()) {
paintInfo.context->fillRect(selectionRect(), selectionBackgroundColor(), style()->colorSpace());
}
}
void RenderWidget::setOverlapTestResult(bool isOverlapped)
{
ASSERT(m_widget);
ASSERT(m_widget->isFrameView());
static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
}
void RenderWidget::deref(RenderArena *arena)
{
if (--m_refCount <= 0)
arenaDelete(arena, this);
}
void RenderWidget::updateWidgetPosition()
{
if (!m_widget || !node()) return;
FloatPoint absPos = localToAbsolute();
absPos.move(borderLeft() + paddingLeft(), borderTop() + paddingTop());
int w = width() - borderAndPaddingWidth();
int h = height() - borderAndPaddingHeight();
bool boundsChanged = setWidgetGeometry(IntRect(absPos.x(), absPos.y(), w, h));
if (m_widget && m_widget->isFrameView()) {
FrameView* frameView = static_cast<FrameView*>(m_widget.get());
if (boundsChanged || frameView->needsLayout())
frameView->layout();
}
}
void RenderWidget::widgetPositionsUpdated()
{
if (!m_widget)
return;
m_widget->widgetPositionsUpdated();
}
IntRect RenderWidget::windowClipRect() const
{
if (!m_frameView)
return IntRect();
return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
}
void RenderWidget::setSelectionState(SelectionState state)
{
if (selectionState() != state) {
RenderReplaced::setSelectionState(state);
if (m_widget)
m_widget->setIsSelected(isSelected());
}
}
void RenderWidget::clearWidget()
{
m_widget = 0;
}
RenderWidget* RenderWidget::find(const Widget* widget)
{
return widgetRendererMap().get(widget);
}
bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction action)
{
bool hadResult = result.innerNode();
bool inside = RenderReplaced::nodeAtPoint(request, result, x, y, tx, ty, action);
if (inside && !hadResult && result.innerNode() == node())
result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
return inside;
}
}