1/*
2 * Copyright (C) 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "Widget.h"
28
29#include "IntRect.h"
30#include "ScrollView.h"
31
32#include <wtf/Assertions.h>
33
34namespace WebCore {
35
36void Widget::init(PlatformWidget widget)
37{
38    m_parent = 0;
39    m_selfVisible = false;
40    m_parentVisible = false;
41    m_widget = widget;
42    if (m_widget)
43        retainPlatformWidget();
44}
45
46void Widget::setParent(ScrollView* view)
47{
48    ASSERT(!view || !m_parent);
49    if (!view || !view->isVisible())
50        setParentVisible(false);
51    m_parent = view;
52    if (view && view->isVisible())
53        setParentVisible(true);
54}
55
56ScrollView* Widget::root() const
57{
58    const Widget* top = this;
59    while (top->parent())
60        top = top->parent();
61    if (top->isFrameView())
62        return const_cast<ScrollView*>(static_cast<const ScrollView*>(top));
63    return 0;
64}
65
66void Widget::removeFromParent()
67{
68    if (parent())
69        parent()->removeChild(this);
70}
71
72IntRect Widget::convertFromRootView(const IntRect& rootRect) const
73{
74    if (const ScrollView* parentScrollView = parent()) {
75        IntRect parentRect = parentScrollView->convertFromRootView(rootRect);
76        return convertFromContainingView(parentRect);
77    }
78    return rootRect;
79}
80
81IntRect Widget::convertToRootView(const IntRect& localRect) const
82{
83    if (const ScrollView* parentScrollView = parent()) {
84        IntRect parentRect = convertToContainingView(localRect);
85        return parentScrollView->convertToRootView(parentRect);
86    }
87    return localRect;
88}
89
90IntPoint Widget::convertFromRootView(const IntPoint& rootPoint) const
91{
92    if (const ScrollView* parentScrollView = parent()) {
93        IntPoint parentPoint = parentScrollView->convertFromRootView(rootPoint);
94        return convertFromContainingView(parentPoint);
95    }
96    return rootPoint;
97}
98
99IntPoint Widget::convertToRootView(const IntPoint& localPoint) const
100{
101    if (const ScrollView* parentScrollView = parent()) {
102        IntPoint parentPoint = convertToContainingView(localPoint);
103        return parentScrollView->convertToRootView(parentPoint);
104    }
105    return localPoint;
106}
107
108IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const
109{
110    if (const ScrollView* parentScrollView = parent()) {
111        IntRect parentRect = parentScrollView->convertFromContainingWindow(windowRect);
112        return convertFromContainingView(parentRect);
113    }
114    return convertFromContainingWindowToRoot(this, windowRect);
115}
116
117IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
118{
119    if (const ScrollView* parentScrollView = parent()) {
120        IntRect parentRect = convertToContainingView(localRect);
121        return parentScrollView->convertToContainingWindow(parentRect);
122    }
123    return convertFromRootToContainingWindow(this, localRect);
124}
125
126IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
127{
128    if (const ScrollView* parentScrollView = parent()) {
129        IntPoint parentPoint = parentScrollView->convertFromContainingWindow(windowPoint);
130        return convertFromContainingView(parentPoint);
131    }
132    return convertFromContainingWindowToRoot(this, windowPoint);
133}
134
135IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
136{
137    if (const ScrollView* parentScrollView = parent()) {
138        IntPoint parentPoint = convertToContainingView(localPoint);
139        return parentScrollView->convertToContainingWindow(parentPoint);
140    }
141    return convertFromRootToContainingWindow(this, localPoint);
142}
143
144#if !PLATFORM(MAC)
145IntRect Widget::convertFromRootToContainingWindow(const Widget*, const IntRect& rect)
146{
147    return rect;
148}
149
150IntRect Widget::convertFromContainingWindowToRoot(const Widget*, const IntRect& rect)
151{
152    return rect;
153}
154
155IntPoint Widget::convertFromRootToContainingWindow(const Widget*, const IntPoint& point)
156{
157    return point;
158}
159
160IntPoint Widget::convertFromContainingWindowToRoot(const Widget*, const IntPoint& point)
161{
162    return point;
163}
164
165PlatformDisplayID Widget::windowDisplayID() const
166{
167    return 0;
168}
169#endif
170
171IntRect Widget::convertToContainingView(const IntRect& localRect) const
172{
173    if (const ScrollView* parentScrollView = parent()) {
174        IntRect parentRect(localRect);
175        parentRect.setLocation(parentScrollView->convertChildToSelf(this, localRect.location()));
176        return parentRect;
177    }
178    return localRect;
179}
180
181IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
182{
183    if (const ScrollView* parentScrollView = parent()) {
184        IntRect localRect = parentRect;
185        localRect.setLocation(parentScrollView->convertSelfToChild(this, localRect.location()));
186        return localRect;
187    }
188
189    return parentRect;
190}
191
192IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
193{
194    if (const ScrollView* parentScrollView = parent())
195        return parentScrollView->convertChildToSelf(this, localPoint);
196
197    return localPoint;
198}
199
200IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
201{
202    if (const ScrollView* parentScrollView = parent())
203        return parentScrollView->convertSelfToChild(this, parentPoint);
204
205    return parentPoint;
206}
207
208#if !PLATFORM(EFL)
209void Widget::frameRectsChanged()
210{
211}
212#endif
213
214} // namespace WebCore
215