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 <wtf/Forward.h> 32 #include <wtf/RefCounted.h> 33 34 #if PLATFORM(CHROMIUM) 35 #include "PlatformWidget.h" 36 #endif 37 38 #if PLATFORM(MAC) 39 #include <wtf/RetainPtr.h> 40 #endif 41 42 #if PLATFORM(QT) 43 #include <qglobal.h> 44 #include <QWeakPointer> 45 #endif 46 47 #if PLATFORM(MAC) 48 #ifdef __OBJC__ 49 @class NSView; 50 @class NSWindow; 51 #else 52 class NSView; 53 class NSWindow; 54 #endif 55 typedef NSView *PlatformWidget; 56 #endif 57 58 #if PLATFORM(ANDROID) 59 class WebCoreViewBridge; 60 typedef WebCoreViewBridge* PlatformWidget; 61 #endif 62 63 #if PLATFORM(WIN) 64 typedef struct HWND__* HWND; 65 typedef HWND PlatformWidget; 66 #endif 67 68 #if PLATFORM(GTK) 69 typedef struct _GtkWidget GtkWidget; 70 typedef struct _GtkContainer GtkContainer; 71 typedef GtkWidget* PlatformWidget; 72 #endif 73 74 #if PLATFORM(QT) 75 QT_BEGIN_NAMESPACE 76 class QWidget; 77 QT_END_NAMESPACE 78 typedef QWidget* PlatformWidget; 79 #endif 80 81 #if PLATFORM(WX) 82 class wxWindow; 83 typedef wxWindow* PlatformWidget; 84 #endif 85 86 #if PLATFORM(HAIKU) 87 class BView; 88 typedef BView* PlatformWidget; 89 #endif 90 91 #if PLATFORM(BREWMP) 92 typedef void* PlatformWidget; 93 #endif 94 95 #if PLATFORM(EFL) 96 typedef struct _Evas_Object Evas_Object; 97 typedef struct _Evas Evas; 98 typedef struct _Ecore_Evas Ecore_Evas; 99 typedef Evas_Object* PlatformWidget; 100 #endif 101 102 #if PLATFORM(QT) 103 class QWebPageClient; 104 typedef QWebPageClient* PlatformPageClient; 105 #else 106 typedef PlatformWidget PlatformPageClient; 107 #endif 108 109 namespace WebCore { 110 111 class AXObjectCache; 112 class Cursor; 113 class Event; 114 class Font; 115 class GraphicsContext; 116 class PlatformMouseEvent; 117 class ScrollView; 118 class WidgetPrivate; 119 120 enum WidgetNotification { WillPaintFlattened, DidPaintFlattened }; 121 122 // The Widget class serves as a base class for three kinds of objects: 123 // (1) Scrollable areas (ScrollView) 124 // (2) Scrollbars (Scrollbar) 125 // (3) Plugins (PluginView) 126 // 127 // A widget may or may not be backed by a platform-specific object (e.g., HWND on Windows, NSView on Mac, QWidget on Qt). 128 // 129 // Widgets are connected in a hierarchy, with the restriction that plugins and scrollbars are always leaves of the 130 // tree. Only ScrollViews can have children (and therefore the Widget class has no concept of children). 131 // 132 // The rules right now for which widgets get platform-specific objects are as follows: 133 // ScrollView - Mac 134 // Scrollbar - Mac, Gtk 135 // Plugin - Mac, Windows (windowed only), Qt (windowed only, widget is an HWND on windows), Gtk (windowed only) 136 // 137 class Widget : public RefCounted<Widget> { 138 public: 139 Widget(PlatformWidget = 0); 140 virtual ~Widget(); 141 142 PlatformWidget platformWidget() const; 143 void setPlatformWidget(PlatformWidget); 144 145 #if PLATFORM(HAIKU) 146 PlatformWidget topLevelPlatformWidget() const { return m_topLevelPlatformWidget; } 147 void setTopLevelPlatformWidget(PlatformWidget widget) 148 { 149 m_topLevelPlatformWidget = widget; 150 } 151 #endif 152 153 int x() const { return frameRect().x(); } 154 int y() const { return frameRect().y(); } 155 int width() const { return frameRect().width(); } 156 int height() const { return frameRect().height(); } 157 IntSize size() const { return frameRect().size(); } 158 IntPoint pos() const { return frameRect().location(); } 159 160 virtual void setFrameRect(const IntRect&); 161 virtual void setBoundsSize(const IntSize&); 162 virtual IntRect frameRect() const; 163 IntRect boundsRect() const { return IntRect(0, 0, width(), height()); } 164 165 void resize(int w, int h) { setFrameRect(IntRect(x(), y(), w, h)); setBoundsSize(IntSize(w, h)); } 166 void resize(const IntSize& s) { setFrameRect(IntRect(pos(), s)); setBoundsSize(s); } 167 void move(int x, int y) { setFrameRect(IntRect(x, y, width(), height())); } 168 void move(const IntPoint& p) { setFrameRect(IntRect(p, size())); } 169 170 virtual void paint(GraphicsContext*, const IntRect&); 171 void invalidate() { invalidateRect(boundsRect()); } 172 virtual void invalidateRect(const IntRect&) = 0; 173 174 virtual void setFocus(bool); 175 176 void setCursor(const Cursor&); 177 178 virtual void show(); 179 virtual void hide(); 180 bool isSelfVisible() const { return m_selfVisible; } // Whether or not we have been explicitly marked as visible or not. 181 bool isParentVisible() const { return m_parentVisible; } // Whether or not our parent is visible. 182 bool isVisible() const { return m_selfVisible && m_parentVisible; } // Whether or not we are actually visible. 183 virtual void setParentVisible(bool visible) { m_parentVisible = visible; } 184 void setSelfVisible(bool v) { m_selfVisible = v; } 185 186 void setIsSelected(bool); 187 188 virtual bool isFrameView() const { return false; } 189 virtual bool isPluginView() const { return false; } 190 // FIXME: The Mac plug-in code should inherit from PluginView. When this happens PluginViewBase and PluginView can become one class. 191 virtual bool isPluginViewBase() const { return false; } 192 virtual bool isScrollbar() const { return false; } 193 194 void removeFromParent(); 195 virtual void setParent(ScrollView* view); 196 ScrollView* parent() const { return m_parent; } 197 ScrollView* root() const; 198 199 virtual void handleEvent(Event*) { } 200 201 virtual void notifyWidget(WidgetNotification) { } 202 203 // It is important for cross-platform code to realize that Mac has flipped coordinates. Therefore any code 204 // that tries to convert the location of a rect using the point-based convertFromContainingWindow will end 205 // up with an inaccurate rect. Always make sure to use the rect-based convertFromContainingWindow method 206 // when converting window rects. 207 IntRect convertToContainingWindow(const IntRect&) const; 208 IntRect convertFromContainingWindow(const IntRect&) const; 209 210 IntPoint convertToContainingWindow(const IntPoint&) const; 211 IntPoint convertFromContainingWindow(const IntPoint&) const; 212 213 virtual void frameRectsChanged(); 214 215 // Notifies this widget that other widgets on the page have been repositioned. 216 virtual void widgetPositionsUpdated() {} 217 218 #if PLATFORM(MAC) 219 NSView* getOuterView() const; 220 221 static void beforeMouseDown(NSView*, Widget*); 222 static void afterMouseDown(NSView*, Widget*); 223 224 void removeFromSuperview(); 225 #endif 226 227 #if PLATFORM(EFL) 228 // FIXME: These should really go to PlatformWidget. They're here currently since 229 // the EFL port considers that Evas_Object (a C object) is a PlatformWidget, but 230 // encapsulating that into a C++ class will make this header clean as it should be. 231 Evas* evas() const; 232 233 void setEvasObject(Evas_Object*); 234 Evas_Object* evasObject() const; 235 236 const String edjeTheme() const; 237 void setEdjeTheme(const String &); 238 const String edjeThemeRecursive() const; 239 #endif 240 241 #if PLATFORM(CHROMIUM) 242 virtual bool isPluginContainer() const { return false; } 243 #endif 244 245 #if PLATFORM(QT) 246 QObject* bindingObject() const; 247 void setBindingObject(QObject*); 248 #endif 249 250 // Virtual methods to convert points to/from the containing ScrollView 251 virtual IntRect convertToContainingView(const IntRect&) const; 252 virtual IntRect convertFromContainingView(const IntRect&) const; 253 virtual IntPoint convertToContainingView(const IntPoint&) const; 254 virtual IntPoint convertFromContainingView(const IntPoint&) const; 255 256 // A means to access the AX cache when this object can get a pointer to it. 257 virtual AXObjectCache* axObjectCache() const { return 0; } 258 259 private: 260 void init(PlatformWidget); // Must be called by all Widget constructors to initialize cross-platform data. 261 262 void releasePlatformWidget(); 263 void retainPlatformWidget(); 264 265 // These methods are used to convert from the root widget to the containing window, 266 // which has behavior that may differ between platforms (e.g. Mac uses flipped window coordinates). 267 static IntRect convertFromRootToContainingWindow(const Widget* rootWidget, const IntRect&); 268 static IntRect convertFromContainingWindowToRoot(const Widget* rootWidget, const IntRect&); 269 270 static IntPoint convertFromRootToContainingWindow(const Widget* rootWidget, const IntPoint&); 271 static IntPoint convertFromContainingWindowToRoot(const Widget* rootWidget, const IntPoint&); 272 273 private: 274 ScrollView* m_parent; 275 #if !PLATFORM(MAC) 276 PlatformWidget m_widget; 277 #else 278 RetainPtr<NSView> m_widget; 279 #endif 280 bool m_selfVisible; 281 bool m_parentVisible; 282 283 IntRect m_frame; // Not used when a native widget exists. 284 285 #if PLATFORM(EFL) 286 // FIXME: Please see the previous #if PLATFORM(EFL) block. 287 Ecore_Evas* ecoreEvas() const; 288 289 void applyFallbackCursor(); 290 void applyCursor(); 291 #endif 292 293 #if PLATFORM(MAC) || PLATFORM(EFL) 294 WidgetPrivate* m_data; 295 #endif 296 297 #if PLATFORM(QT) 298 QWeakPointer<QObject> m_bindingObject; 299 #endif 300 301 #if PLATFORM(HAIKU) 302 PlatformWidget m_topLevelPlatformWidget; 303 #endif 304 #if PLATFORM(ANDROID) 305 public: 306 int textWrapWidth() const; 307 #endif 308 }; 309 310 #if !PLATFORM(MAC) 311 312 inline PlatformWidget Widget::platformWidget() const 313 { 314 return m_widget; 315 } 316 317 inline void Widget::setPlatformWidget(PlatformWidget widget) 318 { 319 if (widget != m_widget) { 320 releasePlatformWidget(); 321 m_widget = widget; 322 retainPlatformWidget(); 323 } 324 } 325 326 #endif 327 328 #if !PLATFORM(GTK) && !PLATFORM(ANDROID) 329 330 inline void Widget::releasePlatformWidget() 331 { 332 } 333 334 inline void Widget::retainPlatformWidget() 335 { 336 } 337 338 #endif 339 340 } // namespace WebCore 341 342 #endif // Widget_h 343