1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* Copyright (C) 2000 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderWidget.h"
#include "AXObjectCache.h"
#include "AnimationController.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "RenderCounter.h"
#include "RenderLayer.h"
#include "RenderView.h"
#include "RenderWidgetProtector.h"
#include <wtf/StackStats.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;
}
unsigned WidgetHierarchyUpdatesSuspensionScope::s_widgetHierarchyUpdateSuspendCount = 0;
WidgetHierarchyUpdatesSuspensionScope::WidgetToParentMap& WidgetHierarchyUpdatesSuspensionScope::widgetNewParentMap()
{
DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
return map;
}
void WidgetHierarchyUpdatesSuspensionScope::moveWidgets()
{
WidgetToParentMap map;
widgetNewParentMap().swap(map);
WidgetToParentMap::iterator end = map.end();
for (WidgetToParentMap::iterator it = map.begin(); it != end; ++it) {
Widget* child = it->key.get();
ScrollView* currentParent = child->parent();
FrameView* newParent = it->value;
if (newParent != currentParent) {
if (currentParent)
currentParent->removeChild(child);
if (newParent)
newParent->addChild(child);
}
}
}
static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
{
if (!WidgetHierarchyUpdatesSuspensionScope::isSuspended()) {
if (parent)
parent->addChild(child);
else
child->removeFromParent();
return;
}
WidgetHierarchyUpdatesSuspensionScope::scheduleWidgetToMove(child, parent);
}
RenderWidget::RenderWidget(Element* element)
: RenderReplaced(element)
, m_widget(0)
, m_frameView(element->document()->view())
// Reference counting is used to prevent the widget from being
// destroyed while inside the Widget code, which might not be
// able to handle that.
, m_refCount(1)
{
view()->addWidget(this);
}
void RenderWidget::willBeDestroyed()
{
if (RenderView* v = view())
v->removeWidget(this);
if (AXObjectCache* cache = document()->existingAXObjectCache()) {
cache->childrenChanged(this->parent());
cache->remove(this);
}
setWidget(0);
RenderReplaced::willBeDestroyed();
}
void RenderWidget::destroy()
{
willBeDestroyed();
// Grab the arena from node()->document()->renderArena() before clearing the node pointer.
// Clear the node before deref-ing, as this may be deleted when deref is called.
RenderArena* arena = renderArena();
clearNode();
deref(arena);
}
RenderWidget::~RenderWidget()
{
ASSERT(m_refCount <= 0);
clearWidget();
}
// Widgets are always placed on integer boundaries, so rounding the size is actually
// the desired behavior. This function is here because it's otherwise seldom what we
// want to do with a LayoutRect.
static inline IntRect roundedIntRect(const LayoutRect& rect)
{
return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size()));
}
bool RenderWidget::setWidgetGeometry(const LayoutRect& frame)
{
if (!node())
return false;
IntRect clipRect = roundedIntRect(enclosingLayer()->childrenClipRect());
IntRect newFrame = roundedIntRect(frame);
bool clipChanged = m_clipRect != clipRect;
bool boundsChanged = m_widget->frameRect() != newFrame;
if (!boundsChanged && !clipChanged)
return false;
m_clipRect = clipRect;
RenderWidgetProtector protector(this);
RefPtr<Node> protectedNode(node());
m_widget->setFrameRect(newFrame);
if (clipChanged && !boundsChanged)
m_widget->clipRectChanged();
#if USE(ACCELERATED_COMPOSITING)
if (hasLayer() && layer()->isComposited())
layer()->backing()->updateAfterWidgetResize();
#endif
return boundsChanged;
}
bool RenderWidget::updateWidgetGeometry()
{
LayoutRect contentBox = contentBoxRect();
if (!m_widget->transformsAffectFrameRect())
return setWidgetGeometry(absoluteContentBox());
LayoutRect absoluteContentBox(localToAbsoluteQuad(FloatQuad(contentBox)).boundingBox());
if (m_widget->isFrameView()) {
contentBox.setLocation(absoluteContentBox.location());
return setWidgetGeometry(contentBox);
}
return setWidgetGeometry(absoluteContentBox);
}
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 we've already received a layout, apply the calculated space to the
// widget immediately, but we have to have really been fully constructed (with a non-null
// style pointer).
if (style()) {
if (!needsLayout())
updateWidgetGeometry();
if (style()->visibility() != VISIBLE)
m_widget->hide();
else {
m_widget->show();
repaint();
}
}
moveWidgetToParentSoon(m_widget.get(), m_frameView);
}
}
void RenderWidget::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
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::notifyWidget(WidgetNotification notification)
{
if (m_widget)
m_widget->notifyWidget(notification);
}
void RenderWidget::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
LayoutPoint adjustedPaintOffset = paintOffset + location();
// Tell the widget to paint now. This is the only time the widget is allowed
// to paint itself. That way it will composite properly with z-indexed layers.
IntPoint widgetLocation = m_widget->frameRect().location();
IntPoint paintLocation(roundToInt(adjustedPaintOffset.x() + borderLeft() + paddingLeft()),
roundToInt(adjustedPaintOffset.y() + borderTop() + paddingTop()));
IntRect paintRect = paintInfo.rect;
IntSize widgetPaintOffset = paintLocation - widgetLocation;
// When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
// not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
if (!widgetPaintOffset.isZero()) {
paintInfo.context->translate(widgetPaintOffset);
paintRect.move(-widgetPaintOffset);
}
m_widget->paint(paintInfo.context, paintRect);
if (!widgetPaintOffset.isZero())
paintInfo.context->translate(-widgetPaintOffset);
if (m_widget->isFrameView()) {
FrameView* frameView = toFrameView(m_widget.get());
bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
if (paintInfo.overlapTestRequests && runOverlapTests) {
ASSERT(!paintInfo.overlapTestRequests->contains(this));
paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
}
}
}
void RenderWidget::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!shouldPaint(paintInfo, paintOffset))
return;
LayoutPoint adjustedPaintOffset = paintOffset + location();
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, adjustedPaintOffset);
if (paintInfo.phase == PaintPhaseMask) {
paintMask(paintInfo, adjustedPaintOffset);
return;
}
if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && hasOutline())
paintOutline(paintInfo, LayoutRect(adjustedPaintOffset, size()));
if (!m_frameView || paintInfo.phase != PaintPhaseForeground)
return;
#if PLATFORM(MAC)
if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
paintCustomHighlight(paintOffset, style()->highlight(), true);
#endif
if (style()->hasBorderRadius()) {
LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
if (borderRect.isEmpty())
return;
// Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
paintInfo.context->save();
RoundedRect roundedInnerRect = style()->getRoundedInnerBorderFor(borderRect,
paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true);
clipRoundedInnerRect(paintInfo.context, borderRect, roundedInnerRect);
}
if (m_widget)
paintContents(paintInfo, paintOffset);
if (style()->hasBorderRadius())
paintInfo.context->restore();
// Paint a partially transparent wash over selected widgets.
if (isSelected() && !document()->printing()) {
// FIXME: selectionRect() is in absolute, not painting coordinates.
paintInfo.context->fillRect(pixelSnappedIntRect(selectionRect()), selectionBackgroundColor(), style()->colorSpace());
}
if (hasLayer() && layer()->canResize())
layer()->paintResizer(paintInfo.context, roundedIntPoint(adjustedPaintOffset), paintInfo.rect);
}
void RenderWidget::setOverlapTestResult(bool isOverlapped)
{
ASSERT(m_widget);
ASSERT(m_widget->isFrameView());
toFrameView(m_widget.get())->setIsOverlapped(isOverlapped);
}
void RenderWidget::deref(RenderArena *arena)
{
if (--m_refCount <= 0)
arenaDelete(arena, this);
}
void RenderWidget::updateWidgetPosition()
{
if (!m_widget || !node()) // Check the node in case destroy() has been called.
return;
bool boundsChanged = updateWidgetGeometry();
// if the frame bounds got changed, or if view needs layout (possibly indicating
// content size is wrong) we have to do a layout to set the right widget size
if (m_widget && m_widget->isFrameView()) {
FrameView* frameView = toFrameView(m_widget.get());
// Check the frame's page to make sure that the frame isn't in the process of being destroyed.
if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
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)
{
// The selection state for our containing block hierarchy is updated by the base class call.
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, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
bool hadResult = result.innerNode();
bool inside = RenderReplaced::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, action);
// Check to see if we are really over the widget itself (and not just in the border/padding area).
if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
return inside;
}
CursorDirective RenderWidget::getCursor(const LayoutPoint& point, Cursor& cursor) const
{
if (widget() && widget()->isPluginViewBase()) {
// A plug-in is responsible for setting the cursor when the pointer is over it.
return DoNotSetCursor;
}
return RenderReplaced::getCursor(point, cursor);
}
} // namespace WebCore
|