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
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.
28 #include "platform/Widget.h"
31 #include "wtf/Assertions.h"
37 , m_selfVisible(false)
38 , m_parentVisible(false)
47 void Widget::setParent(Widget* widget)
49 ASSERT(!widget || !m_parent);
50 if (!widget || !widget->isVisible())
51 setParentVisible(false);
53 if (widget && widget->isVisible())
54 setParentVisible(true);
57 Widget* Widget::root() const
59 const Widget* top = this;
62 if (top->isFrameView())
63 return const_cast<Widget*>(static_cast<const Widget*>(top));
67 IntRect Widget::convertFromRootView(const IntRect& rootRect) const
69 if (const Widget* parentWidget = parent()) {
70 IntRect parentRect = parentWidget->convertFromRootView(rootRect);
71 return convertFromContainingView(parentRect);
76 IntRect Widget::convertToRootView(const IntRect& localRect) const
78 if (const Widget* parentWidget = parent()) {
79 IntRect parentRect = convertToContainingView(localRect);
80 return parentWidget->convertToRootView(parentRect);
85 IntPoint Widget::convertFromRootView(const IntPoint& rootPoint) const
87 if (const Widget* parentWidget = parent()) {
88 IntPoint parentPoint = parentWidget->convertFromRootView(rootPoint);
89 return convertFromContainingView(parentPoint);
94 IntPoint Widget::convertToRootView(const IntPoint& localPoint) const
96 if (const Widget* parentWidget = parent()) {
97 IntPoint parentPoint = convertToContainingView(localPoint);
98 return parentWidget->convertToRootView(parentPoint);
103 IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const
105 if (const Widget* parentWidget = parent()) {
106 IntRect parentRect = parentWidget->convertFromContainingWindow(windowRect);
107 return convertFromContainingView(parentRect);
112 IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
114 if (const Widget* parentWidget = parent()) {
115 IntRect parentRect = convertToContainingView(localRect);
116 return parentWidget->convertToContainingWindow(parentRect);
121 IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
123 if (const Widget* parentWidget = parent()) {
124 IntPoint parentPoint = parentWidget->convertFromContainingWindow(windowPoint);
125 return convertFromContainingView(parentPoint);
130 FloatPoint Widget::convertFromContainingWindow(const FloatPoint& windowPoint) const
132 // Widgets / windows are required to be IntPoint aligned, but we may need to convert
133 // FloatPoint values within them (eg. for event co-ordinates).
134 IntPoint flooredPoint = flooredIntPoint(windowPoint);
135 FloatPoint parentPoint = this->convertFromContainingWindow(flooredPoint);
136 FloatSize windowFraction = windowPoint - flooredPoint;
137 // Use linear interpolation handle any fractional value (eg. for iframes subject to a transform
138 // beyond just a simple translation).
139 // FIXME: Add FloatPoint variants of all co-ordinate space conversion APIs.
140 if (!windowFraction.isEmpty()) {
141 const int kFactor = 1000;
142 IntPoint parentLineEnd = this->convertFromContainingWindow(flooredPoint + roundedIntSize(windowFraction.scaledBy(kFactor)));
143 FloatSize parentFraction = (parentLineEnd - parentPoint).scaledBy(1.0f / kFactor);
144 parentPoint.move(parentFraction);
149 IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
151 if (const Widget* parentWidget = parent()) {
152 IntPoint parentPoint = convertToContainingView(localPoint);
153 return parentWidget->convertToContainingWindow(parentPoint);
158 IntRect Widget::convertToContainingView(const IntRect& localRect) const
160 if (const Widget* parentWidget = parent()) {
161 IntRect parentRect(localRect);
162 parentRect.setLocation(parentWidget->convertChildToSelf(this, localRect.location()));
168 IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
170 if (const Widget* parentWidget = parent()) {
171 IntRect localRect = parentRect;
172 localRect.setLocation(parentWidget->convertSelfToChild(this, localRect.location()));
179 IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
181 if (const Widget* parentWidget = parent())
182 return parentWidget->convertChildToSelf(this, localPoint);
187 IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
189 if (const Widget* parentWidget = parent())
190 return parentWidget->convertSelfToChild(this, parentPoint);
195 IntPoint Widget::convertChildToSelf(const Widget*, const IntPoint& point) const
200 IntPoint Widget::convertSelfToChild(const Widget*, const IntPoint& point) const