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