1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
16 import org.eclipse.swt.internal.ole.win32.*;
17 import org.eclipse.swt.internal.win32.*;
20 * The class <code>RTFTransfer</code> provides a platform specific mechanism
21 * for converting text in RTF format represented as a java <code>String</code>
22 * to a platform specific representation of the data and vice versa.
24 * <p>An example of a java <code>String</code> containing RTF text is shown
28 * String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}";
33 public class RTFTransfer extends ByteArrayTransfer {
35 private static RTFTransfer _instance = new RTFTransfer();
36 private static final String CF_RTF = "Rich Text Format"; //$NON-NLS-1$
37 private static final int CF_RTFID = registerType(CF_RTF);
39 private RTFTransfer() {}
42 * Returns the singleton instance of the RTFTransfer class.
44 * @return the singleton instance of the RTFTransfer class
46 public static RTFTransfer getInstance () {
51 * This implementation of <code>javaToNative</code> converts RTF-formatted text
52 * represented by a java <code>String</code> to a platform specific representation.
54 * @param object a java <code>String</code> containing RTF text
55 * @param transferData an empty <code>TransferData</code> object that will
56 * be filled in on return with the platform specific format of the data
58 * @see Transfer#nativeToJava
61 public void javaToNative (Object object, TransferData transferData){
62 if (!checkRTF(object) || !isSupportedType(transferData)) {
63 DND.error(DND.ERROR_INVALID_DATA);
65 // CF_RTF is stored as a null terminated byte array
66 String string = (String)object;
67 int count = string.length();
68 char[] chars = new char[count + 1];
69 string.getChars(0, count, chars, 0);
70 int codePage = OS.GetACP();
71 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null);
72 if (cchMultiByte == 0) {
73 transferData.stgmedium = new STGMEDIUM();
74 transferData.result = COM.DV_E_STGMEDIUM;
77 long lpMultiByteStr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte);
78 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null);
79 transferData.stgmedium = new STGMEDIUM();
80 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
81 transferData.stgmedium.unionField = lpMultiByteStr;
82 transferData.stgmedium.pUnkForRelease = 0;
83 transferData.result = COM.S_OK;
88 * This implementation of <code>nativeToJava</code> converts a platform specific
89 * representation of RTF text to a java <code>String</code>.
91 * @param transferData the platform specific representation of the data to be converted
92 * @return a java <code>String</code> containing RTF text if the conversion was successful;
95 * @see Transfer#javaToNative
98 public Object nativeToJava(TransferData transferData){
99 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
100 IDataObject data = new IDataObject(transferData.pIDataObject);
102 STGMEDIUM stgmedium = new STGMEDIUM();
103 FORMATETC formatetc = transferData.formatetc;
104 stgmedium.tymed = COM.TYMED_HGLOBAL;
105 transferData.result = getData(data, formatetc, stgmedium);
107 if (transferData.result != COM.S_OK) return null;
108 long hMem = stgmedium.unionField;
110 long lpMultiByteStr = OS.GlobalLock(hMem);
111 if (lpMultiByteStr == 0) return null;
113 int codePage = OS.GetACP();
114 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0);
115 if (cchWideChar == 0) return null;
116 char[] lpWideCharStr = new char [cchWideChar - 1];
117 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length);
118 return new String(lpWideCharStr);
120 OS.GlobalUnlock(hMem);
128 protected int[] getTypeIds(){
129 return new int[] {CF_RTFID};
133 protected String[] getTypeNames(){
134 return new String[] {CF_RTF};
137 boolean checkRTF(Object object) {
138 return (object != null && object instanceof String && ((String)object).length() > 0);
142 protected boolean validate(Object object) {
143 return checkRTF(object);