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