Mercurial > projects > dwt-mac
comparison dwt/dnd/TextTransfer.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | e76aa0b07480 |
children | 2fb76eb49f04 |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 *******************************************************************************/ | 10 *******************************************************************************/ |
11 module dwt.dnd; | 11 module dwt.dnd.TextTransfer; |
12 | 12 |
13 import dwt.internal.carbon.CFRange; | 13 import dwt.dwthelper.utils; |
14 import dwt.internal.carbon.OS; | 14 |
15 import dwt.internal.cocoa.NSString; | |
16 import dwt.internal.cocoa.OS; | |
15 | 17 |
16 /** | 18 /** |
17 * The class <code>TextTransfer</code> provides a platform specific mechanism | 19 * The class <code>TextTransfer</code> provides a platform specific mechanism |
18 * for converting plain text represented as a java <code>String</code> | 20 * for converting plain text represented as a java <code>String</code> |
19 * to a platform specific representation of the data and vice versa. | 21 * to a platform specific representation of the data and vice versa. |
25 * String textData = "Hello World"; | 27 * String textData = "Hello World"; |
26 * </code></pre> | 28 * </code></pre> |
27 * | 29 * |
28 * @see Transfer | 30 * @see Transfer |
29 */ | 31 */ |
30 public class TextTransfer : ByteArrayTransfer { | 32 public class TextTransfer extends ByteArrayTransfer { |
31 | 33 |
32 static TextTransfer _instance = new TextTransfer(); | 34 static TextTransfer _instance = new TextTransfer(); |
33 static final String TEXT = "TEXT"; //$NON-NLS-1$ | 35 |
34 static final String UTEXT = "utxt"; //$NON-NLS-1$ | 36 static final String ID_NAME = OS.NSStringPboardType.getString(); |
35 static final int TEXTID = OS.kScrapFlavorTypeText; | 37 static final int ID = registerType(ID_NAME); |
36 static final int UTEXTID = OS.kScrapFlavorTypeUnicode; | |
37 | 38 |
38 this() {} | 39 TextTransfer() {} |
39 | 40 |
40 /** | 41 /** |
41 * Returns the singleton instance of the TextTransfer class. | 42 * Returns the singleton instance of the TextTransfer class. |
42 * | 43 * |
43 * @return the singleton instance of the TextTransfer class | 44 * @return the singleton instance of the TextTransfer class |
49 /** | 50 /** |
50 * This implementation of <code>javaToNative</code> converts plain text | 51 * This implementation of <code>javaToNative</code> converts plain text |
51 * represented by a java <code>String</code> to a platform specific representation. | 52 * represented by a java <code>String</code> to a platform specific representation. |
52 * | 53 * |
53 * @param object a java <code>String</code> containing text | 54 * @param object a java <code>String</code> containing text |
54 * @param transferData an empty <code>TransferData</code> object; this object | 55 * @param transferData an empty <code>TransferData</code> object that will |
55 * will be filled in on return with the platform specific format of the data | 56 * be filled in on return with the platform specific format of the data |
56 * | 57 * |
57 * @see Transfer#javaToNative | 58 * @see Transfer#nativeToJava |
58 */ | 59 */ |
59 public void javaToNative (Object object, TransferData transferData) { | 60 public void javaToNative (Object object, TransferData transferData) { |
60 if (!checkText(object) || !isSupportedType(transferData)) { | 61 if (!checkText(object) || !isSupportedType(transferData)) { |
61 DND.error(DND.ERROR_INVALID_DATA); | 62 DND.error(DND.ERROR_INVALID_DATA); |
62 } | 63 } |
63 String String = cast(String)object; | 64 transferData.data = NSString.stringWith((String) object); |
64 char[] chars = new char[String.length()]; | |
65 String.getChars (0, chars.length, chars, 0); | |
66 transferData.result = -1; | |
67 switch (transferData.type) { | |
68 case TEXTID: { | |
69 int cfString = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, chars.length); | |
70 if (cfString is 0) return; | |
71 byte[] buffer = null; | |
72 try { | |
73 CFRange range = new CFRange(); | |
74 range.length = chars.length; | |
75 int encoding = OS.CFStringGetSystemEncoding(); | |
76 int[] size = new int[1]; | |
77 int numChars = OS.CFStringGetBytes(cfString, range, encoding, cast(byte)'?', true, null, 0, size); | |
78 if (numChars is 0) return; | |
79 buffer = new byte[size[0]]; | |
80 numChars = OS.CFStringGetBytes(cfString, range, encoding, cast(byte)'?', true, buffer, size [0], size); | |
81 if (numChars is 0) return; | |
82 } finally { | |
83 OS.CFRelease(cfString); | |
84 } | |
85 transferData.data = new byte[1][]; | |
86 transferData.data[0] = buffer; | |
87 transferData.result = OS.noErr; | |
88 break; | |
89 } | |
90 case UTEXTID: { | |
91 byte[] buffer = new byte[chars.length * 2]; | |
92 OS.memmove(buffer, chars, buffer.length); | |
93 transferData.data = new byte[1][]; | |
94 transferData.data[0] = buffer; | |
95 transferData.result = OS.noErr; | |
96 break; | |
97 } | |
98 } | |
99 } | 65 } |
100 | 66 |
101 /** | 67 /** |
102 * This implementation of <code>nativeToJava</code> converts a platform specific | 68 * This implementation of <code>nativeToJava</code> converts a platform specific |
103 * representation of plain text to a java <code>String</code>. | 69 * representation of plain text to a java <code>String</code>. |
104 * | 70 * |
105 * @param transferData the platform specific representation of the data to be converted | 71 * @param transferData the platform specific representation of the data to be converted |
106 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null | 72 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null |
107 * | 73 * |
108 * @see Transfer#nativeToJava | 74 * @see Transfer#javaToNative |
109 */ | 75 */ |
110 public Object nativeToJava(TransferData transferData){ | 76 public Object nativeToJava(TransferData transferData){ |
111 if (!isSupportedType(transferData) || transferData.data is null) return null; | 77 if (!isSupportedType(transferData) || transferData.data is null) return null; |
112 if (transferData.data.length is 0 || transferData.data[0].length is 0) return null; | 78 NSString string = (NSString) transferData.data; |
113 byte[] buffer = transferData.data[0]; | 79 return string.getString(); |
114 switch (transferData.type) { | |
115 case TEXTID: { | |
116 int encoding = OS.CFStringGetSystemEncoding(); | |
117 int cfString = OS.CFStringCreateWithBytes(OS.kCFAllocatorDefault, buffer, buffer.length, encoding, true); | |
118 if (cfString is 0) return null; | |
119 try { | |
120 int length = OS.CFStringGetLength(cfString); | |
121 if (length is 0) return null; | |
122 char[] chars = new char[length]; | |
123 CFRange range = new CFRange(); | |
124 range.length = length; | |
125 OS.CFStringGetCharacters(cfString, range, chars); | |
126 return new String(chars); | |
127 } finally { | |
128 OS.CFRelease(cfString); | |
129 } | |
130 } | |
131 case UTEXTID: { | |
132 char[] chars = new char[(buffer.length + 1) / 2]; | |
133 OS.memmove(chars, buffer, buffer.length); | |
134 return new String(chars); | |
135 } | |
136 } | |
137 return null; | |
138 } | 80 } |
139 | 81 |
140 protected int[] getTypeIds() { | 82 protected int[] getTypeIds() { |
141 return new int[] {UTEXTID, TEXTID}; | 83 return new int[] {ID}; |
142 } | 84 } |
143 | 85 |
144 protected String[] getTypeNames() { | 86 protected String[] getTypeNames() { |
145 return new String[] {UTEXT, TEXT}; | 87 return new String[] {ID_NAME}; |
146 } | 88 } |
147 | 89 |
148 bool checkText(Object object) { | 90 bool checkText(Object object) { |
149 return (object !is null && null !is cast(String)object && (cast(String)object).length() > 0); | 91 return (object !is null && object instanceof String && ((String)object).length() > 0); |
150 } | 92 } |
151 protected bool validate(Object object) { | 93 protected bool validate(Object object) { |
152 return checkText(object); | 94 return checkText(object); |
153 } | 95 } |
154 } | 96 } |