Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / Widget.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2013 Google Inc. All rights reserved.
4  *
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.
13  *
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.
25  */
26
27 #include "config.h"
28 #include "platform/Widget.h"
29
30
31 #include "wtf/Assertions.h"
32
33 namespace blink {
34
35 Widget::Widget()
36     : m_parent(nullptr)
37     , m_selfVisible(false)
38     , m_parentVisible(false)
39 {
40 }
41
42 Widget::~Widget()
43 {
44 #if !ENABLE(OILPAN)
45     ASSERT(!parent());
46 #endif
47 }
48
49 void Widget::trace(Visitor* visitor)
50 {
51     visitor->trace(m_parent);
52 }
53
54 void Widget::setParent(Widget* widget)
55 {
56     ASSERT(!widget || !m_parent);
57     if (!widget || !widget->isVisible())
58         setParentVisible(false);
59     m_parent = widget;
60     if (widget && widget->isVisible())
61         setParentVisible(true);
62 }
63
64 Widget* Widget::root() const
65 {
66     const Widget* top = this;
67     while (top->parent())
68         top = top->parent();
69     if (top->isFrameView())
70         return const_cast<Widget*>(static_cast<const Widget*>(top));
71     return 0;
72 }
73
74 IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const
75 {
76     if (const Widget* parentWidget = parent()) {
77         IntRect parentRect = parentWidget->convertFromContainingWindow(windowRect);
78         return convertFromContainingView(parentRect);
79     }
80     return windowRect;
81 }
82
83 IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
84 {
85     if (const Widget* parentWidget = parent()) {
86         IntRect parentRect = convertToContainingView(localRect);
87         return parentWidget->convertToContainingWindow(parentRect);
88     }
89     return localRect;
90 }
91
92 IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
93 {
94     if (const Widget* parentWidget = parent()) {
95         IntPoint parentPoint = parentWidget->convertFromContainingWindow(windowPoint);
96         return convertFromContainingView(parentPoint);
97     }
98     return windowPoint;
99 }
100
101 FloatPoint Widget::convertFromContainingWindow(const FloatPoint& windowPoint) const
102 {
103     // Widgets / windows are required to be IntPoint aligned, but we may need to convert
104     // FloatPoint values within them (eg. for event co-ordinates).
105     IntPoint flooredPoint = flooredIntPoint(windowPoint);
106     FloatPoint parentPoint = this->convertFromContainingWindow(flooredPoint);
107     FloatSize windowFraction = windowPoint - flooredPoint;
108     // Use linear interpolation handle any fractional value (eg. for iframes subject to a transform
109     // beyond just a simple translation).
110     // FIXME: Add FloatPoint variants of all co-ordinate space conversion APIs.
111     if (!windowFraction.isEmpty()) {
112         const int kFactor = 1000;
113         IntPoint parentLineEnd = this->convertFromContainingWindow(flooredPoint + roundedIntSize(windowFraction.scaledBy(kFactor)));
114         FloatSize parentFraction = (parentLineEnd - parentPoint).scaledBy(1.0f / kFactor);
115         parentPoint.move(parentFraction);
116     }
117     return parentPoint;
118 }
119
120 IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
121 {
122     if (const Widget* parentWidget = parent()) {
123         IntPoint parentPoint = convertToContainingView(localPoint);
124         return parentWidget->convertToContainingWindow(parentPoint);
125     }
126     return localPoint;
127 }
128
129 IntRect Widget::convertToContainingView(const IntRect& localRect) const
130 {
131     if (const Widget* parentWidget = parent()) {
132         IntRect parentRect(localRect);
133         parentRect.setLocation(parentWidget->convertChildToSelf(this, localRect.location()));
134         return parentRect;
135     }
136     return localRect;
137 }
138
139 IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
140 {
141     if (const Widget* parentWidget = parent()) {
142         IntRect localRect = parentRect;
143         localRect.setLocation(parentWidget->convertSelfToChild(this, localRect.location()));
144         return localRect;
145     }
146
147     return parentRect;
148 }
149
150 IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
151 {
152     if (const Widget* parentWidget = parent())
153         return parentWidget->convertChildToSelf(this, localPoint);
154
155     return localPoint;
156 }
157
158 IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
159 {
160     if (const Widget* parentWidget = parent())
161         return parentWidget->convertSelfToChild(this, parentPoint);
162
163     return parentPoint;
164 }
165
166 IntPoint Widget::convertChildToSelf(const Widget*, const IntPoint& point) const
167 {
168     return point;
169 }
170
171 IntPoint Widget::convertSelfToChild(const Widget*, const IntPoint& point) const
172 {
173     return point;
174 }
175
176 } // namespace blink