Mercurial > projects > dwt-linux
annotate dwt/dnd/TextTransfer.d @ 255:5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 15 Jun 2008 22:32:20 +0200 |
parents | 380bad9f6852 |
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, 2006 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.TextTransfer; | |
14 | |
15 import dwt.internal.Converter; | |
16 import dwt.internal.gtk.OS; | |
17 import dwt.dwthelper.utils; | |
18 import dwt.dnd.ByteArrayTransfer; | |
19 import dwt.dnd.TransferData; | |
20 import dwt.dnd.DND; | |
21 | |
22 | |
23 /** | |
24 * The class <code>TextTransfer</code> provides a platform specific mechanism | |
25 * for converting plain text represented as a java <code>String</code> | |
26 * to a platform specific representation of the data and vice versa. | |
27 * | |
28 * <p>An example of a java <code>String</code> containing plain text is shown | |
29 * below:</p> | |
30 * | |
31 * <code><pre> | |
32 * String textData = "Hello World"; | |
33 * </code></pre> | |
34 * | |
35 * @see Transfer | |
36 */ | |
37 public class TextTransfer : ByteArrayTransfer { | |
38 | |
39 private static TextTransfer _instance; | |
238 | 40 private static const String COMPOUND_TEXT = "COMPOUND_TEXT"; //$NON-NLS-1$ |
41 private static const String UTF8_STRING = "UTF8_STRING"; //$NON-NLS-1$ | |
42 private static const String STRING = "STRING"; //$NON-NLS-1$ | |
92 | 43 private static const int COMPOUND_TEXT_ID; |
44 private static const int UTF8_STRING_ID; | |
45 private static const int STRING_ID; | |
46 | |
47 static this(){ | |
48 COMPOUND_TEXT_ID = registerType(COMPOUND_TEXT); | |
49 UTF8_STRING_ID = registerType(UTF8_STRING); | |
50 STRING_ID = registerType(STRING); | |
51 _instance = new TextTransfer(); | |
52 } | |
53 | |
54 private this() {} | |
55 | |
56 /** | |
57 * Returns the singleton instance of the TextTransfer class. | |
58 * | |
59 * @return the singleton instance of the TextTransfer class | |
60 */ | |
61 public static TextTransfer getInstance () { | |
62 return _instance; | |
63 } | |
64 | |
65 /** | |
66 * This implementation of <code>javaToNative</code> converts plain text | |
67 * represented by a java <code>String</code> to a platform specific representation. | |
68 * | |
69 * @param object a java <code>String</code> containing text | |
70 * @param transferData an empty <code>TransferData</code> object; this object | |
71 * will be filled in on return with the platform specific format of the data | |
72 * | |
73 * @see Transfer#javaToNative | |
74 */ | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
75 override public void javaToNative (Object object, TransferData transferData) { |
92 | 76 transferData.result = 0; |
77 if (!checkText(object) || !isSupportedType(transferData)) { | |
78 DND.error(DND.ERROR_INVALID_DATA); | |
79 } | |
238 | 80 String string = (cast(ArrayWrapperString)object).array; |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
238
diff
changeset
|
81 char* utf8 = toStringz(string); |
92 | 82 if (transferData.type is cast(void*) COMPOUND_TEXT_ID) { |
83 void* encoding; | |
84 int format; | |
85 char* ctext; | |
86 int length; | |
87 bool result = cast(bool) OS.gdk_utf8_to_compound_text(utf8, &encoding, &format, &ctext, &length); | |
88 if (!result) return; | |
89 transferData.type = encoding; | |
90 transferData.format = format; | |
91 transferData.length = length; | |
92 transferData.pValue = ctext; | |
93 transferData.result = 1; | |
94 } | |
95 if (transferData.type is cast(void*)UTF8_STRING_ID) { | |
96 char* pValue = cast(char*)OS.g_malloc(string.length+1); | |
97 if (pValue is null) return; | |
98 pValue[ 0 .. string.length ] = string; | |
99 pValue[ string.length ] = '\0'; | |
100 transferData.type = cast(void*)UTF8_STRING_ID; | |
101 transferData.format = 8; | |
102 transferData.length = string.length; | |
103 transferData.pValue = pValue; | |
104 transferData.result = 1; | |
105 } | |
106 if (transferData.type is cast(void*)STRING_ID) { | |
107 auto string_target = OS.gdk_utf8_to_string_target(utf8); | |
108 if (string_target is null) return; | |
109 transferData.type = cast(void*)STRING_ID; | |
110 transferData.format = 8; | |
111 transferData.length = tango.stdc.string.strlen(string_target); | |
112 transferData.pValue = string_target; | |
113 transferData.result = 1; | |
114 } | |
115 } | |
116 | |
117 /** | |
118 * This implementation of <code>nativeToJava</code> converts a platform specific | |
119 * representation of plain text to a java <code>String</code>. | |
120 * | |
121 * @param transferData the platform specific representation of the data to be converted | |
122 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null | |
123 * | |
124 * @see Transfer#nativeToJava | |
125 */ | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
126 override public Object nativeToJava(TransferData transferData){ |
92 | 127 if (!isSupportedType(transferData) || transferData.pValue is null) return null; |
128 char** list; | |
129 int count = OS.gdk_text_property_to_utf8_list(transferData.type, transferData.format, transferData.pValue, transferData.length, &list); | |
130 if (count is 0) return null; | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
238
diff
changeset
|
131 String utf8 = fromStringz( list[0] ).dup; |
92 | 132 OS.g_strfreev(list); |
133 return new ArrayWrapperString( utf8 ); | |
134 } | |
135 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
136 override protected int[] getTypeIds() { |
92 | 137 return [UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID]; |
138 } | |
139 | |
238 | 140 override protected String[] getTypeNames() { |
92 | 141 return [UTF8_STRING, COMPOUND_TEXT, STRING]; |
142 } | |
143 | |
144 bool checkText(Object object) { | |
145 if( object is null ) return false; | |
146 ArrayWrapperString astr = cast(ArrayWrapperString)object; | |
147 if( astr is null ) return false; | |
148 return astr.array.length > 0; | |
149 } | |
150 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
151 protected override bool validate(Object object) { |
92 | 152 return checkText(object); |
153 } | |
154 } |