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