package org.eclipse.swt.graphics; /* * Copyright (c) 2000, 2002 IBM Corp. All rights reserved. * This file is 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 */ import org.eclipse.swt.*; import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.gtk.*; /** * 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; /* Colormap and reference count */ GdkColor [] gdkColors; int [] colorRefCount; /* Disposed flag */ boolean disposed; /* Warning and Error Handlers */ int logProc; Callback logCallback; //NOT DONE - get list of valid names String [] log_domains = {"GLib-GObject", "GLib", "GObject", "Pango", "ATK", "GdkPixbuf", "Gdk", "Gtk"}; int [] handler_ids = new int [log_domains.length]; int warningLevel; /* * The following colors are listed in the Windows * Programmer's Reference as the colors in the default * palette. */ Color COLOR_BLACK, COLOR_DARK_RED, COLOR_DARK_GREEN, COLOR_DARK_YELLOW, COLOR_DARK_BLUE; Color COLOR_DARK_MAGENTA, COLOR_DARK_CYAN, COLOR_GRAY, COLOR_DARK_GRAY, COLOR_RED; Color COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE; /* System Font */ Font systemFont; /* * 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. *

* You must dispose the device when it is no longer required. *

* * @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; } if (tracking) { errors = new Error [128]; objects = new Object [128]; } create (data); init (); /* Initialize the system font slot */ systemFont = getSystemFont (); } protected void checkDevice () { if (disposed) SWT.error(SWT.ERROR_DEVICE_DISPOSED); } protected void create (DeviceData data) { } /** * Disposes of the operating system resources associated with * the receiver. After this method has been invoked, the receiver * will answer true when sent the message * isDisposed(). * * @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 *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • * */ public Rectangle getBounds () { checkDevice (); return new Rectangle(0, 0, 0, 0); } /** * Returns a DeviceData based on the receiver. * Modifications made to this DeviceData will not * affect the receiver. * * @return a DeviceData containing the device's data and attributes * * @exception SWTException * * @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 *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • * * * @see #getBounds */ public Rectangle getClientArea () { checkDevice (); 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 */ public int getDepth () { checkDevice (); return 0; } /** * 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 */ public Point getDPI () { checkDevice (); return new Point (72, 72); } /** * Returns FontData objects which describe * the fonts that match the given arguments. If the * faceName is null, all fonts will be returned. * * @param faceName the name of the font to look for, or null * @param scalable true if scalable fonts should be returned. * @return the matching font data * * @exception SWTException */ public FontData[] getFontList (String faceName, boolean scalable) { checkDevice (); //FIXME - check scalable flag int[] family = new int[1]; int[] face = new int[1]; int[] families = new int[1]; int[] n_families = new int[1]; int[] faces = new int[1]; int[] n_faces = new int[1]; int context = OS.gdk_pango_context_get(); OS.pango_context_list_families(context, families, n_families); int nFds = 0; FontData[] fds = new FontData[faceName != null ? 4 : n_families[0]]; for (int i=0; iSWT. Any value other * than one of the SWT color constants which is passed * in will result in the color black. This color should * not be free'd because it was allocated by the system, * not the application. * * @param id the color constant * @return the matching color * * @exception SWTException
      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    * * @see SWT */ public Color getSystemColor (int id) { checkDevice (); switch (id) { case SWT.COLOR_BLACK: return COLOR_BLACK; case SWT.COLOR_DARK_RED: return COLOR_DARK_RED; case SWT.COLOR_DARK_GREEN: return COLOR_DARK_GREEN; case SWT.COLOR_DARK_YELLOW: return COLOR_DARK_YELLOW; case SWT.COLOR_DARK_BLUE: return COLOR_DARK_BLUE; case SWT.COLOR_DARK_MAGENTA: return COLOR_DARK_MAGENTA; case SWT.COLOR_DARK_CYAN: return COLOR_DARK_CYAN; case SWT.COLOR_GRAY: return COLOR_GRAY; case SWT.COLOR_DARK_GRAY: return COLOR_DARK_GRAY; case SWT.COLOR_RED: return COLOR_RED; case SWT.COLOR_GREEN: return COLOR_GREEN; case SWT.COLOR_YELLOW: return COLOR_YELLOW; case SWT.COLOR_BLUE: return COLOR_BLUE; case SWT.COLOR_MAGENTA: return COLOR_MAGENTA; case SWT.COLOR_CYAN: return COLOR_CYAN; case SWT.COLOR_WHITE: return COLOR_WHITE; } return COLOR_BLACK; } /** * Returns a reasonable font for applications to use. * On some platforms, this will match the "default font" * or "system font" if such can be found. This font * should not be free'd because it was allocated by the * system, not the application. *

    * Typically, applications which want the default look * should simply not set the font on the widgets they * create. Widgets are always created with the correct * default font for the class of user-interface component * they represent. *

    * * @return a font * * @exception SWTException
      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    */ public Font getSystemFont () { checkDevice (); return systemFont; } /** * Returns true if the underlying window system prints out * warning messages on the console, and setWarnings * had previously been called with true. * * @return trueif warnings are being handled, and false otherwise * * @exception SWTException
      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    */ public boolean getWarnings () { checkDevice (); return warningLevel == 0; } protected void init () { /* Create GTK warnings and error callbacks */ logCallback = new Callback (this, "logProc", 4); logProc = logCallback.getAddress (); if (logProc == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS); /* Set GTK warning and error handlers */ if (debug) { int flags = OS.G_LOG_LEVEL_MASK | OS.G_LOG_FLAG_FATAL | OS.G_LOG_FLAG_RECURSION; for (int i=0; i * IMPORTANT: This method is not part of the public * API for Device. It is marked public only so that it * can be shared within the packages provided by SWT. It is not * available on all platforms, and should never be called from * application code. *

    * * @param data the platform specific GC data * @return the platform specific GC handle * * @private */ public abstract int internal_new_GC (GCData data); /** * Invokes platform specific functionality to dispose a GC handle. *

    * IMPORTANT: This method is not part of the public * API for Device. It is marked public only so that it * can be shared within the packages provided by SWT. It is not * available on all platforms, and should never be called from * application code. *

    * * @param handle the platform specific GC handle * @param data the platform specific GC data * * @private */ public abstract void internal_dispose_GC (int handle, GCData data); /** * Returns true if the device has been disposed, * and false otherwise. *

    * This method gets the dispose state for the device. * When a device has been disposed, it is an error to * invoke any other method using the device. * * @return true when the device is disposed and false otherwise */ public boolean isDisposed () { return disposed; } int logProc (int log_domain, int log_level, int message, int user_data) { if (DEBUG || (debug && warningLevel == 0)) { new Error ().printStackTrace (); OS.g_log_default_handler (log_domain, log_level, message, 0); } return 0; } void new_Object (Object object) { for (int i=0; i 0) { OS.gdk_colormap_free_colors(colormap, color, 1); --colorRefCount [i]; } } } } gdkColors = null; colorRefCount = null; COLOR_BLACK = COLOR_DARK_RED = COLOR_DARK_GREEN = COLOR_DARK_YELLOW = COLOR_DARK_BLUE = COLOR_DARK_MAGENTA = COLOR_DARK_CYAN = COLOR_GRAY = COLOR_DARK_GRAY = COLOR_RED = COLOR_GREEN = COLOR_YELLOW = COLOR_BLUE = COLOR_MAGENTA = COLOR_CYAN = COLOR_WHITE = null; /* Free the GTK error and warning handler */ for (int i=0; itrue prevents these * messages from being printed. If the argument is false * message printing is not blocked. * * @param warnings trueif warnings should be handled, and false otherwise * * @exception SWTException

      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    */ public void setWarnings (boolean warnings) { checkDevice (); if (warnings) { if (--warningLevel == 0) { if (debug) return; for (int i=0; i