1/*
2 * Copyright (C) 2004-2018 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora Ltd. 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 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 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#pragma once
28
29#if PLATFORM(IOS_FAMILY)
30#ifndef NSView
31#define NSView WAKView
32#endif
33#endif
34
35#include "IntRect.h"
36#include "PlatformScreen.h"
37#include <wtf/Forward.h>
38#include <wtf/RefCounted.h>
39#include <wtf/TypeCasts.h>
40#include <wtf/WeakPtr.h>
41
42#if PLATFORM(COCOA)
43#include <wtf/RetainPtr.h>
44#endif
45
46#if PLATFORM(COCOA)
47OBJC_CLASS NSView;
48OBJC_CLASS NSWindow;
49typedef NSView *PlatformWidget;
50#elif PLATFORM(WIN)
51typedef struct HWND__* HWND;
52typedef HWND PlatformWidget;
53#elif PLATFORM(GTK)
54typedef struct _GtkWidget GtkWidget;
55typedef struct _GtkContainer GtkContainer;
56typedef GtkWidget* PlatformWidget;
57#else
58typedef void* PlatformWidget;
59#endif
60
61typedef PlatformWidget PlatformPageClient;
62
63namespace WebCore {
64
65class Cursor;
66class Event;
67class FontCascade;
68class FrameView;
69class GraphicsContext;
70class PlatformMouseEvent;
71class ScrollView;
72
73enum WidgetNotification { WillPaintFlattened, DidPaintFlattened };
74
75// The Widget class serves as a base class for three kinds of objects:
76// (1) Scrollable areas (ScrollView)
77// (2) Scrollbars (Scrollbar)
78// (3) Plugins (PluginView)
79//
80// A widget may or may not be backed by a platform-specific object (e.g., HWND on Windows, NSView on Mac, QWidget on Qt).
81//
82// Widgets are connected in a hierarchy, with the restriction that plugins and scrollbars are always leaves of the
83// tree. Only ScrollViews can have children (and therefore the Widget class has no concept of children).
84//
85// The rules right now for which widgets get platform-specific objects are as follows:
86// ScrollView - Mac
87// Scrollbar - Mac, Gtk
88// Plugin - Mac, Windows (windowed only), Qt (windowed only, widget is an HWND on windows), Gtk (windowed only)
89//
90class Widget : public RefCounted<Widget>, public CanMakeWeakPtr<Widget> {
91public:
92 WEBCORE_EXPORT explicit Widget(PlatformWidget = nullptr);
93 WEBCORE_EXPORT virtual ~Widget();
94
95 WEBCORE_EXPORT PlatformWidget platformWidget() const;
96 WEBCORE_EXPORT void setPlatformWidget(PlatformWidget);
97
98 int x() const { return frameRect().x(); }
99 int y() const { return frameRect().y(); }
100 int width() const { return frameRect().width(); }
101 int height() const { return frameRect().height(); }
102 IntSize size() const { return frameRect().size(); }
103 IntPoint location() const { return frameRect().location(); }
104
105 WEBCORE_EXPORT virtual void setFrameRect(const IntRect&);
106 WEBCORE_EXPORT IntRect frameRect() const;
107 IntRect boundsRect() const { return IntRect(0, 0, width(), height()); }
108
109 void resize(int w, int h) { setFrameRect(IntRect(x(), y(), w, h)); }
110 void resize(const IntSize& s) { setFrameRect(IntRect(location(), s)); }
111 void move(int x, int y) { setFrameRect(IntRect(x, y, width(), height())); }
112 void move(const IntPoint& p) { setFrameRect(IntRect(p, size())); }
113
114 enum class SecurityOriginPaintPolicy { AnyOrigin, AccessibleOriginOnly };
115
116 WEBCORE_EXPORT virtual void paint(GraphicsContext&, const IntRect&, SecurityOriginPaintPolicy = SecurityOriginPaintPolicy::AnyOrigin);
117 void invalidate() { invalidateRect(boundsRect()); }
118 virtual void invalidateRect(const IntRect&) = 0;
119
120 WEBCORE_EXPORT virtual void setFocus(bool);
121
122 void setCursor(const Cursor&);
123
124 WEBCORE_EXPORT virtual void show();
125 WEBCORE_EXPORT virtual void hide();
126 bool isSelfVisible() const { return m_selfVisible; } // Whether or not we have been explicitly marked as visible or not.
127 bool isParentVisible() const { return m_parentVisible; } // Whether or not our parent is visible.
128 bool isVisible() const { return m_selfVisible && m_parentVisible; } // Whether or not we are actually visible.
129 virtual void setParentVisible(bool visible) { m_parentVisible = visible; }
130 void setSelfVisible(bool v) { m_selfVisible = v; }
131
132 void setIsSelected(bool);
133
134 virtual bool isFrameView() const { return false; }
135 virtual bool isPluginView() const { return false; }
136 // FIXME: The Mac plug-in code should inherit from PluginView. When this happens PluginViewBase and PluginView can become one class.
137 virtual bool isPluginViewBase() const { return false; }
138 virtual bool isScrollbar() const { return false; }
139 virtual bool isScrollView() const { return false; }
140
141 WEBCORE_EXPORT void removeFromParent();
142 WEBCORE_EXPORT virtual void setParent(ScrollView* view);
143 WEBCORE_EXPORT ScrollView* parent() const;
144 FrameView* root() const;
145
146 virtual void handleEvent(Event&) { }
147
148 virtual void notifyWidget(WidgetNotification) { }
149
150 WEBCORE_EXPORT IntRect convertToRootView(const IntRect&) const;
151 IntRect convertFromRootView(const IntRect&) const;
152
153 FloatRect convertToRootView(const FloatRect&) const;
154 FloatRect convertFromRootView(const FloatRect&) const;
155
156 IntPoint convertToRootView(const IntPoint&) const;
157 IntPoint convertFromRootView(const IntPoint&) const;
158
159 FloatPoint convertToRootView(const FloatPoint&) const;
160 FloatPoint convertFromRootView(const FloatPoint&) const;
161
162 // It is important for cross-platform code to realize that Mac has flipped coordinates. Therefore any code
163 // that tries to convert the location of a rect using the point-based convertFromContainingWindow will end
164 // up with an inaccurate rect. Always make sure to use the rect-based convertFromContainingWindow method
165 // when converting window rects.
166 WEBCORE_EXPORT IntRect convertToContainingWindow(const IntRect&) const;
167 IntRect convertFromContainingWindow(const IntRect&) const;
168
169 WEBCORE_EXPORT IntPoint convertToContainingWindow(const IntPoint&) const;
170 IntPoint convertFromContainingWindow(const IntPoint&) const;
171
172 virtual void frameRectsChanged() { }
173
174 // Notifies this widget that its clip rect changed.
175 virtual void clipRectChanged() { }
176
177 // Whether transforms affect the frame rect. FIXME: We get rid of this and have
178 // the frame rects be the same no matter what transforms are applied.
179 virtual bool transformsAffectFrameRect() { return true; }
180
181#if PLATFORM(COCOA)
182 NSView* getOuterView() const;
183
184 void removeFromSuperview();
185#endif
186#if PLATFORM(IOS_FAMILY)
187 void addToSuperview(NSView*);
188#endif
189
190 // Virtual methods to convert points to/from the containing ScrollView
191 WEBCORE_EXPORT virtual IntRect convertToContainingView(const IntRect&) const;
192 WEBCORE_EXPORT virtual IntRect convertFromContainingView(const IntRect&) const;
193 WEBCORE_EXPORT virtual FloatRect convertToContainingView(const FloatRect&) const;
194 WEBCORE_EXPORT virtual FloatRect convertFromContainingView(const FloatRect&) const;
195 WEBCORE_EXPORT virtual IntPoint convertToContainingView(const IntPoint&) const;
196 WEBCORE_EXPORT virtual IntPoint convertFromContainingView(const IntPoint&) const;
197 WEBCORE_EXPORT virtual FloatPoint convertToContainingView(const FloatPoint&) const;
198 WEBCORE_EXPORT virtual FloatPoint convertFromContainingView(const FloatPoint&) const;
199
200private:
201 void init(PlatformWidget); // Must be called by all Widget constructors to initialize cross-platform data.
202
203 void releasePlatformWidget();
204 void retainPlatformWidget();
205
206 // These methods are used to convert from the root widget to the containing window,
207 // which has behavior that may differ between platforms (e.g. Mac uses flipped window coordinates).
208 static IntRect convertFromRootToContainingWindow(const Widget* rootWidget, const IntRect&);
209 static IntRect convertFromContainingWindowToRoot(const Widget* rootWidget, const IntRect&);
210
211 static IntPoint convertFromRootToContainingWindow(const Widget* rootWidget, const IntPoint&);
212 static IntPoint convertFromContainingWindowToRoot(const Widget* rootWidget, const IntPoint&);
213
214private:
215 bool m_selfVisible { false };
216 bool m_parentVisible { false };
217
218 WeakPtr<ScrollView> m_parent;
219#if !PLATFORM(COCOA)
220 PlatformWidget m_widget;
221#else
222 RetainPtr<NSView> m_widget;
223#endif
224
225 IntRect m_frame; // Not used when a native widget exists.
226};
227
228#if !PLATFORM(COCOA)
229
230inline PlatformWidget Widget::platformWidget() const
231{
232 return m_widget;
233}
234
235inline void Widget::setPlatformWidget(PlatformWidget widget)
236{
237 if (widget != m_widget) {
238 releasePlatformWidget();
239 m_widget = widget;
240 retainPlatformWidget();
241 }
242}
243
244#endif
245
246#if !PLATFORM(GTK)
247
248inline void Widget::releasePlatformWidget()
249{
250}
251
252inline void Widget::retainPlatformWidget()
253{
254}
255
256#endif
257
258} // namespace WebCore
259
260#define SPECIALIZE_TYPE_TRAITS_WIDGET(ToValueTypeName, predicate) \
261SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToValueTypeName) \
262 static bool isType(const WebCore::Widget& widget) { return widget.predicate; } \
263SPECIALIZE_TYPE_TRAITS_END()
264
265