# HG changeset patch # User Frank Benoit # Date 1202912584 -3600 # Node ID b479f7e2f431c32b4353535f41da7f75f0552d02 # Parent 3665cb9211b24cc729f13fc40d091effaf5b64ab FileTransfer diff -r 3665cb9211b2 -r b479f7e2f431 dwt/dnd/FileTransfer.d --- a/dwt/dnd/FileTransfer.d Wed Feb 13 14:52:31 2008 +0100 +++ b/dwt/dnd/FileTransfer.d Wed Feb 13 15:23:04 2008 +0100 @@ -13,8 +13,16 @@ module dwt.dnd.FileTransfer; import dwt.internal.ole.win32.COM; +import dwt.internal.ole.win32.OBJIDL; import dwt.internal.win32.OS; +import dwt.dnd.ByteArrayTransfer; +import dwt.dnd.TransferData; +import dwt.dnd.DND; + +import dwt.dwthelper.utils; +static import tango.text.Text; +alias tango.text.Text.Text!(char) StringBuffer; /** * The class FileTransfer provides a platform specific mechanism @@ -37,10 +45,10 @@ */ public class FileTransfer : ByteArrayTransfer { - private static FileTransfer _instance = new FileTransfer(); - private static final String CF_HDROP = "CF_HDROP "; //$NON-NLS-1$ + private static FileTransfer _instance; + private static final char[] CF_HDROP = "CF_HDROP "; //$NON-NLS-1$ private static final int CF_HDROPID = COM.CF_HDROP; - private static final String CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$ + private static final char[] CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$ private this() {} @@ -50,6 +58,13 @@ * @return the singleton instance of the FileTransfer class */ public static FileTransfer getInstance () { + if( _instance is null ){ + synchronized { + if( _instance is null ){ + _instance = new FileTransfer(); + } + } + } return _instance; } @@ -69,28 +84,31 @@ if (!checkFile(object) || !isSupportedType(transferData)) { DND.error(DND.ERROR_INVALID_DATA); } - String[] fileNames = (String[]) object; + char[][] fileNames; + if( auto strs = cast(ArrayWrapperString2) object ){ + fileNames = strs.array; + } StringBuffer allFiles = new StringBuffer(); for (int i = 0; i < fileNames.length; i++) { allFiles.append(fileNames[i]); allFiles.append(CF_HDROP_SEPARATOR); // each name is null terminated } - TCHAR buffer = new TCHAR(0, allFiles.toString(), true); // there is an extra null terminator at the very end - DROPFILES dropfiles = new DROPFILES(); + TCHAR[] buffer = StrToTCHARs(0, allFiles.toString(), true); // there is an extra null terminator at the very end + DROPFILES dropfiles; dropfiles.pFiles = DROPFILES.sizeof; - dropfiles.pt_x = dropfiles.pt_y = 0; + dropfiles.pt.x = dropfiles.pt.y = 0; dropfiles.fNC = 0; dropfiles.fWide = OS.IsUnicode ? 1 : 0; // Allocate the memory because the caller (DropTarget) has not handed it in // The caller of this method must release the data when it is done with it. - int byteCount = buffer.length() * TCHAR.sizeof; - int newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, DROPFILES.sizeof + byteCount); - OS.MoveMemory(newPtr, dropfiles, DROPFILES.sizeof); - OS.MoveMemory(newPtr + DROPFILES.sizeof, buffer, byteCount); + int byteCount = buffer.length * TCHAR.sizeof; + auto newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, DROPFILES.sizeof + byteCount); + OS.MoveMemory(newPtr, &dropfiles, DROPFILES.sizeof); + OS.MoveMemory(newPtr + DROPFILES.sizeof, buffer.ptr, byteCount); transferData.stgmedium = new STGMEDIUM(); transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; transferData.stgmedium.unionField = newPtr; - transferData.stgmedium.pUnkForRelease = 0; + transferData.stgmedium.pUnkForRelease = null; transferData.result = COM.S_OK; } @@ -106,49 +124,52 @@ * conversion was successful; otherwise null */ public Object nativeToJava(TransferData transferData) { - if (!isSupportedType(transferData) || transferData.pIDataObject is 0) return null; + if (!isSupportedType(transferData) || transferData.pIDataObject is null) return null; // get file names from IDataObject - IDataObject dataObject = new IDataObject(transferData.pIDataObject); + IDataObject dataObject = transferData.pIDataObject; dataObject.AddRef(); - FORMATETC formatetc = new FORMATETC(); + FORMATETC* formatetc = new FORMATETC(); formatetc.cfFormat = COM.CF_HDROP; - formatetc.ptd = 0; + formatetc.ptd = null; formatetc.dwAspect = COM.DVASPECT_CONTENT; formatetc.lindex = -1; formatetc.tymed = COM.TYMED_HGLOBAL; - STGMEDIUM stgmedium = new STGMEDIUM(); + STGMEDIUM* stgmedium = new STGMEDIUM(); stgmedium.tymed = COM.TYMED_HGLOBAL; transferData.result = dataObject.GetData(formatetc, stgmedium); dataObject.Release(); if (transferData.result !is COM.S_OK) return null; // How many files are there? int count = OS.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0); - String[] fileNames = new String[count]; + char[][] fileNames = new char[][](count); for (int i = 0; i < count; i++) { // How long is the name ? int size = OS.DragQueryFile(stgmedium.unionField, i, null, 0) + 1; - TCHAR lpszFile = new TCHAR(0, size); + TCHAR[] lpszFile = NewTCHARs(0, size); // Get file name and append it to string - OS.DragQueryFile(stgmedium.unionField, i, lpszFile, size); - fileNames[i] = lpszFile.toString(0, lpszFile.strlen()); + OS.DragQueryFile(stgmedium.unionField, i, lpszFile.ptr, size); + fileNames[i] = TCHARzToStr(lpszFile.ptr); } OS.DragFinish(stgmedium.unionField); // frees data associated with HDROP data - return fileNames; + return new ArrayWrapperString2(fileNames); } protected int[] getTypeIds(){ - return new int[] {CF_HDROPID}; + return [CF_HDROPID]; } -protected String[] getTypeNames(){ - return new String[] {CF_HDROP}; +protected char[][] getTypeNames(){ + return [CF_HDROP]; } bool checkFile(Object object) { - if (object is null || !(object instanceof String[]) || ((String[])object).length is 0) return false; - String[] strings = (String[])object; + char[][] strings; + if( auto strs = cast(ArrayWrapperString2)object ){ + strings = strs.array; + } + if (!strings) return false; for (int i = 0; i < strings.length; i++) { - if (strings[i] is null || strings[i].length() is 0) return false; + if (strings[i] is null || strings[i].length is 0) return false; } return true; }