1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 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 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.dnd.TextTransfer; 14 15 import org.eclipse.swt.internal.Converter; 16 import org.eclipse.swt.internal.gtk.OS; 17 import java.lang.all; 18 import org.eclipse.swt.dnd.ByteArrayTransfer; 19 import org.eclipse.swt.dnd.TransferData; 20 import org.eclipse.swt.dnd.DND; 21 22 23 /** 24 * The class <code>TextTransfer</code> provides a platform specific mechanism 25 * for converting plain text represented as a java <code>String</code> 26 * to a platform specific representation of the data and vice versa. 27 * 28 * <p>An example of a java <code>String</code> containing plain text is shown 29 * below:</p> 30 * 31 * <code><pre> 32 * String textData = "Hello World"; 33 * </code></pre> 34 * 35 * @see Transfer 36 */ 37 public class TextTransfer : ByteArrayTransfer { 38 39 private static TextTransfer _instance; 40 private static const String COMPOUND_TEXT = "COMPOUND_TEXT"; //$NON-NLS-1$ 41 private static const String UTF8_STRING = "UTF8_STRING"; //$NON-NLS-1$ 42 private static const String STRING = "STRING"; //$NON-NLS-1$ 43 private static const int COMPOUND_TEXT_ID; 44 private static const int UTF8_STRING_ID; 45 private static const int STRING_ID; 46 47 static this(){ 48 COMPOUND_TEXT_ID = registerType(COMPOUND_TEXT); 49 UTF8_STRING_ID = registerType(UTF8_STRING); 50 STRING_ID = registerType(STRING); 51 _instance = new TextTransfer(); 52 } 53 54 private this() {} 55 56 /** 57 * Returns the singleton instance of the TextTransfer class. 58 * 59 * @return the singleton instance of the TextTransfer class 60 */ 61 public static TextTransfer getInstance () { 62 return _instance; 63 } 64 65 /** 66 * This implementation of <code>javaToNative</code> converts plain text 67 * represented by a java <code>String</code> to a platform specific representation. 68 * 69 * @param object a java <code>String</code> containing text 70 * @param transferData an empty <code>TransferData</code> object that will 71 * be filled in on return with the platform specific format of the data 72 * 73 * @see Transfer#nativeToJava 74 */ 75 override public void javaToNative (Object object, TransferData transferData) { 76 transferData.result = 0; 77 if (!checkText(object) || !isSupportedType(transferData)) { 78 DND.error(DND.ERROR_INVALID_DATA); 79 } 80 String str = stringcast(object); 81 char* utf8 = toStringz(str); 82 if (transferData.type is cast(void*) COMPOUND_TEXT_ID) { 83 void* encoding; 84 int format; 85 char* ctext; 86 int length; 87 bool result = cast(bool) OS.gdk_utf8_to_compound_text(utf8, &encoding, &format, &ctext, &length); 88 if (!result) return; 89 transferData.type = encoding; 90 transferData.format = format; 91 transferData.length = length; 92 transferData.pValue = ctext; 93 transferData.result = 1; 94 } 95 if (transferData.type is cast(void*)UTF8_STRING_ID) { 96 char* pValue = cast(char*)OS.g_malloc(str.length+1); 97 if (pValue is null) return; 98 pValue[ 0 .. str.length ] = str; 99 pValue[ str.length ] = '\0'; 100 transferData.type = cast(void*)UTF8_STRING_ID; 101 transferData.format = 8; 102 transferData.length = cast(int)/*64bit*/str.length; 103 transferData.pValue = pValue; 104 transferData.result = 1; 105 } 106 if (transferData.type is cast(void*)STRING_ID) { 107 auto string_target = OS.gdk_utf8_to_string_target(utf8); 108 if (string_target is null) return; 109 transferData.type = cast(void*)STRING_ID; 110 transferData.format = 8; 111 transferData.length = OS.strlen(string_target); 112 transferData.pValue = string_target; 113 transferData.result = 1; 114 } 115 } 116 117 /** 118 * This implementation of <code>nativeToJava</code> converts a platform specific 119 * representation of plain text to a java <code>String</code>. 120 * 121 * @param transferData the platform specific representation of the data to be converted 122 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null 123 * 124 * @see Transfer#javaToNative 125 */ 126 override public Object nativeToJava(TransferData transferData){ 127 if (!isSupportedType(transferData) || transferData.pValue is null) return null; 128 char** list; 129 ptrdiff_t count = OS.gdk_text_property_to_utf8_list(transferData.type, transferData.format, transferData.pValue, transferData.length, &list); 130 if (count is 0) return null; 131 String utf8 = fromStringz( list[0] )._idup(); 132 OS.g_strfreev(list); 133 return new ArrayWrapperString( utf8 ); 134 } 135 136 override protected int[] getTypeIds() { 137 return [UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID]; 138 } 139 140 override protected String[] getTypeNames() { 141 return [UTF8_STRING, COMPOUND_TEXT, STRING]; 142 } 143 144 bool checkText(Object object) { 145 if( object is null ) return false; 146 ArrayWrapperString astr = cast(ArrayWrapperString)object; 147 if( astr is null ) return false; 148 return astr.array.length > 0; 149 } 150 151 protected override bool validate(Object object) { 152 return checkText(object); 153 } 154 }