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