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