Home | History | Annotate | Download | only in platform
      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 <wtf/Platform.h>
     31 
     32 #if PLATFORM(MAC)
     33 #ifdef __OBJC__
     34 @class NSView;
     35 @class NSWindow;
     36 #else
     37 class NSView;
     38 class NSWindow;
     39 #endif
     40 typedef NSView* PlatformWidget;
     41 #endif
     42 
     43 #if PLATFORM(ANDROID)
     44 class WebCoreViewBridge;
     45 typedef WebCoreViewBridge* PlatformWidget;
     46 #endif
     47 
     48 #if PLATFORM(WIN)
     49 typedef struct HWND__* HWND;
     50 typedef HWND PlatformWidget;
     51 #endif
     52 
     53 #if PLATFORM(GTK)
     54 typedef struct _GdkDrawable GdkDrawable;
     55 typedef struct _GtkWidget GtkWidget;
     56 typedef struct _GtkContainer GtkContainer;
     57 typedef GtkWidget* PlatformWidget;
     58 #endif
     59 
     60 #if PLATFORM(QT)
     61 #include <qglobal.h>
     62 QT_BEGIN_NAMESPACE
     63 class QWidget;
     64 QT_END_NAMESPACE
     65 typedef QWidget* PlatformWidget;
     66 #endif
     67 
     68 #if PLATFORM(WX)
     69 class wxWindow;
     70 typedef wxWindow* PlatformWidget;
     71 #endif
     72 
     73 #if PLATFORM(HAIKU)
     74 class BView;
     75 typedef BView* PlatformWidget;
     76 #endif
     77 
     78 #if PLATFORM(CHROMIUM)
     79 #include "PlatformWidget.h"
     80 #endif
     81 
     82 #if PLATFORM(QT)
     83 class QWebPageClient;
     84 typedef QWebPageClient* PlatformPageClient;
     85 #else
     86 typedef PlatformWidget PlatformPageClient;
     87 #endif
     88 
     89 #include "IntPoint.h"
     90 #include "IntRect.h"
     91 #include "IntSize.h"
     92 
     93 #include <wtf/RefCounted.h>
     94 
     95 namespace WebCore {
     96 
     97 class Cursor;
     98 class Event;
     99 class Font;
    100 class GraphicsContext;
    101 class PlatformMouseEvent;
    102 class ScrollView;
    103 class WidgetPrivate;
    104 
    105 // The Widget class serves as a base class for three kinds of objects:
    106 // (1) Scrollable areas (ScrollView)
    107 // (2) Scrollbars (Scrollbar)
    108 // (3) Plugins (PluginView)
    109 //
    110 // A widget may or may not be backed by a platform-specific object (e.g., HWND on Windows, NSView on Mac, QWidget on Qt).
    111 //
    112 // Widgets are connected in a hierarchy, with the restriction that plugins and scrollbars are always leaves of the
    113 // tree.  Only ScrollViews can have children (and therefore the Widget class has no concept of children).
    114 //
    115 // The rules right now for which widgets get platform-specific objects are as follows:
    116 // ScrollView - Mac
    117 // Scrollbar - Mac, Gtk
    118 // Plugin - Mac, Windows (windowed only), Qt (windowed only, widget is an HWND on windows), Gtk (windowed only)
    119 //
    120 class Widget : public RefCounted<Widget> {
    121 public:
    122     Widget(PlatformWidget = 0);
    123     virtual ~Widget();
    124 
    125     PlatformWidget platformWidget() const { return m_widget; }
    126     void setPlatformWidget(PlatformWidget widget)
    127     {
    128         if (widget != m_widget) {
    129             releasePlatformWidget();
    130             m_widget = widget;
    131             retainPlatformWidget();
    132         }
    133     }
    134 
    135     int x() const { return frameRect().x(); }
    136     int y() const { return frameRect().y(); }
    137     int width() const { return frameRect().width(); }
    138     int height() const { return frameRect().height(); }
    139     IntSize size() const { return frameRect().size(); }
    140     IntPoint pos() const { return frameRect().location(); }
    141 
    142     virtual void setFrameRect(const IntRect&);
    143     virtual IntRect frameRect() const;
    144     IntRect boundsRect() const { return IntRect(0, 0, width(),  height()); }
    145 
    146     void resize(int w, int h) { setFrameRect(IntRect(x(), y(), w, h)); }
    147     void resize(const IntSize& s) { setFrameRect(IntRect(pos(), s)); }
    148     void move(int x, int y) { setFrameRect(IntRect(x, y, width(), height())); }
    149     void move(const IntPoint& p) { setFrameRect(IntRect(p, size())); }
    150 
    151     virtual void paint(GraphicsContext*, const IntRect&);
    152     void invalidate() { invalidateRect(boundsRect()); }
    153     virtual void invalidateRect(const IntRect&) = 0;
    154 
    155     virtual void setFocus();
    156 
    157     void setCursor(const Cursor&);
    158 
    159     virtual void show();
    160     virtual void hide();
    161     bool isSelfVisible() const { return m_selfVisible; } // Whether or not we have been explicitly marked as visible or not.
    162     bool isParentVisible() const { return m_parentVisible; } // Whether or not our parent is visible.
    163     bool isVisible() const { return m_selfVisible && m_parentVisible; } // Whether or not we are actually visible.
    164     virtual void setParentVisible(bool visible) { m_parentVisible = visible; }
    165     void setSelfVisible(bool v) { m_selfVisible = v; }
    166 
    167     void setIsSelected(bool);
    168 
    169     virtual bool isFrameView() const { return false; }
    170     virtual bool isPluginView() const { return false; }
    171     // FIXME: The Mac plug-in code should inherit from PluginView. When this happens PluginWidget and PluginView can become one class.
    172     virtual bool isPluginWidget() const { return false; }
    173     virtual bool isScrollbar() const { return false; }
    174 
    175     void removeFromParent();
    176     virtual void setParent(ScrollView* view);
    177     ScrollView* parent() const { return m_parent; }
    178     ScrollView* root() const;
    179 
    180     virtual void handleEvent(Event*) { }
    181 
    182     // It is important for cross-platform code to realize that Mac has flipped coordinates.  Therefore any code
    183     // that tries to convert the location of a rect using the point-based convertFromContainingWindow will end
    184     // up with an inaccurate rect.  Always make sure to use the rect-based convertFromContainingWindow method
    185     // when converting window rects.
    186     IntRect convertToContainingWindow(const IntRect&) const;
    187     IntRect convertFromContainingWindow(const IntRect&) const;
    188 
    189     IntPoint convertToContainingWindow(const IntPoint&) const;
    190     IntPoint convertFromContainingWindow(const IntPoint&) const;
    191 
    192     virtual void frameRectsChanged() {}
    193 
    194 #if PLATFORM(MAC)
    195     NSView* getOuterView() const;
    196 
    197     static void beforeMouseDown(NSView*, Widget*);
    198     static void afterMouseDown(NSView*, Widget*);
    199 
    200     void removeFromSuperview();
    201 #endif
    202 
    203     // Virtual methods to convert points to/from the containing ScrollView
    204     virtual IntRect convertToContainingView(const IntRect&) const;
    205     virtual IntRect convertFromContainingView(const IntRect&) const;
    206     virtual IntPoint convertToContainingView(const IntPoint&) const;
    207     virtual IntPoint convertFromContainingView(const IntPoint&) const;
    208 
    209 private:
    210     void init(PlatformWidget); // Must be called by all Widget constructors to initialize cross-platform data.
    211 
    212     void releasePlatformWidget();
    213     void retainPlatformWidget();
    214 
    215     // These methods are used to convert from the root widget to the containing window,
    216     // which has behavior that may differ between platforms (e.g. Mac uses flipped window coordinates).
    217     static IntRect convertFromRootToContainingWindow(const Widget* rootWidget, const IntRect&);
    218     static IntRect convertFromContainingWindowToRoot(const Widget* rootWidget, const IntRect&);
    219 
    220     static IntPoint convertFromRootToContainingWindow(const Widget* rootWidget, const IntPoint&);
    221     static IntPoint convertFromContainingWindowToRoot(const Widget* rootWidget, const IntPoint&);
    222 
    223 private:
    224     ScrollView* m_parent;
    225     PlatformWidget m_widget;
    226     bool m_selfVisible;
    227     bool m_parentVisible;
    228 
    229     IntRect m_frame; // Not used when a native widget exists.
    230 
    231 #if PLATFORM(MAC)
    232     WidgetPrivate* m_data;
    233 #endif
    234 #if PLATFORM(ANDROID)
    235 public:
    236     int screenWidth() const;
    237 #endif
    238 };
    239 
    240 } // namespace WebCore
    241 
    242 #endif // Widget_h
    243