Home | History | Annotate | Download | only in cursors
      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 WEBKIT_COMMON_CURSORS_WEBCURSOR_H_
      6 #define WEBKIT_COMMON_CURSORS_WEBCURSOR_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "third_party/WebKit/public/web/WebCursorInfo.h"
     10 #include "ui/gfx/display.h"
     11 #include "ui/gfx/native_widget_types.h"
     12 #include "ui/gfx/point.h"
     13 #include "ui/gfx/size.h"
     14 #include "webkit/common/webkit_common_export.h"
     15 
     16 #include <vector>
     17 
     18 #if defined(OS_WIN)
     19 typedef struct HINSTANCE__* HINSTANCE;
     20 typedef struct HICON__* HICON;
     21 typedef HICON HCURSOR;
     22 #elif defined(TOOLKIT_GTK)
     23 typedef struct _GdkCursor GdkCursor;
     24 #elif defined(OS_MACOSX)
     25 #ifdef __OBJC__
     26 @class NSCursor;
     27 #else
     28 class NSCursor;
     29 #endif
     30 #endif
     31 
     32 class Pickle;
     33 class PickleIterator;
     34 
     35 // This class encapsulates a cross-platform description of a cursor.  Platform
     36 // specific methods are provided to translate the cross-platform cursor into a
     37 // platform specific cursor.  It is also possible to serialize / de-serialize a
     38 // WebCursor.
     39 class WEBKIT_COMMON_EXPORT WebCursor {
     40  public:
     41   struct CursorInfo {
     42     explicit CursorInfo(WebKit::WebCursorInfo::Type cursor_type)
     43         : type(cursor_type),
     44           image_scale_factor(1) {
     45 #if defined(OS_WIN)
     46       external_handle = NULL;
     47 #endif
     48     }
     49 
     50     CursorInfo()
     51         : type(WebKit::WebCursorInfo::TypePointer),
     52           image_scale_factor(1) {
     53 #if defined(OS_WIN)
     54       external_handle = NULL;
     55 #endif
     56     }
     57 
     58     WebKit::WebCursorInfo::Type type;
     59     gfx::Point hotspot;
     60     float image_scale_factor;
     61     SkBitmap custom_image;
     62 #if defined(OS_WIN)
     63     HCURSOR external_handle;
     64 #endif
     65   };
     66 
     67   WebCursor();
     68   explicit WebCursor(const CursorInfo& cursor_info);
     69   ~WebCursor();
     70 
     71   // Copy constructor/assignment operator combine.
     72   WebCursor(const WebCursor& other);
     73   const WebCursor& operator=(const WebCursor& other);
     74 
     75   // Conversion from/to CursorInfo.
     76   void InitFromCursorInfo(const CursorInfo& cursor_info);
     77   void GetCursorInfo(CursorInfo* cursor_info) const;
     78 
     79   // Serialization / De-serialization
     80   bool Deserialize(PickleIterator* iter);
     81   bool Serialize(Pickle* pickle) const;
     82 
     83   // Returns true if GetCustomCursor should be used to allocate a platform
     84   // specific cursor object.  Otherwise GetCursor should be used.
     85   bool IsCustom() const;
     86 
     87   // Returns true if the current cursor object contains the same cursor as the
     88   // cursor object passed in. If the current cursor is a custom cursor, we also
     89   // compare the bitmaps to verify whether they are equal.
     90   bool IsEqual(const WebCursor& other) const;
     91 
     92   // Returns a native cursor representing the current WebCursor instance.
     93   gfx::NativeCursor GetNativeCursor();
     94 
     95 #if defined(OS_WIN)
     96   // Initialize this from the given Windows cursor. The caller must ensure that
     97   // the HCURSOR remains valid by not invoking the DestroyCursor/DestroyIcon
     98   // APIs on it.
     99   void InitFromExternalCursor(HCURSOR handle);
    100 #endif
    101 
    102 #if defined(USE_AURA)
    103   const ui::PlatformCursor GetPlatformCursor();
    104 
    105   // Updates |device_scale_factor_| and |rotation_| based on |display|.
    106   void SetDisplayInfo(const gfx::Display& display);
    107 
    108 #elif defined(OS_WIN)
    109   // Returns a HCURSOR representing the current WebCursor instance.
    110   // The ownership of the HCURSOR (does not apply to external cursors) remains
    111   // with the WebCursor instance.
    112   HCURSOR GetCursor(HINSTANCE module_handle);
    113 
    114 #elif defined(TOOLKIT_GTK)
    115   // Return the stock GdkCursorType for this cursor, or GDK_CURSOR_IS_PIXMAP
    116   // if it's a custom cursor. Return GDK_LAST_CURSOR to indicate that the cursor
    117   // should be set to the system default.
    118   // Returns an int so we don't need to include GDK headers in this header file.
    119   int GetCursorType() const;
    120 
    121   // Return a new GdkCursor* for this cursor.  Only valid if GetCursorType
    122   // returns GDK_CURSOR_IS_PIXMAP.
    123   GdkCursor* GetCustomCursor();
    124 #elif defined(OS_MACOSX)
    125   // Initialize this from the given Cocoa NSCursor.
    126   void InitFromNSCursor(NSCursor* cursor);
    127 #endif
    128 
    129  private:
    130   // Copies the contents of the WebCursor instance passed in.
    131   void Copy(const WebCursor& other);
    132 
    133   // Cleans up the WebCursor instance.
    134   void Clear();
    135 
    136   // Platform specific initialization goes here.
    137   void InitPlatformData();
    138 
    139   // Platform specific Serialization / De-serialization
    140   bool SerializePlatformData(Pickle* pickle) const;
    141   bool DeserializePlatformData(PickleIterator* iter);
    142 
    143   // Returns true if the platform data in the current cursor object
    144   // matches that of the cursor passed in.
    145   bool IsPlatformDataEqual(const WebCursor& other) const ;
    146 
    147   // Copies platform specific data from the WebCursor instance passed in.
    148   void CopyPlatformData(const WebCursor& other);
    149 
    150   // Platform specific cleanup.
    151   void CleanupPlatformData();
    152 
    153   void SetCustomData(const SkBitmap& image);
    154   void ImageFromCustomData(SkBitmap* image) const;
    155 
    156   // Clamp the hotspot to the custom image's bounds, if this is a custom cursor.
    157   void ClampHotspot();
    158 
    159   // WebCore::PlatformCursor type.
    160   int type_;
    161 
    162   // Hotspot in cursor image in pixels.
    163   gfx::Point hotspot_;
    164 
    165   // Custom cursor data, as 32-bit RGBA.
    166   // Platform-inspecific because it can be serialized.
    167   gfx::Size custom_size_;  // In pixels.
    168   float custom_scale_;
    169   std::vector<char> custom_data_;
    170 
    171 #if defined(OS_WIN)
    172   // An externally generated HCURSOR. We assume that it remains valid, i.e we
    173   // don't attempt to copy the HCURSOR.
    174   HCURSOR external_cursor_;
    175 #endif
    176 
    177 #if defined(USE_AURA) && defined(USE_X11)
    178   // Only used for custom cursors.
    179   ui::PlatformCursor platform_cursor_;
    180   float device_scale_factor_;
    181   gfx::Display::Rotation rotation_;
    182 #elif defined(OS_WIN)
    183   // A custom cursor created from custom bitmap data by Webkit.
    184   HCURSOR custom_cursor_;
    185 #elif defined(TOOLKIT_GTK)
    186   // A custom cursor created that should be unref'ed from the destructor.
    187   GdkCursor* unref_;
    188 #endif
    189 };
    190 
    191 #endif  // WEBKIT_COMMON_CURSORS_WEBCURSOR_H_
    192