Cleanup: Update the path to gfx size headers.
[chromium-blink-merge.git] / content / common / cursors / webcursor.h
blobcd78c5800043f4fc3d02d1066f27f970c911e5c6
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.
5 #ifndef CONTENT_COMMON_CURSORS_WEBCURSOR_H_
6 #define CONTENT_COMMON_CURSORS_WEBCURSOR_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "content/common/content_export.h"
12 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
13 #include "ui/gfx/display.h"
14 #include "ui/gfx/geometry/point.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/native_widget_types.h"
18 #if defined(USE_AURA)
19 #include "ui/base/cursor/cursor.h"
20 #endif
22 #if defined(OS_WIN)
23 typedef struct HINSTANCE__* HINSTANCE;
24 typedef struct HICON__* HICON;
25 typedef HICON HCURSOR;
26 #elif defined(OS_MACOSX)
27 #ifdef __OBJC__
28 @class NSCursor;
29 #else
30 class NSCursor;
31 #endif
32 #endif
34 class Pickle;
35 class PickleIterator;
37 namespace content {
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 CONTENT_EXPORT WebCursor {
44 public:
45 struct CursorInfo {
46 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
54 CursorInfo()
55 : type(blink::WebCursorInfo::TypePointer),
56 image_scale_factor(1) {
57 #if defined(OS_WIN)
58 external_handle = NULL;
59 #endif
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
71 WebCursor();
72 explicit WebCursor(const CursorInfo& cursor_info);
73 ~WebCursor();
75 // Copy constructor/assignment operator combine.
76 WebCursor(const WebCursor& other);
77 const WebCursor& operator=(const WebCursor& other);
79 // Conversion from/to CursorInfo.
80 void InitFromCursorInfo(const CursorInfo& cursor_info);
81 void GetCursorInfo(CursorInfo* cursor_info) const;
83 // Serialization / De-serialization
84 bool Deserialize(PickleIterator* iter);
85 bool Serialize(Pickle* pickle) const;
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;
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;
96 // Returns a native cursor representing the current WebCursor instance.
97 gfx::NativeCursor GetNativeCursor();
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
106 #if defined(USE_AURA)
107 ui::PlatformCursor GetPlatformCursor();
109 // Updates |device_scale_factor_| and |rotation_| based on |display|.
110 void SetDisplayInfo(const gfx::Display& display);
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);
118 #elif defined(OS_MACOSX)
119 // Initialize this from the given Cocoa NSCursor.
120 void InitFromNSCursor(NSCursor* cursor);
121 #endif
123 private:
124 // Copies the contents of the WebCursor instance passed in.
125 void Copy(const WebCursor& other);
127 // Cleans up the WebCursor instance.
128 void Clear();
130 // Platform specific initialization goes here.
131 void InitPlatformData();
133 // Platform specific Serialization / De-serialization
134 bool SerializePlatformData(Pickle* pickle) const;
135 bool DeserializePlatformData(PickleIterator* iter);
137 // Returns true if the platform data in the current cursor object
138 // matches that of the cursor passed in.
139 bool IsPlatformDataEqual(const WebCursor& other) const ;
141 // Copies platform specific data from the WebCursor instance passed in.
142 void CopyPlatformData(const WebCursor& other);
144 // Platform specific cleanup.
145 void CleanupPlatformData();
147 void SetCustomData(const SkBitmap& image);
148 void ImageFromCustomData(SkBitmap* image) const;
150 // Clamp the hotspot to the custom image's bounds, if this is a custom cursor.
151 void ClampHotspot();
153 // WebCore::PlatformCursor type.
154 int type_;
156 // Hotspot in cursor image in pixels.
157 gfx::Point hotspot_;
159 // Custom cursor data, as 32-bit RGBA.
160 // Platform-inspecific because it can be serialized.
161 gfx::Size custom_size_; // In pixels.
162 float custom_scale_;
163 std::vector<char> custom_data_;
165 #if defined(OS_WIN)
166 // An externally generated HCURSOR. We assume that it remains valid, i.e we
167 // don't attempt to copy the HCURSOR.
168 HCURSOR external_cursor_;
169 #endif
171 #if defined(USE_AURA) && (defined(USE_X11) || defined(USE_OZONE))
172 // Only used for custom cursors.
173 ui::PlatformCursor platform_cursor_;
174 float device_scale_factor_;
175 #elif defined(OS_WIN)
176 // A custom cursor created from custom bitmap data by Webkit.
177 HCURSOR custom_cursor_;
178 #endif
180 #if defined(USE_OZONE)
181 gfx::Display::Rotation rotation_;
182 #endif
185 } // namespace content
187 #endif // CONTENT_COMMON_CURSORS_WEBCURSOR_H_