Mercurial > projects > dwt-linux
annotate dwt/dnd/RTFTransfer.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | 380bad9f6852 |
children | 6383fb4cdfc3 |
rev | line source |
---|---|
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
92 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
92 | 12 *******************************************************************************/ |
13 module dwt.dnd.RTFTransfer; | |
14 | |
15 import dwt.internal.Converter; | |
16 import dwt.internal.gtk.OS; | |
17 import dwt.dnd.ByteArrayTransfer; | |
18 import dwt.dnd.TransferData; | |
19 import dwt.dnd.DND; | |
20 | |
21 import dwt.dwthelper.utils; | |
22 static import tango.text.Util; | |
23 | |
24 /** | |
25 * The class <code>RTFTransfer</code> provides a platform specific mechanism | |
26 * for converting text in RTF format represented as a java <code>String</code> | |
259 | 27 * to a platform specific representation of the data and vice versa. |
92 | 28 * |
29 * <p>An example of a java <code>String</code> containing RTF text is shown | |
30 * below:</p> | |
31 * | |
32 * <code><pre> | |
33 * String rtfData = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i Hello World}"; | |
34 * </code></pre> | |
259 | 35 * |
36 * @see Transfer | |
92 | 37 */ |
38 public class RTFTransfer : ByteArrayTransfer { | |
39 | |
40 private static RTFTransfer _instance; | |
238 | 41 private static const String TEXT_RTF = "text/rtf"; //$NON-NLS-1$ |
92 | 42 private static const int TEXT_RTF_ID; |
238 | 43 private static const String TEXT_RTF2 = "TEXT/RTF"; //$NON-NLS-1$ |
92 | 44 private static const int TEXT_RTF2_ID; |
238 | 45 private static const String APPLICATION_RTF = "application/rtf"; //$NON-NLS-1$ |
92 | 46 private static const int APPLICATION_RTF_ID; |
47 | |
48 static this(){ | |
49 TEXT_RTF_ID = registerType(TEXT_RTF); | |
50 TEXT_RTF2_ID = registerType(TEXT_RTF2); | |
51 APPLICATION_RTF_ID = registerType(APPLICATION_RTF); | |
52 _instance = new RTFTransfer(); | |
53 } | |
54 | |
55 private this() {} | |
56 | |
57 /** | |
58 * Returns the singleton instance of the RTFTransfer class. | |
59 * | |
60 * @return the singleton instance of the RTFTransfer class | |
61 */ | |
62 public static RTFTransfer getInstance () { | |
63 return _instance; | |
64 } | |
65 | |
66 /** | |
67 * This implementation of <code>javaToNative</code> converts RTF-formatted text | |
68 * represented by a java <code>String</code> to a platform specific representation. | |
69 * | |
70 * @param object a java <code>String</code> containing RTF text | |
259 | 71 * @param transferData an empty <code>TransferData</code> object that will |
72 * be filled in on return with the platform specific format of the data | |
73 * | |
74 * @see Transfer#nativeToJava | |
92 | 75 */ |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
76 public override void javaToNative (Object object, TransferData transferData){ |
92 | 77 transferData.result = 0; |
78 if (!checkRTF(object) || !isSupportedType(transferData)) { | |
79 DND.error(DND.ERROR_INVALID_DATA); | |
80 } | |
238 | 81 String string = (cast(ArrayWrapperString)object).array; |
92 | 82 char* pValue = cast(char*)OS.g_malloc(string.length + 1); |
83 if (pValue is null) return; | |
84 pValue[ 0 .. string.length ] = string; | |
85 pValue[ string.length ] = '\0'; | |
86 transferData.length = string.length; | |
87 transferData.format = 8; | |
88 transferData.pValue = pValue; | |
89 transferData.result = 1; | |
90 } | |
91 | |
92 /** | |
93 * This implementation of <code>nativeToJava</code> converts a platform specific | |
94 * representation of RTF text to a java <code>String</code>. | |
95 * | |
259 | 96 * @param transferData the platform specific representation of the data to be converted |
97 * @return a java <code>String</code> containing RTF text if the conversion was successful; | |
98 * otherwise null | |
99 * | |
100 * @see Transfer#javaToNative | |
92 | 101 */ |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
102 public override Object nativeToJava(TransferData transferData){ |
92 | 103 if ( !isSupportedType(transferData) || transferData.pValue is null ) return null; |
104 int size = transferData.format * transferData.length / 8; | |
105 if (size is 0) return null; | |
106 byte[] buffer = new byte[size]; | |
107 char [] chars = transferData.pValue[ 0 .. size]; | |
108 return new ArrayWrapperString( chars[ 0 .. tango.text.Util.locate( chars, '\0' ) ] ); | |
109 } | |
110 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
111 protected override int[] getTypeIds() { |
92 | 112 return [TEXT_RTF_ID, TEXT_RTF2_ID, APPLICATION_RTF_ID]; |
113 } | |
114 | |
238 | 115 protected override String[] getTypeNames() { |
92 | 116 return [TEXT_RTF, TEXT_RTF2, APPLICATION_RTF]; |
117 } | |
118 | |
119 bool checkRTF(Object object) { | |
120 if( object is null ) return false; | |
121 ArrayWrapperString astr = cast(ArrayWrapperString)object; | |
122 if( astr is null ) return false; | |
123 return astr.array.length > 0; | |
124 } | |
125 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
126 protected override bool validate(Object object) { |
92 | 127 return checkRTF(object); |
128 } | |
129 } |