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.RTFTransfer;
14 
15 import org.eclipse.swt.internal.Converter;
16 import org.eclipse.swt.internal.gtk.OS;
17 import org.eclipse.swt.dnd.ByteArrayTransfer;
18 import org.eclipse.swt.dnd.TransferData;
19 import org.eclipse.swt.dnd.DND;
20 
21 import java.lang.all;
22 version(Tango){
23 static import tango.text.Util;
24 } else { // Phobos
25 }
26 
27 /**
28  * The class <code>RTFTransfer</code> provides a platform specific mechanism
29  * for converting text in RTF format represented as a java <code>String</code>
30  * to a platform specific representation of the data and vice versa.
31  *
32  * <p>An example of a java <code>String</code> containing RTF text is shown
33  * below:</p>
34  *
35  * <code><pre>
36  *     String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}";
37  * </code></pre>
38  *
39  * @see Transfer
40  */
41 public class RTFTransfer : ByteArrayTransfer {
42 
43     private static RTFTransfer _instance;
44     private static const String TEXT_RTF = "text/rtf"; //$NON-NLS-1$
45     private static const int TEXT_RTF_ID;
46     private static const String TEXT_RTF2 = "TEXT/RTF"; //$NON-NLS-1$
47     private static const int TEXT_RTF2_ID;
48     private static const String APPLICATION_RTF = "application/rtf"; //$NON-NLS-1$
49     private static const int APPLICATION_RTF_ID;
50 
51 static this(){
52     TEXT_RTF_ID = registerType(TEXT_RTF);
53     TEXT_RTF2_ID = registerType(TEXT_RTF2);
54     APPLICATION_RTF_ID = registerType(APPLICATION_RTF);
55     _instance = new RTFTransfer();
56 }
57 
58 private this() {}
59 
60 /**
61  * Returns the singleton instance of the RTFTransfer class.
62  *
63  * @return the singleton instance of the RTFTransfer class
64  */
65 public static RTFTransfer getInstance () {
66     return _instance;
67 }
68 
69 /**
70  * This implementation of <code>javaToNative</code> converts RTF-formatted text
71  * represented by a java <code>String</code> to a platform specific representation.
72  *
73  * @param object a java <code>String</code> containing RTF text
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 override void javaToNative (Object object, TransferData transferData){
80     transferData.result = 0;
81     if (!checkRTF(object) || !isSupportedType(transferData)) {
82         DND.error(DND.ERROR_INVALID_DATA);
83     }
84     String str = stringcast(object);
85     char* pValue = cast(char*)OS.g_malloc(str.length + 1);
86     if (pValue is null) return;
87     pValue[ 0 .. str.length ] = str;
88     pValue[ str.length ] = '\0';
89     transferData.length = cast(int)/*64bit*/str.length;
90     transferData.format = 8;
91     transferData.pValue = pValue;
92     transferData.result = 1;
93 }
94 
95 /**
96  * This implementation of <code>nativeToJava</code> converts a platform specific
97  * representation of RTF text to a java <code>String</code>.
98  *
99  * @param transferData the platform specific representation of the data to be converted
100  * @return a java <code>String</code> containing RTF text if the conversion was successful;
101  *      otherwise null
102  *
103  * @see Transfer#javaToNative
104  */
105 public override Object nativeToJava(TransferData transferData){
106     if ( !isSupportedType(transferData) ||  transferData.pValue is null ) return null;
107     auto size = transferData.format * transferData.length / 8;
108     if (size is 0) return null;
109     char [] chars = transferData.pValue[ 0 .. size];
110     auto end = chars.indexOf( '\0' );
111     return new ArrayWrapperString( (end is -1)? chars.dup : chars[ 0 .. end ].dup );
112 }
113 
114 protected override int[] getTypeIds() {
115     return [TEXT_RTF_ID, TEXT_RTF2_ID, APPLICATION_RTF_ID];
116 }
117 
118 protected override String[] getTypeNames() {
119     return [TEXT_RTF, TEXT_RTF2, APPLICATION_RTF];
120 }
121 
122 bool checkRTF(Object object) {
123     if( object is null ) return false;
124     ArrayWrapperString astr = cast(ArrayWrapperString)object;
125     if( astr is null ) return false;
126     return astr.array.length > 0;
127 }
128 
129 protected override bool validate(Object object) {
130     return checkRTF(object);
131 }
132 }