root/dwt/dnd/URLTransfer.d

Revision 320:da968414c383, 5.5 kB (checked in by Frank Benoit <benoit@tionex.de>, 1 month ago)

Merge changes SWT 3.4.1

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 20007 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.URLTransfer;
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  * The class <code>URLTransfer</code> provides a platform specific mechanism
29  * for converting text in URL format represented as a java <code>String</code>
30  * to a platform specific representation of the data and vice versa. The string
31  * must contain a fully specified url.
32  *
33  * <p>An example of a java <code>String</code> containing a URL is shown below:</p>
34  *
35  * <code><pre>
36  *     String url = "http://www.eclipse.org";
37  * </code></pre>
38  *
39  * @see Transfer
40  */
41 public class URLTransfer : ByteArrayTransfer {
42
43     static URLTransfer _instance;
44     static const String CFSTR_INETURL = "UniformResourceLocator"; //$NON-NLS-1$
45     static const int CFSTR_INETURLID;
46
47 static this(){
48     CFSTR_INETURLID = registerType(CFSTR_INETURL);
49 }
50
51 private this() {}
52
53 /**
54  * Returns the singleton instance of the URLTransfer class.
55  *
56  * @return the singleton instance of the URLTransfer class
57  */
58 public static URLTransfer getInstance () {
59     if( _instance is null ){
60         synchronized {
61             if( _instance is null ){
62                 _instance = new URLTransfer();
63             }
64         }
65     }
66     return _instance;
67 }
68
69 /**
70  * This implementation of <code>javaToNative</code> converts a URL
71  * represented by a java <code>String</code> to a platform specific representation.
72  *
73  * @param object a java <code>String</code> containing a URL
74  * @param transferData an empty <code>TransferData</code> object that will
75  *      be filled in on return with the platform specific format of the data
76  *
77  * @see Transfer#nativeToJava
78  */
79 public void javaToNative (Object object, TransferData transferData){
80     if (!checkURL(object) || !isSupportedType(transferData)) {
81         DND.error(DND.ERROR_INVALID_DATA);
82     }
83     transferData.result = COM.E_FAIL;
84     // URL is stored as a null terminated byte array
85     String url = (cast(ArrayWrapperString)object).array;
86     int codePage = OS.GetACP();
87     wchar[] chars = StrToWCHARs(codePage, url, true );
88     int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars.ptr, -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(CHAR*)OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte);
95     OS.WideCharToMultiByte(codePage, 0, chars.ptr, -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
106  * specific representation of a URL 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 a URL 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 [CFSTR_INETURLID];
145 }
146
147 protected String[] getTypeNames(){
148     return [CFSTR_INETURL];
149 }
150
151 bool checkURL(Object object) {
152     return object !is null && (null !is cast(ArrayWrapperString)object) && (cast(ArrayWrapperString)object).array.length > 0;
153
154 }
155
156 protected bool validate(Object object) {
157     return checkURL(object);
158 }
159 }
Note: See TracBrowser for help on using the browser.