summaryrefslogtreecommitdiffstats
path: root/content/common/cursors/webcursor.h
blob: 3efc2ee234cd0df92d9bfe4781d5f791843e4923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_COMMON_CURSORS_WEBCURSOR_H_
#define CONTENT_COMMON_CURSORS_WEBCURSOR_H_

#include <vector>

#include "base/basictypes.h"
#include "content/common/content_export.h"
#include "third_party/WebKit/public/platform/WebCursorInfo.h"
#include "ui/gfx/display.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/native_widget_types.h"

#if defined(USE_AURA)
#include "ui/base/cursor/cursor.h"
#endif

#if defined(OS_WIN)
typedef struct HINSTANCE__* HINSTANCE;
typedef struct HICON__* HICON;
typedef HICON HCURSOR;
#elif defined(OS_MACOSX)
#ifdef __OBJC__
@class NSCursor;
#else
class NSCursor;
#endif
#endif

namespace base {
class Pickle;
class PickleIterator;
}

namespace content {

// This class encapsulates a cross-platform description of a cursor.  Platform
// specific methods are provided to translate the cross-platform cursor into a
// platform specific cursor.  It is also possible to serialize / de-serialize a
// WebCursor.
class CONTENT_EXPORT WebCursor {
 public:
  struct CursorInfo {
    explicit CursorInfo(blink::WebCursorInfo::Type cursor_type)
        : type(cursor_type),
          image_scale_factor(1) {
#if defined(OS_WIN)
      external_handle = NULL;
#endif
    }

    CursorInfo()
        : type(blink::WebCursorInfo::TypePointer),
          image_scale_factor(1) {
#if defined(OS_WIN)
      external_handle = NULL;
#endif
    }

    blink::WebCursorInfo::Type type;
    gfx::Point hotspot;
    float image_scale_factor;
    SkBitmap custom_image;
#if defined(OS_WIN)
    HCURSOR external_handle;
#endif
  };

  WebCursor();
  explicit WebCursor(const CursorInfo& cursor_info);
  ~WebCursor();

  // Copy constructor/assignment operator combine.
  WebCursor(const WebCursor& other);
  const WebCursor& operator=(const WebCursor& other);

  // Conversion from/to CursorInfo.
  void InitFromCursorInfo(const CursorInfo& cursor_info);
  void GetCursorInfo(CursorInfo* cursor_info) const;

  // Serialization / De-serialization
  bool Deserialize(base::PickleIterator* iter);
  bool Serialize(base::Pickle* pickle) const;

  // Returns true if GetCustomCursor should be used to allocate a platform
  // specific cursor object.  Otherwise GetCursor should be used.
  bool IsCustom() const;

  // Returns true if the current cursor object contains the same cursor as the
  // cursor object passed in. If the current cursor is a custom cursor, we also
  // compare the bitmaps to verify whether they are equal.
  bool IsEqual(const WebCursor& other) const;

  // Returns a native cursor representing the current WebCursor instance.
  gfx::NativeCursor GetNativeCursor();

#if defined(OS_WIN)
  // Initialize this from the given Windows cursor. The caller must ensure that
  // the HCURSOR remains valid by not invoking the DestroyCursor/DestroyIcon
  // APIs on it.
  void InitFromExternalCursor(HCURSOR handle);
#endif

#if defined(USE_AURA)
  ui::PlatformCursor GetPlatformCursor();

  // Updates |device_scale_factor_| and |rotation_| based on |display|.
  void SetDisplayInfo(const gfx::Display& display);

#elif defined(OS_WIN)
  // Returns a HCURSOR representing the current WebCursor instance.
  // The ownership of the HCURSOR (does not apply to external cursors) remains
  // with the WebCursor instance.
  HCURSOR GetCursor(HINSTANCE module_handle);

#elif defined(OS_MACOSX)
  // Initialize this from the given Cocoa NSCursor.
  void InitFromNSCursor(NSCursor* cursor);
#endif

 private:
  // Copies the contents of the WebCursor instance passed in.
  void Copy(const WebCursor& other);

  // Cleans up the WebCursor instance.
  void Clear();

  // Platform specific initialization goes here.
  void InitPlatformData();

  // Platform specific Serialization / De-serialization
  bool SerializePlatformData(base::Pickle* pickle) const;
  bool DeserializePlatformData(base::PickleIterator* iter);

  // Returns true if the platform data in the current cursor object
  // matches that of the cursor passed in.
  bool IsPlatformDataEqual(const WebCursor& other) const ;

  // Copies platform specific data from the WebCursor instance passed in.
  void CopyPlatformData(const WebCursor& other);

  // Platform specific cleanup.
  void CleanupPlatformData();

  void SetCustomData(const SkBitmap& image);
  void ImageFromCustomData(SkBitmap* image) const;

  // Clamp the hotspot to the custom image's bounds, if this is a custom cursor.
  void ClampHotspot();

  // WebCore::PlatformCursor type.
  int type_;

  // Hotspot in cursor image in pixels.
  gfx::Point hotspot_;

  // Custom cursor data, as 32-bit RGBA.
  // Platform-inspecific because it can be serialized.
  gfx::Size custom_size_;  // In pixels.
  float custom_scale_;
  std::vector<char> custom_data_;

#if defined(OS_WIN)
  // An externally generated HCURSOR. We assume that it remains valid, i.e we
  // don't attempt to copy the HCURSOR.
  HCURSOR external_cursor_;
#endif

#if defined(USE_AURA) && (defined(USE_X11) || defined(USE_OZONE))
  // Only used for custom cursors.
  ui::PlatformCursor platform_cursor_;
  float device_scale_factor_;
#elif defined(OS_WIN)
  // A custom cursor created from custom bitmap data by Webkit.
  HCURSOR custom_cursor_;
#endif

#if defined(USE_OZONE)
  gfx::Display::Rotation rotation_;
#endif
};

}  // namespace content

#endif  // CONTENT_COMMON_CURSORS_WEBCURSOR_H_