comparison dwt/graphics/Cursor.d @ 35:7d135fe0caf2

Ported dwt.graphics.Cursor and dwt.widgets.MenuItem
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sun, 14 Sep 2008 23:32:29 +0200
parents a9ab4c738ed8
children db5a898b2119
comparison
equal deleted inserted replaced
34:5123b17c98ef 35:7d135fe0caf2
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
10 *******************************************************************************/ 13 *******************************************************************************/
11 module dwt.graphics.Cursor; 14 module dwt.graphics.Cursor;
12
13 import dwt.dwthelper.utils;
14 15
15 16
16 import dwt.DWT; 17 import dwt.DWT;
17 import dwt.DWTError; 18 import dwt.DWTError;
18 import dwt.internal.cocoa.NSBitmapImageRep; 19 import dwt.internal.cocoa.NSBitmapImageRep;
20 import dwt.internal.cocoa.NSImage; 21 import dwt.internal.cocoa.NSImage;
21 import dwt.internal.cocoa.NSPoint; 22 import dwt.internal.cocoa.NSPoint;
22 import dwt.internal.cocoa.NSSize; 23 import dwt.internal.cocoa.NSSize;
23 import dwt.internal.cocoa.NSString; 24 import dwt.internal.cocoa.NSString;
24 import dwt.internal.cocoa.OS; 25 import dwt.internal.cocoa.OS;
26
27 import tango.text.convert.Format;
28
29 import dwt.dwthelper.utils;
30 import dwt.graphics.Device;
31 import dwt.graphics.ImageData;
32 import dwt.graphics.PaletteData;
33 import dwt.graphics.Resource;
34 import dwt.graphics.RGB;
35 import dwt.internal.cocoa.NSInteger;
25 36
26 /** 37 /**
27 * Instances of this class manage operating system resources that 38 * Instances of this class manage operating system resources that
28 * specify the appearance of the on-screen pointer. To create a 39 * specify the appearance of the on-screen pointer. To create a
29 * cursor you specify the device and either a simple cursor style 40 * cursor you specify the device and either a simple cursor style
220 createNSCursor(hotspotX, hotspotY, data, source.width, source.height); 231 createNSCursor(hotspotX, hotspotY, data, source.width, source.height);
221 init(); 232 init();
222 } 233 }
223 234
224 void createNSCursor(int hotspotX, int hotspotY, byte[] buffer, int width, int height) { 235 void createNSCursor(int hotspotX, int hotspotY, byte[] buffer, int width, int height) {
225 NSImage nsImage = cast(NSImage)new NSImage().alloc(); 236 NSImage nsImage = cast(NSImage)(new NSImage()).alloc();
226 NSBitmapImageRep nsImageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc(); 237 NSBitmapImageRep nsImageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
227 handle = cast(NSCursor)new NSCursor().alloc(); 238 handle = cast(NSCursor)(new NSCursor()).alloc();
228 NSSize size = new NSSize(); 239 NSSize size = NSSize();
229 size.width = width; 240 size.width = width;
230 size.height = height; 241 size.height = height;
231 nsImage = nsImage.initWithSize(size); 242 nsImage = nsImage.initWithSize(size);
232 nsImageRep = nsImageRep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, 243 nsImageRep = nsImageRep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, cast(NSInteger) width, cast(NSInteger) height,
233 8, 4, true, false, new NSString(OS.NSDeviceRGBColorSpace()), 244 8, 4, true, false, new NSString(OS.NSDeviceRGBColorSpace()),
234 OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32); 245 OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) (width * 4, 32));
235 OS.memmove(nsImageRep.bitmapData(), buffer, buffer.length); 246 OS.memmove(nsImageRep.bitmapData(), buffer, buffer.length);
236 nsImage.addRepresentation(nsImageRep); 247 nsImage.addRepresentation(nsImageRep);
237 NSPoint point = new NSPoint(); 248 NSPoint point = NSPoint();
238 point.x = hotspotX; 249 point.x = hotspotX;
239 point.y = hotspotY; 250 point.y = hotspotY;
240 handle = handle.initWithImage_hotSpot_(nsImage, point); 251 handle = handle.initWithImage_hotSpot_(nsImage, point);
241 nsImageRep.release(); 252 nsImageRep.release();
242 nsImage.release(); 253 nsImage.release();
343 * @param object the object to compare with this object 354 * @param object the object to compare with this object
344 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise 355 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
345 * 356 *
346 * @see #hashCode 357 * @see #hashCode
347 */ 358 */
348 public bool equals (Object object) { 359 public int opEquals (Object object) {
349 if (object is this) return true; 360 if (object is this) return true;
350 if (!( null !is cast(Cursor)object )) return false; 361 if (!( null !is cast(Cursor)object )) return false;
351 Cursor cursor = cast(Cursor) object; 362 Cursor cursor = cast(Cursor) object;
352 return device is cursor.device && handle is cursor.handle; 363 return device is cursor.device && handle is cursor.handle;
353 } 364 }
354 365
366 alias opEequals equals;
367
355 /** 368 /**
356 * Returns an integer hash code for the receiver. Any two 369 * Returns an integer hash code for the receiver. Any two
357 * objects that return <code>true</code> when passed to 370 * objects that return <code>true</code> when passed to
358 * <code>equals</code> must return the same value for this 371 * <code>equals</code> must return the same value for this
359 * method. 372 * method.
360 * 373 *
361 * @return the receiver's hash 374 * @return the receiver's hash
362 * 375 *
363 * @see #equals 376 * @see #equals
364 */ 377 */
365 public int hashCode () { 378 public hast_t toHash () {
366 return handle !is null ? handle.id : 0; 379 return handle !is null ? handle.id_ : 0;
367 } 380 }
381
382 alias toHash hashCode;
368 383
369 /** 384 /**
370 * Returns <code>true</code> if the cursor has been disposed, 385 * Returns <code>true</code> if the cursor has been disposed,
371 * and <code>false</code> otherwise. 386 * and <code>false</code> otherwise.
372 * <p> 387 * <p>
386 * 401 *
387 * @return a string representation of the receiver 402 * @return a string representation of the receiver
388 */ 403 */
389 public String toString () { 404 public String toString () {
390 if (isDisposed()) return "Cursor {*DISPOSED*}"; 405 if (isDisposed()) return "Cursor {*DISPOSED*}";
391 return "Cursor {" + handle + "}"; 406 return Format("Cursor {{}{}" , handle , "}");
392 } 407 }
393 408
394 /** 409 /**
395 * Invokes platform specific functionality to allocate a new cursor. 410 * Invokes platform specific functionality to allocate a new cursor.
396 * <p> 411 * <p>