summaryrefslogtreecommitdiffstats
path: root/webkit/glue/webcursor_mac.mm
blob: 90ff096ed5b246dd678e96bfb7253d623fb53127 (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
190
191
192
193
194
195
196
197
// Copyright (c) 2008 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.

#include "webkit/glue/webcursor.h"

#import <AppKit/AppKit.h>

#include "config.h"
#include "PlatformCursor.h"
#include "RetainPtr.h"

#undef LOG
#include "base/logging.h"

using WebCore::PlatformCursor;

namespace {

// TODO(avi): Is loading resources what we want to do here?
NSCursor* LoadCursor(const char* name, int x, int y) {
  NSString* file_name = [NSString stringWithUTF8String:name];
  DCHECK(file_name);
  NSImage* cursor_image = [NSImage imageNamed:file_name];
  DCHECK(cursor_image);
  return [[[NSCursor alloc] initWithImage:cursor_image
                                  hotSpot:NSMakePoint(x, y)] autorelease];
}

NSCursor* CreateCustomCursor(const std::vector<char>& custom_data,
                             const gfx::Size& custom_size,
                             const gfx::Point& hotspot) {
  RetainPtr<CGColorSpace> cg_color(AdoptCF, CGColorSpaceCreateDeviceRGB());
  // this is safe since we're not going to draw into the context we're creating
  void* data = const_cast<void *>(static_cast<const void*>(&custom_data[0]));
  // settings here match SetCustomData() below; keep in sync
  RetainPtr<CGContextRef> context(AdoptCF, CGBitmapContextCreate(
                                             data,
                                             custom_size.width(),
                                             custom_size.height(),
                                             8,
                                             custom_size.width()*4,
                                             cg_color.get(),
                                             kCGImageAlphaPremultipliedLast |
                                             kCGBitmapByteOrder32Big));
  RetainPtr<CGImage> cg_image(AdoptCF,
                              CGBitmapContextCreateImage(context.get()));

  RetainPtr<NSBitmapImageRep> ns_bitmap(
      AdoptNS, [[NSBitmapImageRep alloc] initWithCGImage:cg_image.get()]);
  RetainPtr<NSImage> cursor_image([[NSImage alloc] init]);
  [cursor_image.get() addRepresentation:ns_bitmap.get()];
  DCHECK(cursor_image);
  return [[[NSCursor alloc] initWithImage:cursor_image.get()
                                  hotSpot:NSMakePoint(hotspot.x(),
                                                      hotspot.y())]
          autorelease];
}

}

// We're matching Safari's cursor choices; see platform/mac/CursorMac.mm
NSCursor* WebCursor::GetCursor() const {
  switch (type_) {
    case PlatformCursor::TypePointer:
      return [NSCursor arrowCursor];
    case PlatformCursor::TypeCross:
      return LoadCursor("crossHairCursor", 11, 11);
    case PlatformCursor::TypeHand:
      return LoadCursor("linkCursor", 6, 1);
    case PlatformCursor::TypeIBeam:
      return [NSCursor IBeamCursor];
    case PlatformCursor::TypeWait:
      return LoadCursor("waitCursor", 7, 7);
    case PlatformCursor::TypeHelp:
      return LoadCursor("helpCursor", 8, 8);
    case PlatformCursor::TypeEastResize:
    case PlatformCursor::TypeEastPanning:
      return LoadCursor("eastResizeCursor", 14, 7);
    case PlatformCursor::TypeNorthResize:
    case PlatformCursor::TypeNorthPanning:
      return LoadCursor("northResizeCursor", 7, 1);
    case PlatformCursor::TypeNorthEastResize:
    case PlatformCursor::TypeNorthEastPanning:
      return LoadCursor("northEastResizeCursor", 14, 1);
    case PlatformCursor::TypeNorthWestResize:
    case PlatformCursor::TypeNorthWestPanning:
      return LoadCursor("northWestResizeCursor", 0, 0);
    case PlatformCursor::TypeSouthResize:
    case PlatformCursor::TypeSouthPanning:
      return LoadCursor("southResizeCursor", 7, 14);
    case PlatformCursor::TypeSouthEastResize:
    case PlatformCursor::TypeSouthEastPanning:
      return LoadCursor("southEastResizeCursor", 14, 14);
    case PlatformCursor::TypeSouthWestResize:
    case PlatformCursor::TypeSouthWestPanning:
      return LoadCursor("southWestResizeCursor", 1, 14);
    case PlatformCursor::TypeWestResize:
    case PlatformCursor::TypeWestPanning:
      return LoadCursor("westResizeCursor", 1, 7);
    case PlatformCursor::TypeNorthSouthResize:
      return LoadCursor("northSouthResizeCursor", 7, 7);
    case PlatformCursor::TypeEastWestResize:
      return LoadCursor("eastWestResizeCursor", 7, 7);
    case PlatformCursor::TypeNorthEastSouthWestResize:
      return LoadCursor("northEastSouthWestResizeCursor", 7, 7);
    case PlatformCursor::TypeNorthWestSouthEastResize:
      return LoadCursor("northWestSouthEastResizeCursor", 7, 7);
    case PlatformCursor::TypeColumnResize:
      return [NSCursor resizeLeftRightCursor];
    case PlatformCursor::TypeRowResize:
      return [NSCursor resizeUpDownCursor];
    case PlatformCursor::TypeMiddlePanning:
    case PlatformCursor::TypeMove:
      return LoadCursor("moveCursor", 7, 7);
    case PlatformCursor::TypeVerticalText:
      return LoadCursor("verticalTextCursor", 7, 7);
    case PlatformCursor::TypeCell:
      return LoadCursor("cellCursor", 7, 7);
    case PlatformCursor::TypeContextMenu:
      return LoadCursor("contextMenuCursor", 3, 2);
    case PlatformCursor::TypeAlias:
      return LoadCursor("aliasCursor", 11, 3);
    case PlatformCursor::TypeProgress:
      return LoadCursor("progressCursor", 3, 2);
    case PlatformCursor::TypeNoDrop:
      return LoadCursor("noDropCursor", 3, 1);
    case PlatformCursor::TypeCopy:
      return LoadCursor("copyCursor", 3, 2);
    case PlatformCursor::TypeNone:
      return LoadCursor("noneCursor", 7, 7);
    case PlatformCursor::TypeNotAllowed:
      return LoadCursor("notAllowedCursor", 11, 11);
    case PlatformCursor::TypeZoomIn:
      return LoadCursor("zoomInCursor", 7, 7);
    case PlatformCursor::TypeZoomOut:
      return LoadCursor("zoomOutCursor", 7, 7);
    case PlatformCursor::TypeCustom:
      return CreateCustomCursor(custom_data_, custom_size_, hotspot_);
  }
  NOTREACHED();
  return nil;
}

void WebCursor::SetCustomData(WebCore::Image* image) {
  WebCore::NativeImagePtr image_ptr = image->nativeImageForCurrentFrame();
  if (!image_ptr)
    return;

  RetainPtr<CGColorSpace> cg_color(AdoptCF, CGColorSpaceCreateDeviceRGB());

  size_t size = CGImageGetHeight(image_ptr)*CGImageGetWidth(image_ptr)*4;
  custom_data_.resize(size);
  custom_size_.set_width(CGImageGetWidth(image_ptr));
  custom_size_.set_height(CGImageGetHeight(image_ptr));

  // These settings match up with the code in CreateCustomCursor() above; keep
  // them in sync.
  // TODO(avi): test to ensure that the flags here are correct for RGBA
  RetainPtr<CGContextRef> context(AdoptCF, CGBitmapContextCreate(
                                               &custom_data_[0],
                                               CGImageGetWidth(image_ptr),
                                               CGImageGetHeight(image_ptr),
                                               8,
                                               CGImageGetWidth(image_ptr)*4,
                                               cg_color.get(),
                                               kCGImageAlphaPremultipliedLast |
                                               kCGBitmapByteOrder32Big));
  CGRect rect = CGRectMake(0, 0,
                           CGImageGetWidth(image_ptr),
                           CGImageGetHeight(image_ptr));
  CGContextDrawImage(context.get(), rect, image_ptr);
}

void WebCursor::InitPlatformData() {
  return;
}

bool WebCursor::SerializePlatformData(Pickle* pickle) const {
  return true;
}

bool WebCursor::DeserializePlatformData(const Pickle* pickle, void** iter) {
  return true;
}

bool WebCursor::IsPlatformDataEqual(const WebCursor& other) const {
  return true;
}

void WebCursor::CleanupPlatformData() {
  return;
}

void WebCursor::CopyPlatformData(const WebCursor& other) {
  return;
}