| 1 | /* |
| 2 | * Copyright (C) 2004-2018 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 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 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 "FrameView.h" |
| 30 | #include "IntRect.h" |
| 31 | #include "NotImplemented.h" |
| 32 | #include <wtf/Assertions.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | void Widget::init(PlatformWidget widget) |
| 37 | { |
| 38 | m_selfVisible = false; |
| 39 | m_parentVisible = false; |
| 40 | m_widget = widget; |
| 41 | if (m_widget) |
| 42 | retainPlatformWidget(); |
| 43 | } |
| 44 | |
| 45 | void Widget::setParent(ScrollView* view) |
| 46 | { |
| 47 | ASSERT(!view || !m_parent); |
| 48 | if (!view || !view->isVisible()) |
| 49 | setParentVisible(false); |
| 50 | m_parent = makeWeakPtr(view); |
| 51 | if (view && view->isVisible()) |
| 52 | setParentVisible(true); |
| 53 | } |
| 54 | |
| 55 | FrameView* Widget::root() const |
| 56 | { |
| 57 | const Widget* top = this; |
| 58 | while (top->parent()) |
| 59 | top = top->parent(); |
| 60 | if (is<FrameView>(*top)) |
| 61 | return const_cast<FrameView*>(downcast<FrameView>(top)); |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | void Widget::removeFromParent() |
| 66 | { |
| 67 | if (parent()) |
| 68 | parent()->removeChild(*this); |
| 69 | } |
| 70 | |
| 71 | IntRect Widget::convertFromRootView(const IntRect& rootRect) const |
| 72 | { |
| 73 | if (const ScrollView* parentScrollView = parent()) { |
| 74 | IntRect parentRect = parentScrollView->convertFromRootView(rootRect); |
| 75 | return convertFromContainingView(parentRect); |
| 76 | } |
| 77 | return rootRect; |
| 78 | } |
| 79 | |
| 80 | FloatRect Widget::convertFromRootView(const FloatRect& rootRect) const |
| 81 | { |
| 82 | if (const ScrollView* parentScrollView = parent()) { |
| 83 | FloatRect parentRect = parentScrollView->convertFromRootView(rootRect); |
| 84 | return convertFromContainingView(parentRect); |
| 85 | } |
| 86 | return rootRect; |
| 87 | } |
| 88 | |
| 89 | IntRect Widget::convertToRootView(const IntRect& localRect) const |
| 90 | { |
| 91 | if (const ScrollView* parentScrollView = parent()) { |
| 92 | IntRect parentRect = convertToContainingView(localRect); |
| 93 | return parentScrollView->convertToRootView(parentRect); |
| 94 | } |
| 95 | return localRect; |
| 96 | } |
| 97 | |
| 98 | FloatRect Widget::convertToRootView(const FloatRect& localRect) const |
| 99 | { |
| 100 | if (const ScrollView* parentScrollView = parent()) { |
| 101 | FloatRect parentRect = convertToContainingView(localRect); |
| 102 | return parentScrollView->convertToRootView(parentRect); |
| 103 | } |
| 104 | return localRect; |
| 105 | } |
| 106 | |
| 107 | IntPoint Widget::convertFromRootView(const IntPoint& rootPoint) const |
| 108 | { |
| 109 | if (const ScrollView* parentScrollView = parent()) { |
| 110 | IntPoint parentPoint = parentScrollView->convertFromRootView(rootPoint); |
| 111 | return convertFromContainingView(parentPoint); |
| 112 | } |
| 113 | return rootPoint; |
| 114 | } |
| 115 | |
| 116 | IntPoint Widget::convertToRootView(const IntPoint& localPoint) const |
| 117 | { |
| 118 | if (const ScrollView* parentScrollView = parent()) { |
| 119 | IntPoint parentPoint = convertToContainingView(localPoint); |
| 120 | return parentScrollView->convertToRootView(parentPoint); |
| 121 | } |
| 122 | return localPoint; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | FloatPoint Widget::convertFromRootView(const FloatPoint& rootPoint) const |
| 127 | { |
| 128 | if (const ScrollView* parentScrollView = parent()) { |
| 129 | FloatPoint parentPoint = parentScrollView->convertFromRootView(rootPoint); |
| 130 | return convertFromContainingView(parentPoint); |
| 131 | } |
| 132 | return rootPoint; |
| 133 | } |
| 134 | |
| 135 | FloatPoint Widget::convertToRootView(const FloatPoint& localPoint) const |
| 136 | { |
| 137 | if (const ScrollView* parentScrollView = parent()) { |
| 138 | FloatPoint parentPoint = convertToContainingView(localPoint); |
| 139 | return parentScrollView->convertToRootView(parentPoint); |
| 140 | } |
| 141 | return localPoint; |
| 142 | } |
| 143 | |
| 144 | IntRect Widget::convertFromContainingWindow(const IntRect& windowRect) const |
| 145 | { |
| 146 | if (const ScrollView* parentScrollView = parent()) { |
| 147 | IntRect parentRect = parentScrollView->convertFromContainingWindow(windowRect); |
| 148 | return convertFromContainingView(parentRect); |
| 149 | } |
| 150 | return convertFromContainingWindowToRoot(this, windowRect); |
| 151 | } |
| 152 | |
| 153 | IntRect Widget::convertToContainingWindow(const IntRect& localRect) const |
| 154 | { |
| 155 | if (const ScrollView* parentScrollView = parent()) { |
| 156 | IntRect parentRect = convertToContainingView(localRect); |
| 157 | return parentScrollView->convertToContainingWindow(parentRect); |
| 158 | } |
| 159 | return convertFromRootToContainingWindow(this, localRect); |
| 160 | } |
| 161 | |
| 162 | IntPoint Widget::convertFromContainingWindow(const IntPoint& windowPoint) const |
| 163 | { |
| 164 | if (const ScrollView* parentScrollView = parent()) { |
| 165 | IntPoint parentPoint = parentScrollView->convertFromContainingWindow(windowPoint); |
| 166 | return convertFromContainingView(parentPoint); |
| 167 | } |
| 168 | return convertFromContainingWindowToRoot(this, windowPoint); |
| 169 | } |
| 170 | |
| 171 | IntPoint Widget::convertToContainingWindow(const IntPoint& localPoint) const |
| 172 | { |
| 173 | if (const ScrollView* parentScrollView = parent()) { |
| 174 | IntPoint parentPoint = convertToContainingView(localPoint); |
| 175 | return parentScrollView->convertToContainingWindow(parentPoint); |
| 176 | } |
| 177 | return convertFromRootToContainingWindow(this, localPoint); |
| 178 | } |
| 179 | |
| 180 | #if !PLATFORM(COCOA) |
| 181 | |
| 182 | Widget::Widget(PlatformWidget widget) |
| 183 | { |
| 184 | init(widget); |
| 185 | } |
| 186 | |
| 187 | IntRect Widget::frameRect() const |
| 188 | { |
| 189 | return m_frame; |
| 190 | } |
| 191 | |
| 192 | IntRect Widget::convertFromRootToContainingWindow(const Widget*, const IntRect& rect) |
| 193 | { |
| 194 | return rect; |
| 195 | } |
| 196 | |
| 197 | IntRect Widget::convertFromContainingWindowToRoot(const Widget*, const IntRect& rect) |
| 198 | { |
| 199 | return rect; |
| 200 | } |
| 201 | |
| 202 | IntPoint Widget::convertFromRootToContainingWindow(const Widget*, const IntPoint& point) |
| 203 | { |
| 204 | return point; |
| 205 | } |
| 206 | |
| 207 | IntPoint Widget::convertFromContainingWindowToRoot(const Widget*, const IntPoint& point) |
| 208 | { |
| 209 | return point; |
| 210 | } |
| 211 | |
| 212 | #endif // !PLATFORM(COCOA) |
| 213 | |
| 214 | IntRect Widget::convertToContainingView(const IntRect& localRect) const |
| 215 | { |
| 216 | if (const ScrollView* parentScrollView = parent()) { |
| 217 | IntRect parentRect(localRect); |
| 218 | parentRect.setLocation(parentScrollView->convertChildToSelf(this, localRect.location())); |
| 219 | return parentRect; |
| 220 | } |
| 221 | return localRect; |
| 222 | } |
| 223 | |
| 224 | IntRect Widget::convertFromContainingView(const IntRect& parentRect) const |
| 225 | { |
| 226 | if (const ScrollView* parentScrollView = parent()) { |
| 227 | IntRect localRect = parentRect; |
| 228 | localRect.setLocation(parentScrollView->convertSelfToChild(this, localRect.location())); |
| 229 | return localRect; |
| 230 | } |
| 231 | |
| 232 | return parentRect; |
| 233 | } |
| 234 | |
| 235 | FloatRect Widget::convertToContainingView(const FloatRect& localRect) const |
| 236 | { |
| 237 | return convertToContainingView(IntRect(localRect)); |
| 238 | } |
| 239 | |
| 240 | FloatRect Widget::convertFromContainingView(const FloatRect& parentRect) const |
| 241 | { |
| 242 | return convertFromContainingView(IntRect(parentRect)); |
| 243 | } |
| 244 | |
| 245 | IntPoint Widget::convertToContainingView(const IntPoint& localPoint) const |
| 246 | { |
| 247 | if (const ScrollView* parentScrollView = parent()) |
| 248 | return parentScrollView->convertChildToSelf(this, localPoint); |
| 249 | |
| 250 | return localPoint; |
| 251 | } |
| 252 | |
| 253 | IntPoint Widget::convertFromContainingView(const IntPoint& parentPoint) const |
| 254 | { |
| 255 | if (const ScrollView* parentScrollView = parent()) |
| 256 | return parentScrollView->convertSelfToChild(this, parentPoint); |
| 257 | |
| 258 | return parentPoint; |
| 259 | } |
| 260 | |
| 261 | FloatPoint Widget::convertToContainingView(const FloatPoint& localPoint) const |
| 262 | { |
| 263 | return convertToContainingView(IntPoint(localPoint)); |
| 264 | } |
| 265 | |
| 266 | FloatPoint Widget::convertFromContainingView(const FloatPoint& parentPoint) const |
| 267 | { |
| 268 | return convertFromContainingView(IntPoint(parentPoint)); |
| 269 | } |
| 270 | |
| 271 | #if !PLATFORM(COCOA) && !PLATFORM(GTK) && !PLATFORM(WIN) |
| 272 | |
| 273 | Widget::~Widget() |
| 274 | { |
| 275 | ASSERT(!parent()); |
| 276 | notImplemented(); |
| 277 | } |
| 278 | |
| 279 | void Widget::setFrameRect(const IntRect& rect) |
| 280 | { |
| 281 | m_frame = rect; |
| 282 | notImplemented(); |
| 283 | } |
| 284 | |
| 285 | void Widget::paint(GraphicsContext&, const IntRect&, SecurityOriginPaintPolicy) |
| 286 | { |
| 287 | notImplemented(); |
| 288 | } |
| 289 | |
| 290 | void Widget::setFocus(bool) |
| 291 | { |
| 292 | notImplemented(); |
| 293 | } |
| 294 | |
| 295 | void Widget::setCursor(const Cursor&) |
| 296 | { |
| 297 | notImplemented(); |
| 298 | } |
| 299 | |
| 300 | void Widget::show() |
| 301 | { |
| 302 | notImplemented(); |
| 303 | } |
| 304 | |
| 305 | void Widget::hide() |
| 306 | { |
| 307 | notImplemented(); |
| 308 | } |
| 309 | |
| 310 | void Widget::setIsSelected(bool) |
| 311 | { |
| 312 | notImplemented(); |
| 313 | } |
| 314 | |
| 315 | #endif // !PLATFORM(COCOA) && !PLATFORM(GTK) && !PLATFORM(WIN) |
| 316 | |
| 317 | } // namespace WebCore |
| 318 | |