Mercurial > projects > dwt-win
comparison dwt/dnd/FileTransfer.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 | b479f7e2f431 |
comparison
equal
deleted
inserted
replaced
134:fa7d7d66b9ed | 135:242e33c0e383 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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.FileTransfer; | |
14 | |
15 import dwt.internal.ole.win32.COM; | |
16 import dwt.internal.win32.OS; | |
17 | |
18 | |
19 /** | |
20 * The class <code>FileTransfer</code> provides a platform specific mechanism | |
21 * for converting a list of files represented as a java <code>String[]</code> to a | |
22 * platform specific representation of the data and vice versa. | |
23 * Each <code>String</code> in the array contains the absolute path for a single | |
24 * file or directory. | |
25 * See <code>Transfer</code> for additional information. | |
26 * | |
27 * <p>An example of a java <code>String[]</code> containing a list of files is shown | |
28 * below:</p> | |
29 * | |
30 * <code><pre> | |
31 * File file1 = new File("C:\temp\file1"); | |
32 * File file2 = new File("C:\temp\file2"); | |
33 * String[] fileData = new String[2]; | |
34 * fileData[0] = file1.getAbsolutePath(); | |
35 * fileData[1] = file2.getAbsolutePath(); | |
36 * </code></pre> | |
37 */ | |
38 public class FileTransfer : ByteArrayTransfer { | |
39 | |
40 private static FileTransfer _instance = new FileTransfer(); | |
41 private static final String CF_HDROP = "CF_HDROP "; //$NON-NLS-1$ | |
42 private static final int CF_HDROPID = COM.CF_HDROP; | |
43 private static final String CF_HDROP_SEPARATOR = "\0"; //$NON-NLS-1$ | |
44 | |
45 private this() {} | |
46 | |
47 /** | |
48 * Returns the singleton instance of the FileTransfer class. | |
49 * | |
50 * @return the singleton instance of the FileTransfer class | |
51 */ | |
52 public static FileTransfer getInstance () { | |
53 return _instance; | |
54 } | |
55 | |
56 /** | |
57 * This implementation of <code>javaToNative</code> converts a list of file names | |
58 * represented by a java <code>String[]</code> to a platform specific representation. | |
59 * Each <code>String</code> in the array contains the absolute path for a single | |
60 * file or directory. For additional information see | |
61 * <code>Transfer#javaToNative</code>. | |
62 * | |
63 * @param object a java <code>String[]</code> containing the file names to be | |
64 * converted | |
65 * @param transferData an empty <code>TransferData</code> object; this | |
66 * object will be filled in on return with the platform specific format of the data | |
67 */ | |
68 public void javaToNative(Object object, TransferData transferData) { | |
69 if (!checkFile(object) || !isSupportedType(transferData)) { | |
70 DND.error(DND.ERROR_INVALID_DATA); | |
71 } | |
72 String[] fileNames = (String[]) object; | |
73 StringBuffer allFiles = new StringBuffer(); | |
74 for (int i = 0; i < fileNames.length; i++) { | |
75 allFiles.append(fileNames[i]); | |
76 allFiles.append(CF_HDROP_SEPARATOR); // each name is null terminated | |
77 } | |
78 TCHAR buffer = new TCHAR(0, allFiles.toString(), true); // there is an extra null terminator at the very end | |
79 DROPFILES dropfiles = new DROPFILES(); | |
80 dropfiles.pFiles = DROPFILES.sizeof; | |
81 dropfiles.pt_x = dropfiles.pt_y = 0; | |
82 dropfiles.fNC = 0; | |
83 dropfiles.fWide = OS.IsUnicode ? 1 : 0; | |
84 // Allocate the memory because the caller (DropTarget) has not handed it in | |
85 // 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); | |
90 transferData.stgmedium = new STGMEDIUM(); | |
91 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; | |
92 transferData.stgmedium.unionField = newPtr; | |
93 transferData.stgmedium.pUnkForRelease = 0; | |
94 transferData.result = COM.S_OK; | |
95 } | |
96 | |
97 /** | |
98 * This implementation of <code>nativeToJava</code> converts a platform specific | |
99 * representation of a list of file names to a java <code>String[]</code>. | |
100 * Each String in the array contains the absolute path for a single file or directory. | |
101 * For additional information see <code>Transfer#nativeToJava</code>. | |
102 * | |
103 * @param transferData the platform specific representation of the data to be | |
104 * been converted | |
105 * @return a java <code>String[]</code> containing a list of file names if the | |
106 * conversion was successful; otherwise null | |
107 */ | |
108 public Object nativeToJava(TransferData transferData) { | |
109 if (!isSupportedType(transferData) || transferData.pIDataObject is 0) return null; | |
110 | |
111 // get file names from IDataObject | |
112 IDataObject dataObject = new IDataObject(transferData.pIDataObject); | |
113 dataObject.AddRef(); | |
114 FORMATETC formatetc = new FORMATETC(); | |
115 formatetc.cfFormat = COM.CF_HDROP; | |
116 formatetc.ptd = 0; | |
117 formatetc.dwAspect = COM.DVASPECT_CONTENT; | |
118 formatetc.lindex = -1; | |
119 formatetc.tymed = COM.TYMED_HGLOBAL; | |
120 STGMEDIUM stgmedium = new STGMEDIUM(); | |
121 stgmedium.tymed = COM.TYMED_HGLOBAL; | |
122 transferData.result = dataObject.GetData(formatetc, stgmedium); | |
123 dataObject.Release(); | |
124 if (transferData.result !is COM.S_OK) return null; | |
125 // How many files are there? | |
126 int count = OS.DragQueryFile(stgmedium.unionField, 0xFFFFFFFF, null, 0); | |
127 String[] fileNames = new String[count]; | |
128 for (int i = 0; i < count; i++) { | |
129 // How long is the name ? | |
130 int size = OS.DragQueryFile(stgmedium.unionField, i, null, 0) + 1; | |
131 TCHAR lpszFile = new TCHAR(0, size); | |
132 // Get file name and append it to string | |
133 OS.DragQueryFile(stgmedium.unionField, i, lpszFile, size); | |
134 fileNames[i] = lpszFile.toString(0, lpszFile.strlen()); | |
135 } | |
136 OS.DragFinish(stgmedium.unionField); // frees data associated with HDROP data | |
137 return fileNames; | |
138 } | |
139 | |
140 protected int[] getTypeIds(){ | |
141 return new int[] {CF_HDROPID}; | |
142 } | |
143 | |
144 protected String[] getTypeNames(){ | |
145 return new String[] {CF_HDROP}; | |
146 } | |
147 bool checkFile(Object object) { | |
148 if (object is null || !(object instanceof String[]) || ((String[])object).length is 0) return false; | |
149 String[] strings = (String[])object; | |
150 for (int i = 0; i < strings.length; i++) { | |
151 if (strings[i] is null || strings[i].length() is 0) return false; | |
152 } | |
153 return true; | |
154 } | |
155 | |
156 protected bool validate(Object object) { | |
157 return checkFile(object); | |
158 } | |
159 } |