Home | History | Annotate | Download | only in gfx
      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 
     48 class SkRegion;
     49 namespace aura {
     50 class Window;
     51 }
     52 namespace ui {
     53 class Event;
     54 }
     55 #endif  // defined(USE_AURA)
     56 
     57 #if defined(OS_WIN)
     58 #include <windows.h>  // NOLINT
     59 typedef struct HFONT__* HFONT;
     60 struct IAccessible;
     61 #elif defined(OS_IOS)
     62 struct CGContext;
     63 #ifdef __OBJC__
     64 @class UIEvent;
     65 @class UIFont;
     66 @class UIImage;
     67 @class UIView;
     68 @class UIWindow;
     69 @class UITextField;
     70 #else
     71 class UIEvent;
     72 class UIFont;
     73 class UIImage;
     74 class UIView;
     75 class UIWindow;
     76 class UITextField;
     77 #endif  // __OBJC__
     78 #elif defined(OS_MACOSX)
     79 struct 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
     89 class NSCursor;
     90 class NSEvent;
     91 class NSFont;
     92 class NSImage;
     93 struct NSView;
     94 class NSWindow;
     95 class NSTextField;
     96 #endif  // __OBJC__
     97 #elif defined(OS_POSIX)
     98 typedef struct _PangoFontDescription PangoFontDescription;
     99 typedef struct _cairo cairo_t;
    100 #endif
    101 
    102 #if defined(TOOLKIT_GTK)
    103 typedef struct _GdkCursor GdkCursor;
    104 typedef union _GdkEvent GdkEvent;
    105 typedef struct _GdkPixbuf GdkPixbuf;
    106 typedef struct _GdkRegion GdkRegion;
    107 typedef struct _GtkWidget GtkWidget;
    108 typedef struct _GtkWindow GtkWindow;
    109 #elif defined(OS_ANDROID)
    110 struct ANativeWindow;
    111 namespace ui {
    112 class WindowAndroid;
    113 class ViewAndroid;
    114 }
    115 #endif
    116 class SkBitmap;
    117 
    118 namespace gfx {
    119 
    120 #if defined(USE_AURA)
    121 typedef ui::Cursor NativeCursor;
    122 typedef aura::Window* NativeView;
    123 typedef aura::Window* NativeWindow;
    124 typedef SkRegion* NativeRegion;
    125 typedef ui::Event* NativeEvent;
    126 #elif defined(OS_WIN)
    127 typedef HCURSOR NativeCursor;
    128 typedef HWND NativeView;
    129 typedef HWND NativeWindow;
    130 typedef HRGN NativeRegion;
    131 typedef MSG NativeEvent;
    132 #elif defined(OS_IOS)
    133 typedef void* NativeCursor;
    134 typedef UIView* NativeView;
    135 typedef UIWindow* NativeWindow;
    136 typedef UIEvent* NativeEvent;
    137 #elif defined(OS_MACOSX)
    138 typedef NSCursor* NativeCursor;
    139 typedef NSView* NativeView;
    140 typedef NSWindow* NativeWindow;
    141 typedef NSEvent* NativeEvent;
    142 #elif defined(TOOLKIT_GTK)
    143 typedef GdkCursor* NativeCursor;
    144 typedef GtkWidget* NativeView;
    145 typedef GtkWindow* NativeWindow;
    146 typedef GdkRegion* NativeRegion;
    147 typedef GdkEvent* NativeEvent;
    148 #elif defined(OS_ANDROID)
    149 typedef void* NativeCursor;
    150 typedef ui::ViewAndroid* NativeView;
    151 typedef ui::WindowAndroid* NativeWindow;
    152 typedef void* NativeRegion;
    153 typedef jobject NativeEvent;
    154 #endif
    155 
    156 #if defined(OS_WIN)
    157 typedef HFONT NativeFont;
    158 typedef HWND NativeEditView;
    159 typedef HDC NativeDrawingContext;
    160 typedef IAccessible* NativeViewAccessible;
    161 #elif defined(OS_IOS)
    162 typedef UIFont* NativeFont;
    163 typedef UITextField* NativeEditView;
    164 typedef CGContext* NativeDrawingContext;
    165 #elif defined(OS_MACOSX)
    166 typedef NSFont* NativeFont;
    167 typedef NSTextField* NativeEditView;
    168 typedef CGContext* NativeDrawingContext;
    169 typedef void* NativeViewAccessible;
    170 #elif defined(TOOLKIT_GTK)
    171 typedef PangoFontDescription* NativeFont;
    172 typedef GtkWidget* NativeEditView;
    173 typedef cairo_t* NativeDrawingContext;
    174 typedef void* NativeViewAccessible;
    175 #elif defined(USE_AURA)
    176 typedef PangoFontDescription* NativeFont;
    177 typedef void* NativeEditView;
    178 typedef cairo_t* NativeDrawingContext;
    179 typedef void* NativeViewAccessible;
    180 #elif defined(OS_ANDROID)
    181 typedef void* NativeFont;
    182 typedef void* NativeEditView;
    183 typedef void* NativeDrawingContext;
    184 typedef void* NativeViewAccessible;
    185 #endif
    186 
    187 // A constant value to indicate that gfx::NativeCursor refers to no cursor.
    188 #if defined(USE_AURA)
    189 const int kNullCursor = 0;
    190 #else
    191 const gfx::NativeCursor kNullCursor = static_cast<gfx::NativeCursor>(NULL);
    192 #endif
    193 
    194 #if defined(OS_IOS)
    195 typedef UIImage NativeImageType;
    196 #elif defined(OS_MACOSX)
    197 typedef NSImage NativeImageType;
    198 #elif defined(TOOLKIT_GTK)
    199 typedef GdkPixbuf NativeImageType;
    200 #else
    201 typedef SkBitmap NativeImageType;
    202 #endif
    203 typedef 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.
    210 typedef 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.
    217 static 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.
    231 static 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 
    271 enum SurfaceType {
    272   EMPTY,
    273   NATIVE_DIRECT,
    274   NATIVE_TRANSPORT,
    275   TEXTURE_TRANSPORT
    276 };
    277 
    278 struct 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)
    307 typedef HWND AcceleratedWidget;
    308 const AcceleratedWidget kNullAcceleratedWidget = NULL;
    309 #elif defined(USE_X11)
    310 typedef unsigned long AcceleratedWidget;
    311 const AcceleratedWidget kNullAcceleratedWidget = 0;
    312 #elif defined(OS_IOS)
    313 typedef UIView* AcceleratedWidget;
    314 const AcceleratedWidget kNullAcceleratedWidget = 0;
    315 #elif defined(OS_MACOSX)
    316 typedef NSView* AcceleratedWidget;
    317 const AcceleratedWidget kNullAcceleratedWidget = 0;
    318 #elif defined(OS_ANDROID)
    319 typedef ANativeWindow* AcceleratedWidget;
    320 const AcceleratedWidget kNullAcceleratedWidget = 0;
    321 #elif defined(USE_OZONE)
    322 typedef intptr_t AcceleratedWidget;
    323 const 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