1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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 org.eclipse.swt.dnd.HTMLTransfer;
14 
15 import org.eclipse.swt.internal.gtk.OS;
16 import org.eclipse.swt.dnd.ByteArrayTransfer;
17 import org.eclipse.swt.dnd.TransferData;
18 import org.eclipse.swt.dnd.DND;
19 import java.lang.all;
20 
21 version(Tango){
22 static import tango.text.Util;
23 } else { // Phobos
24 }
25 
26 /**
27  * The class <code>HTMLTransfer</code> provides a platform specific mechanism
28  * for converting text in HTML format represented as a java <code>String</code>
29  * to a platform specific representation of the data and vice versa.
30  *
31  * <p>An example of a java <code>String</code> containing HTML text is shown
32  * below:</p>
33  *
34  * <code><pre>
35  *     String htmlData = "<p>This is a paragraph of text.</p>";
36  * </code></pre>
37  *
38  * @see Transfer
39  */
40 public class HTMLTransfer : ByteArrayTransfer {
41 
42     private static HTMLTransfer _instance;
43     private static const String TEXT_HTML = "text/html"; //$NON-NLS-1$
44     private static const int TEXT_HTML_ID;
45     private static const String TEXT_HTML2 = "TEXT/HTML"; //$NON-NLS-1$
46     private static const int TEXT_HTML2_ID;
47 
48 static this(){
49     _instance = new HTMLTransfer();
50     TEXT_HTML_ID = registerType(TEXT_HTML);
51     TEXT_HTML2_ID = registerType(TEXT_HTML2);
52 }
53 
54 private this() {}
55 
56 /**
57  * Returns the singleton instance of the HTMLTransfer class.
58  *
59  * @return the singleton instance of the HTMLTransfer class
60  */
61 public static HTMLTransfer getInstance () {
62     return _instance;
63 }
64 
65 /**
66  * This implementation of <code>javaToNative</code> converts HTML-formatted text
67  * represented by a java <code>String</code> to a platform specific representation.
68  *
69  * @param object a java <code>String</code> containing HTML text
70  * @param transferData an empty <code>TransferData</code> object that will
71  *      be filled in on return with the platform specific format of the data
72  * 
73  * @see Transfer#nativeToJava
74  */
75 public override void javaToNative (Object object, TransferData transferData){
76     transferData.result = 0;
77     if (!checkHTML(object) || !isSupportedType(transferData)) {
78         DND.error(DND.ERROR_INVALID_DATA);
79     }
80     String str = stringcast(object);
81     char* pValue = cast(char*)OS.g_malloc(str.length);
82     if (pValue is null) return;
83     pValue[0 .. str.length ] = str;
84     transferData.length = cast(int)/*64bit*/str.length;
85     transferData.format = 8;
86     transferData.pValue = pValue;
87     transferData.result = 1;
88 }
89 
90 /**
91  * This implementation of <code>nativeToJava</code> converts a platform specific
92  * representation of HTML text to a java <code>String</code>.
93  *
94  * @param transferData the platform specific representation of the data to be converted
95  * @return a java <code>String</code> containing HTML text if the conversion was successful;
96  *      otherwise null
97  * 
98  * @see Transfer#javaToNative
99  */
100 public override Object nativeToJava(TransferData transferData){
101     if ( !isSupportedType(transferData) ||  transferData.pValue is null ) return null;
102     /* Ensure byteCount is a multiple of 2 bytes */
103     auto size = (transferData.format * transferData.length / 8) / 2 * 2;
104     if (size <= 0) return null;
105     String chars = transferData.pValue[ 0 .. size ]._idup();
106     auto end = chars.indexOf('\0');
107     return new ArrayWrapperString( (end is -1 )? chars : chars[ 0 .. end ] );
108 }
109 protected override int[] getTypeIds() {
110     return [TEXT_HTML_ID, TEXT_HTML2_ID];
111 }
112 
113 protected override String[] getTypeNames() {
114     return [TEXT_HTML, TEXT_HTML2];
115 }
116 
117 bool checkHTML(Object object) {
118     if( object is null ) return false;
119     auto arr = cast(ArrayWrapperString)object;
120     if( arr is null ) return false;
121     if( arr.array.length is 0 ) return false;
122     return true;
123 }
124 
125 protected override bool validate(Object object) {
126     return checkHTML(object);
127 }
128 }