tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderWidget.cpp
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22
23 #include "config.h"
24 #include "RenderWidget.h"
25
26 #include "AXObjectCache.h"
27 #include "AnimationController.h"
28 #include "Frame.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"
35
36 #if USE(ACCELERATED_COMPOSITING)
37 #include "RenderLayerBacking.h"
38 #endif
39
40 using namespace std;
41
42 namespace WebCore {
43
44 static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
45 {
46     static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
47     return *staticWidgetRendererMap;
48 }
49
50 static unsigned widgetHierarchyUpdateSuspendCount;
51
52 typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
53
54 static WidgetToParentMap& widgetNewParentMap()
55 {
56     DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
57     return map;
58 }
59
60 void RenderWidget::suspendWidgetHierarchyUpdates()
61 {
62     widgetHierarchyUpdateSuspendCount++;
63 }
64
65 void RenderWidget::resumeWidgetHierarchyUpdates()
66 {
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) {
77                 if (currentParent)
78                     currentParent->removeChild(child);
79                 if (newParent)
80                     newParent->addChild(child);
81             }
82         }
83     }
84     widgetHierarchyUpdateSuspendCount--;
85 }
86
87 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
88 {
89     if (!widgetHierarchyUpdateSuspendCount) {
90         if (parent)
91             parent->addChild(child);
92         else
93             child->removeFromParent();
94         return;
95     }
96     widgetNewParentMap().set(child, parent);
97 }
98
99 RenderWidget::RenderWidget(Node* node)
100     : RenderReplaced(node)
101     , m_widget(0)
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.
106     , m_refCount(1)
107 {
108     view()->addWidget(this);
109 }
110
111 void RenderWidget::willBeDestroyed()
112 {
113     if (RenderView* v = view())
114         v->removeWidget(this);
115     
116     if (AXObjectCache::accessibilityEnabled()) {
117         document()->axObjectCache()->childrenChanged(this->parent());
118         document()->axObjectCache()->remove(this);
119     }
120
121     setWidget(0);
122
123     RenderReplaced::willBeDestroyed();
124 }
125
126 void RenderWidget::destroy()
127 {
128     willBeDestroyed();
129
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();
133     setNode(0);
134     deref(arena);
135 }
136
137 RenderWidget::~RenderWidget()
138 {
139     ASSERT(m_refCount <= 0);
140     clearWidget();
141 }
142
143 bool RenderWidget::setWidgetGeometry(const IntRect& frame)
144 {
145     if (!node())
146         return false;
147
148     IntRect clipRect = enclosingLayer()->childrenClipRect();
149     bool clipChanged = m_clipRect != clipRect;
150     bool boundsChanged = m_widget->frameRect() != frame;
151
152     if (!boundsChanged && !clipChanged)
153         return false;
154
155     m_clipRect = clipRect;
156
157     RenderWidgetProtector protector(this);
158     RefPtr<Node> protectedNode(node());
159     m_widget->setFrameRect(frame);
160     
161 #if USE(ACCELERATED_COMPOSITING)
162     if (hasLayer() && layer()->isComposited())
163         layer()->backing()->updateAfterWidgetResize();
164 #endif
165     
166     return boundsChanged;
167 }
168
169 bool RenderWidget::updateWidgetGeometry()
170 {
171     IntRect contentBox = contentBoxRect();
172     if (!m_widget->transformsAffectFrameRect())
173         return setWidgetGeometry(absoluteContentBox());
174
175     IntRect absoluteContentBox = IntRect(localToAbsoluteQuad(FloatQuad(contentBox)).boundingBox());
176     if (m_widget->isFrameView()) {
177         contentBox.setLocation(absoluteContentBox.location());
178         return setWidgetGeometry(contentBox);
179     }
180
181     return setWidgetGeometry(absoluteContentBox);
182 }
183
184 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
185 {
186     if (widget == m_widget)
187         return;
188
189     if (m_widget) {
190         moveWidgetToParentSoon(m_widget.get(), 0);
191         widgetRendererMap().remove(m_widget.get());
192         clearWidget();
193     }
194     m_widget = widget;
195     if (m_widget) {
196         widgetRendererMap().add(m_widget.get(), this);
197         // If we've already received a layout, apply the calculated space to the
198         // widget immediately, but we have to have really been fully constructed (with a non-null
199         // style pointer).
200         if (style()) {
201             if (!needsLayout())
202                 updateWidgetGeometry();
203
204             if (style()->visibility() != VISIBLE)
205                 m_widget->hide();
206             else {
207                 m_widget->show();
208                 repaint();
209             }
210         }
211         moveWidgetToParentSoon(m_widget.get(), m_frameView);
212     }
213 }
214
215 void RenderWidget::layout()
216 {
217     ASSERT(needsLayout());
218
219     setNeedsLayout(false);
220 }
221
222 void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
223 {
224     RenderReplaced::styleDidChange(diff, oldStyle);
225     if (m_widget) {
226         if (style()->visibility() != VISIBLE)
227             m_widget->hide();
228         else
229             m_widget->show();
230     }
231 }
232
233 void RenderWidget::notifyWidget(WidgetNotification notification)
234 {
235     if (m_widget)
236         m_widget->notifyWidget(notification);
237 }
238
239 void RenderWidget::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
240 {
241     if (!shouldPaint(paintInfo, paintOffset))
242         return;
243
244     LayoutPoint adjustedPaintOffset = paintOffset + location();
245
246     if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
247         paintBoxDecorations(paintInfo, adjustedPaintOffset);
248
249     if (paintInfo.phase == PaintPhaseMask) {
250         paintMask(paintInfo, adjustedPaintOffset);
251         return;
252     }
253
254     if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && hasOutline())
255         paintOutline(paintInfo.context, LayoutRect(adjustedPaintOffset, size()));
256
257     if (!m_frameView || paintInfo.phase != PaintPhaseForeground)
258         return;
259
260 #if PLATFORM(MAC)
261     if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
262         paintCustomHighlight(paintOffset, style()->highlight(), true);
263 #endif
264
265     if (style()->hasBorderRadius()) {
266         LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
267
268         if (borderRect.isEmpty())
269             return;
270
271         // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
272         paintInfo.context->save();
273         paintInfo.context->addRoundedRectClip(style()->getRoundedBorderFor(borderRect));
274     }
275
276     if (m_widget) {
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         LayoutPoint widgetLocation = m_widget->frameRect().location();
280         LayoutPoint paintLocation(adjustedPaintOffset.x() + borderLeft() + paddingLeft(), adjustedPaintOffset.y() + borderTop() + paddingTop());
281         LayoutRect paintRect = paintInfo.rect;
282
283         LayoutSize widgetPaintOffset = paintLocation - widgetLocation;
284         // When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
285         // not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
286         if (!widgetPaintOffset.isZero()) {
287             paintInfo.context->translate(widgetPaintOffset);
288             paintRect.move(-widgetPaintOffset);
289         }
290         m_widget->paint(paintInfo.context, paintRect);
291
292         if (!widgetPaintOffset.isZero())
293             paintInfo.context->translate(-widgetPaintOffset);
294
295         if (m_widget->isFrameView()) {
296             FrameView* frameView = static_cast<FrameView*>(m_widget.get());
297             bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
298             if (paintInfo.overlapTestRequests && runOverlapTests) {
299                 ASSERT(!paintInfo.overlapTestRequests->contains(this));
300                 paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
301             }
302          }
303     }
304
305     if (style()->hasBorderRadius())
306         paintInfo.context->restore();
307
308     // Paint a partially transparent wash over selected widgets.
309     if (isSelected() && !document()->printing()) {
310         // FIXME: selectionRect() is in absolute, not painting coordinates.
311         paintInfo.context->fillRect(selectionRect(), selectionBackgroundColor(), style()->colorSpace());
312     }
313 }
314
315 void RenderWidget::setOverlapTestResult(bool isOverlapped)
316 {
317     ASSERT(m_widget);
318     ASSERT(m_widget->isFrameView());
319     static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
320 }
321
322 void RenderWidget::deref(RenderArena *arena)
323 {
324     if (--m_refCount <= 0)
325         arenaDelete(arena, this);
326 }
327
328 void RenderWidget::updateWidgetPosition()
329 {
330     if (!m_widget || !node()) // Check the node in case destroy() has been called.
331         return;
332
333     bool boundsChanged = updateWidgetGeometry();
334     
335     // if the frame bounds got changed, or if view needs layout (possibly indicating
336     // content size is wrong) we have to do a layout to set the right widget size
337     if (m_widget && m_widget->isFrameView()) {
338         FrameView* frameView = static_cast<FrameView*>(m_widget.get());
339         // Check the frame's page to make sure that the frame isn't in the process of being destroyed.
340         if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
341             frameView->layout();
342     }
343 }
344
345 void RenderWidget::widgetPositionsUpdated()
346 {
347     if (!m_widget)
348         return;
349     m_widget->widgetPositionsUpdated();
350 }
351
352 IntRect RenderWidget::windowClipRect() const
353 {
354     if (!m_frameView)
355         return IntRect();
356
357     return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
358 }
359
360 void RenderWidget::setSelectionState(SelectionState state)
361 {
362     if (selectionState() != state) {
363         RenderReplaced::setSelectionState(state);
364         if (m_widget)
365             m_widget->setIsSelected(isSelected());
366     }
367 }
368
369 void RenderWidget::clearWidget()
370 {
371     m_widget = 0;
372 }
373
374 RenderWidget* RenderWidget::find(const Widget* widget)
375 {
376     return widgetRendererMap().get(widget);
377 }
378
379 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
380 {
381     bool hadResult = result.innerNode();
382     bool inside = RenderReplaced::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, action);
383     
384     // Check to see if we are really over the widget itself (and not just in the border/padding area).
385     if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
386         result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
387     return inside;
388 }
389
390 CursorDirective RenderWidget::getCursor(const LayoutPoint& point, Cursor& cursor) const
391 {
392     if (widget() && widget()->isPluginViewBase()) {
393         // A plug-in is responsible for setting the cursor when the pointer is over it.
394         return DoNotSetCursor;
395     }
396     return RenderReplaced::getCursor(point, cursor);
397 }
398
399 } // namespace WebCore