Changeset 142:c48567faeab1
- Timestamp:
- 02/13/08 10:26:35
(1 year ago)
- Author:
- Frank Benoit <benoit@tionex.de>
- branch:
- default
- Message:
TextTransfer?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r135 |
r142 |
|
| 14 | 14 | |
|---|
| 15 | 15 | import dwt.internal.ole.win32.COM; |
|---|
| | 16 | import dwt.internal.ole.win32.OBJIDL; |
|---|
| 16 | 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.Integer; |
|---|
| | 24 | import dwt.dwthelper.utils; |
|---|
| | 25 | static import tango.text.Text; |
|---|
| | 26 | alias tango.text.Text.Text!(char) StringBuffer; |
|---|
| 17 | 27 | |
|---|
| 18 | 28 | /** |
|---|
| … | … | |
| 32 | 42 | public class TextTransfer : ByteArrayTransfer { |
|---|
| 33 | 43 | |
|---|
| 34 | | private static TextTransfer _instance = new TextTransfer(); |
|---|
| 35 | | private static final String CF_UNICODETEXT = "CF_UNICODETEXT"; //$NON-NLS-1$ |
|---|
| 36 | | private static final String CF_TEXT = "CF_TEXT"; //$NON-NLS-1$ |
|---|
| 37 | | private static final int CF_UNICODETEXTID = COM.CF_UNICODETEXT; |
|---|
| 38 | | private static final int CF_TEXTID = COM.CF_TEXT; |
|---|
| | 44 | private static TextTransfer _instance; |
|---|
| | 45 | private static const char[] CF_UNICODETEXT = "CF_UNICODETEXT"; //$NON-NLS-1$ |
|---|
| | 46 | private static const char[] CF_TEXT = "CF_TEXT"; //$NON-NLS-1$ |
|---|
| | 47 | private static const int CF_UNICODETEXTID = COM.CF_UNICODETEXT; |
|---|
| | 48 | private static const int CF_TEXTID = COM.CF_TEXT; |
|---|
| 39 | 49 | |
|---|
| 40 | 50 | private this() {} |
|---|
| … | … | |
| 46 | 56 | */ |
|---|
| 47 | 57 | public static TextTransfer getInstance () { |
|---|
| | 58 | if( _instance is null ){ |
|---|
| | 59 | synchronized { |
|---|
| | 60 | if( _instance is null ){ |
|---|
| | 61 | _instance = new TextTransfer(); |
|---|
| | 62 | } |
|---|
| | 63 | } |
|---|
| | 64 | } |
|---|
| 48 | 65 | return _instance; |
|---|
| 49 | 66 | } |
|---|
| … | … | |
| 64 | 81 | } |
|---|
| 65 | 82 | transferData.result = COM.E_FAIL; |
|---|
| 66 | | String string = (String)object; |
|---|
| | 83 | char[] string = (cast(ArrayWrapperString)object).array; |
|---|
| 67 | 84 | switch (transferData.type) { |
|---|
| 68 | 85 | case COM.CF_UNICODETEXT: { |
|---|
| 69 | | int charCount = string.length (); |
|---|
| 70 | | char[] chars = new char[charCount+1]; |
|---|
| 71 | | string.getChars (0, charCount, chars, 0); |
|---|
| | 86 | wchar[] chars = StrToWCHARs(0,string, true); |
|---|
| | 87 | int charCount = chars.length; |
|---|
| 72 | 88 | int byteCount = chars.length * 2; |
|---|
| 73 | | int newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount); |
|---|
| 74 | | OS.MoveMemory(newPtr, chars, byteCount); |
|---|
| | 89 | auto newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount); |
|---|
| | 90 | OS.MoveMemory(newPtr, chars.ptr, byteCount); |
|---|
| 75 | 91 | transferData.stgmedium = new STGMEDIUM(); |
|---|
| 76 | 92 | transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 77 | 93 | transferData.stgmedium.unionField = newPtr; |
|---|
| 78 | | transferData.stgmedium.pUnkForRelease = 0; |
|---|
| | 94 | transferData.stgmedium.pUnkForRelease = null; |
|---|
| 79 | 95 | transferData.result = COM.S_OK; |
|---|
| 80 | 96 | break; |
|---|
| 81 | 97 | } |
|---|
| 82 | 98 | case COM.CF_TEXT: { |
|---|
| 83 | | int count = string.length(); |
|---|
| 84 | | char[] chars = new char[count + 1]; |
|---|
| 85 | | string.getChars(0, count, chars, 0); |
|---|
| | 99 | wchar[] chars = StrToWCHARs(0,string, true); |
|---|
| | 100 | int count = chars.length; |
|---|
| 86 | 101 | int codePage = OS.GetACP(); |
|---|
| 87 | | int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null); |
|---|
| | 102 | int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars.ptr, -1, null, 0, null, null); |
|---|
| 88 | 103 | if (cchMultiByte is 0) { |
|---|
| 89 | 104 | transferData.stgmedium = new STGMEDIUM(); |
|---|
| … | … | |
| 91 | 106 | return; |
|---|
| 92 | 107 | } |
|---|
| 93 | | int lpMultiByteStr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte); |
|---|
| 94 | | OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null); |
|---|
| | 108 | auto lpMultiByteStr = cast(char*)OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, cchMultiByte); |
|---|
| | 109 | OS.WideCharToMultiByte(codePage, 0, chars.ptr, -1, lpMultiByteStr, cchMultiByte, null, null); |
|---|
| 95 | 110 | transferData.stgmedium = new STGMEDIUM(); |
|---|
| 96 | 111 | transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 97 | 112 | transferData.stgmedium.unionField = lpMultiByteStr; |
|---|
| 98 | | transferData.stgmedium.pUnkForRelease = 0; |
|---|
| | 113 | transferData.stgmedium.pUnkForRelease = null; |
|---|
| 99 | 114 | transferData.result = COM.S_OK; |
|---|
| 100 | 115 | break; |
|---|
| … | … | |
| 114 | 129 | */ |
|---|
| 115 | 130 | public Object nativeToJava(TransferData transferData){ |
|---|
| 116 | | if (!isSupportedType(transferData) || transferData.pIDataObject is 0) return null; |
|---|
| 117 | | |
|---|
| 118 | | IDataObject data = new IDataObject(transferData.pIDataObject); |
|---|
| | 131 | if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null; |
|---|
| | 132 | |
|---|
| | 133 | IDataObject data = transferData.pIDataObject; |
|---|
| 119 | 134 | data.AddRef(); |
|---|
| 120 | | FORMATETC formatetc = transferData.formatetc; |
|---|
| 121 | | STGMEDIUM stgmedium = new STGMEDIUM(); |
|---|
| | 135 | FORMATETC* formatetc = transferData.formatetc; |
|---|
| | 136 | STGMEDIUM* stgmedium = new STGMEDIUM(); |
|---|
| 122 | 137 | stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 123 | 138 | transferData.result = data.GetData(formatetc, stgmedium); |
|---|
| 124 | 139 | data.Release(); |
|---|
| 125 | 140 | if (transferData.result !is COM.S_OK) return null; |
|---|
| 126 | | int hMem = stgmedium.unionField; |
|---|
| | 141 | auto hMem = stgmedium.unionField; |
|---|
| 127 | 142 | try { |
|---|
| 128 | 143 | switch (transferData.type) { |
|---|
| … | … | |
| 131 | 146 | int size = OS.GlobalSize(hMem) / 2 * 2; |
|---|
| 132 | 147 | if (size is 0) return null; |
|---|
| 133 | | char[] chars = new char[size/2]; |
|---|
| 134 | | int ptr = OS.GlobalLock(hMem); |
|---|
| 135 | | if (ptr is 0) return null; |
|---|
| | 148 | wchar[] chars = new wchar[size/2]; |
|---|
| | 149 | auto ptr = OS.GlobalLock(hMem); |
|---|
| | 150 | if (ptr is null) return null; |
|---|
| 136 | 151 | try { |
|---|
| 137 | | OS.MoveMemory(chars, ptr, size); |
|---|
| 138 | | int length = chars.length; |
|---|
| | 152 | OS.MoveMemory(chars.ptr, ptr, size); |
|---|
| | 153 | int length_ = chars.length; |
|---|
| 139 | 154 | for (int i=0; i<chars.length; i++) { |
|---|
| 140 | 155 | if (chars [i] is '\0') { |
|---|
| 141 | | length = i; |
|---|
| | 156 | length_ = i; |
|---|
| 142 | 157 | break; |
|---|
| 143 | 158 | } |
|---|
| 144 | 159 | } |
|---|
| 145 | | return new String (chars, 0, length); |
|---|
| | 160 | return new ArrayWrapperString (WCHARsToStr(chars[ 0 .. length_])); |
|---|
| 146 | 161 | } finally { |
|---|
| 147 | 162 | OS.GlobalUnlock(hMem); |
|---|
| … | … | |
| 149 | 164 | } |
|---|
| 150 | 165 | case CF_TEXTID: { |
|---|
| 151 | | int lpMultiByteStr = OS.GlobalLock(hMem); |
|---|
| 152 | | if (lpMultiByteStr is 0) return null; |
|---|
| | 166 | auto lpMultiByteStr = cast(char*)OS.GlobalLock(hMem); |
|---|
| | 167 | if (lpMultiByteStr is null) return null; |
|---|
| 153 | 168 | try { |
|---|
| 154 | 169 | int codePage = OS.GetACP(); |
|---|
| 155 | 170 | int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0); |
|---|
| 156 | 171 | if (cchWideChar is 0) return null; |
|---|
| 157 | | char[] lpWideCharStr = new char [cchWideChar - 1]; |
|---|
| 158 | | OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length); |
|---|
| 159 | | return new String(lpWideCharStr); |
|---|
| | 172 | wchar[] lpWideCharStr = new wchar [cchWideChar - 1]; |
|---|
| | 173 | OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr.ptr, lpWideCharStr.length); |
|---|
| | 174 | return new ArrayWrapperString( WCHARzToStr(lpWideCharStr.ptr)); |
|---|
| 160 | 175 | } finally { |
|---|
| 161 | 176 | OS.GlobalUnlock(hMem); |
|---|
| … | … | |
| 170 | 185 | |
|---|
| 171 | 186 | protected int[] getTypeIds(){ |
|---|
| 172 | | return new int[] {CF_UNICODETEXTID, CF_TEXTID}; |
|---|
| 173 | | } |
|---|
| 174 | | |
|---|
| 175 | | protected String[] getTypeNames(){ |
|---|
| 176 | | return new String[] {CF_UNICODETEXT, CF_TEXT}; |
|---|
| | 187 | return [CF_UNICODETEXTID, CF_TEXTID]; |
|---|
| | 188 | } |
|---|
| | 189 | |
|---|
| | 190 | protected char[][] getTypeNames(){ |
|---|
| | 191 | return [CF_UNICODETEXT, CF_TEXT]; |
|---|
| 177 | 192 | } |
|---|
| 178 | 193 | |
|---|
| 179 | 194 | bool checkText(Object object) { |
|---|
| 180 | | return (object !is null && object instanceof String && ((String)object).length() > 0); |
|---|
| | 195 | if( auto s = cast(ArrayWrapperString)object ){ |
|---|
| | 196 | return s.array.length > 0; |
|---|
| | 197 | } |
|---|
| | 198 | return false; |
|---|
| 181 | 199 | } |
|---|
| 182 | 200 | |
|---|