2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Simon Hausmann <hausmann@kde.org>
4 * (C) 2000 Stefan Schimanski (1Stein@gmx.de)
5 * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include "RenderEmbeddedObject.h"
28 #include "ChromeClient.h"
29 #include "CSSValueKeywords.h"
31 #include "FontSelector.h"
33 #include "FrameLoaderClient.h"
34 #include "GraphicsContext.h"
35 #include "HTMLEmbedElement.h"
36 #include "HTMLIFrameElement.h"
37 #include "HTMLNames.h"
38 #include "HTMLObjectElement.h"
39 #include "HTMLParamElement.h"
40 #include "LocalizedStrings.h"
41 #include "MIMETypeRegistry.h"
42 #include "MouseEvent.h"
44 #include "PaintInfo.h"
46 #include "PluginViewBase.h"
47 #include "RenderTheme.h"
48 #include "RenderView.h"
49 #include "RenderWidgetProtector.h"
54 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
55 #include "HTMLVideoElement.h"
60 using namespace HTMLNames;
62 static const float replacementTextRoundedRectHeight = 18;
63 static const float replacementTextRoundedRectLeftRightTextMargin = 6;
64 static const float replacementTextRoundedRectOpacity = 0.20f;
65 static const float replacementTextPressedRoundedRectOpacity = 0.65f;
66 static const float replacementTextRoundedRectRadius = 5;
67 static const float replacementTextTextOpacity = 0.55f;
68 static const float replacementTextPressedTextOpacity = 0.65f;
70 static const Color& replacementTextRoundedRectPressedColor()
72 static const Color lightGray(205, 205, 205);
76 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
78 , m_hasFallbackContent(false)
79 , m_showsMissingPluginIndicator(false)
80 , m_missingPluginIndicatorIsPressed(false)
81 , m_mouseDownWasInMissingPluginIndicator(false)
83 view()->frameView()->setIsVisuallyNonEmpty();
84 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
85 if (element->hasTagName(videoTag) || element->hasTagName(audioTag))
86 setHasIntrinsicSize();
90 RenderEmbeddedObject::~RenderEmbeddedObject()
93 frameView()->removeWidgetToUpdate(this);
96 #if USE(ACCELERATED_COMPOSITING)
97 bool RenderEmbeddedObject::requiresLayer() const
99 if (RenderPart::requiresLayer())
102 return allowsAcceleratedCompositing();
105 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
107 return widget() && widget()->isPluginViewBase() && static_cast<PluginViewBase*>(widget())->platformLayer();
111 void RenderEmbeddedObject::setShowsMissingPluginIndicator()
113 ASSERT(m_replacementText.isEmpty());
114 m_replacementText = missingPluginText();
115 m_showsMissingPluginIndicator = true;
118 void RenderEmbeddedObject::setShowsCrashedPluginIndicator()
120 ASSERT(m_replacementText.isEmpty());
121 m_replacementText = crashedPluginText();
124 bool RenderEmbeddedObject::pluginCrashedOrWasMissing() const
126 return !m_replacementText.isNull();
129 void RenderEmbeddedObject::setMissingPluginIndicatorIsPressed(bool pressed)
131 if (m_missingPluginIndicatorIsPressed == pressed)
134 m_missingPluginIndicatorIsPressed = pressed;
138 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
140 if (pluginCrashedOrWasMissing()) {
141 RenderReplaced::paint(paintInfo, paintOffset);
145 RenderPart::paint(paintInfo, paintOffset);
148 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
150 if (!pluginCrashedOrWasMissing())
153 if (paintInfo.phase == PaintPhaseSelection)
156 GraphicsContext* context = paintInfo.context;
157 if (context->paintingDisabled())
160 FloatRect contentRect;
162 FloatRect replacementTextRect;
166 if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
169 GraphicsContextStateSaver stateSaver(*context);
170 context->clip(contentRect);
171 context->setAlpha(m_missingPluginIndicatorIsPressed ? replacementTextPressedRoundedRectOpacity : replacementTextRoundedRectOpacity);
172 context->setFillColor(m_missingPluginIndicatorIsPressed ? replacementTextRoundedRectPressedColor() : Color::white, style()->colorSpace());
173 context->fillPath(path);
175 const FontMetrics& fontMetrics = font.fontMetrics();
176 float labelX = roundf(replacementTextRect.location().x() + (replacementTextRect.size().width() - textWidth) / 2);
177 float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent());
178 context->setAlpha(m_missingPluginIndicatorIsPressed ? replacementTextPressedTextOpacity : replacementTextTextOpacity);
179 context->setFillColor(Color::black, style()->colorSpace());
180 context->drawBidiText(font, run, FloatPoint(labelX, labelY));
183 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth)
185 contentRect = contentBoxRect();
186 contentRect.moveBy(accumulatedOffset);
188 FontDescription fontDescription;
189 RenderTheme::defaultTheme()->systemFont(CSSValueWebkitSmallControl, fontDescription);
190 fontDescription.setWeight(FontWeightBold);
191 Settings* settings = document()->settings();
195 fontDescription.setRenderingMode(settings->fontRenderingMode());
196 fontDescription.setComputedSize(fontDescription.specifiedSize());
197 font = Font(fontDescription, 0, 0);
200 run = TextRun(m_replacementText);
201 textWidth = font.width(run);
203 replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftRightTextMargin * 2, replacementTextRoundedRectHeight));
204 float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
205 float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
206 replacementTextRect.setLocation(FloatPoint(x, y));
208 path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
213 void RenderEmbeddedObject::layout()
215 ASSERT(needsLayout());
217 computeLogicalWidth();
218 computeLogicalHeight();
220 RenderPart::layout();
223 addBoxShadowAndBorderOverflow();
225 updateLayerTransform();
227 if (!widget() && frameView())
228 frameView()->addWidgetToUpdate(this);
230 setNeedsLayout(false);
233 void RenderEmbeddedObject::viewCleared()
235 // This is required for <object> elements whose contents are rendered by WebCore (e.g. src="foo.html").
236 if (node() && widget() && widget()->isFrameView()) {
237 FrameView* view = static_cast<FrameView*>(widget());
238 int marginWidth = -1;
239 int marginHeight = -1;
240 if (node()->hasTagName(iframeTag)) {
241 HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(node());
242 marginWidth = frame->marginWidth();
243 marginHeight = frame->marginHeight();
245 if (marginWidth != -1)
246 view->setMarginWidth(marginWidth);
247 if (marginHeight != -1)
248 view->setMarginHeight(marginHeight);
252 bool RenderEmbeddedObject::isInMissingPluginIndicator(MouseEvent* event)
254 FloatRect contentRect;
256 FloatRect replacementTextRect;
260 if (!getReplacementTextGeometry(IntPoint(), contentRect, path, replacementTextRect, font, run, textWidth))
263 return path.contains(absoluteToLocal(event->absoluteLocation(), false, true));
266 void RenderEmbeddedObject::handleMissingPluginIndicatorEvent(Event* event)
268 if (Page* page = document()->page()) {
269 if (!page->chrome()->client()->shouldMissingPluginMessageBeButton())
273 if (!event->isMouseEvent())
276 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
277 HTMLPlugInElement* element = static_cast<HTMLPlugInElement*>(node());
278 if (event->type() == eventNames().mousedownEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
279 m_mouseDownWasInMissingPluginIndicator = isInMissingPluginIndicator(mouseEvent);
280 if (m_mouseDownWasInMissingPluginIndicator) {
281 if (Frame* frame = document()->frame()) {
282 frame->eventHandler()->setCapturingMouseEventsNode(element);
283 element->setIsCapturingMouseEvents(true);
285 setMissingPluginIndicatorIsPressed(true);
287 event->setDefaultHandled();
289 if (event->type() == eventNames().mouseupEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
290 if (m_missingPluginIndicatorIsPressed) {
291 if (Frame* frame = document()->frame()) {
292 frame->eventHandler()->setCapturingMouseEventsNode(0);
293 element->setIsCapturingMouseEvents(false);
295 setMissingPluginIndicatorIsPressed(false);
297 if (m_mouseDownWasInMissingPluginIndicator && isInMissingPluginIndicator(mouseEvent)) {
298 if (Page* page = document()->page())
299 page->chrome()->client()->missingPluginButtonClicked(element);
301 m_mouseDownWasInMissingPluginIndicator = false;
302 event->setDefaultHandled();
304 if (event->type() == eventNames().mousemoveEvent) {
305 setMissingPluginIndicatorIsPressed(m_mouseDownWasInMissingPluginIndicator && isInMissingPluginIndicator(mouseEvent));
306 event->setDefaultHandled();