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