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