Mercurial > projects > dwt-mac
comparison dwt/dnd/ImageTransfer.d @ 106:ed7dd292bfb3
Ported dwt.dnd.ImageTransfer
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 31 Dec 2008 14:43:40 +0100 |
parents | d8635bb48c7c |
children |
comparison
equal
deleted
inserted
replaced
105:6a1ed74f02e8 | 106:ed7dd292bfb3 |
---|---|
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 * Outhink - support for typeFileURL | 10 * Outhink - support for typeFileURL |
11 * | |
12 * Port to the D programming language: | |
13 * Jacob Carlborg <doob@me.com> | |
11 *******************************************************************************/ | 14 *******************************************************************************/ |
12 module dwt.dnd.ImageTransfer; | 15 module dwt.dnd.ImageTransfer; |
13 | 16 |
14 import dwt.dwthelper.utils; | 17 import dwt.dwthelper.utils; |
15 | 18 |
18 import dwt.graphics.ImageData; | 21 import dwt.graphics.ImageData; |
19 import dwt.internal.cocoa.NSData; | 22 import dwt.internal.cocoa.NSData; |
20 import dwt.internal.cocoa.NSImage; | 23 import dwt.internal.cocoa.NSImage; |
21 import dwt.internal.cocoa.OS; | 24 import dwt.internal.cocoa.OS; |
22 import dwt.widgets.Display; | 25 import dwt.widgets.Display; |
26 | |
27 import dwt.dnd.DND; | |
28 import dwt.dnd.ByteArrayTransfer; | |
29 import dwt.dnd.TransferData; | |
23 | 30 |
24 /** | 31 /** |
25 * The class <code>ImageTransfer</code> provides a platform specific mechanism | 32 * The class <code>ImageTransfer</code> provides a platform specific mechanism |
26 * for converting an Image represented as a java <code>ImageData</code> to a | 33 * for converting an Image represented as a java <code>ImageData</code> to a |
27 * platform specific representation of the data and vice versa. | 34 * platform specific representation of the data and vice versa. |
35 * | 42 * |
36 * @see Transfer | 43 * @see Transfer |
37 * | 44 * |
38 * @since 3.4 | 45 * @since 3.4 |
39 */ | 46 */ |
40 public class ImageTransfer extends ByteArrayTransfer { | 47 public class ImageTransfer : ByteArrayTransfer { |
41 | 48 |
42 static ImageTransfer _instance = new ImageTransfer(); | 49 static ImageTransfer _instance; |
43 static final String TIFF = OS.NSTIFFPboardType.getString(); | 50 static const String TIFF; |
44 static final int TIFFID = registerType(TIFF); | 51 static const int TIFFID; |
45 | 52 |
46 ImageTransfer() { | 53 static this () |
54 { | |
55 _instance = new ImageTransfer(); | |
56 TIFF = OS.NSTIFFPboardType.getString(); | |
57 TIFFID = registerType(TIFF); | |
58 } | |
59 | |
60 this() { | |
47 } | 61 } |
48 | 62 |
49 /** | 63 /** |
50 * Returns the singleton instance of the ImageTransfer class. | 64 * Returns the singleton instance of the ImageTransfer class. |
51 * | 65 * |
67 */ | 81 */ |
68 public void javaToNative(Object object, TransferData transferData) { | 82 public void javaToNative(Object object, TransferData transferData) { |
69 if (!checkImage(object) || !isSupportedType(transferData)) { | 83 if (!checkImage(object) || !isSupportedType(transferData)) { |
70 DND.error(DND.ERROR_INVALID_DATA); | 84 DND.error(DND.ERROR_INVALID_DATA); |
71 } | 85 } |
72 ImageData imgData = (ImageData) object; | 86 ImageData imgData = cast(ImageData) object; |
73 Image image = new Image(Display.getCurrent(), imgData); | 87 Image image = new Image(Display.getCurrent(), imgData); |
74 NSImage handle = image.handle; | 88 NSImage handle = image.handle; |
75 transferData.data = handle.TIFFRepresentation(); | 89 transferData.data = handle.TIFFRepresentation(); |
76 image.dispose(); | 90 image.dispose(); |
77 } | 91 } |
86 * | 100 * |
87 * @see Transfer#javaToNative | 101 * @see Transfer#javaToNative |
88 */ | 102 */ |
89 public Object nativeToJava(TransferData transferData) { | 103 public Object nativeToJava(TransferData transferData) { |
90 if (!isSupportedType(transferData) || transferData.data is null) return null; | 104 if (!isSupportedType(transferData) || transferData.data is null) return null; |
91 NSData data = (NSData) transferData.data; | 105 NSData data = cast(NSData) transferData.data; |
92 if (data.length() is 0) return null; | 106 if (data.length() is 0) return null; |
93 NSImage nsImage = (NSImage) new NSImage().alloc(); | 107 NSImage nsImage = cast(NSImage) (new NSImage()).alloc(); |
94 nsImage.initWithData(data); | 108 nsImage.initWithData(data); |
95 //TODO: Image representation wrong??? | 109 //TODO: Image representation wrong??? |
96 Image image = Image.cocoa_new(Display.getCurrent(), DWT.BITMAP, nsImage); | 110 Image image = Image.cocoa_new(Display.getCurrent(), DWT.BITMAP, nsImage); |
97 ImageData imageData = image.getImageData(); | 111 ImageData imageData = image.getImageData(); |
98 image.dispose(); | 112 image.dispose(); |
99 return imageData; | 113 return imageData; |
100 } | 114 } |
101 | 115 |
102 protected int[] getTypeIds() { | 116 protected int[] getTypeIds() { |
103 return new int[] { TIFFID }; | 117 return [ TIFFID ]; |
104 } | 118 } |
105 | 119 |
106 protected String[] getTypeNames() { | 120 protected String[] getTypeNames() { |
107 return new String[] { TIFF }; | 121 return [ TIFF ]; |
108 } | 122 } |
109 | 123 |
110 bool checkImage(Object object) { | 124 bool checkImage(Object object) { |
111 if (object is null || !(object instanceof ImageData)) return false; | 125 if (object is null || !(cast(ImageData) object)) return false; |
112 return true; | 126 return true; |
113 } | 127 } |
114 | 128 |
115 protected bool validate(Object object) { | 129 protected bool validate(Object object) { |
116 return checkImage(object); | 130 return checkImage(object); |