1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.internal.carbon.*; 15 import org.eclipse.swt.*; 16 17 41 42 public final class Cursor extends Resource { 43 44 54 public int handle; 55 56 59 static final short [] SIZENS_SOURCE = new short[] { 60 (short)0x0000, 61 (short)0x0180, 62 (short)0x03C0, 63 (short)0x07E0, 64 (short)0x0180, 65 (short)0x0180, 66 (short)0x0180, 67 (short)0x7FFE, 68 (short)0x7FFE, 69 (short)0x0180, 70 (short)0x0180, 71 (short)0x0180, 72 (short)0x07E0, 73 (short)0x03C0, 74 (short)0x0180, 75 (short)0x0000, 76 }; 77 static final short [] SIZENS_MASK = new short[] { 78 (short)0x0180, 79 (short)0x03C0, 80 (short)0x07E0, 81 (short)0x0FF0, 82 (short)0x0FF0, 83 (short)0x03C0, 84 (short)0xFFFF, 85 (short)0xFFFF, 86 (short)0xFFFF, 87 (short)0xFFFF, 88 (short)0x03C0, 89 (short)0x0FF0, 90 (short)0x0FF0, 91 (short)0x07E0, 92 (short)0x03C0, 93 (short)0x0180, 94 }; 95 96 99 Cursor() { 100 } 101 102 143 public Cursor(Device device, int style) { 144 if (device == null) device = Device.getDevice(); 145 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 146 this.device = device; 147 switch (style) { 148 case SWT.CURSOR_HAND: handle = OS.kThemePointingHandCursor; break; 149 case SWT.CURSOR_ARROW: handle = OS.kThemeArrowCursor; break; 150 case SWT.CURSOR_WAIT: handle = OS.kThemeSpinningCursor; break; 151 case SWT.CURSOR_CROSS: handle = OS.kThemeCrossCursor; break; 152 case SWT.CURSOR_APPSTARTING: handle = OS.kThemeArrowCursor; break; 153 case SWT.CURSOR_HELP: handle = OS.kThemeCrossCursor; break; 154 case SWT.CURSOR_SIZEALL: handle = OS.kThemeCrossCursor; break; 155 case SWT.CURSOR_SIZENESW: handle = OS.kThemeCrossCursor; break; 156 case SWT.CURSOR_SIZENS: { 157 org.eclipse.swt.internal.carbon.Cursor cursor = new org.eclipse.swt.internal.carbon.Cursor(); 158 cursor.data = SIZENS_SOURCE; 159 cursor.mask = SIZENS_MASK; 160 cursor.hotSpot_h = 7; 161 cursor.hotSpot_v = 7; 162 handle = OS.NewPtr(org.eclipse.swt.internal.carbon.Cursor.sizeof); 163 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 164 OS.memcpy(handle, cursor, org.eclipse.swt.internal.carbon.Cursor.sizeof); 165 break; 166 } 167 case SWT.CURSOR_SIZENWSE: handle = OS.kThemeCrossCursor; break; 168 case SWT.CURSOR_SIZEWE: handle = OS.kThemeResizeLeftRightCursor; break; 169 case SWT.CURSOR_SIZEN: handle = OS.kThemeCrossCursor; break; 170 case SWT.CURSOR_SIZES: handle = OS.kThemeCrossCursor; break; 171 case SWT.CURSOR_SIZEE: handle = OS.kThemeResizeRightCursor; break; 172 case SWT.CURSOR_SIZEW: handle = OS.kThemeResizeLeftCursor; break; 173 case SWT.CURSOR_SIZENE: handle = OS.kThemeCrossCursor; break; 174 case SWT.CURSOR_SIZESE: handle = OS.kThemeCrossCursor; break; 175 case SWT.CURSOR_SIZESW: handle = OS.kThemeCrossCursor; break; 176 case SWT.CURSOR_SIZENW: handle = OS.kThemeCrossCursor; break; 177 case SWT.CURSOR_UPARROW: handle = OS.kThemeCrossCursor; break; 178 case SWT.CURSOR_IBEAM: handle = OS.kThemeIBeamCursor; break; 179 case SWT.CURSOR_NO: handle = OS.kThemeNotAllowedCursor; break; 180 default: 181 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 182 } 183 } 184 185 216 public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int hotspotY) { 217 if (device == null) device = Device.getDevice(); 218 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 219 this.device = device; 220 if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 221 if (mask == null) { 222 if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) { 223 SWT.error(SWT.ERROR_NULL_ARGUMENT); 224 } 225 mask = source.getTransparencyMask(); 226 } 227 228 if (mask.width != source.width || mask.height != source.height) { 229 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 230 } 231 232 if (hotspotX >= source.width || hotspotX < 0 || 233 hotspotY >= source.height || hotspotY < 0) { 234 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 235 } 236 237 mask = ImageData.convertMask(mask); 238 source = ImageData.convertMask(source); 239 240 241 int width = source.width; 242 int height = source.height; 243 int minX = 0, minY = 0; 244 if (width > 16 || height > 16) { 245 minX = width; 246 minY = height; 247 int maxX = 0, maxY = 0; 248 for (int y = 0; y < height; y++) { 249 for (int x = 0; x < width; x++) { 250 if (!(source.getPixel(x, y) == 1 && mask.getPixel(x, y) == 0)) { 251 minX = Math.min(minX, x); 252 minY = Math.min(minY, y); 253 maxX = Math.max(maxX, x); 254 maxY = Math.max(maxY, y); 255 } 256 } 257 } 258 width = maxX - minX + 1; 259 height = maxY - minY + 1; 260 261 262 if (width > 16 || height > 16) { 263 int newWidth = Math.min(width, 16); 264 int newHeight = Math.min(height, 16); 265 ImageData newSource = 266 new ImageData(newWidth, newHeight, source.depth, source.palette, 267 1, null, 0, null, null, -1, -1, source.type, 268 source.x, source.y, source.disposalMethod, source.delayTime); 269 ImageData newMask = new ImageData(newWidth, newHeight, mask.depth, 270 mask.palette, 1, null, 0, null, null, -1, -1, mask.type, 271 mask.x, mask.y, mask.disposalMethod, mask.delayTime); 272 ImageData.blit(ImageData.BLIT_SRC, 273 source.data, source.depth, source.bytesPerLine, source.getByteOrder(), minX, minY, width, height, null, null, null, 274 ImageData.ALPHA_OPAQUE, null, 0, minX, minY, 275 newSource.data, newSource.depth, newSource.bytesPerLine, newSource.getByteOrder(), 0, 0, newWidth, newHeight, null, null, null, 276 false, false); 277 ImageData.blit(ImageData.BLIT_SRC, 278 mask.data, mask.depth, mask.bytesPerLine, mask.getByteOrder(), minX, minY, width, height, null, null, null, 279 ImageData.ALPHA_OPAQUE, null, 0, minX, minY, 280 newMask.data, newMask.depth, newMask.bytesPerLine, newMask.getByteOrder(), 0, 0, newWidth, newHeight, null, null, null, 281 false, false); 282 width = newWidth; 283 height = newHeight; 284 minX = minY = 0; 285 source = newSource; 286 mask = newMask; 287 } 288 } 289 290 291 org.eclipse.swt.internal.carbon.Cursor cursor = new org.eclipse.swt.internal.carbon.Cursor(); 292 short[] srcData = cursor.data; 293 short[] maskData = cursor.mask; 294 for (int y = 0; y < height; y++) { 295 short d = 0, m = 0; 296 for (int x = 0; x < width; x++) { 297 int bit = 1 << (width - 1 - x); 298 if (source.getPixel(minX + x, minY + y) == 0) { 299 m |= bit; 300 if (mask.getPixel(minX + x, minY + y) == 0) d |= bit; 301 } else if (mask.getPixel(minX + x, minY + y) != 0) { 302 d |= bit; 303 } 304 } 305 srcData[y] = d; 306 maskData[y] = m; 307 } 308 cursor.hotSpot_h = (short)Math.max(0, Math.min(15, hotspotY - minX)); 309 cursor.hotSpot_v = (short)Math.max(0, Math.min(15, hotspotY - minY)); 310 handle = OS.NewPtr(org.eclipse.swt.internal.carbon.Cursor.sizeof); 311 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 312 OS.memcpy(handle, cursor, org.eclipse.swt.internal.carbon.Cursor.sizeof); 313 } 314 315 342 public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) { 343 if (device == null) device = Device.getDevice(); 344 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 345 this.device = device; 346 if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 347 if (hotspotX >= source.width || hotspotX < 0 || 348 hotspotY >= source.height || hotspotY < 0) { 349 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 350 } 351 ImageData mask = source.getTransparencyMask(); 352 353 354 if (source.depth > 1) { 355 356 ImageData newSource = new ImageData( 357 source.width, source.height, 1, ImageData.bwPalette(), 358 1, null, 0, null, null, -1, -1, source.type, 359 source.x, source.y, source.disposalMethod, source.delayTime); 360 361 362 PaletteData palette = source.palette; 363 if (palette.isDirect) ImageData.blit(ImageData.BLIT_SRC, 364 source.data, source.depth, source.bytesPerLine, source.getByteOrder(), 0, 0, source.width, source.height, 0, 0, 0, 365 ImageData.ALPHA_OPAQUE, null, 0, 0, 0, 366 newSource.data, newSource.depth, newSource.bytesPerLine, newSource.getByteOrder(), 0, 0, newSource.width, newSource.height, 0, 0, 0, 367 false, false); 368 else ImageData.blit(ImageData.BLIT_SRC, 369 source.data, source.depth, source.bytesPerLine, source.getByteOrder(), 0, 0, source.width, source.height, null, null, null, 370 ImageData.ALPHA_OPAQUE, null, 0, 0, 0, 371 newSource.data, newSource.depth, newSource.bytesPerLine, newSource.getByteOrder(), 0, 0, newSource.width, newSource.height, null, null, null, 372 false, false); 373 source = newSource; 374 } 375 376 377 int width = source.width; 378 int height = source.height; 379 int minX = 0, minY = 0; 380 if (width > 16 || height > 16) { 381 minX = width; 382 minY = height; 383 int maxX = 0, maxY = 0; 384 for (int y = 0; y < height; y++) { 385 for (int x = 0; x < width; x++) { 386 if (!(source.getPixel(x, y) == 1 && mask.getPixel(x, y) == 0)) { 387 minX = Math.min(minX, x); 388 minY = Math.min(minY, y); 389 maxX = Math.max(maxX, x); 390 maxY = Math.max(maxY, y); 391 } 392 } 393 } 394 width = maxX - minX + 1; 395 height = maxY - minY + 1; 396 397 398 if (width > 16 || height > 16) { 399 int newWidth = Math.min(width, 16); 400 int newHeight = Math.min(height, 16); 401 ImageData newSource = 402 new ImageData(newWidth, newHeight, source.depth, source.palette, 403 1, null, 0, null, null, -1, -1, source.type, 404 source.x, source.y, source.disposalMethod, source.delayTime); 405 ImageData newMask = new ImageData(newWidth, newHeight, mask.depth, 406 mask.palette, 1, null, 0, null, null, -1, -1, mask.type, 407 mask.x, mask.y, mask.disposalMethod, mask.delayTime); 408 ImageData.blit(ImageData.BLIT_SRC, 409 source.data, source.depth, source.bytesPerLine, source.getByteOrder(), minX, minY, width, height, null, null, null, 410 ImageData.ALPHA_OPAQUE, null, 0, minX, minY, 411 newSource.data, newSource.depth, newSource.bytesPerLine, newSource.getByteOrder(), 0, 0, newWidth, newHeight, null, null, null, 412 false, false); 413 ImageData.blit(ImageData.BLIT_SRC, 414 mask.data, mask.depth, mask.bytesPerLine, mask.getByteOrder(), minX, minY, width, height, null, null, null, 415 ImageData.ALPHA_OPAQUE, null, 0, minX, minY, 416 newMask.data, newMask.depth, newMask.bytesPerLine, newMask.getByteOrder(), 0, 0, newWidth, newHeight, null, null, null, 417 false, false); 418 width = newWidth; 419 height = newHeight; 420 minX = minY = 0; 421 source = newSource; 422 mask = newMask; 423 } 424 } 425 426 427 org.eclipse.swt.internal.carbon.Cursor cursor = new org.eclipse.swt.internal.carbon.Cursor(); 428 short[] srcData = cursor.data; 429 short[] maskData = cursor.mask; 430 for (int y= 0; y < height; y++) { 431 short d = 0, m = 0; 432 for (int x = 0; x < width; x++) { 433 int bit = 1 << (width - 1 - x); 434 if (source.getPixel(x + minX, y + minY) == 0) { 435 if (mask.getPixel(x + minX, y + minY) != 0) { 436 d |= bit; 437 m |= bit; 438 } 439 } else { 440 if (mask.getPixel(x + minX, y + minY) != 0) m |= bit; 441 } 442 } 443 srcData[y] = d; 444 maskData[y] = m; 445 } 446 cursor.hotSpot_h = (short)Math.max(0, Math.min(15, hotspotY - minX)); 447 cursor.hotSpot_v = (short)Math.max(0, Math.min(15, hotspotY - minY)); 448 handle = OS.NewPtr(org.eclipse.swt.internal.carbon.Cursor.sizeof); 449 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 450 OS.memcpy(handle, cursor, org.eclipse.swt.internal.carbon.Cursor.sizeof); 451 } 452 453 458 public void dispose () { 459 if (handle == -1) return; 460 if (device.isDisposed()) return; 461 switch (handle) { 462 case OS.kThemePointingHandCursor: 463 case OS.kThemeArrowCursor: 464 case OS.kThemeSpinningCursor: 465 case OS.kThemeCrossCursor: 466 case OS.kThemeWatchCursor: 467 case OS.kThemeIBeamCursor: 468 case OS.kThemeNotAllowedCursor: 469 case OS.kThemeResizeLeftRightCursor: 470 case OS.kThemeResizeLeftCursor: 471 case OS.kThemeResizeRightCursor: 472 break; 473 default: 474 OS.DisposePtr(handle); 475 } 476 handle = -1; 477 device = null; 478 } 479 480 490 public boolean equals (Object object) { 491 if (object == this) return true; 492 if (!(object instanceof Cursor)) return false; 493 Cursor cursor = (Cursor) object; 494 return device == cursor.device && handle == cursor.handle; 495 } 496 497 507 public int hashCode () { 508 return handle; 509 } 510 511 521 public boolean isDisposed() { 522 return handle == -1; 523 } 524 525 531 public String toString () { 532 if (isDisposed()) return "Cursor {*DISPOSED*}"; 533 return "Cursor {" + handle + "}"; 534 } 535 536 551 public static Cursor carbon_new(Device device, int handle) { 552 if (device == null) device = Device.getDevice(); 553 Cursor cursor = new Cursor(); 554 cursor.handle = handle; 555 cursor.device = device; 556 return cursor; 557 } 558 559 } 560 | Popular Tags |