|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2000, 2004 IBM Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * IBM Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.swt.dnd; |
| 12 | + |
| 13 | +import org.eclipse.swt.SWT; |
| 14 | +import org.eclipse.swt.graphics.*; |
| 15 | +import org.eclipse.swt.internal.Converter; |
| 16 | +import org.eclipse.swt.internal.gtk.*; |
| 17 | +import org.eclipse.swt.widgets.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * The class <code>ImageTransfer</code> provides a platform specific mechanism |
| 21 | + * for converting a Image represented as a java <code>ImageData</code> to a |
| 22 | + * platform specific representation of the data and vice versa. |
| 23 | + * See <code>Transfer</code> for additional information. |
| 24 | + * |
| 25 | + * <p>An example of a java <code>ImageData</code> is shown |
| 26 | + * below:</p> |
| 27 | + * |
| 28 | + * <code><pre> |
| 29 | + * Image image = new Image("C:\temp\img1.gif"); |
| 30 | + * ImageData imgData = image.getImageData(); |
| 31 | + * </code></pre> |
| 32 | + */ |
| 33 | +public class ImageTransfer extends ByteArrayTransfer { |
| 34 | + |
| 35 | + private static ImageTransfer _instance = new ImageTransfer(); |
| 36 | + |
| 37 | + private static final String JPEG = "image/jpge"; //$NON-NLS-1$ |
| 38 | + private static final int JPEG_ID = registerType(JPEG); |
| 39 | + private static final String PNG = "image/png"; //$NON-NLS-1$ |
| 40 | + private static final int PNG_ID = registerType(PNG); |
| 41 | + private static final String BMP = "image/bmp"; //$NON-NLS-1$ |
| 42 | + private static final int BMP_ID = registerType(BMP); |
| 43 | + private static final String EPS = "image/eps"; //$NON-NLS-1$ |
| 44 | + private static final int EPS_ID = registerType(EPS); |
| 45 | + private static final String PCX = "image/pcx"; //$NON-NLS-1$ |
| 46 | + private static final int PCX_ID = registerType(PCX); |
| 47 | + private static final String PPM = "image/ppm"; //$NON-NLS-1$ |
| 48 | + private static final int PPM_ID = registerType(PPM); |
| 49 | + private static final String RGB = "image/ppm"; //$NON-NLS-1$ |
| 50 | + private static final int RGB_ID = registerType(RGB); |
| 51 | + private static final String TGA = "image/tga"; //$NON-NLS-1$ |
| 52 | + private static final int TGA_ID = registerType(TGA); |
| 53 | + private static final String XBM = "image/xbm"; //$NON-NLS-1$ |
| 54 | + private static final int XBM_ID = registerType(XBM); |
| 55 | + private static final String XPM = "image/xpm"; //$NON-NLS-1$ |
| 56 | + private static final int XPM_ID = registerType(XPM); |
| 57 | + private static final String XV = "image/xv"; //$NON-NLS-1$ |
| 58 | + private static final int XV_ID = registerType(XV); |
| 59 | + |
| 60 | +private ImageTransfer() {} |
| 61 | + |
| 62 | +/** |
| 63 | + * Returns the singleton instance of the ImageTransfer class. |
| 64 | + * |
| 65 | + * @return the singleton instance of the ImageTransfer class |
| 66 | + */ |
| 67 | +public static ImageTransfer getInstance () { |
| 68 | + return _instance; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * This implementation of <code>javaToNative</code> converts an ImageData object represented |
| 73 | + * by java <code>ImageData</code> to a platform specific representation. |
| 74 | + * For additional information see <code>Transfer#javaToNative</code>. |
| 75 | + * |
| 76 | + * @param object a java <code>ImageData</code> containing the ImageData to be |
| 77 | + * converted |
| 78 | + * @param transferData an empty <code>TransferData</code> object; this |
| 79 | + * object will be filled in on return with the platform specific format of the data |
| 80 | + */ |
| 81 | +public void javaToNative(Object object, TransferData transferData) { |
| 82 | + if (!checkImage(object) || !isSupportedType(transferData)) { |
| 83 | + DND.error(DND.ERROR_INVALID_DATA); |
| 84 | + } |
| 85 | + if (OS.GTK_VERSION < OS.VERSION (2, 4, 0)) return; |
| 86 | + |
| 87 | + ImageData imgData = (ImageData)object; |
| 88 | + if (imgData == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 89 | + Image image = new Image(Display.getCurrent(), imgData); |
| 90 | + int /*long*/ pixmap = image.pixmap; |
| 91 | + int width = imgData.width; |
| 92 | + int height = imgData.height; |
| 93 | + int /*long*/ pixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, true, 8, width, height); |
| 94 | + if (pixbuf == 0) SWT.error(SWT.ERROR_NO_HANDLES); |
| 95 | + int /*long*/ colormap = OS.gdk_colormap_get_system(); |
| 96 | + OS.gdk_pixbuf_get_from_drawable(pixbuf, pixmap, colormap, 0, 0, 0, 0, width, height); |
| 97 | + |
| 98 | + String typeStr = ""; |
| 99 | + if (transferData.type == JPEG_ID) typeStr = "jpeg"; |
| 100 | + if (transferData.type == PNG_ID) typeStr = "png"; |
| 101 | + if (transferData.type == BMP_ID) typeStr = "bmp"; |
| 102 | + if (transferData.type == EPS_ID) typeStr = "eps"; |
| 103 | + if (transferData.type == PCX_ID) typeStr = "pcx"; |
| 104 | + if (transferData.type == PPM_ID) typeStr = "ppm"; |
| 105 | + if (transferData.type == RGB_ID) typeStr = "rgb"; |
| 106 | + if (transferData.type == TGA_ID) typeStr = "tga"; |
| 107 | + if (transferData.type == XBM_ID) typeStr = "xbm"; |
| 108 | + if (transferData.type == XPM_ID) typeStr = "xpm"; |
| 109 | + if (transferData.type == XV_ID) typeStr = "xv"; |
| 110 | + byte[] type = Converter.wcsToMbcs(null, typeStr , true); |
| 111 | + int /*long*/ [] buffer = new int /*long*/ [1]; |
| 112 | + int [] len = new int [1]; |
| 113 | + if (type == null) return; |
| 114 | + OS.gdk_pixbuf_save_to_buffer(pixbuf, buffer, len, type , null, null); |
| 115 | + OS.g_object_unref(pixbuf); |
| 116 | + image.dispose(); |
| 117 | + transferData.pValue = buffer[0]; |
| 118 | + transferData.length = len[0]; |
| 119 | + transferData.result = 1; |
| 120 | + transferData.format = 32; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * This implementation of <code>nativeToJava</code> converts a platform specific |
| 125 | + * representation of an <code>ImageData</code> to java. |
| 126 | + * For additional information see <code>Transfer#nativeToJava</code>. |
| 127 | + * |
| 128 | + * @param transferData the platform specific representation of the data to be |
| 129 | + * been converted |
| 130 | + * @return a java <code>ImageData</code> the imageData of the image if |
| 131 | + * conversion was successful; otherwise null |
| 132 | + */ |
| 133 | +public Object nativeToJava(TransferData transferData) { |
| 134 | + ImageData imgData = null; |
| 135 | + if (transferData.length > 0) |
| 136 | + { |
| 137 | + int /*long*/ loader = OS.gdk_pixbuf_loader_new(); |
| 138 | + OS.gdk_pixbuf_loader_write(loader, transferData.pValue, transferData.length, null); |
| 139 | + OS.gdk_pixbuf_loader_close(loader, null); |
| 140 | + int /*long*/ pixbuf = OS.gdk_pixbuf_loader_get_pixbuf(loader); |
| 141 | + if (pixbuf != 0) { |
| 142 | + OS.g_object_ref(pixbuf); |
| 143 | + int /*long*/ [] pixmap_return = new int /*long*/ [1]; |
| 144 | + OS.gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap_return, null, 0); |
| 145 | + int /*long*/ handle = pixmap_return[0]; |
| 146 | + if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); |
| 147 | + OS.g_object_unref(loader); |
| 148 | + Image img = Image.gtk_new(Display.getCurrent(), SWT.BITMAP, handle, 0); |
| 149 | + imgData = img.getImageData(); |
| 150 | + img.dispose(); |
| 151 | + } |
| 152 | + } |
| 153 | + return imgData; |
| 154 | +} |
| 155 | + |
| 156 | +protected int[] getTypeIds(){ |
| 157 | + return new int[]{JPEG_ID, PNG_ID, BMP_ID, EPS_ID, PCX_ID, PPM_ID, RGB_ID, TGA_ID, XBM_ID, XPM_ID, XV_ID}; |
| 158 | +} |
| 159 | + |
| 160 | +protected String[] getTypeNames(){ |
| 161 | + return new String[]{JPEG, PNG, BMP, EPS, PCX, PPM, RGB, TGA, XBM, XPM, XV}; |
| 162 | +} |
| 163 | + |
| 164 | +boolean checkImage(Object object) { |
| 165 | + if (object == null || !(object instanceof ImageData)) return false; |
| 166 | + return true; |
| 167 | +} |
| 168 | + |
| 169 | +protected boolean validate(Object object) { |
| 170 | + return checkImage(object); |
| 171 | +} |
| 172 | +} |
0 commit comments