1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
15
16 import org.eclipse.swt.internal.ole.win32.*;
17 import org.eclipse.swt.internal.win32.*;
18
19 /**
20 * The class <code>TextTransfer</code> provides a platform specific mechanism
21 * for converting plain text represented as a java <code>String</code>
22 * to a platform specific representation of the data and vice versa.
23 *
24 * <p>An example of a java <code>String</code> containing plain text is shown
25 * below:</p>
26 *
27 * <pre><code>
28 * String textData = "Hello World";
29 * </code></pre>
30 *
31 * <p>Note the <code>TextTransfer</code> does not change the content of the text
32 * data. For a better integration with the platform, the application should convert
33 * the line delimiters used in the text data to the standard line delimiter used by the
34 * platform.
35 * </p>
36 *
37 * @see Transfer
38 */
39 public class TextTransfer extends ByteArrayTransfer {
40
41 private static TextTransfer _instance = new TextTransfer();
42 private static final String CF_UNICODETEXT = "CF_UNICODETEXT"; //$NON-NLS-1$
43 private static final String CF_TEXT = "CF_TEXT"; //$NON-NLS-1$
44 private static final int CF_UNICODETEXTID = COM.CF_UNICODETEXT;
45 private static final int CF_TEXTID = COM.CF_TEXT;
46
TextTransfer()47 private TextTransfer() {}
48
49 /**
50 * Returns the singleton instance of the TextTransfer class.
51 *
52 * @return the singleton instance of the TextTransfer class
53 */
getInstance()54 public static TextTransfer getInstance () {
55 return _instance;
56 }
57
58 /**
59 * This implementation of <code>javaToNative</code> converts plain text
60 * represented by a java <code>String</code> to a platform specific representation.
61 *
62 * @param object a java <code>String</code> containing text
63 * @param transferData an empty <code>TransferData</code> object that will
64 * be filled in on return with the platform specific format of the data
65 *
66 * @see Transfer#nativeToJava
67 */
68 @Override
javaToNative(Object object, TransferData transferData)69 public void javaToNative (Object object, TransferData transferData){
70 if (!checkText(object) || !isSupportedType(transferData)) {
71 DND.error(DND.ERROR_INVALID_DATA);
72 }
73 transferData.result = COM.E_FAIL;
74 String string = (String)object;
75 switch (transferData.type) {
76 case COM.CF_UNICODETEXT: {
77 int charCount = string.length ();
78 char[] chars = new char[charCount+1];
79 string.getChars (0, charCount, chars, 0);
80 int byteCount = chars.length * 2;
81 long newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount);
82 OS.MoveMemory(newPtr, chars, byteCount);
83 transferData.stgmedium = new STGMEDIUM();
84 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
85 transferData.stgmedium.unionField = newPtr;
86 transferData.stgmedium.pUnkForRelease = 0;
87 transferData.result = COM.S_OK;
88 break;
89 }
90 case COM.CF_TEXT: {
91 int count = string.length();
92 char[] chars = new char[count + 1];
93 string.getChars(0, count, chars, 0);
94 int codePage = OS.GetACP();
95 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null);
96 if (cchMultiByte == 0) {
97 transferData.stgmedium = new STGMEDIUM();
98 transferData.result = COM.DV_E_STGMEDIUM;
99 return;
100 }
101 long lpMultiByteStr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte);
102 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null);
103 transferData.stgmedium = new STGMEDIUM();
104 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
105 transferData.stgmedium.unionField = lpMultiByteStr;
106 transferData.stgmedium.pUnkForRelease = 0;
107 transferData.result = COM.S_OK;
108 break;
109 }
110 }
111 return;
112 }
113
114 /**
115 * This implementation of <code>nativeToJava</code> converts a platform specific
116 * representation of plain text to a java <code>String</code>.
117 *
118 * @param transferData the platform specific representation of the data to be converted
119 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null
120 *
121 * @see Transfer#javaToNative
122 */
123 @Override
nativeToJava(TransferData transferData)124 public Object nativeToJava(TransferData transferData){
125 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
126
127 IDataObject data = new IDataObject(transferData.pIDataObject);
128 data.AddRef();
129 FORMATETC formatetc = transferData.formatetc;
130 STGMEDIUM stgmedium = new STGMEDIUM();
131 stgmedium.tymed = COM.TYMED_HGLOBAL;
132 transferData.result = getData(data, formatetc, stgmedium);
133 data.Release();
134 if (transferData.result != COM.S_OK) return null;
135 long hMem = stgmedium.unionField;
136 try {
137 switch (transferData.type) {
138 case CF_UNICODETEXTID: {
139 /* Ensure byteCount is a multiple of 2 bytes */
140 int size = OS.GlobalSize(hMem) / 2 * 2;
141 if (size == 0) return null;
142 char[] chars = new char[size/2];
143 long ptr = OS.GlobalLock(hMem);
144 if (ptr == 0) return null;
145 try {
146 OS.MoveMemory(chars, ptr, size);
147 int length = chars.length;
148 for (int i=0; i<chars.length; i++) {
149 if (chars [i] == '\0') {
150 length = i;
151 break;
152 }
153 }
154 return new String (chars, 0, length);
155 } finally {
156 OS.GlobalUnlock(hMem);
157 }
158 }
159 case CF_TEXTID: {
160 long lpMultiByteStr = OS.GlobalLock(hMem);
161 if (lpMultiByteStr == 0) return null;
162 try {
163 int codePage = OS.GetACP();
164 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0);
165 if (cchWideChar == 0) return null;
166 char[] lpWideCharStr = new char [cchWideChar - 1];
167 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length);
168 return new String(lpWideCharStr);
169 } finally {
170 OS.GlobalUnlock(hMem);
171 }
172 }
173 }
174 } finally {
175 OS.GlobalFree(hMem);
176 }
177 return null;
178 }
179
180 @Override
getTypeIds()181 protected int[] getTypeIds(){
182 return new int[] {CF_UNICODETEXTID, CF_TEXTID};
183 }
184
185 @Override
getTypeNames()186 protected String[] getTypeNames(){
187 return new String[] {CF_UNICODETEXT, CF_TEXT};
188 }
189
checkText(Object object)190 boolean checkText(Object object) {
191 return (object != null && object instanceof String && ((String)object).length() > 0);
192 }
193
194 @Override
validate(Object object)195 protected boolean validate(Object object) {
196 return checkText(object);
197 }
198 }
199