?? device.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.graphics; import org.eclipse.swt.internal.*;import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;/** * This class is the abstract superclass of all device objects, * such as the Display device and the Printer device. Devices * can have a graphics context (GC) created for them, and they * can be drawn on by sending messages to the associated GC. */public abstract class Device implements Drawable { /* Debugging */ public static boolean DEBUG; boolean debug = DEBUG; boolean tracking = DEBUG; Error [] errors; Object [] objects; /* Palette * (Warning: This field is platform dependent) */ public int hPalette = 0; int [] colorRefCount; /* System Font */ int systemFont; /* Font Enumeration */ int nFonts = 256; LOGFONT [] logFonts; /* Scripts */ int [] scripts; LOGFONT [] logFontsCache; boolean disposed; /* * TEMPORARY CODE. When a graphics object is * created and the device parameter is null, * the current Display is used. This presents * a problem because SWT graphics does not * reference classes in SWT widgets. The correct * fix is to remove this feature. Unfortunately, * too many application programs rely on this * feature. * * This code will be removed in the future. */ protected static Device CurrentDevice; protected static Runnable DeviceFinder; static { try { Class.forName ("org.eclipse.swt.widgets.Display"); } catch (Throwable e) {} } /** TEMPORARY CODE.*/static Device getDevice () { if (DeviceFinder != null) DeviceFinder.run(); Device device = CurrentDevice; CurrentDevice = null; return device;} /** * Constructs a new instance of this class. * <p> * You must dispose the device when it is no longer required. * </p> * * @param data the DeviceData which describes the receiver * * @see #create * @see #init * @see DeviceData */public Device(DeviceData data) { if (data != null) { debug = data.debug; tracking = data.tracking; } create (data); init (); if (tracking) { errors = new Error [128]; objects = new Object [128]; } /* Initialize the system font slot */ systemFont = getSystemFont().handle;}/** * Throws an <code>SWTException</code> if the receiver can not * be accessed by the caller. This may include both checks on * the state of the receiver and more generally on the entire * execution context. This method <em>should</em> be called by * device implementors to enforce the standard SWT invariants. * <p> * Currently, it is an error to invoke any method (other than * <code>isDisposed()</code> and <code>dispose()</code>) on a * device that has had its <code>dispose()</code> method called. * </p><p> * In future releases of SWT, there may be more or fewer error * checks and exceptions may be thrown for different reasons. * <p> * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * </ul> */protected void checkDevice () { if (disposed) SWT.error(SWT.ERROR_DEVICE_DISPOSED);}/** * Creates the device in the operating system. If the device * does not have a handle, this method may do nothing depending * on the device. * <p> * This method is called before <code>init</code>. * </p><p> * Subclasses are supposed to reimplement this method and not * call the <code>super</code> implementation. * </p> * * @param data the DeviceData which describes the receiver * * @see #init */protected void create (DeviceData data) {}int computePixels(int height) { int hDC = internal_new_GC (null); int pixels = -Compatibility.round(height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY), 72); internal_dispose_GC (hDC, null); return pixels;}int computePoints(LOGFONT logFont) { int hDC = internal_new_GC (null); int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY); int pixels = 0; if (logFont.lfHeight > 0) { /* * Feature in Windows. If the lfHeight of the LOGFONT structure * is positive, the lfHeight measures the height of the entire * cell, including internal leading, in logical units. Since the * height of a font in points does not include the internal leading, * we must subtract the internal leading, which requires a TEXTMETRIC, * which in turn requires font creation. */ int hFont = OS.CreateFontIndirect(logFont); int oldFont = OS.SelectObject(hDC, hFont); TEXTMETRIC lptm = OS.IsUnicode ? (TEXTMETRIC)new TEXTMETRICW() : new TEXTMETRICA(); OS.GetTextMetrics(hDC, lptm); OS.SelectObject(hDC, oldFont); OS.DeleteObject(hFont); pixels = logFont.lfHeight - lptm.tmInternalLeading; } else { pixels = -logFont.lfHeight; } internal_dispose_GC (hDC, null); return Compatibility.round(pixels * 72, logPixelsY);}/** * Destroys the device in the operating system and releases * the device's handle. If the device does not have a handle, * this method may do nothing depending on the device. * <p> * This method is called after <code>release</code>. * </p><p> * Subclasses are supposed to reimplement this method and not * call the <code>super</code> implementation. * </p> * * @see #dispose * @see #release */protected void destroy () {}/** * Disposes of the operating system resources associated with * the receiver. After this method has been invoked, the receiver * will answer <code>true</code> when sent the message * <code>isDisposed()</code>. * * @see #release * @see #destroy * @see #checkDevice */public void dispose () { if (isDisposed()) return; checkDevice (); release (); destroy (); disposed = true; if (tracking) { objects = null; errors = null; }}void dispose_Object (Object object) { for (int i=0; i<objects.length; i++) { if (objects [i] == object) { objects [i] = null; errors [i] = null; return; } }}int EnumFontFamProc (int lpelfe, int lpntme, int FontType, int lParam) { boolean isScalable = (FontType & OS.RASTER_FONTTYPE) == 0; boolean scalable = lParam == 1; if (isScalable == scalable) { /* Add the log font to the list of log fonts */ if (nFonts == logFonts.length) { LOGFONT [] newLogFonts = new LOGFONT [logFonts.length + 128]; System.arraycopy (logFonts, 0, newLogFonts, 0, nFonts); logFonts = newLogFonts; } LOGFONT logFont = logFonts [nFonts]; if (logFont == null) logFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW () : new LOGFONTA (); OS.MoveMemory (logFont, lpelfe, LOGFONT.sizeof); logFonts [nFonts++] = logFont; } return 1;}/** * Returns a rectangle describing the receiver's size and location. * * @return the bounding rectangle * * @exception SWTException <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> * </ul> */public Rectangle getBounds () { checkDevice (); int hDC = internal_new_GC (null); int width = OS.GetDeviceCaps (hDC, OS.HORZRES); int height = OS.GetDeviceCaps (hDC, OS.VERTRES); internal_dispose_GC (hDC, null); return new Rectangle (0, 0, width, height);}/** * Returns a <code>DeviceData</code> based on the receiver. * Modifications made to this <code>DeviceData</code> will not * affect the receiver. * * @return a <code>DeviceData</code> containing the device's data and attributes * * @exception SWTException <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> * </ul> * * @see DeviceData */public DeviceData getDeviceData () { checkDevice(); DeviceData data = new DeviceData (); data.debug = debug; data.tracking = tracking; int count = 0, length = 0; if (tracking) length = objects.length; for (int i=0; i<length; i++) { if (objects [i] != null) count++; } int index = 0; data.objects = new Object [count]; data.errors = new Error [count]; for (int i=0; i<length; i++) { if (objects [i] != null) { data.objects [index] = objects [i]; data.errors [index] = errors [i]; index++; } } return data;}/** * Returns a rectangle which describes the area of the * receiver which is capable of displaying data. * * @return the client area * * @exception SWTException <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> * </ul> * * @see #getBounds */public Rectangle getClientArea () { return getBounds ();}/** * Returns the bit depth of the screen, which is the number of * bits it takes to represent the number of unique colors that * the screen is currently capable of displaying. This number * will typically be one of 1, 8, 15, 16, 24 or 32. * * @return the depth of the screen * * @exception SWTException <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> * </ul> */public int getDepth () { checkDevice (); int hDC = internal_new_GC (null); int bits = OS.GetDeviceCaps (hDC, OS.BITSPIXEL); int planes = OS.GetDeviceCaps (hDC, OS.PLANES); internal_dispose_GC (hDC, null); return bits * planes;}/** * Returns a point whose x coordinate is the horizontal * dots per inch of the display, and whose y coordinate * is the vertical dots per inch of the display. * * @return the horizontal and vertical DPI * * @exception SWTException <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li> * </ul> */public Point getDPI () { checkDevice (); int hDC = internal_new_GC (null); int dpiX = OS.GetDeviceCaps (hDC, OS.LOGPIXELSX); int dpiY = OS.GetDeviceCaps (hDC, OS.LOGPIXELSY); internal_dispose_GC (hDC, null); return new Point (dpiX, dpiY);}/** * Returns <code>FontData</code> objects which describe * the fonts that match the given arguments. If the * <code>faceName</code> is null, all fonts will be returned. * * @param faceName the name of the font to look for, or null * @param scalable if true only scalable fonts are returned, otherwise only non-scalable fonts are returned. * @return the matching font data * * @exception SWTException <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -