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