2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "RenderWidget.h"
26 #include "AXObjectCache.h"
27 #include "AnimationController.h"
29 #include "GraphicsContext.h"
30 #include "HitTestResult.h"
31 #include "RenderCounter.h"
32 #include "RenderLayer.h"
33 #include "RenderView.h"
34 #include "RenderWidgetProtector.h"
36 #if USE(ACCELERATED_COMPOSITING)
37 #include "RenderLayerBacking.h"
44 static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
46 static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
47 return *staticWidgetRendererMap;
50 unsigned WidgetHierarchyUpdatesSuspensionScope::s_widgetHierarchyUpdateSuspendCount = 0;
52 WidgetHierarchyUpdatesSuspensionScope::WidgetToParentMap& WidgetHierarchyUpdatesSuspensionScope::widgetNewParentMap()
54 DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
58 void WidgetHierarchyUpdatesSuspensionScope::moveWidgets()
60 WidgetToParentMap map = widgetNewParentMap();
61 widgetNewParentMap().clear();
62 WidgetToParentMap::iterator end = map.end();
63 for (WidgetToParentMap::iterator it = map.begin(); it != end; ++it) {
64 Widget* child = it->key.get();
65 ScrollView* currentParent = child->parent();
66 FrameView* newParent = it->value;
67 if (newParent != currentParent) {
69 currentParent->removeChild(child);
71 newParent->addChild(child);
76 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
78 if (!WidgetHierarchyUpdatesSuspensionScope::isSuspended()) {
80 parent->addChild(child);
82 child->removeFromParent();
85 WidgetHierarchyUpdatesSuspensionScope::scheduleWidgetToMove(child, parent);
88 RenderWidget::RenderWidget(Element* element)
89 : RenderReplaced(element)
91 , m_frameView(element->document()->view())
92 // Reference counting is used to prevent the widget from being
93 // destroyed while inside the Widget code, which might not be
94 // able to handle that.
97 view()->addWidget(this);
100 void RenderWidget::willBeDestroyed()
102 if (RenderView* v = view())
103 v->removeWidget(this);
105 if (AXObjectCache::accessibilityEnabled()) {
106 document()->axObjectCache()->childrenChanged(this->parent());
107 document()->axObjectCache()->remove(this);
112 RenderReplaced::willBeDestroyed();
115 void RenderWidget::destroy()
119 // Grab the arena from node()->document()->renderArena() before clearing the node pointer.
120 // Clear the node before deref-ing, as this may be deleted when deref is called.
121 RenderArena* arena = renderArena();
126 RenderWidget::~RenderWidget()
128 ASSERT(m_refCount <= 0);
132 // Widgets are always placed on integer boundaries, so rounding the size is actually
133 // the desired behavior. This function is here because it's otherwise seldom what we
134 // want to do with a LayoutRect.
135 static inline IntRect roundedIntRect(const LayoutRect& rect)
137 return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size()));
140 bool RenderWidget::setWidgetGeometry(const LayoutRect& frame)
145 IntRect clipRect = roundedIntRect(enclosingLayer()->childrenClipRect());
146 bool clipChanged = m_clipRect != clipRect;
147 bool boundsChanged = m_widget->frameRect() != frame;
149 if (!boundsChanged && !clipChanged)
152 m_clipRect = clipRect;
154 RenderWidgetProtector protector(this);
155 RefPtr<Node> protectedNode(node());
156 m_widget->setFrameRect(roundedIntRect(frame));
158 #if USE(ACCELERATED_COMPOSITING)
159 if (hasLayer() && layer()->isComposited())
160 layer()->backing()->updateAfterWidgetResize();
163 return boundsChanged;
166 bool RenderWidget::updateWidgetGeometry()
168 LayoutRect contentBox = contentBoxRect();
169 if (!m_widget->transformsAffectFrameRect())
170 return setWidgetGeometry(absoluteContentBox());
172 LayoutRect absoluteContentBox(localToAbsoluteQuad(FloatQuad(contentBox)).boundingBox());
173 if (m_widget->isFrameView()) {
174 contentBox.setLocation(absoluteContentBox.location());
175 return setWidgetGeometry(contentBox);
178 return setWidgetGeometry(absoluteContentBox);
181 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
183 if (widget == m_widget)
187 moveWidgetToParentSoon(m_widget.get(), 0);
188 widgetRendererMap().remove(m_widget.get());
193 widgetRendererMap().add(m_widget.get(), this);
194 // If we've already received a layout, apply the calculated space to the
195 // widget immediately, but we have to have really been fully constructed (with a non-null
199 updateWidgetGeometry();
201 if (style()->visibility() != VISIBLE)
208 moveWidgetToParentSoon(m_widget.get(), m_frameView);
212 void RenderWidget::layout()
214 StackStats::LayoutCheckPoint layoutCheckPoint;
215 ASSERT(needsLayout());
217 setNeedsLayout(false);
220 void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
222 RenderReplaced::styleDidChange(diff, oldStyle);
224 if (style()->visibility() != VISIBLE)
231 void RenderWidget::notifyWidget(WidgetNotification notification)
234 m_widget->notifyWidget(notification);
237 void RenderWidget::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
239 if (!shouldPaint(paintInfo, paintOffset))
242 LayoutPoint adjustedPaintOffset = paintOffset + location();
244 if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
245 paintBoxDecorations(paintInfo, adjustedPaintOffset);
247 if (paintInfo.phase == PaintPhaseMask) {
248 paintMask(paintInfo, adjustedPaintOffset);
252 if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && hasOutline())
253 paintOutline(paintInfo.context, LayoutRect(adjustedPaintOffset, size()));
255 if (!m_frameView || paintInfo.phase != PaintPhaseForeground)
259 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
260 paintCustomHighlight(paintOffset, style()->highlight(), true);
263 if (style()->hasBorderRadius()) {
264 LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
266 if (borderRect.isEmpty())
269 // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
270 paintInfo.context->save();
271 RoundedRect roundedInnerRect = style()->getRoundedInnerBorderFor(borderRect,
272 paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true);
273 clipRoundedInnerRect(paintInfo.context, borderRect, roundedInnerRect);
277 // Tell the widget to paint now. This is the only time the widget is allowed
278 // to paint itself. That way it will composite properly with z-indexed layers.
279 IntPoint widgetLocation = m_widget->frameRect().location();
280 IntPoint paintLocation(roundToInt(adjustedPaintOffset.x() + borderLeft() + paddingLeft()),
281 roundToInt(adjustedPaintOffset.y() + borderTop() + paddingTop()));
282 IntRect paintRect = paintInfo.rect;
284 IntSize widgetPaintOffset = paintLocation - widgetLocation;
285 // When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
286 // not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
287 if (!widgetPaintOffset.isZero()) {
288 paintInfo.context->translate(widgetPaintOffset);
289 paintRect.move(-widgetPaintOffset);
291 m_widget->paint(paintInfo.context, paintRect);
293 if (!widgetPaintOffset.isZero())
294 paintInfo.context->translate(-widgetPaintOffset);
296 if (m_widget->isFrameView()) {
297 FrameView* frameView = static_cast<FrameView*>(m_widget.get());
298 bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
299 if (paintInfo.overlapTestRequests && runOverlapTests) {
300 ASSERT(!paintInfo.overlapTestRequests->contains(this));
301 paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
306 if (style()->hasBorderRadius())
307 paintInfo.context->restore();
309 // Paint a partially transparent wash over selected widgets.
310 if (isSelected() && !document()->printing()) {
311 // FIXME: selectionRect() is in absolute, not painting coordinates.
312 paintInfo.context->fillRect(pixelSnappedIntRect(selectionRect()), selectionBackgroundColor(), style()->colorSpace());
315 if (hasLayer() && layer()->canResize())
316 layer()->paintResizer(paintInfo.context, roundedIntPoint(adjustedPaintOffset), paintInfo.rect);
319 void RenderWidget::setOverlapTestResult(bool isOverlapped)
322 ASSERT(m_widget->isFrameView());
323 static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
326 void RenderWidget::deref(RenderArena *arena)
328 if (--m_refCount <= 0)
329 arenaDelete(arena, this);
332 void RenderWidget::updateWidgetPosition()
334 if (!m_widget || !node()) // Check the node in case destroy() has been called.
337 bool boundsChanged = updateWidgetGeometry();
339 // if the frame bounds got changed, or if view needs layout (possibly indicating
340 // content size is wrong) we have to do a layout to set the right widget size
341 if (m_widget && m_widget->isFrameView()) {
342 FrameView* frameView = static_cast<FrameView*>(m_widget.get());
343 // Check the frame's page to make sure that the frame isn't in the process of being destroyed.
344 if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
349 void RenderWidget::widgetPositionsUpdated()
353 m_widget->widgetPositionsUpdated();
356 IntRect RenderWidget::windowClipRect() const
361 return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
364 void RenderWidget::setSelectionState(SelectionState state)
366 // The selection state for our containing block hierarchy is updated by the base class call.
367 RenderReplaced::setSelectionState(state);
370 m_widget->setIsSelected(isSelected());
373 void RenderWidget::clearWidget()
378 RenderWidget* RenderWidget::find(const Widget* widget)
380 return widgetRendererMap().get(widget);
383 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
385 bool hadResult = result.innerNode();
386 bool inside = RenderReplaced::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, action);
388 // Check to see if we are really over the widget itself (and not just in the border/padding area).
389 if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
390 result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
394 CursorDirective RenderWidget::getCursor(const LayoutPoint& point, Cursor& cursor) const
396 if (widget() && widget()->isPluginViewBase()) {
397 // A plug-in is responsible for setting the cursor when the pointer is over it.
398 return DoNotSetCursor;
400 return RenderReplaced::getCursor(point, cursor);
403 } // namespace WebCore