Mercurial > projects > dwt-win
comparison dwt/dnd/RTFTransfer.d @ 135:242e33c0e383
Added dnd source, ByteArrayTransfer,Clipboard completed
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 13 Feb 2008 04:51:22 +0100 |
parents | |
children | 4e3f19210f93 |
comparison
equal
deleted
inserted
replaced
134:fa7d7d66b9ed | 135:242e33c0e383 |
---|---|
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwt.dnd.RTFTransfer; | |
14 | |
15 import dwt.internal.ole.win32.COM; | |
16 import dwt.internal.win32.OS; | |
17 | |
18 /** | |
19 * The class <code>RTFTransfer</code> provides a platform specific mechanism | |
20 * for converting text in RTF format represented as a java <code>String</code> | |
21 * to a platform specific representation of the data and vice versa. See | |
22 * <code>Transfer</code> for additional information. | |
23 * | |
24 * <p>An example of a java <code>String</code> containing RTF text is shown | |
25 * below:</p> | |
26 * | |
27 * <code><pre> | |
28 * String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}"; | |
29 * </code></pre> | |
30 */ | |
31 public class RTFTransfer : ByteArrayTransfer { | |
32 | |
33 private static RTFTransfer _instance = new RTFTransfer(); | |
34 private static final String CF_RTF = "Rich Text Format"; //$NON-NLS-1$ | |
35 private static final int CF_RTFID = registerType(CF_RTF); | |
36 | |
37 private this() {} | |
38 | |
39 /** | |
40 * Returns the singleton instance of the RTFTransfer class. | |
41 * | |
42 * @return the singleton instance of the RTFTransfer class | |
43 */ | |
44 public static RTFTransfer getInstance () { | |
45 return _instance; | |
46 } | |
47 | |
48 /** | |
49 * This implementation of <code>javaToNative</code> converts RTF-formatted text | |
50 * represented by a java <code>String</code> to a platform specific representation. | |
51 * For additional information see <code>Transfer#javaToNative</code>. | |
52 * | |
53 * @param object a java <code>String</code> containing RTF text | |
54 * @param transferData an empty <code>TransferData</code> object; this | |
55 * object will be filled in on return with the platform specific format of the data | |
56 */ | |
57 public void javaToNative (Object object, TransferData transferData){ | |
58 if (!checkRTF(object) || !isSupportedType(transferData)) { | |
59 DND.error(DND.ERROR_INVALID_DATA); | |
60 } | |
61 // CF_RTF is stored as a null terminated byte array | |
62 String string = (String)object; | |
63 int count = string.length(); | |
64 char[] chars = new char[count + 1]; | |
65 string.getChars(0, count, chars, 0); | |
66 int codePage = OS.GetACP(); | |
67 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null); | |
68 if (cchMultiByte is 0) { | |
69 transferData.stgmedium = new STGMEDIUM(); | |
70 transferData.result = COM.DV_E_STGMEDIUM; | |
71 return; | |
72 } | |
73 int lpMultiByteStr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte); | |
74 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null); | |
75 transferData.stgmedium = new STGMEDIUM(); | |
76 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; | |
77 transferData.stgmedium.unionField = lpMultiByteStr; | |
78 transferData.stgmedium.pUnkForRelease = 0; | |
79 transferData.result = COM.S_OK; | |
80 return; | |
81 } | |
82 | |
83 /** | |
84 * This implementation of <code>nativeToJava</code> converts a platform specific | |
85 * representation of RTF text to a java <code>String</code>. | |
86 * For additional information see <code>Transfer#nativeToJava</code>. | |
87 * | |
88 * @param transferData the platform specific representation of the data to be | |
89 * been converted | |
90 * @return a java <code>String</code> containing RTF text if the | |
91 * conversion was successful; otherwise null | |
92 */ | |
93 public Object nativeToJava(TransferData transferData){ | |
94 if (!isSupportedType(transferData) || transferData.pIDataObject is 0) return null; | |
95 IDataObject data = new IDataObject(transferData.pIDataObject); | |
96 data.AddRef(); | |
97 STGMEDIUM stgmedium = new STGMEDIUM(); | |
98 FORMATETC formatetc = transferData.formatetc; | |
99 stgmedium.tymed = COM.TYMED_HGLOBAL; | |
100 transferData.result = data.GetData(formatetc, stgmedium); | |
101 data.Release(); | |
102 if (transferData.result !is COM.S_OK) return null; | |
103 int hMem = stgmedium.unionField; | |
104 try { | |
105 int lpMultiByteStr = OS.GlobalLock(hMem); | |
106 if (lpMultiByteStr is 0) return null; | |
107 try { | |
108 int codePage = OS.GetACP(); | |
109 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0); | |
110 if (cchWideChar is 0) return null; | |
111 char[] lpWideCharStr = new char [cchWideChar - 1]; | |
112 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length); | |
113 return new String(lpWideCharStr); | |
114 } finally { | |
115 OS.GlobalUnlock(hMem); | |
116 } | |
117 } finally { | |
118 OS.GlobalFree(hMem); | |
119 } | |
120 } | |
121 | |
122 protected int[] getTypeIds(){ | |
123 return new int[] {CF_RTFID}; | |
124 } | |
125 | |
126 protected String[] getTypeNames(){ | |
127 return new String[] {CF_RTF}; | |
128 } | |
129 | |
130 bool checkRTF(Object object) { | |
131 return (object !is null && object instanceof String && ((String)object).length() > 0); | |
132 } | |
133 | |
134 protected bool validate(Object object) { | |
135 return checkRTF(object); | |
136 } | |
137 } |