001: /*
002: * @(#)WindowXWindow.java 1.21 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.awt;
029:
030: import java.awt.event.*;
031:
032: /** The base class for all top level windows. Top level X windows
033: have the X root window as their parent.
034: @version 1.17, 08/19/02
035: @author Nicholas Allen*/
036:
037: class WindowXWindow extends HeavyweightComponentXWindow {
038: public WindowXWindow(Window window) {
039: super (window);
040: topBorder = defaultTopBorder;
041: leftBorder = defaultLeftBorder;
042: bottomBorder = defaultBottomBorder;
043: rightBorder = defaultRightBorder;
044: }
045:
046: void create() {
047: this .windowID = create((component.parent != null && component.parent.xwindow != null) ? component.parent.xwindow.windowID
048: : 0);
049: }
050:
051: native int create(int owner);
052:
053: final native void setTitle(String title);
054:
055: final void setIconImage(Image image) {
056: if (image != null)
057: setIconImageNative(((X11Image) image).Xpixmap);
058: }
059:
060: final native void setIconImageNative(int pixmap);
061:
062: final native void toFront();
063:
064: final native void toBack();
065:
066: final synchronized void setResizable(boolean b) {
067: resizable = b;
068: if (!resizable) {
069: int width = Math.max(1, component.width - leftBorder
070: - rightBorder);
071: int height = Math.max(1, component.height - topBorder
072: - bottomBorder);
073: setSizeHintsNative(width, height, width, height);
074: } else
075: setSizeHints(component.getMinimumSize(), component
076: .getMaximumSize());
077: }
078:
079: /** Sets the event mask for the X window using the supplied event mask (see AWTEvent).
080: This lets the X server know what kinds of events we are interested in. */
081:
082: final native void setMouseEventMask(long mask);
083:
084: final synchronized void setBounds(int x, int y, int width,
085: int height) {
086: width = Math.max(1, width - leftBorder - rightBorder);
087: height = Math.max(1, height - topBorder - bottomBorder);
088: if (!resizable)
089: setSizeHintsNative(width, height, width, height);
090: setBoundsNative(x, y, width, height);
091: }
092:
093: native void setBoundsNative(int x, int y, int width, int height);
094:
095: /** Sets the hints for the window manager for the minimum/maximum size of the window.
096: The window manager may or may not respect this. */
097:
098: final synchronized void setSizeHints(Dimension minSize,
099: Dimension maxSize) {
100: if (resizable) {
101: minSize.width = Math.max(minSize.width - leftBorder
102: - rightBorder, 1);
103: minSize.height = Math.max(minSize.height - topBorder
104: - bottomBorder, 1);
105: maxSize.width = Math.max(Math.min(maxSize.width
106: - leftBorder - rightBorder, Short.MAX_VALUE),
107: minSize.width);
108: maxSize.height = Math.max(Math.min(maxSize.height
109: - topBorder - bottomBorder, Short.MAX_VALUE),
110: minSize.height);
111: setSizeHintsNative(minSize.width, minSize.height,
112: maxSize.width, maxSize.height);
113: }
114: }
115:
116: private final native void setSizeHintsNative(int minWidth,
117: int minHeight, int maxWidth, int maxHeight);
118:
119: void map() {
120: mapNative();
121: }
122:
123: /** Called by the native code when an area of the X Window is exposed.
124: This posts a paint event into Java's event queue for processing by Java
125: so that the exposed area is redrawn. */
126:
127: final void postPaintEvent(int x, int y, int width, int height) {
128: // Translate paint position to take into acount borders of window.
129: // Java expects window coordinates to be relative to the window manager's
130: // window.
131:
132: super .postPaintEvent(x + leftBorder, y + topBorder, width,
133: height);
134: }
135:
136: /** Called when a mouse event is received from the X server for this X window. We post a
137: mouse event into Java's event queue for processing. This is overriden to translate the coordinates
138: to the outer window's coordinate system. */
139:
140: void postMouseButtonEvent(int id, long when, int modifiers, int x,
141: int y) {
142: super .postMouseButtonEvent(id, when, modifiers, x + leftBorder,
143: y + topBorder);
144: }
145:
146: void postMouseEvent(int id, long when, int modifiers, int x, int y) {
147: super .postMouseEvent(id, when, modifiers, x + leftBorder, y
148: + topBorder);
149: }
150:
151: private void postWindowEvent(int id) {
152: Toolkit.getEventQueue().postEvent(
153: new WindowEvent((Window) component, id));
154: }
155:
156: private void postKeyEvent(int id, long when, int modifiers,
157: int keyCode, char keyChar) {
158: Toolkit.getEventQueue().postEvent(
159: new KeyEvent(component, id, when, modifiers, keyCode,
160: keyChar));
161: }
162:
163: /* Sets the borders of this window. This is called if the window is reparented. We store the values
164: so we can translate paint events and component positions as Java expects coordinates relative
165: to the window manager's window and not our own. These values are also used for the insets of the
166: window. */
167:
168: private synchronized void setBorders(int top, int left, int bottom,
169: int right) {
170: if (top != topBorder || left != leftBorder
171: || bottom != bottomBorder || right != rightBorder) {
172: topBorder = top;
173: leftBorder = left;
174: bottomBorder = bottom;
175: rightBorder = right;
176: // Insets will have changed so have to revalidate window
177:
178: EventQueue.invokeLater(new Runnable() {
179: public void run() {
180: component.invalidate();
181: component.validate();
182: component.repaint();
183: }
184: });
185: }
186: setDefaultBorders(top, left, bottom, right);
187: }
188:
189: void setDefaultBorders(int top, int left, int bottom, int right) {
190: defaultTopBorder = top;
191: defaultBottomBorder = bottom;
192: defaultLeftBorder = left;
193: defaultRightBorder = right;
194: }
195:
196: void postMovedEvent() {
197: Toolkit.getEventQueue().postEvent(
198: new ComponentEvent(component,
199: ComponentEvent.COMPONENT_MOVED));
200: }
201:
202: void postResizedEvent() {
203: Toolkit.getEventQueue().postEvent(
204: new ComponentEvent(component,
205: ComponentEvent.COMPONENT_RESIZED));
206: }
207:
208: private int outerWindow;
209: int topBorder, leftBorder, bottomBorder, rightBorder;
210: private boolean resizable = true;
211: private static int defaultTopBorder = 0;
212: private static int defaultLeftBorder = 0;
213: private static int defaultBottomBorder = 0;
214: private static int defaultRightBorder = 0;
215: }
|