Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / platform / Widget.cpp
blobb9c32b485ae3a53d0ea0c1b1ef768d44ed427d72
1 /*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
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.
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.
26 #include "config.h"
27 #include "Widget.h"
29 #include "IntRect.h"
30 #include "ScrollView.h"
32 #include <wtf/Assertions.h>
34 namespace WebCore {
36 void Widget::init(PlatformWidget widget)
38 m_parent = 0;
39 m_selfVisible = false;
40 m_parentVisible = false;
41 m_widget = widget;
42 if (m_widget)
43 retainPlatformWidget();
46 void Widget::setParent(ScrollView* view)
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);
56 ScrollView* Widget::root() const
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;
66 void Widget::removeFromParent()
68 if (parent())
69 parent()->removeChild(this);
72 IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const
74 if (const ScrollView* parentScrollView = parent()) {
75 IntRect parentRect = parentScrollView->convertFromContainingWindow(windowRect);
76 return convertFromContainingView(parentRect);
78 return convertFromContainingWindowToRoot(this, windowRect);
81 IntRect Widget::convertToContainingWindow(const IntRect& localRect) const
83 if (const ScrollView* parentScrollView = parent()) {
84 IntRect parentRect = convertToContainingView(localRect);
85 return parentScrollView->convertToContainingWindow(parentRect);
87 return convertFromRootToContainingWindow(this, localRect);
90 IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const
92 if (const ScrollView* parentScrollView = parent()) {
93 IntPoint parentPoint = parentScrollView->convertFromContainingWindow(windowPoint);
94 return convertFromContainingView(parentPoint);
96 return convertFromContainingWindowToRoot(this, windowPoint);
99 IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const
101 if (const ScrollView* parentScrollView = parent()) {
102 IntPoint parentPoint = convertToContainingView(localPoint);
103 return parentScrollView->convertToContainingWindow(parentPoint);
105 return convertFromRootToContainingWindow(this, localPoint);
108 #if !PLATFORM(MAC) || ENABLE(EXPERIMENTAL_SINGLE_VIEW_MODE)
109 IntRect Widget::convertFromRootToContainingWindow(const Widget*, const IntRect& rect)
111 return rect;
114 IntRect Widget::convertFromContainingWindowToRoot(const Widget*, const IntRect& rect)
116 return rect;
119 IntPoint Widget::convertFromRootToContainingWindow(const Widget*, const IntPoint& point)
121 return point;
124 IntPoint Widget::convertFromContainingWindowToRoot(const Widget*, const IntPoint& point)
126 return point;
128 #endif
130 #if (!PLATFORM(MAC) && !PLATFORM(GTK)) || ENABLE(EXPERIMENTAL_SINGLE_VIEW_MODE)
131 void Widget::releasePlatformWidget()
135 void Widget::retainPlatformWidget()
138 #endif
140 IntRect Widget::convertToContainingView(const IntRect& localRect) const
142 if (const ScrollView* parentScrollView = parent()) {
143 IntRect parentRect(localRect);
144 parentRect.setLocation(parentScrollView->convertChildToSelf(this, localRect.location()));
145 return parentRect;
147 return localRect;
150 IntRect Widget::convertFromContainingView(const IntRect& parentRect) const
152 if (const ScrollView* parentScrollView = parent()) {
153 IntRect localRect = parentRect;
154 localRect.setLocation(parentScrollView->convertSelfToChild(this, localRect.location()));
155 return localRect;
158 return parentRect;
161 IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const
163 if (const ScrollView* parentScrollView = parent())
164 return parentScrollView->convertChildToSelf(this, localPoint);
166 return localPoint;
169 IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const
171 if (const ScrollView* parentScrollView = parent())
172 return parentScrollView->convertSelfToChild(this, parentPoint);
174 return parentPoint;
177 } // namespace WebCore