]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/dnd/RTFTransfer.java
ab1fbb1f8d4e8acd6bd5f1a4d2f1468c36777837
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / dnd / RTFTransfer.java
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>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.
23  *
24  * <p>An example of a java <code>String</code> containing RTF text is shown
25  * below:</p>
26  *
27  * <pre><code>
28  *     String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}";
29  * </code></pre>
30  *
31  * @see Transfer
32  */
33 public class RTFTransfer extends ByteArrayTransfer {
34
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);
38
39 private RTFTransfer() {}
40
41 /**
42  * Returns the singleton instance of the RTFTransfer class.
43  *
44  * @return the singleton instance of the RTFTransfer class
45  */
46 public static RTFTransfer getInstance () {
47         return _instance;
48 }
49
50 /**
51  * This implementation of <code>javaToNative</code> converts RTF-formatted text
52  * represented by a java <code>String</code> to a platform specific representation.
53  *
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
57  *
58  * @see Transfer#nativeToJava
59  */
60 @Override
61 public void javaToNative (Object object, TransferData transferData){
62         if (!checkRTF(object) || !isSupportedType(transferData)) {
63                 DND.error(DND.ERROR_INVALID_DATA);
64         }
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;
75                 return;
76         }
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;
84         return;
85 }
86
87 /**
88  * This implementation of <code>nativeToJava</code> converts a platform specific
89  * representation of RTF text to a java <code>String</code>.
90  *
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;
93  *              otherwise null
94  *
95  * @see Transfer#javaToNative
96  */
97 @Override
98 public Object nativeToJava(TransferData transferData){
99         if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
100         IDataObject data = new IDataObject(transferData.pIDataObject);
101         data.AddRef();
102         STGMEDIUM stgmedium = new STGMEDIUM();
103         FORMATETC formatetc = transferData.formatetc;
104         stgmedium.tymed = COM.TYMED_HGLOBAL;
105         transferData.result = getData(data, formatetc, stgmedium);
106         data.Release();
107         if (transferData.result != COM.S_OK) return null;
108         long hMem = stgmedium.unionField;
109         try {
110                 long lpMultiByteStr = OS.GlobalLock(hMem);
111                 if (lpMultiByteStr == 0) return null;
112                 try {
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);
119                 } finally {
120                         OS.GlobalUnlock(hMem);
121                 }
122         } finally {
123                 OS.GlobalFree(hMem);
124         }
125 }
126
127 @Override
128 protected int[] getTypeIds(){
129         return new int[] {CF_RTFID};
130 }
131
132 @Override
133 protected String[] getTypeNames(){
134         return new String[] {CF_RTF};
135 }
136
137 boolean checkRTF(Object object) {
138         return (object != null  && object instanceof String && ((String)object).length() > 0);
139 }
140
141 @Override
142 protected boolean validate(Object object) {
143         return checkRTF(object);
144 }
145 }