Mercurial > projects > dwt-linux
annotate dwt/dnd/RTFTransfer.d @ 270:58ef057b3e9d
merge
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 15:37:36 +0200 |
parents | 6383fb4cdfc3 0389eeb717f8 |
children |
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 | |
267 | 73 * |
259 | 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 | |
267 | 99 * |
259 | 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 char [] chars = transferData.pValue[ 0 .. size]; | |
258
0389eeb717f8
fix: corrupted string content in DnD RTF
Frank Benoit <benoit@tionex.de>
parents:
238
diff
changeset
|
107 return new ArrayWrapperString( chars[ 0 .. tango.text.Util.locate( chars, '\0' ) ].dup ); |
92 | 108 } |
109 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
110 protected override int[] getTypeIds() { |
92 | 111 return [TEXT_RTF_ID, TEXT_RTF2_ID, APPLICATION_RTF_ID]; |
112 } | |
113 | |
238 | 114 protected override String[] getTypeNames() { |
92 | 115 return [TEXT_RTF, TEXT_RTF2, APPLICATION_RTF]; |
116 } | |
117 | |
118 bool checkRTF(Object object) { | |
119 if( object is null ) return false; | |
120 ArrayWrapperString astr = cast(ArrayWrapperString)object; | |
121 if( astr is null ) return false; | |
122 return astr.array.length > 0; | |
123 } | |
124 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
125 protected override bool validate(Object object) { |
92 | 126 return checkRTF(object); |
127 } | |
128 } |