001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.awt.Dimension;
029: import java.awt.GraphicsEnvironment;
030: import java.awt.Point;
031: import java.awt.Rectangle;
032:
033: import java.util.Collections;
034: import java.util.HashSet;
035: import java.util.Set;
036:
037: import sun.awt.X11GraphicsConfig;
038: import sun.awt.X11GraphicsDevice;
039: import sun.awt.X11GraphicsEnvironment;
040:
041: /*
042: * This class is a collection of utility methods that operate
043: * with native windows.
044: */
045: public class XlibUtil {
046: /**
047: * The constructor is made private to eliminate any
048: * instances of this class
049: */
050: private XlibUtil() {
051: }
052:
053: /**
054: * Xinerama-aware version of XlibWrapper.RootWindow method.
055: */
056: public static long getRootWindow(int screenNumber) {
057: XToolkit.awtLock();
058: try {
059: X11GraphicsEnvironment x11ge = (X11GraphicsEnvironment) GraphicsEnvironment
060: .getLocalGraphicsEnvironment();
061: if (x11ge.runningXinerama()) {
062: // all the Xinerama windows share the same root window
063: return XlibWrapper.RootWindow(XToolkit.getDisplay(), 0);
064: } else {
065: return XlibWrapper.RootWindow(XToolkit.getDisplay(),
066: screenNumber);
067: }
068: } finally {
069: XToolkit.awtUnlock();
070: }
071: }
072:
073: /**
074: * Checks if the given window is a root window for the given screen
075: */
076: static boolean isRoot(long rootCandidate, long screenNumber) {
077: long root;
078:
079: XToolkit.awtLock();
080: try {
081: root = XlibWrapper.RootWindow(XToolkit.getDisplay(),
082: screenNumber);
083: } finally {
084: XToolkit.awtUnlock();
085: }
086:
087: return root == rootCandidate;
088: }
089:
090: /**
091: * Returns the bounds of the given window, in absolute coordinates
092: */
093: static Rectangle getWindowGeometry(long window) {
094: XToolkit.awtLock();
095: try {
096: int res = XlibWrapper.XGetGeometry(XToolkit.getDisplay(),
097: window, XlibWrapper.larg1, // root_return
098: XlibWrapper.larg2, // x_return
099: XlibWrapper.larg3, // y_return
100: XlibWrapper.larg4, // width_return
101: XlibWrapper.larg5, // height_return
102: XlibWrapper.larg6, // border_width_return
103: XlibWrapper.larg7); // depth_return
104: if (res == 0) {
105: return null;
106: }
107:
108: int x = Native.getInt(XlibWrapper.larg2);
109: int y = Native.getInt(XlibWrapper.larg3);
110: long width = Native.getUInt(XlibWrapper.larg4);
111: long height = Native.getUInt(XlibWrapper.larg5);
112:
113: return new Rectangle(x, y, (int) width, (int) height);
114: } finally {
115: XToolkit.awtUnlock();
116: }
117: }
118:
119: /**
120: * Translates the given point from one window to another. Returns
121: * null if the translation is failed
122: */
123: static Point translateCoordinates(long src, long dst, Point p) {
124: Point translated = null;
125:
126: XToolkit.awtLock();
127: try {
128: XTranslateCoordinates xtc = new XTranslateCoordinates(src,
129: dst, p.x, p.y);
130: try {
131: int status = xtc
132: .execute(XToolkit.IgnoreBadWindowHandler);
133: if ((status != 0)
134: && ((XToolkit.saved_error == null) || (XToolkit.saved_error
135: .get_error_code() == XlibWrapper.Success))) {
136: translated = new Point(xtc.get_dest_x(), xtc
137: .get_dest_y());
138: }
139: } finally {
140: xtc.dispose();
141: }
142: } finally {
143: XToolkit.awtUnlock();
144: }
145:
146: return translated;
147: }
148:
149: /**
150: * Translates the given rectangle from one window to another.
151: * Returns null if the translation is failed
152: */
153: static Rectangle translateCoordinates(long src, long dst,
154: Rectangle r) {
155: Point translatedLoc = translateCoordinates(src, dst, r
156: .getLocation());
157: if (translatedLoc == null) {
158: return null;
159: } else {
160: return new Rectangle(translatedLoc, r.getSize());
161: }
162: }
163:
164: /**
165: * Returns the parent for the given window
166: */
167: static long getParentWindow(long window) {
168: XToolkit.awtLock();
169: try {
170: XBaseWindow bw = XToolkit.windowToXWindow(window);
171: if (bw != null) {
172: XBaseWindow pbw = bw.getParentWindow();
173: if (pbw != null) {
174: return pbw.getWindow();
175: }
176: }
177:
178: XQueryTree qt = new XQueryTree(window);
179: try {
180: if (qt.execute() == 0) {
181: return 0;
182: } else {
183: return qt.get_parent();
184: }
185: } finally {
186: qt.dispose();
187: }
188: } finally {
189: XToolkit.awtUnlock();
190: }
191: }
192:
193: /**
194: * Returns all the children for the given window
195: */
196: static Set<Long> getChildWindows(long window) {
197: XToolkit.awtLock();
198: try {
199: XBaseWindow bw = XToolkit.windowToXWindow(window);
200: if (bw != null) {
201: return bw.getChildren();
202: }
203:
204: XQueryTree xqt = new XQueryTree(window);
205: try {
206: int status = xqt.execute();
207: if (status == 0) {
208: return Collections.emptySet();
209: }
210:
211: long children = xqt.get_children();
212:
213: if (children == 0) {
214: return Collections.emptySet();
215: }
216:
217: int childrenCount = xqt.get_nchildren();
218:
219: Set<Long> childrenSet = new HashSet<Long>(childrenCount);
220: for (int i = 0; i < childrenCount; i++) {
221: childrenSet.add(Native.getWindow(children, i));
222: }
223:
224: return childrenSet;
225: } finally {
226: xqt.dispose();
227: }
228: } finally {
229: XToolkit.awtUnlock();
230: }
231: }
232:
233: /**
234: * Checks if the given window is a Java window and is an
235: * instance of XWindowPeer
236: */
237: static boolean isXAWTToplevelWindow(long window) {
238: return XToolkit.windowToXWindow(window) instanceof XWindowPeer;
239: }
240:
241: /**
242: * NOTICE: Right now returns only decorated top-levels (not Window)
243: */
244: static boolean isToplevelWindow(long window) {
245: if (XToolkit.windowToXWindow(window) instanceof XDecoratedPeer) {
246: return true;
247: }
248:
249: XToolkit.awtLock();
250: try {
251: WindowPropertyGetter wpg = new WindowPropertyGetter(window,
252: XWM.XA_WM_STATE, 0, 1, false, XWM.XA_WM_STATE);
253: try {
254: wpg.execute(XToolkit.IgnoreBadWindowHandler);
255: if (wpg.getActualType() == XWM.XA_WM_STATE.getAtom()) {
256: return true;
257: }
258: } finally {
259: wpg.dispose();
260: }
261:
262: return false;
263: } finally {
264: XToolkit.awtUnlock();
265: }
266: }
267:
268: /**
269: * The same as isToplevelWindow(window), but doesn't treat
270: * XEmbeddedFramePeer as toplevel.
271: */
272: static boolean isTrueToplevelWindow(long window) {
273: if (XToolkit.windowToXWindow(window) instanceof XEmbeddedFramePeer) {
274: return false;
275: }
276:
277: return isToplevelWindow(window);
278: }
279:
280: static int getWindowMapState(long window) {
281: XToolkit.awtLock();
282: XWindowAttributes wattr = new XWindowAttributes();
283: try {
284: XToolkit
285: .WITH_XERROR_HANDLER(XToolkit.IgnoreBadWindowHandler);
286: int status = XlibWrapper.XGetWindowAttributes(XToolkit
287: .getDisplay(), window, wattr.pData);
288: XToolkit.RESTORE_XERROR_HANDLER();
289: if ((status != 0)
290: && ((XToolkit.saved_error == null) || (XToolkit.saved_error
291: .get_error_code() == XlibWrapper.Success))) {
292: return wattr.get_map_state();
293: }
294: } finally {
295: wattr.dispose();
296: XToolkit.awtUnlock();
297: }
298:
299: return XlibWrapper.IsUnmapped;
300: }
301: }
|