| 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 dwt.dnd.RTFTransfer; |
|---|
| 14 |
|
|---|
| 15 |
import dwt.internal.ole.win32.COM; |
|---|
| 16 |
import dwt.internal.ole.win32.OBJIDL; |
|---|
| 17 |
import dwt.internal.win32.OS; |
|---|
| 18 |
|
|---|
| 19 |
import dwt.dnd.ByteArrayTransfer; |
|---|
| 20 |
import dwt.dnd.TransferData; |
|---|
| 21 |
import dwt.dnd.DND; |
|---|
| 22 |
|
|---|
| 23 |
import dwt.dwthelper.utils; |
|---|
| 24 |
static import tango.text.Text; |
|---|
| 25 |
alias tango.text.Text.Text!(char) StringBuffer; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
/** |
|---|
| 29 |
* The class <code>RTFTransfer</code> provides a platform specific mechanism |
|---|
| 30 |
* for converting text in RTF format represented as a java <code>String</code> |
|---|
| 31 |
* to a platform specific representation of the data and vice versa. |
|---|
| 32 |
* |
|---|
| 33 |
* <p>An example of a java <code>String</code> containing RTF text is shown |
|---|
| 34 |
* below:</p> |
|---|
| 35 |
* |
|---|
| 36 |
* <code><pre> |
|---|
| 37 |
* String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}"; |
|---|
| 38 |
* </code></pre> |
|---|
| 39 |
* |
|---|
| 40 |
* @see Transfer |
|---|
| 41 |
*/ |
|---|
| 42 |
public class RTFTransfer : ByteArrayTransfer { |
|---|
| 43 |
|
|---|
| 44 |
private static RTFTransfer _instance; |
|---|
| 45 |
private static const String CF_RTF = "Rich Text Format"; //$NON-NLS-1$ |
|---|
| 46 |
private static const int CF_RTFID; |
|---|
| 47 |
|
|---|
| 48 |
static this(){ |
|---|
| 49 |
CF_RTFID = registerType(CF_RTF); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
private this() {} |
|---|
| 53 |
|
|---|
| 54 |
/** |
|---|
| 55 |
* Returns the singleton instance of the RTFTransfer class. |
|---|
| 56 |
* |
|---|
| 57 |
* @return the singleton instance of the RTFTransfer class |
|---|
| 58 |
*/ |
|---|
| 59 |
public static RTFTransfer getInstance () { |
|---|
| 60 |
if( _instance is null ){ |
|---|
| 61 |
synchronized { |
|---|
| 62 |
if( _instance is null ){ |
|---|
| 63 |
_instance = new RTFTransfer(); |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
return _instance; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
/** |
|---|
| 71 |
* This implementation of <code>javaToNative</code> converts RTF-formatted text |
|---|
| 72 |
* represented by a java <code>String</code> to a platform specific representation. |
|---|
| 73 |
* |
|---|
| 74 |
* @param object a java <code>String</code> containing RTF text |
|---|
| 75 |
* @param transferData an empty <code>TransferData</code> object that will |
|---|
| 76 |
* be filled in on return with the platform specific format of the data |
|---|
| 77 |
* |
|---|
| 78 |
* @see Transfer#nativeToJava |
|---|
| 79 |
*/ |
|---|
| 80 |
public void javaToNative (Object object, TransferData transferData){ |
|---|
| 81 |
if (!checkRTF(object) || !isSupportedType(transferData)) { |
|---|
| 82 |
DND.error(DND.ERROR_INVALID_DATA); |
|---|
| 83 |
} |
|---|
| 84 |
// CF_RTF is stored as a null terminated byte array |
|---|
| 85 |
String string = (cast(ArrayWrapperString)object).array; |
|---|
| 86 |
wchar* chars = StrToWCHARz(string); |
|---|
| 87 |
int codePage = OS.GetACP(); |
|---|
| 88 |
int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null); |
|---|
| 89 |
if (cchMultiByte is 0) { |
|---|
| 90 |
transferData.stgmedium = new STGMEDIUM(); |
|---|
| 91 |
transferData.result = COM.DV_E_STGMEDIUM; |
|---|
| 92 |
return; |
|---|
| 93 |
} |
|---|
| 94 |
auto lpMultiByteStr = cast(PCHAR)OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte); |
|---|
| 95 |
OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null); |
|---|
| 96 |
transferData.stgmedium = new STGMEDIUM(); |
|---|
| 97 |
transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 98 |
transferData.stgmedium.unionField = lpMultiByteStr; |
|---|
| 99 |
transferData.stgmedium.pUnkForRelease = null; |
|---|
| 100 |
transferData.result = COM.S_OK; |
|---|
| 101 |
return; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
/** |
|---|
| 105 |
* This implementation of <code>nativeToJava</code> converts a platform specific |
|---|
| 106 |
* representation of RTF text to a java <code>String</code>. |
|---|
| 107 |
* |
|---|
| 108 |
* @param transferData the platform specific representation of the data to be converted |
|---|
| 109 |
* @return a java <code>String</code> containing RTF text if the conversion was successful; |
|---|
| 110 |
* otherwise null |
|---|
| 111 |
* |
|---|
| 112 |
* @see Transfer#javaToNative |
|---|
| 113 |
*/ |
|---|
| 114 |
public Object nativeToJava(TransferData transferData){ |
|---|
| 115 |
if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null; |
|---|
| 116 |
IDataObject data = transferData.pIDataObject; |
|---|
| 117 |
data.AddRef(); |
|---|
| 118 |
STGMEDIUM* stgmedium = new STGMEDIUM(); |
|---|
| 119 |
FORMATETC* formatetc = transferData.formatetc; |
|---|
| 120 |
stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 121 |
transferData.result = getData(data, formatetc, stgmedium); |
|---|
| 122 |
data.Release(); |
|---|
| 123 |
if (transferData.result !is COM.S_OK) return null; |
|---|
| 124 |
auto hMem = stgmedium.unionField; |
|---|
| 125 |
try { |
|---|
| 126 |
auto lpMultiByteStr = cast(CHAR*) OS.GlobalLock(hMem); |
|---|
| 127 |
if (lpMultiByteStr is null) return null; |
|---|
| 128 |
try { |
|---|
| 129 |
int codePage = OS.GetACP(); |
|---|
| 130 |
int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0); |
|---|
| 131 |
if (cchWideChar is 0) return null; |
|---|
| 132 |
wchar[] lpWideCharStr = new wchar [cchWideChar - 1]; |
|---|
| 133 |
OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr.ptr, lpWideCharStr.length); |
|---|
| 134 |
return new ArrayWrapperString( WCHARzToStr(lpWideCharStr.ptr)); |
|---|
| 135 |
} finally { |
|---|
| 136 |
OS.GlobalUnlock(hMem); |
|---|
| 137 |
} |
|---|
| 138 |
} finally { |
|---|
| 139 |
OS.GlobalFree(hMem); |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
protected int[] getTypeIds(){ |
|---|
| 144 |
return [CF_RTFID]; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
protected String[] getTypeNames(){ |
|---|
| 148 |
return [CF_RTF]; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
bool checkRTF(Object object) { |
|---|
| 152 |
if( auto s = cast(ArrayWrapperString)object ){ |
|---|
| 153 |
return s.array.length > 0 ; |
|---|
| 154 |
} |
|---|
| 155 |
return false; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
protected bool validate(Object object) { |
|---|
| 159 |
return checkRTF(object); |
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|