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 "core/platform/Widget.h"
29
30#include "core/platform/HostWindow.h"
31#include "core/platform/ScrollView.h"
32#include "core/platform/graphics/IntRect.h"
33
34#include "wtf/Assertions.h"
35
36namespace WebCore {
37
38Widget::Widget()
39    : m_parent(0)
40    , m_selfVisible(false)
41    , m_parentVisible(false)
42{
43}
44
45Widget::~Widget()
46{
47    ASSERT(!parent());
48}
49
50void Widget::setCursor(const Cursor& cursor)
51{
52    if (ScrollView* view = root())
53        view->hostWindow()->setCursor(cursor);
54}
55
56void Widget::removeFromParent()
57{
58    if (parent())
59        parent()->removeChild(this);
60}
61
62void Widget::setParent(ScrollView* view)
63{
64    ASSERT(!view || !m_parent);
65    if (!view || !view->isVisible())
66        setParentVisible(false);
67    m_parent = view;
68    if (view && view->isVisible())
69        setParentVisible(true);
70}
71
72ScrollView* Widget::root() const
73{
74    const Widget* top = this;
75    while (top->parent())
76        top = top->parent();
77    if (top->isFrameView())
78        return const_cast<ScrollView*>(static_cast<const ScrollView*>(top));
79    return 0;
80}
81
82IntRect Widget::convertFromRootView(const IntRect& rootRect) const
83{
84    if (const ScrollView* parentScrollView = parent()) {
85        IntRect parentRect = parentScrollView->convertFromRootView(rootRect);
86        return convertFromContainingView(parentRect);
87    }
88    return rootRect;
89}
90
91IntRect Widget::convertToRootView(const IntRect& localRect) const
92{
93    if (const ScrollView* parentScrollView = parent()) {
94        IntRect parentRect = convertToContainingView(localRect);
95        return parentScrollView->convertToRootView(parentRect);
96    }
97    return localRect;
98}
99
100IntPoint Widget::convertFromRootView(const IntPoint& rootPoint) const
101{
102    if (const ScrollView* parentScrollView = parent()) {
103        IntPoint parentPoint = parentScrollView->convertFromRootView(rootPoint);
104        return convertFromContainingView(parentPoint);
105    }
106    return rootPoint;
107}
108
109IntPoint Widget::convertToRootView(const IntPoint& localPoint) const
110{
111    if (const ScrollView* parentScrollView = parent()) {
112        IntPoint parentPoint = convertToContainingView(localPoint);
113        return parentScrollView->convertToRootView(parentPoint);
114    }
115    return localPoint;
116}
117
118IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const
119{
120    if (const ScrollView* parentScrollView = parent()) {
121        IntRect parentRect = parentScrollView->convertFromContainingWindow(windowRect);
122        return convertFromContainingView(parentRect);
123    }
124    return windowRect;
125}
126
127IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
128{
129    if (const ScrollView* parentScrollView = parent()) {
130        IntRect parentRect = convertToContainingView(localRect);
131        return parentScrollView->convertToContainingWindow(parentRect);
132    }
133    return localRect;
134}
135
136IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
137{
138    if (const ScrollView* parentScrollView = parent()) {
139        IntPoint parentPoint = parentScrollView->convertFromContainingWindow(windowPoint);
140        return convertFromContainingView(parentPoint);
141    }
142    return windowPoint;
143}
144
145IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
146{
147    if (const ScrollView* parentScrollView = parent()) {
148        IntPoint parentPoint = convertToContainingView(localPoint);
149        return parentScrollView->convertToContainingWindow(parentPoint);
150    }
151    return localPoint;
152}
153
154IntRect Widget::convertToContainingView(const IntRect& localRect) const
155{
156    if (const ScrollView* parentScrollView = parent()) {
157        IntRect parentRect(localRect);
158        parentRect.setLocation(parentScrollView->convertChildToSelf(this, localRect.location()));
159        return parentRect;
160    }
161    return localRect;
162}
163
164IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
165{
166    if (const ScrollView* parentScrollView = parent()) {
167        IntRect localRect = parentRect;
168        localRect.setLocation(parentScrollView->convertSelfToChild(this, localRect.location()));
169        return localRect;
170    }
171
172    return parentRect;
173}
174
175IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
176{
177    if (const ScrollView* parentScrollView = parent())
178        return parentScrollView->convertChildToSelf(this, localPoint);
179
180    return localPoint;
181}
182
183IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
184{
185    if (const ScrollView* parentScrollView = parent())
186        return parentScrollView->convertSelfToChild(this, parentPoint);
187
188    return parentPoint;
189}
190
191} // namespace WebCore
192