Mercurial > projects > dwt-linux
annotate dwt/dnd/HTMLTransfer.d @ 238:380bad9f6852
reverted char[] to String
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:42:55 +0200 |
parents | 17f8449522fd |
children | c0d810de7093 |
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.HTMLTransfer; | |
14 | |
15 import dwt.internal.gtk.OS; | |
16 import dwt.dnd.ByteArrayTransfer; | |
17 import dwt.dnd.TransferData; | |
18 import dwt.dnd.DND; | |
19 import dwt.dwthelper.utils; | |
20 | |
21 static import tango.text.Util; | |
22 | |
23 /** | |
24 * The class <code>HTMLTransfer</code> provides a platform specific mechanism | |
25 * for converting text in HTML format represented as a java <code>String</code> | |
26 * to a platform specific representation of the data and vice versa. See | |
27 * <code>Transfer</code> for additional information. | |
28 * | |
29 * <p>An example of a java <code>String</code> containing HTML text is shown | |
30 * below:</p> | |
31 * | |
32 * <code><pre> | |
33 * String htmlData = "<p>This is a paragraph of text.</p>"; | |
34 * </code></pre> | |
35 */ | |
36 public class HTMLTransfer : ByteArrayTransfer { | |
37 | |
38 private static HTMLTransfer _instance; | |
238 | 39 private static const String TEXT_HTML = "text/html"; //$NON-NLS-1$ |
92 | 40 private static const int TEXT_HTML_ID; |
238 | 41 private static const String TEXT_HTML2 = "TEXT/HTML"; //$NON-NLS-1$ |
92 | 42 private static const int TEXT_HTML2_ID; |
43 | |
44 static this(){ | |
45 _instance = new HTMLTransfer(); | |
46 TEXT_HTML_ID = registerType(TEXT_HTML); | |
47 TEXT_HTML2_ID = registerType(TEXT_HTML2); | |
48 } | |
49 | |
50 private this() {} | |
51 | |
52 /** | |
53 * Returns the singleton instance of the HTMLTransfer class. | |
54 * | |
55 * @return the singleton instance of the HTMLTransfer class | |
56 */ | |
57 public static HTMLTransfer getInstance () { | |
58 return _instance; | |
59 } | |
60 | |
61 /** | |
62 * This implementation of <code>javaToNative</code> converts HTML-formatted text | |
63 * represented by a java <code>String</code> to a platform specific representation. | |
64 * For additional information see <code>Transfer#javaToNative</code>. | |
65 * | |
66 * @param object a java <code>String</code> containing HTML text | |
67 * @param transferData an empty <code>TransferData</code> object; this | |
68 * object will be filled in on return with the platform specific format of the data | |
69 */ | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
70 public override void javaToNative (Object object, TransferData transferData){ |
92 | 71 transferData.result = 0; |
72 if (!checkHTML(object) || !isSupportedType(transferData)) { | |
73 DND.error(DND.ERROR_INVALID_DATA); | |
74 } | |
238 | 75 String string = (cast(ArrayWrapperString)object).array; |
92 | 76 char* pValue = cast(char*)OS.g_malloc(string.length); |
77 if (pValue is null) return; | |
78 pValue[0 .. string.length ] = string; | |
79 transferData.length = string.length; | |
80 transferData.format = 8; | |
81 transferData.pValue = pValue; | |
82 transferData.result = 1; | |
83 } | |
84 | |
85 /** | |
86 * This implementation of <code>nativeToJava</code> converts a platform specific | |
87 * representation of HTML text to a java <code>String</code>. | |
88 * For additional information see <code>Transfer#nativeToJava</code>. | |
89 * | |
90 * @param transferData the platform specific representation of the data to be | |
91 * been converted | |
92 * @return a java <code>String</code> containing HTML text if the | |
93 * conversion was successful; otherwise null | |
94 */ | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
95 public override Object nativeToJava(TransferData transferData){ |
92 | 96 if ( !isSupportedType(transferData) || transferData.pValue is null ) return null; |
97 /* Ensure byteCount is a multiple of 2 bytes */ | |
98 int size = (transferData.format * transferData.length / 8) / 2 * 2; | |
99 if (size <= 0) return null; | |
238 | 100 String chars = transferData.pValue[ 0 .. size ].dup; |
92 | 101 return new ArrayWrapperString( chars[ 0 .. tango.text.Util.locate( chars, '\0' ) ] ); |
102 } | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
103 protected override int[] getTypeIds() { |
92 | 104 return [TEXT_HTML_ID, TEXT_HTML2_ID]; |
105 } | |
106 | |
238 | 107 protected override String[] getTypeNames() { |
92 | 108 return [TEXT_HTML, TEXT_HTML2]; |
109 } | |
110 | |
111 bool checkHTML(Object object) { | |
112 if( object is null ) return false; | |
113 auto arr = cast(ArrayWrapperString)object; | |
114 if( arr is null ) return false; | |
115 if( arr.array.length is 0 ) return false; | |
116 return true; | |
117 } | |
118 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
119 protected override bool validate(Object object) { |
92 | 120 return checkHTML(object); |
121 } | |
122 } |