native_widget_types.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_GFX_NATIVE_WIDGET_TYPES_H_
6#define UI_GFX_NATIVE_WIDGET_TYPES_H_
7
8#include "build/build_config.h"
9
10#if defined(OS_ANDROID)
11#include <jni.h>
12#endif
13
14#include "base/basictypes.h"
15#include "base/logging.h"
16#include "ui/base/ui_export.h"
17
18// This file provides cross platform typedefs for native widget types.
19//   NativeWindow: this is a handle to a native, top-level window
20//   NativeView: this is a handle to a native UI element. It may be the
21//     same type as a NativeWindow on some platforms.
22//   NativeViewId: Often, in our cross process model, we need to pass around a
23//     reference to a "window". This reference will, say, be echoed back from a
24//     renderer to the browser when it wishes to query its size. On Windows we
25//     use an HWND for this.
26//
27//     As a rule of thumb - if you're in the renderer, you should be dealing
28//     with NativeViewIds. This should remind you that you shouldn't be doing
29//     direct operations on platform widgets from the renderer process.
30//
31//     If you're in the browser, you're probably dealing with NativeViews,
32//     unless you're in the IPC layer, which will be translating between
33//     NativeViewIds from the renderer and NativeViews.
34//
35//   NativeEditView: a handle to a native edit-box. The Mac folks wanted this
36//     specific typedef.
37//
38//   NativeImage: The platform-specific image type used for drawing UI elements
39//     in the browser.
40//
41// The name 'View' here meshes with OS X where the UI elements are called
42// 'views' and with our Chrome UI code where the elements are also called
43// 'views'.
44
45#if defined(USE_AURA)
46#include "ui/base/cursor/cursor.h"
47
48class SkRegion;
49namespace aura {
50class Window;
51}
52namespace ui {
53class Event;
54}
55#endif  // defined(USE_AURA)
56
57#if defined(OS_WIN)
58#include <windows.h>  // NOLINT
59typedef struct HFONT__* HFONT;
60struct IAccessible;
61#elif defined(OS_IOS)
62struct CGContext;
63#ifdef __OBJC__
64@class UIEvent;
65@class UIFont;
66@class UIImage;
67@class UIView;
68@class UIWindow;
69@class UITextField;
70#else
71class UIEvent;
72class UIFont;
73class UIImage;
74class UIView;
75class UIWindow;
76class UITextField;
77#endif  // __OBJC__
78#elif defined(OS_MACOSX)
79struct CGContext;
80#ifdef __OBJC__
81@class NSCursor;
82@class NSEvent;
83@class NSFont;
84@class NSImage;
85@class NSView;
86@class NSWindow;
87@class NSTextField;
88#else
89class NSCursor;
90class NSEvent;
91class NSFont;
92class NSImage;
93struct NSView;
94class NSWindow;
95class NSTextField;
96#endif  // __OBJC__
97#elif defined(OS_POSIX)
98typedef struct _PangoFontDescription PangoFontDescription;
99typedef struct _cairo cairo_t;
100#endif
101
102#if defined(TOOLKIT_GTK)
103typedef struct _GdkCursor GdkCursor;
104typedef union _GdkEvent GdkEvent;
105typedef struct _GdkPixbuf GdkPixbuf;
106typedef struct _GdkRegion GdkRegion;
107typedef struct _GtkWidget GtkWidget;
108typedef struct _GtkWindow GtkWindow;
109#elif defined(OS_ANDROID)
110struct ANativeWindow;
111namespace ui {
112class WindowAndroid;
113class ViewAndroid;
114}
115#endif
116class SkBitmap;
117
118namespace gfx {
119
120#if defined(USE_AURA)
121typedef ui::Cursor NativeCursor;
122typedef aura::Window* NativeView;
123typedef aura::Window* NativeWindow;
124typedef SkRegion* NativeRegion;
125typedef ui::Event* NativeEvent;
126#elif defined(OS_WIN)
127typedef HCURSOR NativeCursor;
128typedef HWND NativeView;
129typedef HWND NativeWindow;
130typedef HRGN NativeRegion;
131typedef MSG NativeEvent;
132#elif defined(OS_IOS)
133typedef void* NativeCursor;
134typedef UIView* NativeView;
135typedef UIWindow* NativeWindow;
136typedef UIEvent* NativeEvent;
137#elif defined(OS_MACOSX)
138typedef NSCursor* NativeCursor;
139typedef NSView* NativeView;
140typedef NSWindow* NativeWindow;
141typedef NSEvent* NativeEvent;
142#elif defined(TOOLKIT_GTK)
143typedef GdkCursor* NativeCursor;
144typedef GtkWidget* NativeView;
145typedef GtkWindow* NativeWindow;
146typedef GdkRegion* NativeRegion;
147typedef GdkEvent* NativeEvent;
148#elif defined(OS_ANDROID)
149typedef void* NativeCursor;
150typedef ui::ViewAndroid* NativeView;
151typedef ui::WindowAndroid* NativeWindow;
152typedef void* NativeRegion;
153typedef jobject NativeEvent;
154#endif
155
156#if defined(OS_WIN)
157typedef HFONT NativeFont;
158typedef HWND NativeEditView;
159typedef HDC NativeDrawingContext;
160typedef IAccessible* NativeViewAccessible;
161#elif defined(OS_IOS)
162typedef UIFont* NativeFont;
163typedef UITextField* NativeEditView;
164typedef CGContext* NativeDrawingContext;
165#elif defined(OS_MACOSX)
166typedef NSFont* NativeFont;
167typedef NSTextField* NativeEditView;
168typedef CGContext* NativeDrawingContext;
169typedef void* NativeViewAccessible;
170#elif defined(TOOLKIT_GTK)
171typedef PangoFontDescription* NativeFont;
172typedef GtkWidget* NativeEditView;
173typedef cairo_t* NativeDrawingContext;
174typedef void* NativeViewAccessible;
175#elif defined(USE_AURA)
176typedef PangoFontDescription* NativeFont;
177typedef void* NativeEditView;
178typedef cairo_t* NativeDrawingContext;
179typedef void* NativeViewAccessible;
180#elif defined(OS_ANDROID)
181typedef void* NativeFont;
182typedef void* NativeEditView;
183typedef void* NativeDrawingContext;
184typedef void* NativeViewAccessible;
185#endif
186
187// A constant value to indicate that gfx::NativeCursor refers to no cursor.
188#if defined(USE_AURA)
189const int kNullCursor = 0;
190#else
191const gfx::NativeCursor kNullCursor = static_cast<gfx::NativeCursor>(NULL);
192#endif
193
194#if defined(OS_IOS)
195typedef UIImage NativeImageType;
196#elif defined(OS_MACOSX)
197typedef NSImage NativeImageType;
198#elif defined(TOOLKIT_GTK)
199typedef GdkPixbuf NativeImageType;
200#else
201typedef SkBitmap NativeImageType;
202#endif
203typedef NativeImageType* NativeImage;
204
205// Note: for test_shell we're packing a pointer into the NativeViewId. So, if
206// you make it a type which is smaller than a pointer, you have to fix
207// test_shell.
208//
209// See comment at the top of the file for usage.
210typedef intptr_t NativeViewId;
211
212#if defined(OS_WIN) && !defined(USE_AURA)
213// Convert a NativeViewId to a NativeView.
214//
215// On Windows, we pass an HWND into the renderer. As stated above, the renderer
216// should not be performing operations on the view.
217static inline NativeView NativeViewFromId(NativeViewId id) {
218  return reinterpret_cast<NativeView>(id);
219}
220#define NativeViewFromIdInBrowser(x) NativeViewFromId(x)
221#elif defined(OS_POSIX) || defined(USE_AURA)
222// On Mac, Linux and USE_AURA, a NativeView is a pointer to an object, and is
223// useless outside the process in which it was created. NativeViewFromId should
224// only be used inside the appropriate platform ifdef outside of the browser.
225// (NativeViewFromIdInBrowser can be used everywhere in the browser.) If your
226// cross-platform design involves a call to NativeViewFromId from outside the
227// browser it will never work on Mac or Linux and is fundamentally broken.
228
229// Please do not call this from outside the browser. It won't work; the name
230// should give you a subtle hint.
231static inline NativeView NativeViewFromIdInBrowser(NativeViewId id) {
232  return reinterpret_cast<NativeView>(id);
233}
234#endif  // defined(OS_POSIX)
235
236// PluginWindowHandle is an abstraction wrapping "the types of windows
237// used by NPAPI plugins".  On Windows it's an HWND, on X it's an X
238// window id.
239#if defined(OS_WIN)
240  typedef HWND PluginWindowHandle;
241  const PluginWindowHandle kNullPluginWindow = NULL;
242#elif defined(USE_X11)
243  typedef unsigned long PluginWindowHandle;
244  const PluginWindowHandle kNullPluginWindow = 0;
245#elif defined(USE_AURA) && defined(OS_MACOSX)
246  // Mac-Aura uses NSView-backed GLSurface.  Regular Mac does not.
247  // TODO(dhollowa): Rationalize these two definitions. http://crbug.com/104551.
248  typedef NSView* PluginWindowHandle;
249  const PluginWindowHandle kNullPluginWindow = 0;
250#elif defined(OS_ANDROID)
251  typedef uint64 PluginWindowHandle;
252  const PluginWindowHandle kNullPluginWindow = 0;
253#elif defined(USE_OZONE)
254  typedef intptr_t PluginWindowHandle;
255  const PluginWindowHandle kNullPluginWindow = 0;
256#else
257  // On OS X we don't have windowed plugins.
258  // We use a NULL/0 PluginWindowHandle in shared code to indicate there
259  // is no window present, so mirror that behavior here.
260  //
261  // The GPU plugin is currently an exception to this rule. As of this
262  // writing it uses some NPAPI infrastructure, and minimally we need
263  // to identify the plugin instance via this window handle. When the
264  // GPU plugin becomes a full-on GPU process, this typedef can be
265  // returned to a bool. For now we use a type large enough to hold a
266  // pointer on 64-bit architectures in case we need this capability.
267  typedef uint64 PluginWindowHandle;
268  const PluginWindowHandle kNullPluginWindow = 0;
269#endif
270
271enum SurfaceType {
272  EMPTY,
273  NATIVE_DIRECT,
274  NATIVE_TRANSPORT,
275  TEXTURE_TRANSPORT
276};
277
278struct GLSurfaceHandle {
279  GLSurfaceHandle()
280      : handle(kNullPluginWindow),
281        transport_type(EMPTY),
282        parent_gpu_process_id(0),
283        parent_client_id(0) {
284  }
285  GLSurfaceHandle(PluginWindowHandle handle_, SurfaceType transport_)
286      : handle(handle_),
287        transport_type(transport_),
288        parent_gpu_process_id(0),
289        parent_client_id(0) {
290    DCHECK(!is_null() || handle == kNullPluginWindow);
291    DCHECK(transport_type != TEXTURE_TRANSPORT ||
292           handle == kNullPluginWindow);
293  }
294  bool is_null() const { return transport_type == EMPTY; }
295  bool is_transport() const {
296    return transport_type == NATIVE_TRANSPORT ||
297           transport_type == TEXTURE_TRANSPORT;
298  }
299  PluginWindowHandle handle;
300  SurfaceType transport_type;
301  int parent_gpu_process_id;
302  uint32 parent_client_id;
303};
304
305// AcceleratedWidget provides a surface to compositors to paint pixels.
306#if defined(OS_WIN)
307typedef HWND AcceleratedWidget;
308const AcceleratedWidget kNullAcceleratedWidget = NULL;
309#elif defined(USE_X11)
310typedef unsigned long AcceleratedWidget;
311const AcceleratedWidget kNullAcceleratedWidget = 0;
312#elif defined(OS_IOS)
313typedef UIView* AcceleratedWidget;
314const AcceleratedWidget kNullAcceleratedWidget = 0;
315#elif defined(OS_MACOSX)
316typedef NSView* AcceleratedWidget;
317const AcceleratedWidget kNullAcceleratedWidget = 0;
318#elif defined(OS_ANDROID)
319typedef ANativeWindow* AcceleratedWidget;
320const AcceleratedWidget kNullAcceleratedWidget = 0;
321#elif defined(USE_OZONE)
322typedef intptr_t AcceleratedWidget;
323const AcceleratedWidget kNullAcceleratedWidget = 0;
324#else
325#error unknown platform
326#endif
327
328}  // namespace gfx
329
330#endif  // UI_GFX_NATIVE_WIDGET_TYPES_H_
331