| 1 |
/* gdkcursor-quartz.c |
| 2 |
* |
| 3 |
* Copyright (C) 2005-2007 Imendio AB |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gdkdisplay.h" |
| 24 |
#include "gdkcursor.h" |
| 25 |
#include "gdkprivate-quartz.h" |
| 26 |
|
| 27 |
#include "xcursors.h" |
| 28 |
|
| 29 |
static GdkCursor *cached_xcursors[G_N_ELEMENTS (xcursors)]; |
| 30 |
|
| 31 |
static GdkCursor * |
| 32 |
gdk_quartz_cursor_new_from_nscursor (NSCursor *nscursor, |
| 33 |
GdkCursorType cursor_type) |
| 34 |
{ |
| 35 |
GdkCursorPrivate *private; |
| 36 |
GdkCursor *cursor; |
| 37 |
|
| 38 |
private = g_new (GdkCursorPrivate, 1); |
| 39 |
private->nscursor = nscursor; |
| 40 |
|
| 41 |
cursor = (GdkCursor *)private; |
| 42 |
cursor->type = cursor_type; |
| 43 |
cursor->ref_count = 1; |
| 44 |
|
| 45 |
return cursor; |
| 46 |
} |
| 47 |
|
| 48 |
static GdkCursor * |
| 49 |
create_blank_cursor (void) |
| 50 |
{ |
| 51 |
NSCursor *nscursor; |
| 52 |
NSImage *nsimage; |
| 53 |
NSSize size = { 1.0, 1.0 }; |
| 54 |
|
| 55 |
nsimage = [[NSImage alloc] initWithSize:size]; |
| 56 |
nscursor = [[NSCursor alloc] initWithImage:nsimage |
| 57 |
hotSpot:NSMakePoint(0.0, 0.0)]; |
| 58 |
[nsimage release]; |
| 59 |
|
| 60 |
return gdk_quartz_cursor_new_from_nscursor (nscursor, GDK_BLANK_CURSOR); |
| 61 |
} |
| 62 |
|
| 63 |
static gboolean |
| 64 |
get_bit (const guchar *data, |
| 65 |
gint width, |
| 66 |
gint height, |
| 67 |
gint x, |
| 68 |
gint y) |
| 69 |
{ |
| 70 |
gint bytes_per_line; |
| 71 |
const guchar *src; |
| 72 |
|
| 73 |
if (x < 0 || y < 0 || x >= width || y >= height) |
| 74 |
return FALSE; |
| 75 |
|
| 76 |
bytes_per_line = (width + 7) / 8; |
| 77 |
|
| 78 |
src = &data[y * bytes_per_line]; |
| 79 |
return ((src[x / 8] >> x % 8) & 1); |
| 80 |
} |
| 81 |
|
| 82 |
static GdkCursor * |
| 83 |
create_builtin_cursor (GdkCursorType cursor_type) |
| 84 |
{ |
| 85 |
GdkCursor *cursor; |
| 86 |
NSBitmapImageRep *bitmap_rep; |
| 87 |
NSInteger mask_width, mask_height; |
| 88 |
gint src_width, src_height; |
| 89 |
gint dst_stride; |
| 90 |
const guchar *mask_start, *src_start; |
| 91 |
gint dx, dy; |
| 92 |
gint x, y; |
| 93 |
NSPoint hotspot; |
| 94 |
NSImage *image; |
| 95 |
NSCursor *nscursor; |
| 96 |
|
| 97 |
if (cursor_type >= G_N_ELEMENTS (xcursors) || cursor_type < 0) |
| 98 |
return NULL; |
| 99 |
|
| 100 |
cursor = cached_xcursors[cursor_type]; |
| 101 |
if (cursor) |
| 102 |
return cursor; |
| 103 |
|
| 104 |
GDK_QUARTZ_ALLOC_POOL; |
| 105 |
|
| 106 |
src_width = xcursors[cursor_type].width; |
| 107 |
src_height = xcursors[cursor_type].height; |
| 108 |
mask_width = xcursors[cursor_type+1].width; |
| 109 |
mask_height = xcursors[cursor_type+1].height; |
| 110 |
|
| 111 |
bitmap_rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL |
| 112 |
pixelsWide:mask_width pixelsHigh:mask_height |
| 113 |
bitsPerSample:8 samplesPerPixel:4 |
| 114 |
hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace |
| 115 |
bytesPerRow:0 bitsPerPixel:0]; |
| 116 |
|
| 117 |
dst_stride = [bitmap_rep bytesPerRow]; |
| 118 |
|
| 119 |
src_start = xcursors[cursor_type].bits; |
| 120 |
mask_start = xcursors[cursor_type+1].bits; |
| 121 |
|
| 122 |
dx = xcursors[cursor_type+1].hotx - xcursors[cursor_type].hotx; |
| 123 |
dy = xcursors[cursor_type+1].hoty - xcursors[cursor_type].hoty; |
| 124 |
|
| 125 |
for (y = 0; y < mask_height; y++) |
| 126 |
{ |
| 127 |
guchar *dst = [bitmap_rep bitmapData] + y * dst_stride; |
| 128 |
|
| 129 |
for (x = 0; x < mask_width; x++) |
| 130 |
{ |
| 131 |
if (get_bit (mask_start, mask_width, mask_height, x, y)) |
| 132 |
{ |
| 133 |
if (get_bit (src_start, src_width, src_height, x - dx, y - dy)) |
| 134 |
{ |
| 135 |
*dst++ = 0; |
| 136 |
*dst++ = 0; |
| 137 |
*dst++ = 0; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
*dst++ = 0xff; |
| 142 |
*dst++ = 0xff; |
| 143 |
*dst++ = 0xff; |
| 144 |
} |
| 145 |
|
| 146 |
*dst++ = 0xff; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
*dst++ = 0; |
| 151 |
*dst++ = 0; |
| 152 |
*dst++ = 0; |
| 153 |
*dst++ = 0; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
image = [[NSImage alloc] init]; |
| 159 |
[image addRepresentation:bitmap_rep]; |
| 160 |
[bitmap_rep release]; |
| 161 |
|
| 162 |
hotspot = NSMakePoint (xcursors[cursor_type+1].hotx, |
| 163 |
xcursors[cursor_type+1].hoty); |
| 164 |
|
| 165 |
nscursor = [[NSCursor alloc] initWithImage:image hotSpot:hotspot]; |
| 166 |
[image release]; |
| 167 |
|
| 168 |
cursor = gdk_quartz_cursor_new_from_nscursor (nscursor, GDK_CURSOR_IS_PIXMAP); |
| 169 |
|
| 170 |
cached_xcursors[cursor_type] = gdk_cursor_ref (cursor); |
| 171 |
|
| 172 |
GDK_QUARTZ_RELEASE_POOL; |
| 173 |
|
| 174 |
return cursor; |
| 175 |
} |
| 176 |
|
| 177 |
GdkCursor* |
| 178 |
gdk_cursor_new_for_display (GdkDisplay *display, |
| 179 |
GdkCursorType cursor_type) |
| 180 |
{ |
| 181 |
NSCursor *nscursor; |
| 182 |
|
| 183 |
g_return_val_if_fail (display == gdk_display_get_default (), NULL); |
| 184 |
|
| 185 |
switch (cursor_type) |
| 186 |
{ |
| 187 |
case GDK_XTERM: |
| 188 |
nscursor = [NSCursor IBeamCursor]; |
| 189 |
break; |
| 190 |
case GDK_SB_H_DOUBLE_ARROW: |
| 191 |
nscursor = [NSCursor resizeLeftRightCursor]; |
| 192 |
break; |
| 193 |
case GDK_SB_V_DOUBLE_ARROW: |
| 194 |
nscursor = [NSCursor resizeUpDownCursor]; |
| 195 |
break; |
| 196 |
case GDK_SB_UP_ARROW: |
| 197 |
case GDK_BASED_ARROW_UP: |
| 198 |
case GDK_BOTTOM_TEE: |
| 199 |
case GDK_TOP_SIDE: |
| 200 |
nscursor = [NSCursor resizeUpCursor]; |
| 201 |
break; |
| 202 |
case GDK_SB_DOWN_ARROW: |
| 203 |
case GDK_BASED_ARROW_DOWN: |
| 204 |
case GDK_TOP_TEE: |
| 205 |
case GDK_BOTTOM_SIDE: |
| 206 |
nscursor = [NSCursor resizeDownCursor]; |
| 207 |
break; |
| 208 |
case GDK_SB_LEFT_ARROW: |
| 209 |
case GDK_RIGHT_TEE: |
| 210 |
case GDK_LEFT_SIDE: |
| 211 |
nscursor = [NSCursor resizeLeftCursor]; |
| 212 |
break; |
| 213 |
case GDK_SB_RIGHT_ARROW: |
| 214 |
case GDK_LEFT_TEE: |
| 215 |
case GDK_RIGHT_SIDE: |
| 216 |
nscursor = [NSCursor resizeRightCursor]; |
| 217 |
break; |
| 218 |
case GDK_TCROSS: |
| 219 |
case GDK_CROSS: |
| 220 |
case GDK_CROSSHAIR: |
| 221 |
case GDK_DIAMOND_CROSS: |
| 222 |
nscursor = [NSCursor crosshairCursor]; |
| 223 |
break; |
| 224 |
case GDK_HAND1: |
| 225 |
case GDK_HAND2: |
| 226 |
nscursor = [NSCursor pointingHandCursor]; |
| 227 |
break; |
| 228 |
case GDK_CURSOR_IS_PIXMAP: |
| 229 |
return NULL; |
| 230 |
case GDK_BLANK_CURSOR: |
| 231 |
return create_blank_cursor (); |
| 232 |
default: |
| 233 |
return gdk_cursor_ref (create_builtin_cursor (cursor_type)); |
| 234 |
} |
| 235 |
|
| 236 |
[nscursor retain]; |
| 237 |
return gdk_quartz_cursor_new_from_nscursor (nscursor, cursor_type); |
| 238 |
} |
| 239 |
|
| 240 |
GdkCursor* |
| 241 |
gdk_cursor_new_from_pixmap (GdkPixmap *source, |
| 242 |
GdkPixmap *mask, |
| 243 |
const GdkColor *fg, |
| 244 |
const GdkColor *bg, |
| 245 |
gint x, |
| 246 |
gint y) |
| 247 |
{ |
| 248 |
NSBitmapImageRep *bitmap_rep; |
| 249 |
NSImage *image; |
| 250 |
NSCursor *nscursor; |
| 251 |
GdkCursor *cursor; |
| 252 |
int width, height; |
| 253 |
gint tmp_x, tmp_y; |
| 254 |
guchar *dst_data, *mask_data, *src_data; |
| 255 |
guchar *mask_start, *src_start; |
| 256 |
int dst_stride; |
| 257 |
|
| 258 |
g_return_val_if_fail (GDK_IS_PIXMAP (source), NULL); |
| 259 |
g_return_val_if_fail (GDK_IS_PIXMAP (mask), NULL); |
| 260 |
g_return_val_if_fail (fg != NULL, NULL); |
| 261 |
g_return_val_if_fail (bg != NULL, NULL); |
| 262 |
|
| 263 |
GDK_QUARTZ_ALLOC_POOL; |
| 264 |
|
| 265 |
gdk_drawable_get_size (source, &width, &height); |
| 266 |
|
| 267 |
bitmap_rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL |
| 268 |
pixelsWide:(NSInteger)width pixelsHigh:(NSInteger)height |
| 269 |
bitsPerSample:8 samplesPerPixel:4 |
| 270 |
hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace |
| 271 |
bytesPerRow:0 bitsPerPixel:0]; |
| 272 |
|
| 273 |
dst_stride = [bitmap_rep bytesPerRow]; |
| 274 |
mask_start = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (mask)->impl)->data; |
| 275 |
src_start = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (source)->impl)->data; |
| 276 |
|
| 277 |
for (tmp_y = 0; tmp_y < height; tmp_y++) |
| 278 |
{ |
| 279 |
dst_data = [bitmap_rep bitmapData] + tmp_y * dst_stride; |
| 280 |
mask_data = mask_start + tmp_y * width; |
| 281 |
src_data = src_start + tmp_y * width; |
| 282 |
|
| 283 |
for (tmp_x = 0; tmp_x < width; tmp_x++) |
| 284 |
{ |
| 285 |
if (*mask_data++) |
| 286 |
{ |
| 287 |
const GdkColor *color; |
| 288 |
|
| 289 |
if (*src_data++) |
| 290 |
color = fg; |
| 291 |
else |
| 292 |
color = bg; |
| 293 |
|
| 294 |
*dst_data++ = (color->red >> 8) & 0xff; |
| 295 |
*dst_data++ = (color->green >> 8) & 0xff; |
| 296 |
*dst_data++ = (color->blue >> 8) & 0xff; |
| 297 |
*dst_data++ = 0xff; |
| 298 |
|
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
*dst_data++ = 0x00; |
| 303 |
*dst_data++ = 0x00; |
| 304 |
*dst_data++ = 0x00; |
| 305 |
*dst_data++ = 0x00; |
| 306 |
|
| 307 |
src_data++; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
image = [[NSImage alloc] init]; |
| 312 |
[image addRepresentation:bitmap_rep]; |
| 313 |
[bitmap_rep release]; |
| 314 |
|
| 315 |
nscursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(x, y)]; |
| 316 |
[image release]; |
| 317 |
|
| 318 |
cursor = gdk_quartz_cursor_new_from_nscursor (nscursor, GDK_CURSOR_IS_PIXMAP); |
| 319 |
|
| 320 |
GDK_QUARTZ_RELEASE_POOL; |
| 321 |
|
| 322 |
return cursor; |
| 323 |
} |
| 324 |
|
| 325 |
static NSImage * |
| 326 |
_gdk_quartz_pixbuf_to_ns_image (GdkPixbuf *pixbuf) |
| 327 |
{ |
| 328 |
NSBitmapImageRep *bitmap_rep; |
| 329 |
NSImage *image; |
| 330 |
gboolean has_alpha; |
| 331 |
|
| 332 |
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf); |
| 333 |
|
| 334 |
/* Create a bitmap image rep */ |
| 335 |
bitmap_rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL |
| 336 |
pixelsWide:gdk_pixbuf_get_width (pixbuf) |
| 337 |
pixelsHigh:gdk_pixbuf_get_height (pixbuf) |
| 338 |
bitsPerSample:8 samplesPerPixel:has_alpha ? 4 : 3 |
| 339 |
hasAlpha:has_alpha isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace |
| 340 |
bytesPerRow:0 bitsPerPixel:0]; |
| 341 |
|
| 342 |
{ |
| 343 |
/* Add pixel data to bitmap rep */ |
| 344 |
guchar *src, *dst; |
| 345 |
int src_stride, dst_stride; |
| 346 |
int x, y; |
| 347 |
|
| 348 |
src_stride = gdk_pixbuf_get_rowstride (pixbuf); |
| 349 |
dst_stride = [bitmap_rep bytesPerRow]; |
| 350 |
|
| 351 |
for (y = 0; y < gdk_pixbuf_get_height (pixbuf); y++) |
| 352 |
{ |
| 353 |
src = gdk_pixbuf_get_pixels (pixbuf) + y * src_stride; |
| 354 |
dst = [bitmap_rep bitmapData] + y * dst_stride; |
| 355 |
|
| 356 |
for (x = 0; x < gdk_pixbuf_get_width (pixbuf); x++) |
| 357 |
{ |
| 358 |
if (has_alpha) |
| 359 |
{ |
| 360 |
guchar red, green, blue, alpha; |
| 361 |
|
| 362 |
red = *src++; |
| 363 |
green = *src++; |
| 364 |
blue = *src++; |
| 365 |
alpha = *src++; |
| 366 |
|
| 367 |
*dst++ = (red * alpha) / 255; |
| 368 |
*dst++ = (green * alpha) / 255; |
| 369 |
*dst++ = (blue * alpha) / 255; |
| 370 |
*dst++ = alpha; |
| 371 |
} |
| 372 |
else |
| 373 |
{ |
| 374 |
*dst++ = *src++; |
| 375 |
*dst++ = *src++; |
| 376 |
*dst++ = *src++; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
image = [[NSImage alloc] init]; |
| 383 |
[image addRepresentation:bitmap_rep]; |
| 384 |
[bitmap_rep release]; |
| 385 |
[image autorelease]; |
| 386 |
|
| 387 |
return image; |
| 388 |
} |
| 389 |
|
| 390 |
GdkCursor * |
| 391 |
gdk_cursor_new_from_pixbuf (GdkDisplay *display, |
| 392 |
GdkPixbuf *pixbuf, |
| 393 |
gint x, |
| 394 |
gint y) |
| 395 |
{ |
| 396 |
NSImage *image; |
| 397 |
NSCursor *nscursor; |
| 398 |
GdkCursor *cursor; |
| 399 |
gboolean has_alpha; |
| 400 |
|
| 401 |
g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL); |
| 402 |
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL); |
| 403 |
g_return_val_if_fail (0 <= x && x < gdk_pixbuf_get_width (pixbuf), NULL); |
| 404 |
g_return_val_if_fail (0 <= y && y < gdk_pixbuf_get_height (pixbuf), NULL); |
| 405 |
|
| 406 |
GDK_QUARTZ_ALLOC_POOL; |
| 407 |
|
| 408 |
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf); |
| 409 |
|
| 410 |
image = _gdk_quartz_pixbuf_to_ns_image (pixbuf); |
| 411 |
nscursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(x, y)]; |
| 412 |
|
| 413 |
cursor = gdk_quartz_cursor_new_from_nscursor (nscursor, GDK_CURSOR_IS_PIXMAP); |
| 414 |
|
| 415 |
GDK_QUARTZ_RELEASE_POOL; |
| 416 |
|
| 417 |
return cursor; |
| 418 |
} |
| 419 |
|
| 420 |
GdkCursor* |
| 421 |
gdk_cursor_new_from_name (GdkDisplay *display, |
| 422 |
const gchar *name) |
| 423 |
{ |
| 424 |
/* FIXME: Implement */ |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
|
| 428 |
void |
| 429 |
_gdk_cursor_destroy (GdkCursor *cursor) |
| 430 |
{ |
| 431 |
GdkCursorPrivate *private; |
| 432 |
|
| 433 |
g_return_if_fail (cursor != NULL); |
| 434 |
g_return_if_fail (cursor->ref_count == 0); |
| 435 |
|
| 436 |
private = (GdkCursorPrivate *)cursor; |
| 437 |
[private->nscursor release]; |
| 438 |
|
| 439 |
g_free (private); |
| 440 |
} |
| 441 |
|
| 442 |
gboolean |
| 443 |
gdk_display_supports_cursor_alpha (GdkDisplay *display) |
| 444 |
{ |
| 445 |
g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE); |
| 446 |
|
| 447 |
return TRUE; |
| 448 |
} |
| 449 |
|
| 450 |
gboolean |
| 451 |
gdk_display_supports_cursor_color (GdkDisplay *display) |
| 452 |
{ |
| 453 |
g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE); |
| 454 |
|
| 455 |
return TRUE; |
| 456 |
} |
| 457 |
|
| 458 |
guint |
| 459 |
gdk_display_get_default_cursor_size (GdkDisplay *display) |
| 460 |
{ |
| 461 |
g_return_val_if_fail (GDK_IS_DISPLAY (display), 0); |
| 462 |
|
| 463 |
/* Mac OS X doesn't have the notion of a default size */ |
| 464 |
return 32; |
| 465 |
} |
| 466 |
|
| 467 |
void |
| 468 |
gdk_display_get_maximal_cursor_size (GdkDisplay *display, |
| 469 |
guint *width, |
| 470 |
guint *height) |
| 471 |
{ |
| 472 |
g_return_if_fail (GDK_IS_DISPLAY (display)); |
| 473 |
|
| 474 |
/* Cursor sizes in Mac OS X can be arbitrarily large */ |
| 475 |
*width = 65536; |
| 476 |
*height = 65536; |
| 477 |
} |
| 478 |
|
| 479 |
GdkDisplay * |
| 480 |
gdk_cursor_get_display (GdkCursor *cursor) |
| 481 |
{ |
| 482 |
g_return_val_if_fail (cursor != NULL, NULL); |
| 483 |
|
| 484 |
return gdk_display_get_default (); |
| 485 |
} |
| 486 |
|
| 487 |
GdkPixbuf * |
| 488 |
gdk_cursor_get_image (GdkCursor *cursor) |
| 489 |
{ |
| 490 |
/* FIXME: Implement */ |
| 491 |
return NULL; |
| 492 |
} |
| 493 |
|
| 494 |
NSImage * |
| 495 |
gdk_quartz_pixbuf_to_ns_image_libgtk_only (GdkPixbuf *pixbuf) |
| 496 |
{ |
| 497 |
return _gdk_quartz_pixbuf_to_ns_image (pixbuf); |
| 498 |
} |