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