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 static unsigned widgetHierarchyUpdateSuspendCount;
52 typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
54 static WidgetToParentMap& widgetNewParentMap()
56 DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
60 void RenderWidget::suspendWidgetHierarchyUpdates()
62 widgetHierarchyUpdateSuspendCount++;
65 void RenderWidget::resumeWidgetHierarchyUpdates()
67 ASSERT(widgetHierarchyUpdateSuspendCount);
68 if (widgetHierarchyUpdateSuspendCount == 1) {
69 WidgetToParentMap map = widgetNewParentMap();
70 widgetNewParentMap().clear();
71 WidgetToParentMap::iterator end = map.end();
72 for (WidgetToParentMap::iterator it = map.begin(); it != end; ++it) {
73 Widget* child = it->first.get();
74 ScrollView* currentParent = child->parent();
75 FrameView* newParent = it->second;
76 if (newParent != currentParent) {
78 currentParent->removeChild(child);
80 newParent->addChild(child);
84 widgetHierarchyUpdateSuspendCount--;
87 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
89 if (!widgetHierarchyUpdateSuspendCount) {
91 parent->addChild(child);
93 child->removeFromParent();
96 widgetNewParentMap().set(child, parent);
99 RenderWidget::RenderWidget(Node* node)
100 : RenderReplaced(node)
102 , m_frameView(node->document()->view())
103 // Reference counting is used to prevent the widget from being
104 // destroyed while inside the Widget code, which might not be
105 // able to handle that.
108 view()->addWidget(this);
111 void RenderWidget::willBeDestroyed()
113 if (RenderView* v = view())
114 v->removeWidget(this);
116 if (AXObjectCache::accessibilityEnabled()) {
117 document()->axObjectCache()->childrenChanged(this->parent());
118 document()->axObjectCache()->remove(this);
123 RenderReplaced::willBeDestroyed();
126 void RenderWidget::destroy()
130 // Grab the arena from node()->document()->renderArena() before clearing the node pointer.
131 // Clear the node before deref-ing, as this may be deleted when deref is called.
132 RenderArena* arena = renderArena();
137 RenderWidget::~RenderWidget()
139 ASSERT(m_refCount <= 0);
143 // Widgets are always placed on integer boundaries, so rounding the size is actually
144 // the desired behavior. This function is here because it's otherwise seldom what we
145 // want to do with a LayoutRect.
146 static inline IntRect roundedIntRect(const LayoutRect& rect)
148 return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size()));
151 bool RenderWidget::setWidgetGeometry(const LayoutRect& frame)
156 IntRect clipRect = roundedIntRect(enclosingLayer()->childrenClipRect());
157 bool clipChanged = m_clipRect != clipRect;
158 bool boundsChanged = m_widget->frameRect() != frame;
160 if (!boundsChanged && !clipChanged)
163 m_clipRect = clipRect;
165 RenderWidgetProtector protector(this);
166 RefPtr<Node> protectedNode(node());
167 m_widget->setFrameRect(roundedIntRect(frame));
169 #if USE(ACCELERATED_COMPOSITING)
170 if (hasLayer() && layer()->isComposited())
171 layer()->backing()->updateAfterWidgetResize();
174 return boundsChanged;
177 bool RenderWidget::updateWidgetGeometry()
179 IntRect contentBox = pixelSnappedIntRect(contentBoxRect());
180 if (!m_widget->transformsAffectFrameRect())
181 return setWidgetGeometry(absoluteContentBox());
183 IntRect absoluteContentBox(localToAbsoluteQuad(FloatQuad(contentBox)).boundingBox());
184 if (m_widget->isFrameView()) {
185 contentBox.setLocation(absoluteContentBox.location());
186 return setWidgetGeometry(contentBox);
189 return setWidgetGeometry(absoluteContentBox);
192 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
194 if (widget == m_widget)
198 moveWidgetToParentSoon(m_widget.get(), 0);
199 widgetRendererMap().remove(m_widget.get());
204 widgetRendererMap().add(m_widget.get(), this);
205 // If we've already received a layout, apply the calculated space to the
206 // widget immediately, but we have to have really been fully constructed (with a non-null
210 updateWidgetGeometry();
212 if (style()->visibility() != VISIBLE)
219 moveWidgetToParentSoon(m_widget.get(), m_frameView);
223 void RenderWidget::layout()
225 ASSERT(needsLayout());
227 setNeedsLayout(false);
230 void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
232 RenderReplaced::styleDidChange(diff, oldStyle);
234 if (style()->visibility() != VISIBLE)
241 void RenderWidget::notifyWidget(WidgetNotification notification)
244 m_widget->notifyWidget(notification);
247 void RenderWidget::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
249 if (!shouldPaint(paintInfo, paintOffset))
252 LayoutPoint adjustedPaintOffset = paintOffset + location();
254 if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
255 paintBoxDecorations(paintInfo, adjustedPaintOffset);
257 if (paintInfo.phase == PaintPhaseMask) {
258 paintMask(paintInfo, adjustedPaintOffset);
262 if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && hasOutline())
263 paintOutline(paintInfo.context, LayoutRect(adjustedPaintOffset, size()));
265 if (!m_frameView || paintInfo.phase != PaintPhaseForeground)
269 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
270 paintCustomHighlight(paintOffset, style()->highlight(), true);
273 if (style()->hasBorderRadius()) {
274 LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
276 if (borderRect.isEmpty())
279 // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
280 paintInfo.context->save();
281 paintInfo.context->addRoundedRectClip(style()->getRoundedBorderFor(borderRect, view()));
285 // Tell the widget to paint now. This is the only time the widget is allowed
286 // to paint itself. That way it will composite properly with z-indexed layers.
287 IntPoint widgetLocation = m_widget->frameRect().location();
288 IntPoint paintLocation(roundToInt(adjustedPaintOffset.x() + borderLeft() + paddingLeft()),
289 roundToInt(adjustedPaintOffset.y() + borderTop() + paddingTop()));
290 IntRect paintRect = paintInfo.rect;
292 IntSize widgetPaintOffset = paintLocation - widgetLocation;
293 // When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
294 // not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
295 if (!widgetPaintOffset.isZero()) {
296 paintInfo.context->translate(widgetPaintOffset);
297 paintRect.move(-widgetPaintOffset);
299 m_widget->paint(paintInfo.context, paintRect);
301 if (!widgetPaintOffset.isZero())
302 paintInfo.context->translate(-widgetPaintOffset);
304 if (m_widget->isFrameView()) {
305 FrameView* frameView = static_cast<FrameView*>(m_widget.get());
306 bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
307 if (paintInfo.overlapTestRequests && runOverlapTests) {
308 ASSERT(!paintInfo.overlapTestRequests->contains(this));
309 paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
314 if (style()->hasBorderRadius())
315 paintInfo.context->restore();
317 // Paint a partially transparent wash over selected widgets.
318 if (isSelected() && !document()->printing()) {
319 // FIXME: selectionRect() is in absolute, not painting coordinates.
320 paintInfo.context->fillRect(pixelSnappedIntRect(selectionRect()), selectionBackgroundColor(), style()->colorSpace());
324 void RenderWidget::setOverlapTestResult(bool isOverlapped)
327 ASSERT(m_widget->isFrameView());
328 static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
331 void RenderWidget::deref(RenderArena *arena)
333 if (--m_refCount <= 0)
334 arenaDelete(arena, this);
337 void RenderWidget::updateWidgetPosition()
339 if (!m_widget || !node()) // Check the node in case destroy() has been called.
342 bool boundsChanged = updateWidgetGeometry();
344 // if the frame bounds got changed, or if view needs layout (possibly indicating
345 // content size is wrong) we have to do a layout to set the right widget size
346 if (m_widget && m_widget->isFrameView()) {
347 FrameView* frameView = static_cast<FrameView*>(m_widget.get());
348 // Check the frame's page to make sure that the frame isn't in the process of being destroyed.
349 if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
354 void RenderWidget::widgetPositionsUpdated()
358 m_widget->widgetPositionsUpdated();
361 IntRect RenderWidget::windowClipRect() const
366 return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
369 void RenderWidget::setSelectionState(SelectionState state)
371 // The selection state for our containing block hierarchy is updated by the base class call.
372 RenderReplaced::setSelectionState(state);
375 m_widget->setIsSelected(isSelected());
378 void RenderWidget::clearWidget()
383 RenderWidget* RenderWidget::find(const Widget* widget)
385 return widgetRendererMap().get(widget);
388 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
390 bool hadResult = result.innerNode();
391 bool inside = RenderReplaced::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, action);
393 // Check to see if we are really over the widget itself (and not just in the border/padding area).
394 if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
395 result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
399 CursorDirective RenderWidget::getCursor(const LayoutPoint& point, Cursor& cursor) const
401 if (widget() && widget()->isPluginViewBase()) {
402 // A plug-in is responsible for setting the cursor when the pointer is over it.
403 return DoNotSetCursor;
405 return RenderReplaced::getCursor(point, cursor);
408 } // namespace WebCore