comparison dwt/graphics/Device.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents 642f460a0908
children cfa563df4fdd
comparison
equal deleted inserted replaced
44:ca5e494f2bbf 45:d8635bb48c7c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * 10 *
11 * Port to the D programming language: 11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com> 12 * Jacob Carlborg <doob@me.com>
13 *******************************************************************************/ 13 *******************************************************************************/
14 module dwt.graphics.Device; 14 module dwt.graphics.Device;
15 15
16 import dwt.DWT; 16 import dwt.DWT;
17 import dwt.DWTException; 17 import dwt.DWTException;
18 import dwt.internal.Compatibility; 18 import dwt.internal.Compatibility;
19 import dwt.internal.cocoa.NSArray; 19 import dwt.internal.cocoa.NSArray;
20 import dwt.internal.cocoa.NSAutoreleasePool;
20 import dwt.internal.cocoa.NSDictionary; 21 import dwt.internal.cocoa.NSDictionary;
21 import dwt.internal.cocoa.NSFont; 22 import dwt.internal.cocoa.NSFont;
22 import dwt.internal.cocoa.NSFontManager; 23 import dwt.internal.cocoa.NSFontManager;
24 import dwt.internal.cocoa.NSMutableDictionary;
25 import dwt.internal.cocoa.NSNumber;
23 import dwt.internal.cocoa.NSRect; 26 import dwt.internal.cocoa.NSRect;
24 import dwt.internal.cocoa.NSScreen; 27 import dwt.internal.cocoa.NSScreen;
25 import dwt.internal.cocoa.NSSize; 28 import dwt.internal.cocoa.NSSize;
26 import dwt.internal.cocoa.NSString; 29 import dwt.internal.cocoa.NSString;
30 import dwt.internal.cocoa.NSThread;
27 import dwt.internal.cocoa.NSValue; 31 import dwt.internal.cocoa.NSValue;
28 import dwt.internal.cocoa.OS; 32 import dwt.internal.cocoa.OS;
29 import dwt.internal.cocoa.id; 33 import dwt.internal.cocoa.id;
30 34
31 import dwt.dwthelper.Runnable; 35 import dwt.dwthelper.Runnable;
45 /** 49 /**
46 * This class is the abstract superclass of all device objects, 50 * This class is the abstract superclass of all device objects,
47 * such as the Display device and the Printer device. Devices 51 * such as the Display device and the Printer device. Devices
48 * can have a graphics context cast(GC) created for them, and they 52 * can have a graphics context cast(GC) created for them, and they
49 * can be drawn on by sending messages to the associated GC. 53 * can be drawn on by sending messages to the associated GC.
54 *
55 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
50 */ 56 */
51 public abstract class Device : Drawable { 57 public abstract class Device : Drawable {
52 58
53 /* debug_ing */ 59 /* debug_ing */
54 public static const bool DEBUG = true; 60 public static const bool DEBUG = true;
65 Color COLOR_DARK_MAGENTA, COLOR_DARK_CYAN, COLOR_GRAY, COLOR_DARK_GRAY, COLOR_RED; 71 Color COLOR_DARK_MAGENTA, COLOR_DARK_CYAN, COLOR_GRAY, COLOR_DARK_GRAY, COLOR_RED;
66 Color COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE; 72 Color COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE;
67 73
68 /* System Font */ 74 /* System Font */
69 Font systemFont; 75 Font systemFont;
70 76
77 /* Device DPI */
78 Point dpi;
79
71 /* 80 /*
72 * TEMPORARY CODE. When a graphics object is 81 * TEMPORARY CODE. When a graphics object is
73 * created and the device parameter is null, 82 * created and the device parameter is null,
74 * the current Display is used. This presents 83 * the current Display is used. This presents
75 * a problem because DWT graphics does not 84 * a problem because DWT graphics does not
134 if (tracking) { 143 if (tracking) {
135 errors = new Error [128]; 144 errors = new Error [128];
136 objects = new Object [128]; 145 objects = new Object [128];
137 trackingLock = new Object (); 146 trackingLock = new Object ();
138 } 147 }
148 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
149 NSThread nsthread = NSThread.currentThread();
150 NSMutableDictionary dictionary = nsthread.threadDictionary();
151 NSString key = NSString.stringWith("DWT_NSAutoreleasePool");
152 id obj = dictionary.objectForKey(key);
153 if (obj is null) {
154 NSNumber nsnumber = NSNumber.numberWithInteger(pool.id);
155 dictionary.setObject(nsnumber, key);
156 } else {
157 pool.release();
158 }
159 //check and create pool
139 create (data); 160 create (data);
140 init_ (); 161 init_ ();
141 } 162 }
142 } 163 }
143 164
247 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> 268 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
248 * </ul> 269 * </ul>
249 */ 270 */
250 public Rectangle getBounds () { 271 public Rectangle getBounds () {
251 checkDevice (); 272 checkDevice ();
252 NSScreen screen = NSScreen.mainScreen(); 273 NSRect frame = getPrimaryScreen().frame();
253 NSRect rect = screen.frame(); 274 return new Rectangle(cast(int)frame.x, cast(int)frame.y, cast(int)frame.width, cast(int)frame.height);
254 return new Rectangle(cast(int)rect.x, cast(int)rect.y, cast(int)rect.width, cast(int)rect.height);
255 } 275 }
256 276
257 /** 277 /**
258 * Returns a <code>DeviceData</code> based on the receiver. 278 * Returns a <code>DeviceData</code> based on the receiver.
259 * Modifications made to this <code>DeviceData</code> will not 279 * Modifications made to this <code>DeviceData</code> will not
308 * 328 *
309 * @see #getBounds 329 * @see #getBounds
310 */ 330 */
311 public Rectangle getClientArea () { 331 public Rectangle getClientArea () {
312 checkDevice (); 332 checkDevice ();
313 NSScreen screen = NSScreen.mainScreen(); 333 return getBounds ();
314 NSRect rect = screen.visibleFrame();
315 return new Rectangle(cast(int)rect.x, cast(int)rect.y, cast(int)rect.width, cast(int)rect.height);
316 } 334 }
317 335
318 /** 336 /**
319 * Returns the bit depth of the screen, which is the number of 337 * Returns the bit depth of the screen, which is the number of
320 * bits it takes to represent the number of unique colors that 338 * bits it takes to represent the number of unique colors that
327 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> 345 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
328 * </ul> 346 * </ul>
329 */ 347 */
330 public int getDepth () { 348 public int getDepth () {
331 checkDevice (); 349 checkDevice ();
332 return OS.NSBitsPerPixelFromDepth(NSScreen.mainScreen().depth()); 350 return (int)/*64*/OS.NSBitsPerPixelFromDepth(getPrimaryScreen().depth());
333 } 351 }
334 352
335 /** 353 /**
336 * Returns a point whose x coordinate is the horizontal 354 * Returns a point whose x coordinate is the horizontal
337 * dots per inch of the display, and whose y coordinate 355 * dots per inch of the display, and whose y coordinate
343 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> 361 * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
344 * </ul> 362 * </ul>
345 */ 363 */
346 public Point getDPI () { 364 public Point getDPI () {
347 checkDevice (); 365 checkDevice ();
348 NSDictionary dictionary = NSScreen.mainScreen().deviceDescription(); 366 return getScreenDPI();
349 NSValue value = new NSValue(dictionary.objectForKey(new id(OS.NSDeviceResolution())).id_); 367 }
350 NSSize size = value.sizeValue(); 368
351 return new Point(cast(int)size.width, cast(int)size.height); 369 NSScreen getPrimaryScreen () {
370 NSArray screens = NSScreen.screens();
371 return new NSScreen(screens.objectAtIndex(0));
352 } 372 }
353 373
354 /** 374 /**
355 * Returns <code>FontData</code> objects which describe 375 * Returns <code>FontData</code> objects which describe
356 * the fonts that match the given arguments. If the 376 * the fonts that match the given arguments. If the
365 * </ul> 385 * </ul>
366 */ 386 */
367 public FontData[] getFontList (String faceName, bool scalable) { 387 public FontData[] getFontList (String faceName, bool scalable) {
368 checkDevice (); 388 checkDevice ();
369 if (!scalable) return new FontData[0]; 389 if (!scalable) return new FontData[0];
370 NSArray fonts = NSFontManager.sharedFontManager().availableFonts(); 390 NSArray families = NSFontManager.sharedFontManager().availableFontFamilies();
371 int count = 0; 391 int famCount = families.count();
372 FontData[] fds = new FontData[fonts.count()]; 392 int count = 0;
373 for (NSUInteger i = 0; i < fds.length; i++) { 393 FontData[] fds = new FontData[100];
374 NSString str = new NSString(fonts.objectAtIndex(i)); 394 for (int i = 0; i < famCount; i++) {
375 char[] buffer = new char[str.length()]; 395 NSString nsfamily = new NSString(families.objectAtIndex(i));
376 str.getCharacters_(buffer.toString16().ptr); 396 String family = nsfamily.getString();
377 String nsName = buffer.dup; 397 NSArray fonts = NSFontManager.sharedFontManager().availableMembersOfFontFamily(nsfamily);
378 String name = nsName; 398 int fontCount = fonts.count();
379 int index = nsName.indexOf('-'); 399 for (int j = 0; j < fontCount; j++) {
380 if (index !is -1) name = name.substring(0, index); 400 NSArray fontDetails = new NSArray(fonts.objectAtIndex(j));
381 int style = DWT.NORMAL; 401 NSString str = new NSString(fontDetails.objectAtIndex(0));
382 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC; 402 String nsName = str.getString();
383 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD; 403 String name = nsName;
384 if (faceName is null || Compatibility.equalsIgnoreCase(faceName, name)) { 404 int index = nsName.indexOf('-');
385 FontData data = new FontData(name, 0, style); 405 if (index !is -1) name = name.substring(0, index);
386 data.nsName = nsName; 406 int style = DWT.NORMAL;
387 fds[count++] = data; 407 if (nsName.indexOf("Italic") !is -1) style |= DWT.ITALIC;
408 if (nsName.indexOf("Bold") !is -1) style |= DWT.BOLD;
409 if (faceName is null || Compatibility.equalsIgnoreCase(faceName, name)) {
410 FontData data = new FontData(family, 0, style);
411 data.nsName = nsName;
412 if (count is fds.length) {
413 FontData[] newFds = new FontData[fds.length + 100];
414 System.arraycopy(fds, 0, newFds, 0, fds.length);
415 fds = newFds;
416 }
417 fds[count++] = data;
418 }
388 } 419 }
389 } 420 }
390 if (count is fds.length) return fds; 421 if (count is fds.length) return fds;
391 FontData[] result = new FontData[count]; 422 FontData[] result = new FontData[count];
392 System.arraycopy(fds, 0, result, 0, count); 423 System.arraycopy(fds, 0, result, 0, count);
393 return result; 424 return result;
425 }
426
427 Point getScreenDPI () {
428 NSDictionary dictionary = getPrimaryScreen().deviceDescription();
429 NSValue value = new NSValue(dictionary.objectForKey(new id(OS.NSDeviceResolution())).id);
430 NSSize size = value.sizeValue();
431 return new Point((int)size.width, (int)size.height);
394 } 432 }
395 433
396 /** 434 /**
397 * Returns the matching standard color for the given 435 * Returns the matching standard color for the given
398 * constant, which should be one of the color constants 436 * constant, which should be one of the color constants
505 COLOR_MAGENTA = new Color (this, 0xFF,0,0xFF); 543 COLOR_MAGENTA = new Color (this, 0xFF,0,0xFF);
506 COLOR_CYAN = new Color (this, 0,0xFF,0xFF); 544 COLOR_CYAN = new Color (this, 0,0xFF,0xFF);
507 COLOR_WHITE = new Color (this, 0xFF,0xFF,0xFF); 545 COLOR_WHITE = new Color (this, 0xFF,0xFF,0xFF);
508 546
509 /* Initialize the system font slot */ 547 /* Initialize the system font slot */
510 NSFont font = NSFont.systemFontOfSize(NSFont.systemFontSize()); 548 bool smallFonts = System.getProperty("dwt.internal.carbon.smallFonts") !is null;
549 float systemFontSize = smallFonts ? NSFont.smallSystemFontSize() : NSFont.systemFontSize();
550 Point dpi = this.dpi = getDPI(), screenDPI = getScreenDPI();
551 NSFont font = NSFont.systemFontOfSize(systemFontSize * dpi.y / screenDPI.y);
511 systemFont = Font.cocoa_new(this, font); 552 systemFont = Font.cocoa_new(this, font);
512 } 553 }
513 554
514 /** 555 /**
515 * Invokes platform specific functionality to allocate a new GC handle. 556 * Invokes platform specific functionality to allocate a new GC handle.
577 checkDevice(); 618 checkDevice();
578 if (path is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 619 if (path is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
579 bool result = false; 620 bool result = false;
580 char [] chars = new char [path.length ()]; 621 char [] chars = new char [path.length ()];
581 path.getChars (0, chars.length, chars, 0); 622 path.getChars (0, chars.length, chars, 0);
623 //TODO not done
582 return result; 624 return result;
583 } 625 }
584 626
585 void new_Object (Object object) { 627 void new_Object (Object object) {
586 synchronized (trackingLock) { 628 synchronized (trackingLock) {