blob: d717be3ccfab304aae59b6fc18035bffaff7b025 [file] [log] [blame]
[email protected]5dd07862012-02-16 23:22:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitf5b16fe2008-07-27 00:20:514
[email protected]c2809346d2014-03-20 00:11:035#ifndef CONTENT_COMMON_CURSORS_WEBCURSOR_H_
6#define CONTENT_COMMON_CURSORS_WEBCURSOR_H_
7
8#include <vector>
initial.commitf5b16fe2008-07-27 00:20:519
[email protected]cf4e5eb12009-01-09 01:20:3810#include "base/basictypes.h"
[email protected]c2809346d2014-03-20 00:11:0311#include "content/common/content_export.h"
[email protected]ec173b522013-11-14 11:01:1812#include "third_party/WebKit/public/platform/WebCursorInfo.h"
[email protected]ee37dc652013-08-06 00:33:2713#include "ui/gfx/display.h"
[email protected]08397d52011-02-05 01:53:3814#include "ui/gfx/native_widget_types.h"
15#include "ui/gfx/point.h"
16#include "ui/gfx/size.h"
[email protected]67b25052008-10-23 21:57:5117
[email protected]84f43c972013-09-16 23:08:1918#if defined(USE_AURA)
19#include "ui/base/cursor/cursor.h"
20#endif
21
[email protected]72c25752008-10-09 22:02:5222#if defined(OS_WIN)
[email protected]4c870b42008-11-06 00:36:5223typedef struct HINSTANCE__* HINSTANCE;
24typedef struct HICON__* HICON;
25typedef HICON HCURSOR;
[email protected]a13283cc2012-04-05 00:21:2226#elif defined(TOOLKIT_GTK)
[email protected]031e4d32009-12-29 01:13:2327typedef struct _GdkCursor GdkCursor;
[email protected]01f734512008-11-18 21:00:0428#elif defined(OS_MACOSX)
29#ifdef __OBJC__
30@class NSCursor;
31#else
32class NSCursor;
33#endif
[email protected]aa9811822008-10-09 21:34:4534#endif
[email protected]67b25052008-10-23 21:57:5135
[email protected]4c870b42008-11-06 00:36:5236class Pickle;
[email protected]ce208f872012-03-07 20:42:5637class PickleIterator;
initial.commitf5b16fe2008-07-27 00:20:5138
[email protected]c2809346d2014-03-20 00:11:0339namespace content {
40
[email protected]4c870b42008-11-06 00:36:5241// 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]c2809346d2014-03-20 00:11:0345class CONTENT_EXPORT WebCursor {
[email protected]4c870b42008-11-06 00:36:5246 public:
[email protected]9ec87712013-05-24 23:23:5247 struct CursorInfo {
[email protected]c32e27082013-11-07 06:58:2048 explicit CursorInfo(blink::WebCursorInfo::Type cursor_type)
[email protected]9ec87712013-05-24 23:23:5249 : 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]c32e27082013-11-07 06:58:2057 : type(blink::WebCursorInfo::TypePointer),
[email protected]9ec87712013-05-24 23:23:5258 image_scale_factor(1) {
59#if defined(OS_WIN)
60 external_handle = NULL;
61#endif
62 }
63
[email protected]c32e27082013-11-07 06:58:2064 blink::WebCursorInfo::Type type;
[email protected]9ec87712013-05-24 23:23:5265 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]4c870b42008-11-06 00:36:5273 WebCursor();
[email protected]9ec87712013-05-24 23:23:5274 explicit WebCursor(const CursorInfo& cursor_info);
[email protected]cf4e5eb12009-01-09 01:20:3875 ~WebCursor();
76
77 // Copy constructor/assignment operator combine.
78 WebCursor(const WebCursor& other);
79 const WebCursor& operator=(const WebCursor& other);
[email protected]4c870b42008-11-06 00:36:5280
[email protected]9ec87712013-05-24 23:23:5281 // Conversion from/to CursorInfo.
82 void InitFromCursorInfo(const CursorInfo& cursor_info);
83 void GetCursorInfo(CursorInfo* cursor_info) const;
[email protected]7c51b0ee2009-07-08 21:49:3084
[email protected]4c870b42008-11-06 00:36:5285 // Serialization / De-serialization
[email protected]ce208f872012-03-07 20:42:5686 bool Deserialize(PickleIterator* iter);
[email protected]4c870b42008-11-06 00:36:5287 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.commitf5b16fe2008-07-27 00:20:5196 bool IsEqual(const WebCursor& other) const;
[email protected]4c870b42008-11-06 00:36:5297
[email protected]f0b6b482010-11-23 00:55:2898 // Returns a native cursor representing the current WebCursor instance.
99 gfx::NativeCursor GetNativeCursor();
100
[email protected]47dfa922012-09-12 19:11:36101#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]c7839552012-04-03 21:14:36108#if defined(USE_AURA)
109 const ui::PlatformCursor GetPlatformCursor();
[email protected]70050e72012-07-10 11:38:52110
[email protected]ee37dc652013-08-06 00:33:27111 // Updates |device_scale_factor_| and |rotation_| based on |display|.
112 void SetDisplayInfo(const gfx::Display& display);
113
[email protected]c7839552012-04-03 21:14:36114#elif defined(OS_WIN)
[email protected]cf4e5eb12009-01-09 01:20:38115 // 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]4c870b42008-11-06 00:36:52119
[email protected]a13283cc2012-04-05 00:21:22120#elif defined(TOOLKIT_GTK)
[email protected]be18d3e2008-11-11 18:28:50121 // Return the stock GdkCursorType for this cursor, or GDK_CURSOR_IS_PIXMAP
[email protected]4b576872009-03-25 18:29:00122 // 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]031e4d32009-12-29 01:13:23124 // Returns an int so we don't need to include GDK headers in this header file.
125 int GetCursorType() const;
[email protected]2b99be22008-11-14 18:45:26126
127 // Return a new GdkCursor* for this cursor. Only valid if GetCursorType
128 // returns GDK_CURSOR_IS_PIXMAP.
[email protected]f0b6b482010-11-23 00:55:28129 GdkCursor* GetCustomCursor();
[email protected]01f734512008-11-18 21:00:04130#elif defined(OS_MACOSX)
[email protected]fd4a06b2010-01-19 22:04:32131 // Initialize this from the given Cocoa NSCursor.
132 void InitFromNSCursor(NSCursor* cursor);
[email protected]67b25052008-10-23 21:57:51133#endif
134
[email protected]4c870b42008-11-06 00:36:52135 private:
[email protected]cf4e5eb12009-01-09 01:20:38136 // 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]ce208f872012-03-07 20:42:56147 bool DeserializePlatformData(PickleIterator* iter);
[email protected]cf4e5eb12009-01-09 01:20:38148
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]9ec87712013-05-24 23:23:52159 void SetCustomData(const SkBitmap& image);
160 void ImageFromCustomData(SkBitmap* image) const;
[email protected]4c870b42008-11-06 00:36:52161
[email protected]4605be72010-08-11 04:17:09162 // Clamp the hotspot to the custom image's bounds, if this is a custom cursor.
163 void ClampHotspot();
164
[email protected]2b99be22008-11-14 18:45:26165 // WebCore::PlatformCursor type.
[email protected]4c870b42008-11-06 00:36:52166 int type_;
[email protected]2b99be22008-11-14 18:45:26167
[email protected]f3d63422012-12-15 02:34:29168 // Hotspot in cursor image in pixels.
[email protected]4c870b42008-11-06 00:36:52169 gfx::Point hotspot_;
[email protected]2b99be22008-11-14 18:45:26170
171 // Custom cursor data, as 32-bit RGBA.
172 // Platform-inspecific because it can be serialized.
[email protected]f3d63422012-12-15 02:34:29173 gfx::Size custom_size_; // In pixels.
[email protected]065aef022012-12-07 16:59:53174 float custom_scale_;
[email protected]4c870b42008-11-06 00:36:52175 std::vector<char> custom_data_;
[email protected]cf4e5eb12009-01-09 01:20:38176
[email protected]47dfa922012-09-12 19:11:36177#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]c7839552012-04-03 21:14:36183#if defined(USE_AURA) && defined(USE_X11)
184 // Only used for custom cursors.
185 ui::PlatformCursor platform_cursor_;
[email protected]065aef022012-12-07 16:59:53186 float device_scale_factor_;
[email protected]ee37dc652013-08-06 00:33:27187 gfx::Display::Rotation rotation_;
[email protected]c7839552012-04-03 21:14:36188#elif defined(OS_WIN)
[email protected]cf4e5eb12009-01-09 01:20:38189 // A custom cursor created from custom bitmap data by Webkit.
190 HCURSOR custom_cursor_;
[email protected]a13283cc2012-04-05 00:21:22191#elif defined(TOOLKIT_GTK)
[email protected]f0b6b482010-11-23 00:55:28192 // A custom cursor created that should be unref'ed from the destructor.
193 GdkCursor* unref_;
194#endif
initial.commitf5b16fe2008-07-27 00:20:51195};
196
[email protected]c2809346d2014-03-20 00:11:03197} // namespace content
198
199#endif // CONTENT_COMMON_CURSORS_WEBCURSOR_H_