Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/internal/Compatibility.d @ 120:536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
===D2===
* added [Try]Immutable/Const/Shared templates to work with differenses in D1/D2 instead of version statements
used these templates to work with strict type storage rules of dmd-2.053
* com.ibm.icu now also compilable with D2, but not tested yet
* small fixes
Snippet288 - shared data is in TLS
===Phobos===
* fixed critical bugs in Phobos implemention
completely incorrect segfault prone fromStringz (Linux's port ruthless killer)
terrible, incorrect StringBuffer realization (StyledText killer)
* fixed small bugs as well
Snippet72 - misprint in the snippet
* implemented missed functionality for Phobos
ByteArrayOutputStream implemented (image loading available)
formatting correctly works for all DWT's cases
As a result, folowing snippets now works with Phobos (Snippet### - what is fixed):
Snippet24, 42, 111, 115, 130, 235, 276 - bad string formatting
Snippet48, 282 - crash on image loading
Snippet163, 189, 211, 213, 217, 218, 222 - crash on copy/cut in StyledText
Snippet244 - hang-up
===Tango===
* few changes for the latest Tango trunc-r5661
* few small performance improvments
===General===
* implMissing-s for only one version changed to implMissingInTango/InPhobos
* incorrect calls to Format in toString-s fixed
* fixed loading \uXXXX characters in ResourceBundle
* added good UTF-8 support for StyledText, TextLayout (Win32) and friends
UTF functions revised and tested. It is now in java.nonstandard.*Utf modules
StyledText and TextLayout (Win32) modules revised for UTF-8 support
* removed small diferences in most identical files in *.swt.* folders
*.swt.internal.image, *.swt.events and *.swt.custom are identical in Win32/Linux32
now 179 of 576 (~31%) files in *.swt.* folders are fully identical
* Win32: snippets now have right subsystem, pretty icons and native system style controls
* small fixes in snippets
Snippet44 - it's not Snippet44
Snippet212 - functions work with different images and offsets arrays
Win32: Snippet282 - crash on close if the button has an image
Snippet293 - setGrayed is commented
and others
Win32: As a result, folowing snippets now works
Snippet68 - color doesn't change
Snippet163, 189, 211, 213, 217, 218, 222 - UTF-8 issues (see above)
Snippet193 - no tabel headers
author | Denis Shelomovskij <verylonglogin.reg@gmail.com> |
---|---|
date | Sat, 09 Jul 2011 15:50:20 +0300 |
parents | 70388b0e6dad |
children |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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 | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.internal.Compatibility; | |
14 | |
15 import java.lang.all; | |
16 import java.io.BufferedInputStream; | |
17 import java.io.File; | |
18 import java.io.InputStream; | |
19 import java.io.FileInputStream; | |
20 import java.io.FileOutputStream; | |
21 import java.text.MessageFormat; | |
22 import java.util.MissingResourceException; | |
23 import java.util.ResourceBundle; | |
24 import java.util.zip.InflaterInputStream; | |
25 //import java.util.zip.DeflaterOutputStream; | |
26 | |
27 import org.eclipse.swt.SWT; | |
28 | |
26 | 29 version(Tango){ |
25 | 30 import Unicode = tango.text.Unicode; |
31 import tango.sys.Process; | |
26 | 32 } else { // Phobos |
33 } | |
25 | 34 |
35 /** | |
36 * This class is a placeholder for utility methods commonly | |
37 * used on J2SE platforms but not supported on some J2ME | |
38 * profiles. | |
39 * <p> | |
40 * It is part of our effort to provide support for both J2SE | |
41 * and J2ME platforms. | |
42 * </p> | |
43 * <p> | |
44 * IMPORTANT: some of the methods have been modified from their | |
45 * J2SE parents. Refer to the description of each method for | |
46 * specific changes. | |
47 * </p> | |
48 * <ul> | |
49 * <li>Exceptions thrown may differ since J2ME's set of | |
50 * exceptions is a subset of J2SE's one. | |
51 * </li> | |
52 * <li>The range of the mathematic functions is subject to | |
53 * change. | |
54 * </li> | |
55 * </ul> | |
56 */ | |
57 public final class Compatibility { | |
58 | |
59 /** | |
60 * Returns the PI constant as a double. | |
61 */ | |
62 public static const real PI = Math.PI; | |
63 | |
64 static const real toRadians = PI / 180; | |
65 | |
66 /** | |
67 * Answers the length of the side adjacent to the given angle | |
68 * of a right triangle. In other words, it returns the integer | |
69 * conversion of length * cos (angle). | |
70 * <p> | |
71 * IMPORTANT: the j2me version has an additional restriction on | |
72 * the argument. length must be between -32767 and 32767 (inclusive). | |
73 * </p> | |
74 * | |
75 * @param angle the angle in degrees | |
76 * @param length the length of the triangle's hypotenuse | |
77 * @return the integer conversion of length * cos (angle) | |
78 */ | |
79 public static int cos(int angle, int length) { | |
80 return cast(int)(Math.cos(angle * toRadians) * length); | |
81 } | |
82 | |
83 /** | |
84 * Answers the length of the side opposite to the given angle | |
85 * of a right triangle. In other words, it returns the integer | |
86 * conversion of length * sin (angle). | |
87 * <p> | |
88 * IMPORTANT: the j2me version has an additional restriction on | |
89 * the argument. length must be between -32767 and 32767 (inclusive). | |
90 * </p> | |
91 * | |
92 * @param angle the angle in degrees | |
93 * @param length the length of the triangle's hypotenuse | |
94 * @return the integer conversion of length * sin (angle) | |
95 */ | |
96 public static int sin(int angle, int length) { | |
97 return cast(int)(Math.sin(angle * toRadians) * length); | |
98 } | |
99 | |
100 /** | |
101 * Answers the most negative (i.e. closest to negative infinity) | |
102 * integer value which is greater than the number obtained by dividing | |
103 * the first argument p by the second argument q. | |
104 * | |
105 * @param p numerator | |
106 * @param q denominator (must be different from zero) | |
107 * @return the ceiling of the rational number p / q. | |
108 */ | |
109 public static int ceil(int p, int q) { | |
110 return cast(int)Math.ceil(cast(float)p / q); | |
111 } | |
112 | |
113 /** | |
114 * Answers whether the indicated file exists or not. | |
115 * | |
116 * @param parent the file's parent directory | |
117 * @param child the file's name | |
118 * @return true if the file exists | |
119 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
120 public static bool fileExists(String parent, String child) { |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
121 scope f = new File(parent, child); |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
122 return f.exists(); |
25 | 123 } |
124 | |
125 /** | |
126 * Answers the most positive (i.e. closest to positive infinity) | |
127 * integer value which is less than the number obtained by dividing | |
128 * the first argument p by the second argument q. | |
129 * | |
130 * @param p numerator | |
131 * @param q denominator (must be different from zero) | |
132 * @return the floor of the rational number p / q. | |
133 */ | |
134 public static int floor(int p, int q) { | |
135 return cast(int)Math.floor(cast(double)p / q); | |
136 } | |
137 | |
138 /** | |
139 * Answers the result of rounding to the closest integer the number obtained | |
140 * by dividing the first argument p by the second argument q. | |
141 * <p> | |
142 * IMPORTANT: the j2me version has an additional restriction on | |
143 * the arguments. p must be within the range 0 - 32767 (inclusive). | |
144 * q must be within the range 1 - 32767 (inclusive). | |
145 * </p> | |
146 * | |
147 * @param p numerator | |
148 * @param q denominator (must be different from zero) | |
149 * @return the closest integer to the rational number p / q | |
150 */ | |
151 public static int round(int p, int q) { | |
152 return cast(int)Math.round(cast(float)p / q); | |
153 } | |
154 | |
155 /** | |
156 * Returns 2 raised to the power of the argument. | |
157 * | |
158 * @param n an int value between 0 and 30 (inclusive) | |
159 * @return 2 raised to the power of the argument | |
160 * | |
161 * @exception IllegalArgumentException <ul> | |
162 * <li>ERROR_INVALID_RANGE - if the argument is not between 0 and 30 (inclusive)</li> | |
163 * </ul> | |
164 */ | |
165 public static int pow2(int n) { | |
166 if (n >= 1 && n <= 30) | |
167 return 2 << (n - 1); | |
168 else if (n != 0) { | |
169 SWT.error(SWT.ERROR_INVALID_RANGE); | |
170 } | |
171 return 1; | |
172 } | |
173 | |
174 /** | |
175 * Create an DeflaterOutputStream if such things are supported. | |
176 * | |
177 * @param stream the output stream | |
178 * @return a deflater stream or <code>null</code> | |
179 * @exception IOException | |
180 * | |
181 * @since 3.4 | |
182 */ | |
183 public static OutputStream newDeflaterOutputStream(OutputStream stream) { | |
184 implMissing(__FILE__,__LINE__); | |
185 return null; | |
186 //DWT_TODO return new DeflaterOutputStream(stream); | |
187 } | |
188 | |
189 /** | |
190 * Open a file if such things are supported. | |
191 * | |
192 * @param filename the name of the file to open | |
193 * @return a stream on the file if it could be opened. | |
194 * @exception IOException | |
195 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
196 public static InputStream newFileInputStream(String filename) { |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
197 return new FileInputStream(filename); |
25 | 198 } |
199 | |
200 /** | |
201 * Open a file if such things are supported. | |
202 * | |
203 * @param filename the name of the file to open | |
204 * @return a stream on the file if it could be opened. | |
205 * @exception IOException | |
206 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
207 public static OutputStream newFileOutputStream(String filename) { |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
208 return new FileOutputStream(filename); |
25 | 209 } |
210 | |
211 /** | |
212 * Create an InflaterInputStream if such things are supported. | |
213 * | |
214 * @param stream the input stream | |
215 * @return a inflater stream or <code>null</code> | |
216 * @exception IOException | |
217 * | |
218 * @since 3.3 | |
219 */ | |
220 public static InflaterInputStream newInflaterInputStream(InputStream stream) { | |
221 return new InflaterInputStream(stream); | |
222 } | |
223 | |
224 /** | |
225 * Answers whether the character is a letter. | |
226 * | |
227 * @param c the character | |
228 * @return true when the character is a letter | |
229 */ | |
230 public static bool isLetter(dchar c) { | |
26 | 231 return Character.isLetter(c); |
25 | 232 } |
233 | |
234 /** | |
235 * Answers whether the character is a letter or a digit. | |
236 * | |
237 * @param c the character | |
238 * @return true when the character is a letter or a digit | |
239 */ | |
240 public static bool isLetterOrDigit(dchar c) { | |
26 | 241 return Character.isLetterOrDigit(c); |
25 | 242 } |
243 | |
244 /** | |
245 * Answers whether the character is a Unicode space character. | |
246 * | |
247 * @param c the character | |
248 * @return true when the character is a Unicode space character | |
249 */ | |
250 public static bool isSpaceChar(dchar c) { | |
26 | 251 return Character.isSpace(c); |
25 | 252 } |
253 | |
254 /** | |
255 * Answers whether the character is a whitespace character. | |
256 * | |
257 * @param c the character to test | |
258 * @return true if the character is whitespace | |
259 */ | |
260 public static bool isWhitespace(dchar c) { | |
26 | 261 return Character.isWhitespace(c); |
25 | 262 } |
263 | |
264 /** | |
265 * Execute a program in a separate platform process if the | |
266 * underlying platform support this. | |
267 * <p> | |
268 * The new process inherits the environment of the caller. | |
269 * </p> | |
270 * | |
271 * @param prog the name of the program to execute | |
272 * | |
273 * @exception ProcessException | |
274 * if the program cannot be executed | |
275 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
276 public static void exec(String prog) { |
26 | 277 version(Tango){ |
278 auto proc = new Process( prog ); | |
279 proc.execute; | |
280 } else { // Phobos | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
281 implMissingInPhobos(); |
26 | 282 } |
25 | 283 } |
284 | |
285 /** | |
286 * Execute progArray[0] in a separate platform process if the | |
287 * underlying platform support this. | |
288 * <p> | |
289 * The new process inherits the environment of the caller. | |
290 * <p> | |
291 * | |
292 * @param progArray array containing the program to execute and its arguments | |
293 * | |
294 * @exception ProcessException | |
295 * if the program cannot be executed | |
296 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
297 public static void exec(String[] progArray) { |
26 | 298 version(Tango){ |
299 auto proc = new Process( progArray ); | |
300 proc.execute; | |
301 } else { // Phobos | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
302 implMissingInPhobos(); |
26 | 303 } |
25 | 304 } |
305 | |
306 const ImportData[] SWTMessagesBundleData = [ | |
307 getImportData!( "org.eclipse.swt.internal.SWTMessages.properties" ), | |
308 getImportData!( "org.eclipse.swt.internal.SWTMessages_ar.properties" ), | |
309 getImportData!( "org.eclipse.swt.internal.SWTMessages_cs.properties" ), | |
310 getImportData!( "org.eclipse.swt.internal.SWTMessages_da.properties" ), | |
311 getImportData!( "org.eclipse.swt.internal.SWTMessages_de.properties" ), | |
312 getImportData!( "org.eclipse.swt.internal.SWTMessages_el.properties" ), | |
313 getImportData!( "org.eclipse.swt.internal.SWTMessages_es.properties" ), | |
314 getImportData!( "org.eclipse.swt.internal.SWTMessages_fi.properties" ), | |
315 getImportData!( "org.eclipse.swt.internal.SWTMessages_fr.properties" ), | |
316 getImportData!( "org.eclipse.swt.internal.SWTMessages_hu.properties" ), | |
317 getImportData!( "org.eclipse.swt.internal.SWTMessages_it.properties" ), | |
318 getImportData!( "org.eclipse.swt.internal.SWTMessages_iw.properties" ), | |
319 getImportData!( "org.eclipse.swt.internal.SWTMessages_ja.properties" ), | |
320 getImportData!( "org.eclipse.swt.internal.SWTMessages_ko.properties" ), | |
321 getImportData!( "org.eclipse.swt.internal.SWTMessages_nl.properties" ), | |
322 getImportData!( "org.eclipse.swt.internal.SWTMessages_no.properties" ), | |
323 getImportData!( "org.eclipse.swt.internal.SWTMessages_pl.properties" ), | |
324 getImportData!( "org.eclipse.swt.internal.SWTMessages_pt_BR.properties" ), | |
325 getImportData!( "org.eclipse.swt.internal.SWTMessages_pt.properties" ), | |
326 getImportData!( "org.eclipse.swt.internal.SWTMessages_ru.properties" ), | |
327 getImportData!( "org.eclipse.swt.internal.SWTMessages_sv.properties" ), | |
328 getImportData!( "org.eclipse.swt.internal.SWTMessages_tr.properties" ), | |
329 getImportData!( "org.eclipse.swt.internal.SWTMessages_zh_HK.properties" ), | |
330 getImportData!( "org.eclipse.swt.internal.SWTMessages_zh.properties" ), | |
331 getImportData!( "org.eclipse.swt.internal.SWTMessages_zh_TW.properties" ) | |
332 ]; | |
333 | |
334 private static ResourceBundle msgs = null; | |
335 | |
336 /** | |
337 * Returns the NLS'ed message for the given argument. This is only being | |
338 * called from SWT. | |
339 * | |
340 * @param key the key to look up | |
341 * @return the message for the given key | |
342 * | |
343 * @see SWT#getMessage(String) | |
344 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
345 public static String getMessage(String key) { |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
346 String answer = key; |
25 | 347 |
348 if (key is null) { | |
349 SWT.error (SWT.ERROR_NULL_ARGUMENT); | |
350 } | |
351 if (msgs is null) { | |
352 try { | |
353 msgs = ResourceBundle.getBundle(SWTMessagesBundleData); //$NON-NLS-1$ | |
354 } catch (MissingResourceException ex) { | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
355 answer = key ~ " (no resource bundle)"; //$NON-NLS-1$ |
25 | 356 } |
357 } | |
358 if (msgs !is null) { | |
359 try { | |
360 answer = msgs.getString(key); | |
361 } catch (MissingResourceException ex2) {} | |
362 } | |
363 return answer; | |
364 } | |
365 | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
366 public static String getMessage(String key, Object[] args) { |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
367 String answer = key; |
25 | 368 |
369 if (key is null || args is null) { | |
370 SWT.error (SWT.ERROR_NULL_ARGUMENT); | |
371 } | |
372 if (msgs is null) { | |
373 try { | |
374 msgs = ResourceBundle.getBundle(SWTMessagesBundleData); //$NON-NLS-1$ | |
375 } catch (MissingResourceException ex) { | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
376 answer = key ~ " (no resource bundle)"; //$NON-NLS-1$ |
25 | 377 } |
378 } | |
379 if (msgs !is null) { | |
380 try { | |
51 | 381 String frmt = msgs.getString(key); |
25 | 382 switch( args.length ){ |
383 case 0: answer = Format(frmt); break; | |
384 case 1: answer = Format(frmt, args[0]); break; | |
385 case 2: answer = Format(frmt, args[0], args[1]); break; | |
386 case 3: answer = Format(frmt, args[0], args[1], args[2]); break; | |
387 case 4: answer = Format(frmt, args[0], args[1], args[2], args[3]); break; | |
388 case 5: answer = Format(frmt, args[0], args[1], args[2], args[3], args[4]); break; | |
389 default: | |
390 implMissing(__FILE__, __LINE__ ); | |
391 } | |
392 } catch (MissingResourceException ex2) {} | |
393 } | |
394 return answer; | |
395 } | |
396 | |
397 | |
398 /** | |
399 * Interrupt the current thread. | |
400 * <p> | |
401 * Note that this is not available on CLDC. | |
402 * </p> | |
403 */ | |
404 public static void interrupt() { | |
405 //PORTING_FIXME: how to implement?? | |
406 //Thread.currentThread().interrupt(); | |
407 } | |
408 | |
409 /** | |
410 * Compares two instances of class String ignoring the case of the | |
411 * characters and answers if they are equal. | |
412 * | |
413 * @param s1 string | |
414 * @param s2 string | |
415 * @return true if the two instances of class String are equal | |
416 */ | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
417 public static bool equalsIgnoreCase(in char[] s1, in char[] s2) { |
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
54
diff
changeset
|
418 return .equalsIgnoreCase(s1, s2); |
25 | 419 } |
420 | |
421 } |