Changeset 138:b479f7e2f431
- Timestamp:
- 02/13/08 09:23:04
(1 year ago)
- Author:
- Frank Benoit <benoit@tionex.de>
- branch:
- default
- Message:
FileTransfer?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r135 |
r138 |
|
| 14 | 14 | |
|---|
| 15 | 15 | import dwt.internal.ole.win32.COM; |
|---|
| | 16 | import dwt.internal.ole.win32.OBJIDL; |
|---|
| 16 | 17 | import dwt.internal.win32.OS; |
|---|
| 17 | 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; |
|---|
| 18 | 26 | |
|---|
| 19 | 27 | /** |
|---|
| … | … | |
| 38 | 46 | public class FileTransfer : ByteArrayTransfer { |
|---|
| 39 | 47 | |
|---|
| 40 | | private static FileTransfer _instance = new FileTransfer(); |
|---|
| 41 | | private static final String CF_HDROP = "CF_HDROP "; //$NON-NLS-1$ |
|---|
| | 48 | private static FileTransfer _instance; |
|---|
| | 49 | private static final char[] CF_HDROP = "CF_HDROP "; //$NON-NLS-1$ |
|---|
| 42 | 50 | private static final int CF_HDROPID = COM.CF_HDROP; |
|---|
| 43 | | private static final String CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$ |
|---|
| | 51 | private static final char[] CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$ |
|---|
| 44 | 52 | |
|---|
| 45 | 53 | private this() {} |
|---|
| … | … | |
| 51 | 59 | */ |
|---|
| 52 | 60 | public static FileTransfer getInstance () { |
|---|
| | 61 | if( _instance is null ){ |
|---|
| | 62 | synchronized { |
|---|
| | 63 | if( _instance is null ){ |
|---|
| | 64 | _instance = new FileTransfer(); |
|---|
| | 65 | } |
|---|
| | 66 | } |
|---|
| | 67 | } |
|---|
| 53 | 68 | return _instance; |
|---|
| 54 | 69 | } |
|---|
| … | … | |
| 70 | 85 | DND.error(DND.ERROR_INVALID_DATA); |
|---|
| 71 | 86 | } |
|---|
| 72 | | String[] fileNames = (String[]) object; |
|---|
| | 87 | char[][] fileNames; |
|---|
| | 88 | if( auto strs = cast(ArrayWrapperString2) object ){ |
|---|
| | 89 | fileNames = strs.array; |
|---|
| | 90 | } |
|---|
| 73 | 91 | StringBuffer allFiles = new StringBuffer(); |
|---|
| 74 | 92 | for (int i = 0; i < fileNames.length; i++) { |
|---|
| … | … | |
| 76 | 94 | allFiles.append(CF_HDROP_SEPARATOR); // each name is null terminated |
|---|
| 77 | 95 | } |
|---|
| 78 | | TCHAR buffer = new TCHAR(0, allFiles.toString(), true); // there is an extra null terminator at the very end |
|---|
| 79 | | DROPFILES dropfiles = new DROPFILES(); |
|---|
| | 96 | TCHAR[] buffer = StrToTCHARs(0, allFiles.toString(), true); // there is an extra null terminator at the very end |
|---|
| | 97 | DROPFILES dropfiles; |
|---|
| 80 | 98 | dropfiles.pFiles = DROPFILES.sizeof; |
|---|
| 81 | | dropfiles.pt_x = dropfiles.pt_y = 0; |
|---|
| | 99 | dropfiles.pt.x = dropfiles.pt.y = 0; |
|---|
| 82 | 100 | dropfiles.fNC = 0; |
|---|
| 83 | 101 | dropfiles.fWide = OS.IsUnicode ? 1 : 0; |
|---|
| 84 | 102 | // Allocate the memory because the caller (DropTarget) has not handed it in |
|---|
| 85 | 103 | // The caller of this method must release the data when it is done with it. |
|---|
| 86 | | int byteCount = buffer.length() * TCHAR.sizeof; |
|---|
| 87 | | int newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, DROPFILES.sizeof + byteCount); |
|---|
| 88 | | OS.MoveMemory(newPtr, dropfiles, DROPFILES.sizeof); |
|---|
| 89 | | OS.MoveMemory(newPtr + DROPFILES.sizeof, buffer, byteCount); |
|---|
| | 104 | int byteCount = buffer.length * TCHAR.sizeof; |
|---|
| | 105 | auto newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, DROPFILES.sizeof + byteCount); |
|---|
| | 106 | OS.MoveMemory(newPtr, &dropfiles, DROPFILES.sizeof); |
|---|
| | 107 | OS.MoveMemory(newPtr + DROPFILES.sizeof, buffer.ptr, byteCount); |
|---|
| 90 | 108 | transferData.stgmedium = new STGMEDIUM(); |
|---|
| 91 | 109 | transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 92 | 110 | transferData.stgmedium.unionField = newPtr; |
|---|
| 93 | | transferData.stgmedium.pUnkForRelease = 0; |
|---|
| | 111 | transferData.stgmedium.pUnkForRelease = null; |
|---|
| 94 | 112 | transferData.result = COM.S_OK; |
|---|
| 95 | 113 | } |
|---|
| … | … | |
| 107 | 125 | */ |
|---|
| 108 | 126 | public Object nativeToJava(TransferData transferData) { |
|---|
| 109 | | if (!isSupportedType(transferData) || transferData.pIDataObject is 0) return null; |
|---|
| | 127 | if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null; |
|---|
| 110 | 128 | |
|---|
| 111 | 129 | // get file names from IDataObject |
|---|
| 112 | | IDataObject dataObject = new IDataObject(transferData.pIDataObject); |
|---|
| | 130 | IDataObject dataObject = transferData.pIDataObject; |
|---|
| 113 | 131 | dataObject.AddRef(); |
|---|
| 114 | | FORMATETC formatetc = new FORMATETC(); |
|---|
| | 132 | FORMATETC* formatetc = new FORMATETC(); |
|---|
| 115 | 133 | formatetc.cfFormat = COM.CF_HDROP; |
|---|
| 116 | | formatetc.ptd = 0; |
|---|
| | 134 | formatetc.ptd = null; |
|---|
| 117 | 135 | formatetc.dwAspect = COM.DVASPECT_CONTENT; |
|---|
| 118 | 136 | formatetc.lindex = -1; |
|---|
| 119 | 137 | formatetc.tymed = COM.TYMED_HGLOBAL; |
|---|
| 120 | | STGMEDIUM stgmedium = new STGMEDIUM(); |
|---|
| | 138 | STGMEDIUM* stgmedium = new STGMEDIUM(); |
|---|
| 121 | 139 | stgmedium.tymed = COM.TYMED_HGLOBAL; |
|---|
| 122 | 140 | transferData.result = dataObject.GetData(formatetc, stgmedium); |
|---|
| … | … | |
| 125 | 143 | // How many files are there? |
|---|
| 126 | 144 | int count = OS.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0); |
|---|
| 127 | | String[] fileNames = new String[count]; |
|---|
| | 145 | char[][] fileNames = new char[][](count); |
|---|
| 128 | 146 | for (int i = 0; i < count; i++) { |
|---|
| 129 | 147 | // How long is the name ? |
|---|
| 130 | 148 | int size = OS.DragQueryFile(stgmedium.unionField, i, null, 0) + 1; |
|---|
| 131 | | TCHAR lpszFile = new TCHAR(0, size); |
|---|
| | 149 | TCHAR[] lpszFile = NewTCHARs(0, size); |
|---|
| 132 | 150 | // Get file name and append it to string |
|---|
| 133 | | OS.DragQueryFile(stgmedium.unionField, i, lpszFile, size); |
|---|
| 134 | | fileNames[i] = lpszFile.toString(0, lpszFile.strlen()); |
|---|
| | 151 | OS.DragQueryFile(stgmedium.unionField, i, lpszFile.ptr, size); |
|---|
| | 152 | fileNames[i] = TCHARzToStr(lpszFile.ptr); |
|---|
| 135 | 153 | } |
|---|
| 136 | 154 | OS.DragFinish(stgmedium.unionField); // frees data associated with HDROP data |
|---|
| 137 | | return fileNames; |
|---|
| | 155 | return new ArrayWrapperString2(fileNames); |
|---|
| 138 | 156 | } |
|---|
| 139 | 157 | |
|---|
| 140 | 158 | protected int[] getTypeIds(){ |
|---|
| 141 | | return new int[] {CF_HDROPID}; |
|---|
| | 159 | return [CF_HDROPID]; |
|---|
| 142 | 160 | } |
|---|
| 143 | 161 | |
|---|
| 144 | | protected String[] getTypeNames(){ |
|---|
| 145 | | return new String[] {CF_HDROP}; |
|---|
| | 162 | protected char[][] getTypeNames(){ |
|---|
| | 163 | return [CF_HDROP]; |
|---|
| 146 | 164 | } |
|---|
| 147 | 165 | bool checkFile(Object object) { |
|---|
| 148 | | if (object is null || !(object instanceof String[]) || ((String[])object).length is 0) return false; |
|---|
| 149 | | String[] strings = (String[])object; |
|---|
| | 166 | char[][] strings; |
|---|
| | 167 | if( auto strs = cast(ArrayWrapperString2)object ){ |
|---|
| | 168 | strings = strs.array; |
|---|
| | 169 | } |
|---|
| | 170 | if (!strings) return false; |
|---|
| 150 | 171 | for (int i = 0; i < strings.length; i++) { |
|---|
| 151 | | if (strings[i] is null || strings[i].length() is 0) return false; |
|---|
| | 172 | if (strings[i] is null || strings[i].length is 0) return false; |
|---|
| 152 | 173 | } |
|---|
| 153 | 174 | return true; |
|---|