1/*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, 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::convertFromContainingWindow(const IntRect& windowRect) const
73{
74    if (const ScrollView* parentScrollView = parent()) {
75        IntRect parentRect = parentScrollView->convertFromContainingWindow(windowRect);
76        return convertFromContainingView(parentRect);
77    }
78    return convertFromContainingWindowToRoot(this, windowRect);
79}
80
81IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
82{
83    if (const ScrollView* parentScrollView = parent()) {
84        IntRect parentRect = convertToContainingView(localRect);
85        return parentScrollView->convertToContainingWindow(parentRect);
86    }
87    return convertFromRootToContainingWindow(this, localRect);
88}
89
90IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
91{
92    if (const ScrollView* parentScrollView = parent()) {
93        IntPoint parentPoint = parentScrollView->convertFromContainingWindow(windowPoint);
94        return convertFromContainingView(parentPoint);
95    }
96    return convertFromContainingWindowToRoot(this, windowPoint);
97}
98
99IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
100{
101    if (const ScrollView* parentScrollView = parent()) {
102        IntPoint parentPoint = convertToContainingView(localPoint);
103        return parentScrollView->convertToContainingWindow(parentPoint);
104    }
105    return convertFromRootToContainingWindow(this, localPoint);
106}
107
108#if !PLATFORM(MAC)
109IntRect Widget::convertFromRootToContainingWindow(const Widget*, const IntRect& rect)
110{
111    return rect;
112}
113
114IntRect Widget::convertFromContainingWindowToRoot(const Widget*, const IntRect& rect)
115{
116    return rect;
117}
118
119IntPoint Widget::convertFromRootToContainingWindow(const Widget*, const IntPoint& point)
120{
121    return point;
122}
123
124IntPoint Widget::convertFromContainingWindowToRoot(const Widget*, const IntPoint& point)
125{
126    return point;
127}
128#endif
129
130#if !PLATFORM(MAC) && !PLATFORM(GTK) && !OS(ANDROID)
131void Widget::releasePlatformWidget()
132{
133}
134
135void Widget::retainPlatformWidget()
136{
137}
138#endif
139
140IntRect Widget::convertToContainingView(const IntRect& localRect) const
141{
142    if (const ScrollView* parentScrollView = parent()) {
143        IntRect parentRect(localRect);
144        parentRect.setLocation(parentScrollView->convertChildToSelf(this, localRect.location()));
145        return parentRect;
146    }
147    return localRect;
148}
149
150IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
151{
152    if (const ScrollView* parentScrollView = parent()) {
153        IntRect localRect = parentRect;
154        localRect.setLocation(parentScrollView->convertSelfToChild(this, localRect.location()));
155        return localRect;
156    }
157
158    return parentRect;
159}
160
161IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
162{
163    if (const ScrollView* parentScrollView = parent())
164        return parentScrollView->convertChildToSelf(this, localPoint);
165
166    return localPoint;
167}
168
169IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
170{
171    if (const ScrollView* parentScrollView = parent())
172        return parentScrollView->convertSelfToChild(this, parentPoint);
173
174    return parentPoint;
175}
176
177} // namespace WebCore
178