Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / Widget.cpp
blob186e3d0d688185d724d57290c502571f061574b8
1 /*
2 * Copyright (C) 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "config.h"
28 #include "platform/Widget.h"
30 #include "wtf/Assertions.h"
32 namespace blink {
34 Widget::Widget()
35 : m_parent(nullptr)
36 , m_selfVisible(false)
37 , m_parentVisible(false)
41 Widget::~Widget()
43 #if !ENABLE(OILPAN)
44 ASSERT(!parent());
45 #endif
48 DEFINE_TRACE(Widget)
50 visitor->trace(m_parent);
53 void Widget::setParent(Widget* widget)
55 ASSERT(!widget || !m_parent);
56 if (!widget || !widget->isVisible())
57 setParentVisible(false);
58 m_parent = widget;
59 if (widget && widget->isVisible())
60 setParentVisible(true);
63 Widget* Widget::root() const
65 const Widget* top = this;
66 while (top->parent())
67 top = top->parent();
68 if (top->isFrameView())
69 return const_cast<Widget*>(static_cast<const Widget*>(top));
70 return 0;
73 IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const
75 if (const Widget* parentWidget = parent()) {
76 IntRect parentRect = parentWidget->convertFromContainingWindow(windowRect);
77 return convertFromContainingView(parentRect);
79 return windowRect;
82 IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
84 if (const Widget* parentWidget = parent()) {
85 IntRect parentRect = convertToContainingView(localRect);
86 return parentWidget->convertToContainingWindow(parentRect);
88 return localRect;
91 IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
93 if (const Widget* parentWidget = parent()) {
94 IntPoint parentPoint = parentWidget->convertFromContainingWindow(windowPoint);
95 return convertFromContainingView(parentPoint);
97 return windowPoint;
100 FloatPoint Widget::convertFromContainingWindow(const FloatPoint& windowPoint) const
102 // Widgets / windows are required to be IntPoint aligned, but we may need to convert
103 // FloatPoint values within them (eg. for event co-ordinates).
104 IntPoint flooredPoint = flooredIntPoint(windowPoint);
105 FloatPoint parentPoint = this->convertFromContainingWindow(flooredPoint);
106 FloatSize windowFraction = windowPoint - flooredPoint;
107 // Use linear interpolation handle any fractional value (eg. for iframes subject to a transform
108 // beyond just a simple translation).
109 // FIXME: Add FloatPoint variants of all co-ordinate space conversion APIs.
110 if (!windowFraction.isEmpty()) {
111 const int kFactor = 1000;
112 IntPoint parentLineEnd = this->convertFromContainingWindow(flooredPoint + roundedIntSize(windowFraction.scaledBy(kFactor)));
113 FloatSize parentFraction = (parentLineEnd - parentPoint).scaledBy(1.0f / kFactor);
114 parentPoint.move(parentFraction);
116 return parentPoint;
119 IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
121 if (const Widget* parentWidget = parent()) {
122 IntPoint parentPoint = convertToContainingView(localPoint);
123 return parentWidget->convertToContainingWindow(parentPoint);
125 return localPoint;
128 IntRect Widget::convertToContainingView(const IntRect& localRect) const
130 if (const Widget* parentWidget = parent()) {
131 IntRect parentRect(localRect);
132 parentRect.setLocation(parentWidget->convertChildToSelf(this, localRect.location()));
133 return parentRect;
135 return localRect;
138 IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
140 if (const Widget* parentWidget = parent()) {
141 IntRect localRect = parentRect;
142 localRect.setLocation(parentWidget->convertSelfToChild(this, localRect.location()));
143 return localRect;
146 return parentRect;
149 IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
151 if (const Widget* parentWidget = parent())
152 return parentWidget->convertChildToSelf(this, localPoint);
154 return localPoint;
157 IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
159 if (const Widget* parentWidget = parent())
160 return parentWidget->convertSelfToChild(this, parentPoint);
162 return parentPoint;
165 IntPoint Widget::convertChildToSelf(const Widget*, const IntPoint& point) const
167 return point;
170 IntPoint Widget::convertSelfToChild(const Widget*, const IntPoint& point) const
172 return point;
175 } // namespace blink