SunToolkit.java revision 2520
2362N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A /* Load debug settings for native code */ 0N/A * Special mask for the UngrabEvent events, in addition to the 0N/A * public masks defined in AWTEvent. Should be used as the mask 0N/A * value for Toolkit.addAWTEventListener. 0N/A /* The key to put()/get() the PostEventQueue into/from the AppContext. 1224N/A * By default it's taken from the system. If system value does not 1224N/A * fit into int type range, use our own MAX_BUTTONS_SUPPORT value. 1224N/A /* XFree standard mention 24 buttons as maximum: 1224N/A * We workaround systems supporting more than 24 buttons. 1224N/A * Otherwise, we have to use long type values as masks 1224N/A * which leads to API change. 1224N/A * InputEvent.BUTTON_DOWN_MASK may contain only 21 masks due to 1224N/A * the 4-bytes limit for the int type. (CR 6799099) 1224N/A * One more bit is reserved for FIRST_HIGH_BIT. 0N/A /* If awt.threadgroup is set to class name the instance of 0N/A * this class is created (should be subclass of ThreadGroup) 0N/A * and EventDispatchThread is created inside of it 0N/A * If loaded class overrides uncaughtException instance 0N/A * handles all uncaught exception on EventDispatchThread 0N/A "java.awt.EventQueue");
0N/A * The AWT lock is typically only used on Unix platforms to synchronize 0N/A * access to Xlib, OpenGL, etc. However, these methods are implemented 0N/A * in SunToolkit so that they can be called from shared code (e.g. 0N/A * from the OGL pipeline) or from the X11 pipeline regardless of whether 0N/A * XToolkit or MToolkit is currently in use. There are native macros 0N/A * (such as AWT_LOCK) defined in awt.h, so if the implementation of these 0N/A * methods is changed, make sure it is compatible with the native macros. 0N/A * Note: The following methods (awtLock(), awtUnlock(), etc) should be 0N/A * synchronized (getAWTLock()) { 0N/A * By factoring these methods out specially, we are able to change the 0N/A * implementation of these methods (e.g. use more advanced locking 0N/A * mechanisms) without impacting calling code. 0N/A * private void doStuffWithXlib() { 0N/A * assert !SunToolkit.isAWTLockHeldByCurrentThread(); 0N/A * SunToolkit.awtLock(); 0N/A * XlibWrapper.XDoStuff(); 0N/A * SunToolkit.awtUnlock(); 0N/A * Create a new AppContext, along with its EventQueue, for a 0N/A * new ThreadGroup. Browser code, for example, would use this 0N/A * method to create an AppContext & EventQueue for an Applet. 0N/A "java.awt.EventQueue");
0N/A * Fetch the peer associated with the given target (as specified 0N/A * in the peer creation method). This can be used to determine 0N/A * things like what the parent peer is. If the target is null 0N/A * or the target can't be found (either because the a peer was 0N/A * never created for it or the peer was disposed), a null will 0N/A // WeakHashMap<Component,AppContext> 0N/A * Sets the appContext field of target. If target is not a Component or 0N/A * MenuComponent, this returns false. 0N/A * Returns the appContext field for target. If target is not a 0N/A * Component or MenuComponent this returns null. 0N/A * Fetch the AppContext associated with the given target. 0N/A * This can be used to determine things like which EventQueue 0N/A * to use for posting events to a Component. If the target is 0N/A * null or the target can't be found, a null with be returned. 0N/A * Sets the synchronous status of focus requests on lightweight 0N/A * components in the specified window to the specified value. 0N/A * If the boolean parameter is <code>true</code> then the focus 0N/A * requests on lightweight components will be performed 0N/A * synchronously, if it is <code>false</code>, then asynchronously. 0N/A * By default, all windows have their lightweight request status 0N/A * set to asynchronous. 0N/A * The application can only set the status of lightweight focus 0N/A * requests to synchronous for any of its windows if it doesn't 0N/A * perform focus transfers between different heavyweight containers. 0N/A * In this case the observable focus behaviour is the same as with 0N/A * asynchronous status. 0N/A * If the application performs focus transfer between different 0N/A * heavyweight containers and sets the lightweight focus request 0N/A * status to synchronous for any of its windows, then further focus 0N/A * behaviour is unspecified. 0N/A * @param w window for which the lightweight focus request status 0N/A * @param status the value of lightweight focus request status 0N/A // if this is not XAWT then use default policy 0N/A // because Swing change it 0N/A // Policy was changed 0N/A // Check if it is awt policy or swing policy 0N/A // If it is Swing policy we shouldn't use it in AWT frames 0N/A // If it is AWT policy we shouldn't use it in Swing frames 0N/A // Otherwise we should use this policy 0N/A // Can't use AWT policy in Swing windows - should use Swing's one. 0N/A // New Swing's policy 0N/A // Policy is default, use different default policy for swing 0N/A * Insert a mapping from target to AppContext, for later retrieval 0N/A * via targetToAppContext() above. 0N/A * Post an AWTEvent to the Java EventQueue, using the PostEventQueue 0N/A * to avoid possibly calling client code (EventQueueSubclass.postEvent()) 0N/A * not be called under another lock since it locks the EventQueue. 0N/A * See bugids 4632918, 4526597. 0N/A * Post AWTEvent of high priority. 0N/A * Flush any pending events which haven't been posted to the AWT 2518N/A // Don't call flushPendingEvents() recursively 0N/A * Execute a chunk of code on the Java event handler thread for the 0N/A * given target. Does not wait for the execution to occur before 0N/A * returning to the caller. 0N/A * Fixed 5064013: the InvocationEvent time should be equals 0N/A * the time of the ActionEvent 0N/A * Execute a chunk of code on the Java event handler thread for the 0N/A * given target. Does not wait for the execution to occur before 0N/A * returning to the caller. 0N/A * Execute a chunk of code on the Java event handler thread. The 0N/A * method takes into account provided AppContext and sets 0N/A * <code>SunToolkit.getDefaultToolkit()</code> as a target of the 0N/A * event. See 6451487 for detailes. 0N/A * Does not wait for the execution to occur before returning to 0N/A * Execute a chunk of code on the Java event handler thread for the 0N/A * given target. Waits for the execution to occur before returning 0N/A throw new Error(
"Cannot call executeOnEDTAndWait from any event dispatcher thread");
0N/A * Returns true if the calling thread is the event dispatch thread 0N/A * contained within AppContext which associated with the given target. 0N/A * Use this call to ensure that a given task is being executed 0N/A * (or not being) on the event dispatch thread for the given target. 0N/A // -- Obsolete font names from 1.0.2. It was decided that 0N/A // -- getFontList should not return these old names: 0N/A // "Helvetica", "TimesRoman", "Courier", "ZapfDingbats" 1045N/A * Disables erasing of background on the canvas before painting if 1045N/A * this is supported by the current toolkit. It is recommended to 1045N/A * call this method early, before the Canvas becomes displayable, 1045N/A * because some Toolkit implementations do not support changing 1045N/A * this property once the Canvas becomes displayable. 1045N/A * Disables the native erasing of the background on the given 1045N/A * component before painting if this is supported by the current 1045N/A * toolkit. This only has an effect for certain components such as 1045N/A * Canvas, Panel and Window. It is recommended to call this method 1045N/A * early, before the Component becomes displayable, because some 1045N/A * Toolkit implementations do not support changing this property 1045N/A * once the Component becomes displayable. 0N/A * Returns the value of "sun.awt.noerasebackground" property. Default 0N/A * value is {@code false}. 0N/A * Returns the value of "sun.awt.erasebackgroundonresize" property. Default 0N/A * value is {@code false}. 0N/A // security managers 0N/A // security managers 0N/A if (w ==
0 || h ==
0) {
0N/A if (w ==
0 || h ==
0) {
0N/A // Must be a ToolkitImage 0N/A * Scans {@code imageList} for best-looking image of specified dimensions. 0N/A * Image can be scaled and/or padded with transparency. 0N/A //Iterate imageList looking for best matching image. 0N/A //'Similarity' measure is defined as good scale factor and small insets. 0N/A //best possible similarity is 0 (no scale, no insets). 0N/A //It's found while the experiments that good-looking result is achieved 0N/A //with scale factors x1, x3/4, x2/3, xN, x1/N. 1696N/A "Skipping the image passed into Java because it's null.");
1696N/A "Perhaps the image passed into Java is broken. Skipping this icon.");
0N/A //Calculate scaled image dimensions 0N/A //adjusting scale factor to nearest "good" value 0N/A //Need to enlarge image more than twice 0N/A //Round down scale factor to multiply by integer value 0N/A //Multiply size by 1/scaleDivider 0N/A //where scaleDivider is minimum possible integer 0N/A //larger than 1/scaleFactor 0N/A //No images were found, possibly all are broken 0N/A " x : " + x +
" y : " + y);
1696N/A "Perhaps the image passed into Java is broken. Skipping this icon.");
0N/A // Package private implementation 0N/A * Give native peers the ability to query the native container 0N/A * given a native component (eg the direct parent may be lightweight). 1976N/A * Gives native peers the ability to query the closest HW component. 1976N/A * If the given component is heavyweight, then it returns this. Otherwise, 1976N/A * it goes one level up in the hierarchy and tests next component. 0N/A * Returns a new input method window, with behavior as specified in 0N/A * {@link java.awt.im.spi.InputMethodContext#createInputMethodWindow}. 0N/A * If the inputContext is not null, the window should return it from its 0N/A * getInputContext() method. The window needs to implement 0N/A * sun.awt.im.InputMethodWindow. 0N/A * SunToolkit subclasses can override this method to return better input 0N/A * Returns whether enableInputMethods should be set to true for peered 0N/A * TextComponent instances on this platform. False by default. 0N/A * Returns the locale in which the runtime was started. 0N/A // for compatibility, check for old user.region property 0N/A // region can be of form country, country_variant, or _variant 0N/A * Returns the default keyboard locale of the underlying operating system 0N/A // Support for window closing event notifications 0N/A * @see sun.awt.WindowClosingSupport#getWindowClosingListener 0N/A * @see sun.awt.WindowClosingSupport#setWindowClosingListener 0N/A * @see sun.awt.WindowClosingListener#windowClosingNotify 0N/A * @see sun.awt.WindowClosingListener#windowClosingDelivered 0N/A * Returns whether default toolkit needs the support of the xembed 0N/A * from embedding host(if any). 0N/A * @return <code>true</code>, if XEmbed is needed, <code>false</code> otherwise 0N/A // SunToolkit descendants should override this method to specify 0N/A // concrete behavior 0N/A // Non-SunToolkit doubtly might support XEmbed 0N/A * Returns whether this toolkit needs the support of the xembed 0N/A * from embedding host(if any). 0N/A * @return <code>true</code>, if XEmbed is needed, <code>false</code> otherwise 0N/A * Returns whether the XEmbed server feature is requested by 0N/A * developer. If true, Toolkit should return an 0N/A * XEmbed-server-enabled CanvasPeer instead of the ordinary CanvasPeer. 0N/A * Returns whether the modal exclusion API is supported by the current toolkit. 0N/A * When it isn't supported, calling <code>setModalExcluded</code> has no 0N/A * effect, and <code>isModalExcluded</code> returns false for all windows. 0N/A * @return true if modal exclusion is supported by the toolkit, false otherwise 0N/A * @see sun.awt.SunToolkit#setModalExcluded(java.awt.Window) 0N/A * @see sun.awt.SunToolkit#isModalExcluded(java.awt.Window) 0N/A * Default implementation for isModalExcludedSupportedImpl(), returns false. 0N/A * @see sun.awt.windows.WToolkit#isModalExcludeSupportedImpl 0N/A * @see sun.awt.X11.XToolkit#isModalExcludeSupportedImpl 0N/A * Sets this window to be excluded from being modally blocked. When the 0N/A * toolkit supports modal exclusion and this method is called, input 0N/A * events, focus transfer and z-order will continue to work for the 0N/A * window, it's owned windows and child components, even in the 0N/A * presence of a modal dialog. 0N/A * For details on which <code>Window</code>s are normally blocked 0N/A * by modal dialog, see {@link java.awt.Dialog}. 0N/A * Invoking this method when the modal exclusion API is not supported by 0N/A * the current toolkit has no effect. 0N/A * @param window Window to be marked as not modally blocked 0N/A * @see java.awt.Dialog 0N/A * @see java.awt.Dialog#setModal(boolean) 0N/A * @see sun.awt.SunToolkit#isModalExcludedSupported 0N/A * @see sun.awt.SunToolkit#isModalExcluded(java.awt.Window) 0N/A * Returns whether the specified window is blocked by modal dialogs. 0N/A * If the modal exclusion API isn't supported by the current toolkit, 0N/A * it returns false for all windows. 0N/A * @param window Window to test for modal exclusion 0N/A * @return true if the window is modal excluded, false otherwise. If 0N/A * the modal exclusion isn't supported by the current Toolkit, false 0N/A * @see sun.awt.SunToolkit#isModalExcludedSupported 0N/A * @see sun.awt.SunToolkit#setModalExcluded(java.awt.Window) 0N/A * Overridden in XToolkit and WToolkit 0N/A * Overridden in XToolkit and WToolkit 0N/A /////////////////////////////////////////////////////////////////////////// 0N/A // The following is used by the Java Plug-in to coordinate dialog modality 0N/A // between containing applications (browsers, ActiveX containers etc) and 0N/A /////////////////////////////////////////////////////////////////////////// 0N/A }
// end of class ModalityListenerList 0N/A /////////////////////////////////////////////////////////////////////////// 0N/A /////////////////////////////////////////////////////////////////////////// 0N/A * Parameterless version of realsync which uses default timout (see DEFAUL_WAIT_TIME). 0N/A * Forces toolkit to synchronize with the native windowing 0N/A * sub-system, flushing all pending work and waiting for all the 0N/A * events to be processed. This method guarantees that after 0N/A * return no additional Java events will be generated, unless 0N/A * cause by user. Obviously, the method cannot be used on the 0N/A * event dispatch thread (EDT). In case it nevertheless gets 0N/A * invoked on this thread, the method throws the 0N/A * IllegalThreadException runtime exception. 0N/A * <p> This method allows to write tests without explicit timeouts 0N/A * or wait for some event. Example: 0N/A * f.setVisible(true); 0N/A * ((SunToolkit)Toolkit.getDefaultToolkit()).realSync(); 0N/A * <p> After realSync, <code>f</code> will be completely visible 0N/A * on the screen, its getLocationOnScreen will be returning the 0N/A * right result and it will be the focus owner. 0N/A * <p> Another example: 0N/A * ((SunToolkit)Toolkit.getDefaultToolkit()).realSync(); 0N/A * <p> After realSync, <code>b</code> will be focus owner. 0N/A * <p> Notice that realSync isn't guaranteed to work if recurring 0N/A * actions occur, such as if during processing of some event 0N/A * another request which may generate some events occurs. By 0N/A * default, sync tries to perform as much as {@value MAX_ITERS} 0N/A * cycles of event processing, allowing for roughly {@value 0N/A * MAX_ITERS} additional requests. 0N/A * <p> For example, requestFocus() generates native request, which 0N/A * generates one or two Java focus events, which then generate a 0N/A * serie of paint events, a serie of Java focus events, which then 0N/A * generate a serie of paint events which then are processed - 0N/A * three cycles, minimum. 0N/A * @param timeout the maximum time to wait in milliseconds, negative means "forever". 0N/A // Let's do sync first 0N/A // During the wait process, when we were processing incoming 0N/A // events, we could have made some new request, which can 0N/A // Therefore, we dispatch them as long as there is something 0N/A // native requests were dispatched by X/Window Manager or Windows 0N/A // Moreover, we processed them all on Toolkit thread 0N/A // Now wait while EDT processes them. 0N/A // During processing of some events (focus, for example), 0N/A // some other events could have been generated. So, after 0N/A // waitForIdle, we may end up with full EventQueue 0N/A // Again, for Java events, it was simple to check for new Java 0N/A // events by checking event queue, but what if Java events 0N/A // resulted in native requests? Therefor, check native events again. 0N/A * Platform toolkits need to implement this method to perform the 0N/A * sync of the native queue. The method should wait until native 0N/A * requests are processed, all native events are processed and 0N/A * corresponding Java events are generated. Should return 0N/A * <code>true</code> if some events were processed, 0N/A * <code>false</code> otherwise. 0N/A * Waits for the Java event queue to empty. Ensures that all 0N/A * events are processed (including paint events), and that if 0N/A * recursive events were generated, they are also processed. 0N/A * Should return <code>true</code> if more processing is 0N/A * necessary, <code>false</code> otherwise. 0N/A // Here we block EDT. It could have some 0N/A // events, it should have dispatched them by 0N/A // now. So native requests could have been 0N/A // generated. First, dispatch them. Then, 0N/A // flush Java events again. 0N/A // Lock to force write-cache flush for queueEmpty. 0N/A * Grabs the mouse input for the given window. The window must be 0N/A * visible. The window or its children do not receive any 0N/A * additional mouse events besides those targeted to them. All 0N/A * other events will be dispatched as before - to the respective 0N/A * targets. This Window will receive UngrabEvent when automatic 0N/A * ungrab is about to happen. The event can be listened to by 0N/A * installing AWTEventListener with WINDOW_EVENT_MASK. See 0N/A * UngrabEvent class for the list of conditions when ungrab is 0N/A * Forces ungrab. No event will be sent. 0N/A * Locates the splash screen library in a platform dependent way and closes 0N/A * the splash screen. Should be invoked on first top-level frame display. 0N/A * @see java.awt.SplashScreen 0N/A /* The following methods and variables are to support retrieving 0N/A * desktop text anti-aliasing settings 0N/A /* Need an instance method because setDesktopProperty(..) is protected. */ 0N/A /* Since Swing is the reason for this "extra condition" logic its 0N/A * worth documenting it in some detail. 0N/A * First, a goal is for Swing and applications to both retrieve and 0N/A * use the same desktop property value so that there is complete 0N/A * consistency between the settings used by JDK's Swing implementation 0N/A * and 3rd party custom Swing components, custom L&Fs and any general 0N/A * text rendering that wants to be consistent with these. 0N/A * But by default on Solaris & Linux Swing will not use AA text over 0N/A * remote X11 display (unless Xrender can be used which is TBD and may not 0N/A * always be available anyway) as that is a noticeable performance hit. 0N/A * So there needs to be a way to express that extra condition so that 0N/A * it is seen by all clients of the desktop property API. 0N/A * If this were the only condition it could be handled here as it would 0N/A * be the same for any L&F and could reasonably be considered to be 0N/A * a static behaviour of those systems. 0N/A * But GTK currently has an additional test based on locale which is 0N/A * not applied by Metal. So mixing GTK in a few locales with Metal 0N/A * would mean the last one wins. 0N/A * This could be stored per-app context which would work 0N/A * for different applets, but wouldn't help for a single application 0N/A * using GTK and some other L&F concurrently. 0N/A * But it is expected this will be addressed within GTK and the font 0N/A * system so is a temporary and somewhat unlikely harmless corner case. 0N/A /* Someone already asked for this info, under a different 0N/A * We'll force re-evaluation instead of replicating the 0N/A * logic, then notify any listeners of any change. 0N/A /* "false", "off", ""default" aren't explicitly tested, they 0N/A * just fall through to produce a null return which all are equated to 0N/A /* This method determines whether to use the system font settings, 0N/A * or ignore them if a L&F has specified they should be ignored, or 0N/A * to override both of these with a system property specified value. 0N/A * If the toolkit isn't a SunToolkit, (eg may be headless) then that 0N/A * system property isn't applied as desktop properties are considered 0N/A * to be inapplicable in that case. In that headless case although 0N/A * this method will return "true" the toolkit will return a null map. 0N/A /* If it is anything other than "true", then it may be 0N/A * a hint name , or it may be "off, "default", etc. 0N/A /* If its still true, apply the extra condition */ 0N/A /* A variable defined for the convenience of JDK code */ 0N/A /* Subclass desktop property loading methods call this which 0N/A * in turn calls the appropriate subclass implementation of 0N/A * getDesktopAAHints() when system settings are being used. 0N/A * Its public rather than protected because subclasses may delegate 0N/A * to a helper class. 0N/A }
else {
/* Headless Toolkit */ 0N/A /* cloning not necessary as the return value is cloned later, but 0N/A * consumeNextKeyTyped() method is not currently used, 0N/A * however Swing could use it in the future. 0N/A "consumeNextKeyTyped",
1045N/A * Returns the <code>Window</code> ancestor of the component <code>comp</code>. 1045N/A * @return Window ancestor of the component or component by itself if it is Window; 1045N/A * null, if component is not a part of window hierarchy 2520N/A * Returns the value of the system property indicated by the specified key. 2520N/A * Returns the boolean value of the system property indicated by the specified key. 886N/A * Returns the value of "sun.awt.disableMixing" property. Default 886N/A * value is {@code false}. 0N/A * Returns true if the native GTK libraries are available. The 0N/A * default implementation returns false, but UNIXToolkit overrides this 0N/A * method to provide a more specific answer. 1045N/A * Returns whether or not a containing top level window for the passed 1163N/A * {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT PERPIXEL_TRANSLUCENT}. 1045N/A * @param c a Component which toplevel's to check 1045N/A * @return {@code true} if the passed component is not null and has a 1045N/A * containing toplevel window which is opaque (so per-pixel translucency 1045N/A * is not enabled), {@code false} otherwise 1163N/A * @see GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT 1045N/A * Returns whether or not a containing top level window for the passed 1163N/A * {@link GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT}. 1045N/A * @param c a Component which toplevel's to check 1045N/A * @return {@code true} if the passed component is not null and has a 1045N/A * containing toplevel window which has opacity less than 1045N/A * 1.0f (which means that it is translucent), {@code false} otherwise 1163N/A * @see GraphicsDevice.WindowTranslucency#TRANSLUCENT 1045N/A * Returns whether the native system requires using the peer.updateWindow() 1045N/A * method to update the contents of a non-opaque window, or if usual 1045N/A * painting procedures are sufficient. The default return value covers 1045N/A * the X11 systems. On MS Windows this method is overriden in WToolkit 1224N/A * Descendants of the SunToolkit should override and put their own logic here. 1338N/A * Note that using the instanceof operator causes a class to be loaded. 1338N/A * Using this method doesn't load a class and it can be used instead of 1338N/A * the instanceof operator for performance reasons. 1338N/A * @param obj Object to be checked 0N/A}
// class SunToolkit 0N/A * PostEventQueue is a Thread that runs in the same AppContext as the 0N/A * Java EventQueue. It is a queue of AWTEvents to be posted to the 0N/A * events to this queue, which then calls EventQueue.postEvent(). 0N/A * We do this because EventQueue.postEvent() may be overridden by client 0N/A * code, and we mustn't ever call client code from the toolkit thread. 2518N/A * Continually post pending AWTEvents to the Java EventQueue. The method 2518N/A * is synchronized to ensure the flush is completed before a new event 2518N/A * can be posted to this queue. 0N/A * Enqueue an AWTEvent to be posted to the Java EventQueue. 0N/A synchronized (
this) {
0N/A}
// class PostEventQueue