14
|
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 *******************************************************************************/
|
|
11 module dwt.graphics.Device;
|
|
12
|
|
13 //PORTING_TYPE
|
|
14 class Device{
|
|
15 }
|
|
16 /+++
|
|
17
|
|
18 import dwt.DWT;
|
|
19 import dwt.DWTException;
|
|
20 import dwt.internal.Callback;
|
|
21 import dwt.internal.gdip.Gdip;
|
|
22 import dwt.internal.gdip.GdiplusStartupInput;
|
|
23 import dwt.internal.win32.LOGFONT;
|
|
24 import dwt.internal.win32.LOGFONTA;
|
|
25 import dwt.internal.win32.LOGFONTW;
|
|
26 import dwt.internal.win32.OS;
|
|
27 import dwt.internal.win32.TCHAR;
|
|
28 import dwt.internal.win32.TEXTMETRIC;
|
|
29 import dwt.internal.win32.TEXTMETRICA;
|
|
30 import dwt.internal.win32.TEXTMETRICW;
|
|
31
|
|
32 /**
|
|
33 * This class is the abstract superclass of all device objects,
|
|
34 * such as the Display device and the Printer device. Devices
|
|
35 * can have a graphics context (GC) created for them, and they
|
|
36 * can be drawn on by sending messages to the associated GC.
|
|
37 */
|
|
38 public abstract class Device implements Drawable {
|
|
39
|
|
40 /* Debugging */
|
|
41 public static bool DEBUG;
|
|
42 bool debug = DEBUG;
|
|
43 bool tracking = DEBUG;
|
|
44 Error [] errors;
|
|
45 Object [] objects;
|
|
46
|
|
47 /**
|
|
48 * Palette
|
|
49 * (Warning: This field is platform dependent)
|
|
50 * <p>
|
|
51 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
|
|
52 * public API. It is marked public only so that it can be shared
|
|
53 * within the packages provided by DWT. It is not available on all
|
|
54 * platforms and should never be accessed from application code.
|
|
55 * </p>
|
|
56 */
|
|
57 public int hPalette = 0;
|
|
58 int [] colorRefCount;
|
|
59
|
|
60 /* System Font */
|
|
61 int systemFont;
|
|
62
|
|
63 /* Font Enumeration */
|
|
64 int nFonts = 256;
|
|
65 LOGFONT [] logFonts;
|
|
66 TEXTMETRIC metrics;
|
|
67 int[] pixels;
|
|
68
|
|
69 /* Scripts */
|
|
70 int [] scripts;
|
|
71
|
|
72 /* Advanced Graphics */
|
|
73 int [] gdipToken;
|
|
74
|
|
75 bool disposed;
|
|
76
|
|
77 final static Object CREATE_LOCK = new Object();
|
|
78
|
|
79 /*
|
|
80 * TEMPORARY CODE. When a graphics object is
|
|
81 * created and the device parameter is null,
|
|
82 * the current Display is used. This presents
|
|
83 * a problem because DWT graphics does not
|
|
84 * reference classes in DWT widgets. The correct
|
|
85 * fix is to remove this feature. Unfortunately,
|
|
86 * too many application programs rely on this
|
|
87 * feature.
|
|
88 *
|
|
89 * This code will be removed in the future.
|
|
90 */
|
|
91 protected static Device CurrentDevice;
|
|
92 protected static Runnable DeviceFinder;
|
|
93 static {
|
|
94 try {
|
|
95 Class.forName ("org.eclipse.swt.widgets.Display"); //$NON-NLS-1$
|
|
96 } catch (Throwable e) {}
|
|
97 }
|
|
98
|
|
99 /*
|
|
100 * TEMPORARY CODE.
|
|
101 */
|
|
102 static synchronized Device getDevice () {
|
|
103 if (DeviceFinder !is null) DeviceFinder.run();
|
|
104 Device device = CurrentDevice;
|
|
105 CurrentDevice = null;
|
|
106 return device;
|
|
107 }
|
|
108
|
|
109 /**
|
|
110 * Constructs a new instance of this class.
|
|
111 * <p>
|
|
112 * You must dispose the device when it is no longer required.
|
|
113 * </p>
|
|
114 *
|
|
115 * @see #create
|
|
116 * @see #init
|
|
117 *
|
|
118 * @since 3.1
|
|
119 */
|
|
120 public Device() {
|
|
121 this(null);
|
|
122 }
|
|
123
|
|
124 /**
|
|
125 * Constructs a new instance of this class.
|
|
126 * <p>
|
|
127 * You must dispose the device when it is no longer required.
|
|
128 * </p>
|
|
129 *
|
|
130 * @param data the DeviceData which describes the receiver
|
|
131 *
|
|
132 * @see #create
|
|
133 * @see #init
|
|
134 * @see DeviceData
|
|
135 */
|
|
136 public Device(DeviceData data) {
|
|
137 synchronized (CREATE_LOCK) {
|
|
138 if (data !is null) {
|
|
139 debug = data.debug;
|
|
140 tracking = data.tracking;
|
|
141 }
|
|
142 create (data);
|
|
143 init ();
|
|
144 if (tracking) {
|
|
145 errors = new Error [128];
|
|
146 objects = new Object [128];
|
|
147 }
|
|
148
|
|
149 /* Initialize the system font slot */
|
|
150 systemFont = getSystemFont().handle;
|
|
151 }
|
|
152 }
|
|
153
|
|
154 /**
|
|
155 * Throws an <code>DWTException</code> if the receiver can not
|
|
156 * be accessed by the caller. This may include both checks on
|
|
157 * the state of the receiver and more generally on the entire
|
|
158 * execution context. This method <em>should</em> be called by
|
|
159 * device implementors to enforce the standard DWT invariants.
|
|
160 * <p>
|
|
161 * Currently, it is an error to invoke any method (other than
|
|
162 * <code>isDisposed()</code> and <code>dispose()</code>) on a
|
|
163 * device that has had its <code>dispose()</code> method called.
|
|
164 * </p><p>
|
|
165 * In future releases of DWT, there may be more or fewer error
|
|
166 * checks and exceptions may be thrown for different reasons.
|
|
167 * <p>
|
|
168 *
|
|
169 * @exception DWTException <ul>
|
|
170 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
171 * </ul>
|
|
172 */
|
|
173 protected void checkDevice () {
|
|
174 if (disposed) DWT.error(DWT.ERROR_DEVICE_DISPOSED);
|
|
175 }
|
|
176
|
|
177 void checkGDIP() {
|
|
178 if (gdipToken !is null) return;
|
|
179 if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED);
|
|
180 int oldErrorMode = OS.SetErrorMode (OS.SEM_FAILCRITICALERRORS);
|
|
181 try {
|
|
182 int [] token = new int [1];
|
|
183 GdiplusStartupInput input = new GdiplusStartupInput ();
|
|
184 input.GdiplusVersion = 1;
|
|
185 if (Gdip.GdiplusStartup (token, input, 0) is 0) {
|
|
186 gdipToken = token;
|
|
187 }
|
|
188 } catch (Throwable t) {
|
|
189 DWT.error (DWT.ERROR_NO_GRAPHICS_LIBRARY, t, " [GDI+ is required]"); //$NON-NLS-1$
|
|
190 } finally {
|
|
191 OS.SetErrorMode (oldErrorMode);
|
|
192 }
|
|
193 }
|
|
194
|
|
195 /**
|
|
196 * Creates the device in the operating system. If the device
|
|
197 * does not have a handle, this method may do nothing depending
|
|
198 * on the device.
|
|
199 * <p>
|
|
200 * This method is called before <code>init</code>.
|
|
201 * </p><p>
|
|
202 * Subclasses are supposed to reimplement this method and not
|
|
203 * call the <code>super</code> implementation.
|
|
204 * </p>
|
|
205 *
|
|
206 * @param data the DeviceData which describes the receiver
|
|
207 *
|
|
208 * @see #init
|
|
209 */
|
|
210 protected void create (DeviceData data) {
|
|
211 }
|
|
212
|
|
213 int computePixels(float height) {
|
|
214 int hDC = internal_new_GC (null);
|
|
215 int pixels = -(int)(0.5f + (height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72f));
|
|
216 internal_dispose_GC (hDC, null);
|
|
217 return pixels;
|
|
218 }
|
|
219
|
|
220 float computePoints(LOGFONT logFont, int hFont) {
|
|
221 int hDC = internal_new_GC (null);
|
|
222 int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
|
|
223 int pixels = 0;
|
|
224 if (logFont.lfHeight > 0) {
|
|
225 /*
|
|
226 * Feature in Windows. If the lfHeight of the LOGFONT structure
|
|
227 * is positive, the lfHeight measures the height of the entire
|
|
228 * cell, including internal leading, in logical units. Since the
|
|
229 * height of a font in points does not include the internal leading,
|
|
230 * we must subtract the internal leading, which requires a TEXTMETRIC.
|
|
231 */
|
|
232 int oldFont = OS.SelectObject(hDC, hFont);
|
|
233 TEXTMETRIC lptm = OS.IsUnicode ? (TEXTMETRIC)new TEXTMETRICW() : new TEXTMETRICA();
|
|
234 OS.GetTextMetrics(hDC, lptm);
|
|
235 OS.SelectObject(hDC, oldFont);
|
|
236 pixels = logFont.lfHeight - lptm.tmInternalLeading;
|
|
237 } else {
|
|
238 pixels = -logFont.lfHeight;
|
|
239 }
|
|
240 internal_dispose_GC (hDC, null);
|
|
241 return pixels * 72f / logPixelsY;
|
|
242 }
|
|
243
|
|
244 /**
|
|
245 * Destroys the device in the operating system and releases
|
|
246 * the device's handle. If the device does not have a handle,
|
|
247 * this method may do nothing depending on the device.
|
|
248 * <p>
|
|
249 * This method is called after <code>release</code>.
|
|
250 * </p><p>
|
|
251 * Subclasses are supposed to reimplement this method and not
|
|
252 * call the <code>super</code> implementation.
|
|
253 * </p>
|
|
254 *
|
|
255 * @see #dispose
|
|
256 * @see #release
|
|
257 */
|
|
258 protected void destroy () {
|
|
259 }
|
|
260
|
|
261 /**
|
|
262 * Disposes of the operating system resources associated with
|
|
263 * the receiver. After this method has been invoked, the receiver
|
|
264 * will answer <code>true</code> when sent the message
|
|
265 * <code>isDisposed()</code>.
|
|
266 *
|
|
267 * @see #release
|
|
268 * @see #destroy
|
|
269 * @see #checkDevice
|
|
270 */
|
|
271 public void dispose () {
|
|
272 if (isDisposed()) return;
|
|
273 checkDevice ();
|
|
274 release ();
|
|
275 destroy ();
|
|
276 disposed = true;
|
|
277 if (tracking) {
|
|
278 objects = null;
|
|
279 errors = null;
|
|
280 }
|
|
281 }
|
|
282
|
|
283 void dispose_Object (Object object) {
|
|
284 for (int i=0; i<objects.length; i++) {
|
|
285 if (objects [i] is object) {
|
|
286 objects [i] = null;
|
|
287 errors [i] = null;
|
|
288 return;
|
|
289 }
|
|
290 }
|
|
291 }
|
|
292
|
|
293 int EnumFontFamProc (int lpelfe, int lpntme, int FontType, int lParam) {
|
|
294 bool isScalable = (FontType & OS.RASTER_FONTTYPE) is 0;
|
|
295 bool scalable = lParam is 1;
|
|
296 if (isScalable is scalable) {
|
|
297 /* Add the log font to the list of log fonts */
|
|
298 if (nFonts is logFonts.length) {
|
|
299 LOGFONT [] newLogFonts = new LOGFONT [logFonts.length + 128];
|
|
300 System.arraycopy (logFonts, 0, newLogFonts, 0, nFonts);
|
|
301 logFonts = newLogFonts;
|
|
302 int[] newPixels = new int[newLogFonts.length];
|
|
303 System.arraycopy (pixels, 0, newPixels, 0, nFonts);
|
|
304 pixels = newPixels;
|
|
305 }
|
|
306 LOGFONT logFont = logFonts [nFonts];
|
|
307 if (logFont is null) logFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW () : new LOGFONTA ();
|
|
308 OS.MoveMemory (logFont, lpelfe, LOGFONT.sizeof);
|
|
309 logFonts [nFonts] = logFont;
|
|
310 if (logFont.lfHeight > 0) {
|
|
311 /*
|
|
312 * Feature in Windows. If the lfHeight of the LOGFONT structure
|
|
313 * is positive, the lfHeight measures the height of the entire
|
|
314 * cell, including internal leading, in logical units. Since the
|
|
315 * height of a font in points does not include the internal leading,
|
|
316 * we must subtract the internal leading, which requires a TEXTMETRIC,
|
|
317 * which in turn requires font creation.
|
|
318 */
|
|
319 OS.MoveMemory(metrics, lpntme, TEXTMETRIC.sizeof);
|
|
320 pixels[nFonts] = logFont.lfHeight - metrics.tmInternalLeading;
|
|
321 } else {
|
|
322 pixels[nFonts] = -logFont.lfHeight;
|
|
323 }
|
|
324 nFonts++;
|
|
325 }
|
|
326 return 1;
|
|
327 }
|
|
328
|
|
329 /**
|
|
330 * Returns a rectangle describing the receiver's size and location.
|
|
331 *
|
|
332 * @return the bounding rectangle
|
|
333 *
|
|
334 * @exception DWTException <ul>
|
|
335 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
336 * </ul>
|
|
337 */
|
|
338 public Rectangle getBounds () {
|
|
339 checkDevice ();
|
|
340 int hDC = internal_new_GC (null);
|
|
341 int width = OS.GetDeviceCaps (hDC, OS.HORZRES);
|
|
342 int height = OS.GetDeviceCaps (hDC, OS.VERTRES);
|
|
343 internal_dispose_GC (hDC, null);
|
|
344 return new Rectangle (0, 0, width, height);
|
|
345 }
|
|
346
|
|
347 /**
|
|
348 * Returns a <code>DeviceData</code> based on the receiver.
|
|
349 * Modifications made to this <code>DeviceData</code> will not
|
|
350 * affect the receiver.
|
|
351 *
|
|
352 * @return a <code>DeviceData</code> containing the device's data and attributes
|
|
353 *
|
|
354 * @exception DWTException <ul>
|
|
355 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
356 * </ul>
|
|
357 *
|
|
358 * @see DeviceData
|
|
359 */
|
|
360 public DeviceData getDeviceData () {
|
|
361 checkDevice();
|
|
362 DeviceData data = new DeviceData ();
|
|
363 data.debug = debug;
|
|
364 data.tracking = tracking;
|
|
365 int count = 0, length = 0;
|
|
366 if (tracking) length = objects.length;
|
|
367 for (int i=0; i<length; i++) {
|
|
368 if (objects [i] !is null) count++;
|
|
369 }
|
|
370 int index = 0;
|
|
371 data.objects = new Object [count];
|
|
372 data.errors = new Error [count];
|
|
373 for (int i=0; i<length; i++) {
|
|
374 if (objects [i] !is null) {
|
|
375 data.objects [index] = objects [i];
|
|
376 data.errors [index] = errors [i];
|
|
377 index++;
|
|
378 }
|
|
379 }
|
|
380 return data;
|
|
381 }
|
|
382
|
|
383 /**
|
|
384 * Returns a rectangle which describes the area of the
|
|
385 * receiver which is capable of displaying data.
|
|
386 *
|
|
387 * @return the client area
|
|
388 *
|
|
389 * @exception DWTException <ul>
|
|
390 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
391 * </ul>
|
|
392 *
|
|
393 * @see #getBounds
|
|
394 */
|
|
395 public Rectangle getClientArea () {
|
|
396 return getBounds ();
|
|
397 }
|
|
398
|
|
399 /**
|
|
400 * Returns the bit depth of the screen, which is the number of
|
|
401 * bits it takes to represent the number of unique colors that
|
|
402 * the screen is currently capable of displaying. This number
|
|
403 * will typically be one of 1, 8, 15, 16, 24 or 32.
|
|
404 *
|
|
405 * @return the depth of the screen
|
|
406 *
|
|
407 * @exception DWTException <ul>
|
|
408 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
409 * </ul>
|
|
410 */
|
|
411 public int getDepth () {
|
|
412 checkDevice ();
|
|
413 int hDC = internal_new_GC (null);
|
|
414 int bits = OS.GetDeviceCaps (hDC, OS.BITSPIXEL);
|
|
415 int planes = OS.GetDeviceCaps (hDC, OS.PLANES);
|
|
416 internal_dispose_GC (hDC, null);
|
|
417 return bits * planes;
|
|
418 }
|
|
419
|
|
420 /**
|
|
421 * Returns a point whose x coordinate is the horizontal
|
|
422 * dots per inch of the display, and whose y coordinate
|
|
423 * is the vertical dots per inch of the display.
|
|
424 *
|
|
425 * @return the horizontal and vertical DPI
|
|
426 *
|
|
427 * @exception DWTException <ul>
|
|
428 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
429 * </ul>
|
|
430 */
|
|
431 public Point getDPI () {
|
|
432 checkDevice ();
|
|
433 int hDC = internal_new_GC (null);
|
|
434 int dpiX = OS.GetDeviceCaps (hDC, OS.LOGPIXELSX);
|
|
435 int dpiY = OS.GetDeviceCaps (hDC, OS.LOGPIXELSY);
|
|
436 internal_dispose_GC (hDC, null);
|
|
437 return new Point (dpiX, dpiY);
|
|
438 }
|
|
439
|
|
440 /**
|
|
441 * Returns <code>FontData</code> objects which describe
|
|
442 * the fonts that match the given arguments. If the
|
|
443 * <code>faceName</code> is null, all fonts will be returned.
|
|
444 *
|
|
445 * @param faceName the name of the font to look for, or null
|
|
446 * @param scalable if true only scalable fonts are returned, otherwise only non-scalable fonts are returned.
|
|
447 * @return the matching font data
|
|
448 *
|
|
449 * @exception DWTException <ul>
|
|
450 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
451 * </ul>
|
|
452 */
|
|
453 public FontData [] getFontList (String faceName, bool scalable) {
|
|
454 checkDevice ();
|
|
455
|
|
456 /* Create the callback */
|
|
457 Callback callback = new Callback (this, "EnumFontFamProc", 4); //$NON-NLS-1$
|
|
458 int lpEnumFontFamProc = callback.getAddress ();
|
|
459 if (lpEnumFontFamProc is 0) DWT.error (DWT.ERROR_NO_MORE_CALLBACKS);
|
|
460
|
|
461 /* Initialize the instance variables */
|
|
462 metrics = OS.IsUnicode ? (TEXTMETRIC)new TEXTMETRICW() : new TEXTMETRICA();
|
|
463 pixels = new int[nFonts];
|
|
464 logFonts = new LOGFONT [nFonts];
|
|
465 for (int i=0; i<logFonts.length; i++) {
|
|
466 logFonts [i] = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA ();
|
|
467 }
|
|
468 nFonts = 0;
|
|
469
|
|
470 /* Enumerate */
|
|
471 int offset = 0;
|
|
472 int hDC = internal_new_GC (null);
|
|
473 if (faceName is null) {
|
|
474 /* The user did not specify a face name, so they want all versions of all available face names */
|
|
475 OS.EnumFontFamilies (hDC, null, lpEnumFontFamProc, scalable ? 1 : 0);
|
|
476
|
|
477 /**
|
|
478 * For bitmapped fonts, EnumFontFamilies only enumerates once for each font, regardless
|
|
479 * of how many styles are available. If the user wants bitmapped fonts, enumerate on
|
|
480 * each face name now.
|
|
481 */
|
|
482 offset = nFonts;
|
|
483 for (int i=0; i<offset; i++) {
|
|
484 LOGFONT lf = logFonts [i];
|
|
485 /**
|
|
486 * Bug in Windows 98. When EnumFontFamiliesEx is called with a specified face name, it
|
|
487 * should enumerate for each available style of that font. Instead, it only enumerates
|
|
488 * once. The fix is to call EnumFontFamilies, which works as expected.
|
|
489 */
|
|
490 if (OS.IsUnicode) {
|
|
491 OS.EnumFontFamiliesW (hDC, ((LOGFONTW)lf).lfFaceName, lpEnumFontFamProc, scalable ? 1 : 0);
|
|
492 } else {
|
|
493 OS.EnumFontFamiliesA (hDC, ((LOGFONTA)lf).lfFaceName, lpEnumFontFamProc, scalable ? 1 : 0);
|
|
494 }
|
|
495 }
|
|
496 } else {
|
|
497 /* Use the character encoding for the default locale */
|
|
498 TCHAR lpFaceName = new TCHAR (0, faceName, true);
|
|
499 /**
|
|
500 * Bug in Windows 98. When EnumFontFamiliesEx is called with a specified face name, it
|
|
501 * should enumerate for each available style of that font. Instead, it only enumerates
|
|
502 * once. The fix is to call EnumFontFamilies, which works as expected.
|
|
503 */
|
|
504 OS.EnumFontFamilies (hDC, lpFaceName, lpEnumFontFamProc, scalable ? 1 : 0);
|
|
505 }
|
|
506 int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
|
|
507 internal_dispose_GC (hDC, null);
|
|
508
|
|
509 /* Create the fontData from the logfonts */
|
|
510 int count = 0;
|
|
511 FontData [] result = new FontData [nFonts - offset];
|
|
512 for (int i=offset; i<nFonts; i++) {
|
|
513 FontData fd = FontData.win32_new (logFonts [i], pixels [i] * 72f / logPixelsY);
|
|
514 int j;
|
|
515 for (j = 0; j < count; j++) {
|
|
516 if (fd.equals (result [j])) break;
|
|
517 }
|
|
518 if (j is count) result [count++] = fd;
|
|
519 }
|
|
520 if (count !is result.length) {
|
|
521 FontData [] newResult = new FontData [count];
|
|
522 System.arraycopy (result, 0, newResult, 0, count);
|
|
523 result = newResult;
|
|
524 }
|
|
525
|
|
526 /* Clean up */
|
|
527 callback.dispose ();
|
|
528 logFonts = null;
|
|
529 pixels = null;
|
|
530 metrics = null;
|
|
531 return result;
|
|
532 }
|
|
533
|
|
534 String getLastError () {
|
|
535 int error = OS.GetLastError();
|
|
536 if (error is 0) return ""; //$NON-NLS-1$
|
|
537 return " [GetLastError=0x" + Integer.toHexString(error) + "]"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
538 }
|
|
539
|
|
540 String getLastErrorText () {
|
|
541 int error = OS.GetLastError();
|
|
542 if (error is 0) return ""; //$NON-NLS-1$
|
|
543 int[] buffer = new int[1];
|
|
544 int dwFlags = OS.FORMAT_MESSAGE_ALLOCATE_BUFFER | OS.FORMAT_MESSAGE_FROM_SYSTEM | OS.FORMAT_MESSAGE_IGNORE_INSERTS;
|
|
545 int length = OS.FormatMessage(dwFlags, 0, error, OS.LANG_USER_DEFAULT, buffer, 0, 0);
|
|
546 if (length is 0) return " [GetLastError=0x" + Integer.toHexString(error) + "]"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
547 TCHAR buffer1 = new TCHAR(0, length);
|
|
548 OS.MoveMemory(buffer1, buffer[0], length * TCHAR.sizeof);
|
|
549 if (buffer[0] !is 0) OS.LocalFree(buffer[0]);
|
|
550 return buffer1.toString(0, length);
|
|
551 }
|
|
552
|
|
553 /**
|
|
554 * Returns the matching standard color for the given
|
|
555 * constant, which should be one of the color constants
|
|
556 * specified in class <code>DWT</code>. Any value other
|
|
557 * than one of the DWT color constants which is passed
|
|
558 * in will result in the color black. This color should
|
|
559 * not be freed because it was allocated by the system,
|
|
560 * not the application.
|
|
561 *
|
|
562 * @param id the color constant
|
|
563 * @return the matching color
|
|
564 *
|
|
565 * @exception DWTException <ul>
|
|
566 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
567 * </ul>
|
|
568 *
|
|
569 * @see DWT
|
|
570 */
|
|
571 public Color getSystemColor (int id) {
|
|
572 checkDevice ();
|
|
573 int pixel = 0x00000000;
|
|
574 switch (id) {
|
|
575 case DWT.COLOR_WHITE: pixel = 0x00FFFFFF; break;
|
|
576 case DWT.COLOR_BLACK: pixel = 0x00000000; break;
|
|
577 case DWT.COLOR_RED: pixel = 0x000000FF; break;
|
|
578 case DWT.COLOR_DARK_RED: pixel = 0x00000080; break;
|
|
579 case DWT.COLOR_GREEN: pixel = 0x0000FF00; break;
|
|
580 case DWT.COLOR_DARK_GREEN: pixel = 0x00008000; break;
|
|
581 case DWT.COLOR_YELLOW: pixel = 0x0000FFFF; break;
|
|
582 case DWT.COLOR_DARK_YELLOW: pixel = 0x00008080; break;
|
|
583 case DWT.COLOR_BLUE: pixel = 0x00FF0000; break;
|
|
584 case DWT.COLOR_DARK_BLUE: pixel = 0x00800000; break;
|
|
585 case DWT.COLOR_MAGENTA: pixel = 0x00FF00FF; break;
|
|
586 case DWT.COLOR_DARK_MAGENTA: pixel = 0x00800080; break;
|
|
587 case DWT.COLOR_CYAN: pixel = 0x00FFFF00; break;
|
|
588 case DWT.COLOR_DARK_CYAN: pixel = 0x00808000; break;
|
|
589 case DWT.COLOR_GRAY: pixel = 0x00C0C0C0; break;
|
|
590 case DWT.COLOR_DARK_GRAY: pixel = 0x00808080; break;
|
|
591 }
|
|
592 return Color.win32_new (this, pixel);
|
|
593 }
|
|
594
|
|
595 /**
|
|
596 * Returns a reasonable font for applications to use.
|
|
597 * On some platforms, this will match the "default font"
|
|
598 * or "system font" if such can be found. This font
|
|
599 * should not be freed because it was allocated by the
|
|
600 * system, not the application.
|
|
601 * <p>
|
|
602 * Typically, applications which want the default look
|
|
603 * should simply not set the font on the widgets they
|
|
604 * create. Widgets are always created with the correct
|
|
605 * default font for the class of user-interface component
|
|
606 * they represent.
|
|
607 * </p>
|
|
608 *
|
|
609 * @return a font
|
|
610 *
|
|
611 * @exception DWTException <ul>
|
|
612 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
613 * </ul>
|
|
614 */
|
|
615 public Font getSystemFont () {
|
|
616 checkDevice ();
|
|
617 int hFont = OS.GetStockObject (OS.SYSTEM_FONT);
|
|
618 return Font.win32_new (this, hFont);
|
|
619 }
|
|
620
|
|
621 /**
|
|
622 * Returns <code>true</code> if the underlying window system prints out
|
|
623 * warning messages on the console, and <code>setWarnings</code>
|
|
624 * had previously been called with <code>true</code>.
|
|
625 *
|
|
626 * @return <code>true</code>if warnings are being handled, and <code>false</code> otherwise
|
|
627 *
|
|
628 * @exception DWTException <ul>
|
|
629 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
630 * </ul>
|
|
631 */
|
|
632 public bool getWarnings () {
|
|
633 checkDevice ();
|
|
634 return false;
|
|
635 }
|
|
636
|
|
637 /**
|
|
638 * Initializes any internal resources needed by the
|
|
639 * device.
|
|
640 * <p>
|
|
641 * This method is called after <code>create</code>.
|
|
642 * </p><p>
|
|
643 * If subclasses reimplement this method, they must
|
|
644 * call the <code>super</code> implementation.
|
|
645 * </p>
|
|
646 *
|
|
647 * @see #create
|
|
648 */
|
|
649 protected void init () {
|
|
650 if (debug) {
|
|
651 if (!OS.IsWinCE) OS.GdiSetBatchLimit(1);
|
|
652 }
|
|
653
|
|
654 /* Initialize scripts list */
|
|
655 if (!OS.IsWinCE) {
|
|
656 int [] ppSp = new int [1];
|
|
657 int [] piNumScripts = new int [1];
|
|
658 OS.ScriptGetProperties (ppSp, piNumScripts);
|
|
659 scripts = new int [piNumScripts [0]];
|
|
660 OS.MoveMemory (scripts, ppSp [0], scripts.length * 4);
|
|
661 }
|
|
662
|
|
663 /*
|
|
664 * If we're not on a device which supports palettes,
|
|
665 * don't create one.
|
|
666 */
|
|
667 int hDC = internal_new_GC (null);
|
|
668 int rc = OS.GetDeviceCaps (hDC, OS.RASTERCAPS);
|
|
669 int bits = OS.GetDeviceCaps (hDC, OS.BITSPIXEL);
|
|
670 int planes = OS.GetDeviceCaps (hDC, OS.PLANES);
|
|
671
|
|
672 bits *= planes;
|
|
673 if ((rc & OS.RC_PALETTE) is 0 || bits !is 8) {
|
|
674 internal_dispose_GC (hDC, null);
|
|
675 return;
|
|
676 }
|
|
677
|
|
678 int numReserved = OS.GetDeviceCaps (hDC, OS.NUMRESERVED);
|
|
679 int numEntries = OS.GetDeviceCaps (hDC, OS.SIZEPALETTE);
|
|
680
|
|
681 if (OS.IsWinCE) {
|
|
682 /*
|
|
683 * Feature on WinCE. For some reason, certain 8 bit WinCE
|
|
684 * devices return 0 for the number of reserved entries in
|
|
685 * the system palette. Their system palette correctly contains
|
|
686 * the usual 20 system colors. The workaround is to assume
|
|
687 * there are 20 reserved system colors instead of 0.
|
|
688 */
|
|
689 if (numReserved is 0 && numEntries >= 20) numReserved = 20;
|
|
690 }
|
|
691
|
|
692 /* Create the palette and reference counter */
|
|
693 colorRefCount = new int [numEntries];
|
|
694
|
|
695 /* 4 bytes header + 4 bytes per entry * numEntries entries */
|
|
696 byte [] logPalette = new byte [4 + 4 * numEntries];
|
|
697
|
|
698 /* 2 bytes = special header */
|
|
699 logPalette [0] = 0x00;
|
|
700 logPalette [1] = 0x03;
|
|
701
|
|
702 /* 2 bytes = number of colors, LSB first */
|
|
703 logPalette [2] = 0;
|
|
704 logPalette [3] = 1;
|
|
705
|
|
706 /*
|
|
707 * Create a palette which contains the system entries
|
|
708 * as they are located in the system palette. The
|
|
709 * MSDN article 'Memory Device Contexts' describes
|
|
710 * where system entries are located. On an 8 bit
|
|
711 * display with 20 reserved colors, the system colors
|
|
712 * will be the first 10 entries and the last 10 ones.
|
|
713 */
|
|
714 byte[] lppe = new byte [4 * numEntries];
|
|
715 OS.GetSystemPaletteEntries (hDC, 0, numEntries, lppe);
|
|
716 /* Copy all entries from the system palette */
|
|
717 System.arraycopy (lppe, 0, logPalette, 4, 4 * numEntries);
|
|
718 /* Lock the indices corresponding to the system entries */
|
|
719 for (int i = 0; i < numReserved / 2; i++) {
|
|
720 colorRefCount [i] = 1;
|
|
721 colorRefCount [numEntries - 1 - i] = 1;
|
|
722 }
|
|
723 internal_dispose_GC (hDC, null);
|
|
724 hPalette = OS.CreatePalette (logPalette);
|
|
725 }
|
|
726 /**
|
|
727 * Invokes platform specific functionality to allocate a new GC handle.
|
|
728 * <p>
|
|
729 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
730 * API for <code>Device</code>. It is marked public only so that it
|
|
731 * can be shared within the packages provided by DWT. It is not
|
|
732 * available on all platforms, and should never be called from
|
|
733 * application code.
|
|
734 * </p>
|
|
735 *
|
|
736 * @param data the platform specific GC data
|
|
737 * @return the platform specific GC handle
|
|
738 */
|
|
739 public abstract int internal_new_GC (GCData data);
|
|
740
|
|
741 /**
|
|
742 * Invokes platform specific functionality to dispose a GC handle.
|
|
743 * <p>
|
|
744 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
745 * API for <code>Device</code>. It is marked public only so that it
|
|
746 * can be shared within the packages provided by DWT. It is not
|
|
747 * available on all platforms, and should never be called from
|
|
748 * application code.
|
|
749 * </p>
|
|
750 *
|
|
751 * @param hDC the platform specific GC handle
|
|
752 * @param data the platform specific GC data
|
|
753 */
|
|
754 public abstract void internal_dispose_GC (int hDC, GCData data);
|
|
755
|
|
756 /**
|
|
757 * Returns <code>true</code> if the device has been disposed,
|
|
758 * and <code>false</code> otherwise.
|
|
759 * <p>
|
|
760 * This method gets the dispose state for the device.
|
|
761 * When a device has been disposed, it is an error to
|
|
762 * invoke any other method using the device.
|
|
763 *
|
|
764 * @return <code>true</code> when the device is disposed and <code>false</code> otherwise
|
|
765 */
|
|
766 public bool isDisposed () {
|
|
767 return disposed;
|
|
768 }
|
|
769
|
|
770 /**
|
|
771 * Loads the font specified by a file. The font will be
|
|
772 * present in the list of fonts available to the application.
|
|
773 *
|
|
774 * @param path the font file path
|
|
775 * @return whether the font was successfully loaded
|
|
776 *
|
|
777 * @exception DWTException <ul>
|
|
778 * <li>ERROR_NULL_ARGUMENT - if path is null</li>
|
|
779 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
780 * </ul>
|
|
781 *
|
|
782 * @see Font
|
|
783 *
|
|
784 * @since 3.3
|
|
785 */
|
|
786 public bool loadFont (String path) {
|
|
787 checkDevice();
|
|
788 if (path is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
|
|
789 if (OS.IsWinNT && OS.WIN32_VERSION >= OS.VERSION (4, 10)) {
|
|
790 TCHAR lpszFilename = new TCHAR (0, path, true);
|
|
791 return OS.AddFontResourceEx (lpszFilename, OS.FR_PRIVATE, 0) !is 0;
|
|
792 }
|
|
793 return false;
|
|
794 }
|
|
795
|
|
796 void new_Object (Object object) {
|
|
797 for (int i=0; i<objects.length; i++) {
|
|
798 if (objects [i] is null) {
|
|
799 objects [i] = object;
|
|
800 errors [i] = new Error ();
|
|
801 return;
|
|
802 }
|
|
803 }
|
|
804 Object [] newObjects = new Object [objects.length + 128];
|
|
805 System.arraycopy (objects, 0, newObjects, 0, objects.length);
|
|
806 newObjects [objects.length] = object;
|
|
807 objects = newObjects;
|
|
808 Error [] newErrors = new Error [errors.length + 128];
|
|
809 System.arraycopy (errors, 0, newErrors, 0, errors.length);
|
|
810 newErrors [errors.length] = new Error ();
|
|
811 errors = newErrors;
|
|
812 }
|
|
813
|
|
814 /**
|
|
815 * Releases any internal resources back to the operating
|
|
816 * system and clears all fields except the device handle.
|
|
817 * <p>
|
|
818 * When a device is destroyed, resources that were acquired
|
|
819 * on behalf of the programmer need to be returned to the
|
|
820 * operating system. For example, if the device allocated a
|
|
821 * font to be used as the system font, this font would be
|
|
822 * freed in <code>release</code>. Also,to assist the garbage
|
|
823 * collector and minimize the amount of memory that is not
|
|
824 * reclaimed when the programmer keeps a reference to a
|
|
825 * disposed device, all fields except the handle are zero'd.
|
|
826 * The handle is needed by <code>destroy</code>.
|
|
827 * </p>
|
|
828 * This method is called before <code>destroy</code>.
|
|
829 * </p><p>
|
|
830 * If subclasses reimplement this method, they must
|
|
831 * call the <code>super</code> implementation.
|
|
832 * </p>
|
|
833 *
|
|
834 * @see #dispose
|
|
835 * @see #destroy
|
|
836 */
|
|
837 protected void release () {
|
|
838 if (gdipToken !is null) {
|
|
839 Gdip.GdiplusShutdown (gdipToken[0]);
|
|
840 }
|
|
841 gdipToken = null;
|
|
842 scripts = null;
|
|
843 if (hPalette !is 0) OS.DeleteObject (hPalette);
|
|
844 hPalette = 0;
|
|
845 colorRefCount = null;
|
|
846 logFonts = null;
|
|
847 nFonts = 0;
|
|
848 }
|
|
849
|
|
850 /**
|
|
851 * If the underlying window system supports printing warning messages
|
|
852 * to the console, setting warnings to <code>false</code> prevents these
|
|
853 * messages from being printed. If the argument is <code>true</code> then
|
|
854 * message printing is not blocked.
|
|
855 *
|
|
856 * @param warnings <code>true</code>if warnings should be printed, and <code>false</code> otherwise
|
|
857 *
|
|
858 * @exception DWTException <ul>
|
|
859 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
|
|
860 * </ul>
|
|
861 */
|
|
862 public void setWarnings (bool warnings) {
|
|
863 checkDevice ();
|
|
864 }
|
|
865
|
|
866 }
|
|
867 +++/
|
|
868
|
|
869
|