All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.swt.graphics.Device Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.graphics;


import java.io.*;
import java.util.function.*;
import java.util.stream.*;

import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gtk.*;
import org.eclipse.swt.internal.gtk3.*;
import org.eclipse.swt.internal.gtk4.*;

/**
 * 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.
 *
 * @see Sample code and further information
 */
public abstract class Device implements Drawable {
	/**
	 * @noreference This field is not intended to be referenced by clients.
	 * @since 3.105
	 */
	protected static final int CHANGE_SCALEFACTOR = 1;
	/* Settings callbacks */
	long gsettingsProc;
	Callback gsettingsCallback;
	boolean isConnected = false;
	long displaySettings; //gsettings Dictionary

	/**
	 * the handle to the X Display
	 * (Warning: This field is platform dependent)
	 * 

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

* * @noreference This field is not intended to be referenced by clients. */ protected long xDisplay; long shellHandle; /* Debugging */ public static boolean DEBUG; boolean debug = DEBUG; boolean tracking = DEBUG; Error [] errors; Object [] objects; Object trackingLock; /* Disposed flag */ volatile boolean disposed; /* Warning and Error Handlers */ long logProc; Callback logCallback; //NOT DONE - get list of valid names String [] log_domains = {"", "GLib-GObject", "GLib", "GObject", "Pango", "ATK", "GdkPixbuf", "Gdk", "Gtk", "GnomeVFS", "GIO"}; int [] handler_ids = new int [log_domains.length]; int warningLevel; /* X Warning and Error Handlers */ static Callback XErrorCallback, XIOErrorCallback; static long XErrorProc, XIOErrorProc, XNullErrorProc, XNullIOErrorProc; static Device[] Devices = new Device[4]; /* * 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_TRANSPARENT; Color COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE; /* System Font */ Font systemFont; /* Device dpi */ Point dpi; long emptyTab; /* * 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. */ protected static Device CurrentDevice; protected static Runnable DeviceFinder; static { try { Class.forName ("org.eclipse.swt.widgets.Display"); } catch (ClassNotFoundException e) {} } /* * TEMPORARY CODE. */ static synchronized 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. *

* * @see #create * @see #init * * @since 3.1 */ public Device() { this(null); } /** * 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) { synchronized (Device.class) { if (data != null) { debug = data.debug; tracking = data.tracking; } if (tracking) { startTracking(); } create (data); init (); register (this); } } /** * * @exception SWTException
    *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • *
* @since 3.115 */ public boolean isTracking() { checkDevice(); return tracking; } /** * @exception SWTException
    *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • *
* @since 3.115 */ public void setTracking(boolean tracking) { checkDevice(); if (tracking == this.tracking) { return; } this.tracking = tracking; if (tracking) { startTracking(); } else { stopTracking(); } } private void startTracking() { errors = new Error [128]; objects = new Object [128]; trackingLock = new Object (); } private void stopTracking() { synchronized (trackingLock) { objects = null; errors = null; trackingLock = null; } } /** * Throws an SWTException 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 should be called by * device implementors to enforce the standard SWT invariants. *

* Currently, it is an error to invoke any method (other than * isDisposed() and dispose()) on a * device that has had its dispose() method called. *

* In future releases of SWT, there may be more or fewer error * checks and exceptions may be thrown for different reasons. *

* * @exception SWTException
    *
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • *
*/ 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. *

* This method is called before init. *

* Subclasses are supposed to reimplement this method and not * call the super implementation. *

* * @param data the DeviceData which describes the receiver * * @see #init */ 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 () { synchronized (Device.class) { try (ExceptionStash exceptions = new ExceptionStash ()) { if (isDisposed ()) return; checkDevice (); try { release (); } catch (Error | RuntimeException ex) { exceptions.stash (ex); } destroy (); deregister (this); xDisplay = 0; disposed = true; if (tracking) { tracking = false; stopTracking (); } } } } void dispose_Object (Object object) { synchronized (trackingLock) { for (int i=0; i * This method is called after release. *

* Subclasses are supposed to reimplement this method and not * call the super implementation. *

* * @see #dispose * @see #release */ protected void destroy () { } /** * Returns a rectangle describing the receiver's size and location. * * @return the bounding rectangle * * @exception SWTException
    *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • *
*/ public Rectangle getBounds () { checkDevice (); return DPIUtil.autoScaleDown (getBoundsInPixels ()); } private Rectangle getBoundsInPixels () { 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
    *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • *
* * @see DeviceData */ public DeviceData getDeviceData () { checkDevice(); DeviceData data = new DeviceData (); data.debug = debug; data.tracking = tracking; if (tracking) { synchronized (trackingLock) { int count = 0, length = objects.length; for (int i=0; i *
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
  • * * * @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
      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    */ public int getDepth () { checkDevice (); return 0; } /** * Returns a point whose x coordinate is the logical horizontal * dots per inch of the display, and whose y coordinate * is the logical vertical dots per inch of the display. * * @return the horizontal and vertical DPI * * @exception SWTException
      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    */ public Point getDPI () { checkDevice (); return getScreenDPI(); } /** * 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 if true only scalable fonts are returned, otherwise only non-scalable fonts are returned. * @return the matching font data * * @exception SWTException
      *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    */ public FontData[] getFontList (String faceName, boolean scalable) { checkDevice (); if (!scalable) return new FontData[0]; long [] family = new long [1]; long [] face = new long [1]; long [] families = new long [1]; int[] n_families = new int[1]; long [] faces = new long [1]; int[] n_faces = new int[1]; long context; if (GTK.GTK4) { long fontMap = OS.pango_cairo_font_map_get_default (); context = OS.pango_font_map_create_context (fontMap); } else { context = GDK.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 freed 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_TRANSPARENT: return COLOR_TRANSPARENT; 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 freed 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; } /** * Initializes any internal resources needed by the * device. *

    * This method is called after create. *

    * If subclasses reimplement this method, they must * call the super implementation. *

    * * @see #create */ protected void init () { if (debug) { if (xDisplay != 0) { /* Create the warning and error callbacks */ Class clazz = getClass (); synchronized (clazz) { int index = 0; while (index < Devices.length) { if (Devices [index] != null) break; index++; } if (index == Devices.length) { XErrorCallback = new Callback (clazz, "XErrorProc", 2); XNullErrorProc = XErrorCallback.getAddress (); XIOErrorCallback = new Callback (clazz, "XIOErrorProc", 1); XNullIOErrorProc = XIOErrorCallback.getAddress (); XErrorProc = OS.XSetErrorHandler (XNullErrorProc); XIOErrorProc = OS.XSetIOErrorHandler (XNullIOErrorProc); } } if (debug) OS.XSynchronize (xDisplay, true); } } /* Create GTK warnings and error callbacks */ if (xDisplay != 0) { logCallback = new Callback (this, "logProc", 4); logProc = logCallback.getAddress (); /* 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 load = (path, isResource) -> { try { BufferedReader buffer; if (isResource) { buffer = new BufferedReader(new InputStreamReader(Device.class.getResourceAsStream(path))); } else { buffer = new BufferedReader(new FileReader(new File(path))); } return buffer.lines().collect(Collectors.joining("\n")); } catch (IOException e) { System.err.println("SWT Warning: Failed to load " + (isResource ? "resource: " : "file: ") + path); return ""; } }; StringBuilder combinedCSS = new StringBuilder(); if (!GTK.GTK4) { // Load functional CSS fixes. Such as keyboard functionality for some widgets. combinedCSS.append(load.apply("/org/eclipse/swt/internal/gtk/swt_functional_gtk_3_20.css", true)); } // By default, load CSS theme fixes to overcome things such as excessive padding that breaks SWT otherwise. // Initially designed for Adwaita light/dark theme, but after investigation other themes (like Ubuntu's Ambiance + dark) seem to benefit from this also. // However, a few themes break with these fixes, so we allow them to be turned off by user and allow them to load their own fixes manually instead. // To turn on this flag, add the following vm argument: -Dorg.eclipse.swt.internal.gtk.noThemingFixes // Note: // - Display.create() may override the theme name. See Display.create() ... OS.getThemeName(..). // - These fixes should not contain any color information, otherwise it might break a light/dark variant of the theme. // Color fixes should be put either into the theme itself or via swt user api. if (System.getProperty("org.eclipse.swt.internal.gtk.noThemingFixes") == null) { combinedCSS.append(load.apply("/org/eclipse/swt/internal/gtk/swt_theming_fixes_gtk_3_20.css", true)); if (GTK.GTK_VERSION >= OS.VERSION(3, 24, 5)) { combinedCSS.append(load.apply("/org/eclipse/swt/internal/gtk/swt_theming_fixes_gtk_3_24_5.css", true)); } } // Load CSS from user-defined CSS file. String additionalCSSPath = System.getProperty("org.eclipse.swt.internal.gtk.cssFile"); if (additionalCSSPath != null){ // Warning: // - gtk css syntax changed in 3.20. If you load custom css, it could break things depending on gtk version on system. // - Also, a lot of custom css/themes are buggy and may result in additional console warnings. combinedCSS.append(load.apply(additionalCSSPath, false)); } if (GTK.GTK4) { long display = GDK.gdk_display_get_default(); if (display == 0 || provider == 0) { System.err.println("SWT Warning: Override of theme values failed. Reason: could not acquire display or provider."); return; } GTK4.gtk_style_context_add_provider_for_display (display, provider, GTK.GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); } else { long screen = GDK.gdk_screen_get_default(); if (screen == 0 || provider == 0) { System.err.println("SWT Warning: Override of theme values failed. Reason: could not acquire screen or provider."); return; } GTK3.gtk_style_context_add_provider_for_screen (screen, provider, GTK.GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); } if (GTK.GTK4) { GTK4.gtk_css_provider_load_from_data (provider, Converter.wcsToMbcs (combinedCSS.toString(), true), -1); } else { GTK3.gtk_css_provider_load_from_data (provider, Converter.wcsToMbcs (combinedCSS.toString(), true), -1, null); } } /** * Invokes platform specific functionality to allocate a new 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 data the platform specific GC data * @return the platform specific GC handle * * @noreference This method is not intended to be referenced by clients. */ @Override public abstract long 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 hDC the platform specific GC handle * @param data the platform specific GC data * * @noreference This method is not intended to be referenced by clients. */ @Override public abstract void internal_dispose_GC (long hDC, 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; } /** * Loads the font specified by a file. The font will be * present in the list of fonts available to the application. * * @param path the font file path * @return whether the font was successfully loaded * * @exception SWTException

      *
    • ERROR_NULL_ARGUMENT - if path is null
    • *
    • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
    • *
    * * @see Font * * @since 3.3 */ public boolean loadFont (String path) { checkDevice(); if (path == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); byte [] buffer = Converter.wcsToMbcs (path, true); return OS.FcConfigAppFontAddFile (0, buffer); } long logProc (long log_domain, long log_level, long message, long user_data) { if (DEBUG) { new Error ().printStackTrace (); } if (warningLevel == 0) { if (DEBUG || debug) { new Error ().printStackTrace (); } OS.g_log_default_handler (log_domain, (int)log_level, message, 0); } return 0; } void new_Object (Object object) { synchronized (trackingLock) { for (int i=0; i * When a device is destroyed, resources that were acquired * on behalf of the programmer need to be returned to the * operating system. For example, if the device allocated a * font to be used as the system font, this font would be * freed in release. Also,to assist the garbage * collector and minimize the amount of memory that is not * reclaimed when the programmer keeps a reference to a * disposed device, all fields except the handle are zero'd. * The handle is needed by destroy. *

    * This method is called before destroy. *

    * If subclasses reimplement this method, they must * call the super implementation. *

    * * @see #dispose * @see #destroy */ protected void release () { if (shellHandle != 0) { if (GTK.GTK4) { GTK4.gtk_window_destroy(shellHandle); } else { GTK3.gtk_widget_destroy(shellHandle); } } shellHandle = 0; /* Dispose the default font */ if (systemFont != null) systemFont.dispose (); systemFont = 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; if (emptyTab != 0) OS.pango_tab_array_free(emptyTab); emptyTab = 0; /* Free the GTK error and warning handler */ if (xDisplay != 0) { for (int i=0; ifalse prevents these * messages from being printed. If the argument is true then * message printing is not blocked. * * @param warnings trueif warnings should be printed, 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; if (logProc != 0) { for (int i=0; i




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy