package sun.awt.motif;
import java.util.Vector;
import java.awt.*;
import java.awt.peer.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import sun.awt.image.ImageRepresentation;
import sun.awt.motif.MInputMethod;
import sun.awt.motif.MInputMethodControl;
import sun.awt.im.*;
import sun.awt.DisplayChangedListener;
import sun.awt.SunToolkit;
import sun.awt.X11GraphicsDevice;
class MWindowPeer extends MPanelPeer implements WindowPeer,
DisplayChangedListener {
Insets insets = new Insets( 0, 0, 0, 0 );
MWindowAttributes winAttr;
static Vector allWindows = new Vector();
int iconWidth = -1;
int iconHeight = -1;
int dropTargetCount = 0;
boolean alwaysOnTop;
native void pCreate(MComponentPeer parent, String targetClassName, boolean isFocusableWindow);
native void pShow();
native void pToFront();
native void pShowModal(boolean isModal);
native void pHide();
native void pReshape(int x, int y, int width, int height);
native void pDispose();
native void pSetTitle(String title);
public native void setState(int state);
public native int getState();
public native void setResizable(boolean resizable);
native void addTextComponentNative(MComponentPeer tc);
native void removeTextComponentNative();
native void pSetIMMOption(String option);
native void (MMenuBarPeer mbpeer);
native void setSaveUnder(boolean state);
native void registerX11DropTarget(Component target);
native void unregisterX11DropTarget(Component target);
native void updateAlwaysOnTop(boolean isAlwaysOnTop);
private static native void initIDs();
static {
initIDs();
}
private static int getInset(final String name, final int def) {
Integer tmp = (Integer) java.security.AccessController.doPrivileged(
new sun.security.action.GetIntegerAction(name, def));
return tmp.intValue();
}
MWindowPeer() {
insets = new Insets(0,0,0,0);
winAttr = new MWindowAttributes();
}
MWindowPeer(Window target) {
this();
init(target);
allWindows.addElement(this);
}
void create(MComponentPeer parent) {
pCreate(parent, target.getClass().getName(), ((Window)target).isFocusableWindow());
}
void init( Window target ) {
if ( winAttr.nativeDecor == true ) {
insets.top = getInset("awt.frame.topInset", -1);
insets.left = getInset("awt.frame.leftInset", -1);
insets.bottom = getInset("awt.frame.bottomInset", -1);
insets.right = getInset("awt.frame.rightInset", -1);
}
Rectangle bounds = target.getBounds();
sysX = bounds.x;
sysY = bounds.y;
sysW = bounds.width;
sysH = bounds.height;
super.init(target);
InputMethodManager imm = InputMethodManager.getInstance();
String menuString = imm.getTriggerMenuString();
if (menuString != null)
{
pSetIMMOption(menuString);
}
pSetTitle(winAttr.title);
setResizable(winAttr.isResizable);
setSaveUnder(true);
Font f = target.getFont();
if (f == null) {
f = defaultFont;
target.setFont(f);
setFont(f);
}
Color c = target.getBackground();
if (c == null) {
target.setBackground(SystemColor.window);
setBackground(SystemColor.window);
}
c = target.getForeground();
if (c == null) {
target.setForeground(SystemColor.windowText);
setForeground(SystemColor.windowText);
}
alwaysOnTop = ((Window)target).isAlwaysOnTop() && ((Window)target).isAlwaysOnTopSupported();
GraphicsConfiguration gc = getGraphicsConfiguration();
((X11GraphicsDevice)gc.getDevice()).addDisplayChangedListener(this);
}
public void updateIconImages() {
if (this instanceof MFramePeer) {
((MFramePeer)this).setIconImage(((Frame)target).getIconImage());
}
}
public void updateMinimumSize() {
}
protected void disposeImpl() {
allWindows.removeElement(this);
super.disposeImpl();
}
public native void toBack();
public void setAlwaysOnTop(boolean alwaysOnTop) {
this.alwaysOnTop = alwaysOnTop;
updateAlwaysOnTop(alwaysOnTop);
}
public void updateAlwaysOnTopState() {
setAlwaysOnTop(((Window)target).isAlwaysOnTop());
}
public void toFront() {
if (target.isVisible()) {
updateFocusableWindowState();
pToFront();
}
}
public void updateFocusableWindowState() {
setFocusableWindow(((Window)target).isFocusableWindow());
}
native void setFocusableWindow(boolean value);
public void setVisible( boolean b ) {
if (b) {
updateFocusableWindowState();
}
super.setVisible(b);
updateAlwaysOnTop(alwaysOnTop);
}
public Insets getInsets() {
return insets;
}
public void handleQuit() {
postEvent(new WindowEvent((Window)target, WindowEvent.WINDOW_CLOSING));
}
public void handleDestroy() {
final Window target = (Window)this.target;
SunToolkit.executeOnEventHandlerThread(target,
new Runnable() {
public void run() {
target.dispose();
}
});
}
public void handleIconify() {
postEvent(new WindowEvent((Window)target, WindowEvent.WINDOW_ICONIFIED));
}
public void handleDeiconify() {
postEvent(new WindowEvent((Window)target, WindowEvent.WINDOW_DEICONIFIED));
}
public void handleStateChange(int oldState, int newState) {
postEvent(new WindowEvent((Window)target,
WindowEvent.WINDOW_STATE_CHANGED,
oldState, newState));
}
public void handleResize(int width, int height) {
sysW = width;
sysH = height;
if (!tcList.isEmpty() &&
!imList.isEmpty()){
int i;
for (i = 0; i < imList.size(); i++){
((MInputMethod)imList.elementAt(i)).configureStatus();
}
}
validateSurface(width, height);
postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED));
}
public Insets insets() {
return getInsets();
}
public void handleMoved(int x, int y) {
sysX = x;
sysY = y;
postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_MOVED));
}
private native AWTEvent wrapInSequenced(AWTEvent event);
public void handleWindowFocusIn() {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS);
postEvent(wrapInSequenced((AWTEvent) we));
}
public void handleWindowFocusOut(Window oppositeWindow) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS,
oppositeWindow);
postEvent(wrapInSequenced((AWTEvent) we));
}
private Vector imList = new Vector();
private Vector tcList = new Vector();
void notifyIMMOptionChange(){
InputMethodManager.getInstance().notifyChangeRequest(target);
}
public void addInputMethod(MInputMethod im) {
if (!imList.contains(im))
imList.addElement(im);
}
public void removeInputMethod(MInputMethod im) {
if (imList.contains(im))
imList.removeElement(im);
}
public void addTextComponent(MComponentPeer tc) {
if (tcList.contains(tc))
return;
if (tcList.isEmpty()){
addTextComponentNative(tc);
if (!imList.isEmpty()) {
for (int i = 0; i < imList.size(); i++) {
((MInputMethod)imList.elementAt(i)).reconfigureXIC((MInputMethodControl)this);
}
}
MToolkit.executeOnEventHandlerThread(target, new Runnable() {
public void run() {
synchronized(target.getTreeLock()) {
target.doLayout();
}
}
});
}
tcList.addElement(tc);
}
public void removeTextComponent(MComponentPeer tc) {
if (!tcList.contains(tc))
return;
tcList.removeElement(tc);
if (tcList.isEmpty()){
removeTextComponentNative();
if (!imList.isEmpty()) {
for (int i = 0; i < imList.size(); i++) {
((MInputMethod)imList.elementAt(i)).reconfigureXIC((MInputMethodControl)this);
}
}
target.doLayout();
}
}
public MComponentPeer getTextComponent() {
if (!tcList.isEmpty()) {
return (MComponentPeer)tcList.firstElement();
} else {
return null;
}
}
boolean hasDecorations(int decor) {
if (!winAttr.nativeDecor) {
return false;
}
else {
int myDecor = winAttr.decorations;
boolean hasBits = ((myDecor & decor) == decor);
if ((myDecor & MWindowAttributes.AWT_DECOR_ALL) != 0)
return !hasBits;
else
return hasBits;
}
}
public boolean checkNativePaintOnSetBounds(int width, int height) {
return (width > oldWidth) || (height > oldHeight);
}
native void resetTargetGC(Component target);
public void draggedToNewScreen(int screenNum) {
final int finalScreenNum = screenNum;
SunToolkit.executeOnEventHandlerThread((Component)target, new Runnable()
{
public void run() {
displayChanged(finalScreenNum);
}
});
}
public void displayChanged(int screenNum) {
resetLocalGC(screenNum);
resetTargetGC(target);
super.displayChanged(screenNum);
}
private void executeDisplayChangedOnEDT(int screenNum) {
final int finalScreenNum = screenNum;
Runnable dc = new Runnable() {
public void run() {
displayChanged(finalScreenNum);
}
};
SunToolkit.executeOnEventHandlerThread((Component)target, dc);
}
public void displayChanged() {
GraphicsConfiguration gc = getGraphicsConfiguration();
int curScreenNum = ((X11GraphicsDevice)gc.getDevice()).getScreen();
executeDisplayChangedOnEDT(curScreenNum);
}
public void paletteChanged() {
}
public synchronized void addDropTarget() {
if (dropTargetCount == 0) {
registerX11DropTarget(target);
}
dropTargetCount++;
}
public synchronized void removeDropTarget() {
dropTargetCount--;
if (dropTargetCount == 0) {
unregisterX11DropTarget(target);
}
}
protected synchronized void updateDropTarget() {
if (dropTargetCount > 0) {
unregisterX11DropTarget(target);
registerX11DropTarget(target);
}
}
public boolean requestWindowFocus() {
return false;
}
public void setModalBlocked(Dialog blocker, boolean blocked) {
}
public void postUngrabEvent() {
postEvent(new sun.awt.UngrabEvent((Window)target));
}
boolean isOwnerOf(MComponentPeer child) {
if (child == null) return false;
Component comp = child.target;
while (comp != null && !(comp instanceof Window)) {
comp = getParent_NoClientCode(comp);
}
if (!(comp instanceof Window)) {
return false;
}
while (comp != null && !(comp == target) && !(comp instanceof Dialog)) {
comp = getParent_NoClientCode(comp);
}
return (comp == target);
}
boolean processUngrabMouseEvent(MComponentPeer compPeer, int x_root, int y_root, int type) {
switch (type) {
case 4:
if (!isOwnerOf(compPeer)) {
postUngrabEvent();
return true;
}
}
return false;
}
private final boolean hasWarningWindow() {
return ((Window)target).getWarningString() != null;
}
boolean isTargetUndecorated() {
return true;
}
private volatile int sysX = 0;
private volatile int sysY = 0;
private volatile int sysW = 0;
private volatile int sysH = 0;
Rectangle constrainBounds(int x, int y, int width, int height) {
if (!hasWarningWindow()) {
return new Rectangle(x, y, width, height);
}
int newX = x;
int newY = y;
int newW = width;
int newH = height;
GraphicsConfiguration gc = ((Window)target).getGraphicsConfiguration();
Rectangle sB = gc.getBounds();
Insets sIn = ((Window)target).getToolkit().getScreenInsets(gc);
int screenW = sB.width - sIn.left - sIn.right;
int screenH = sB.height - sIn.top - sIn.bottom;
if (!target.isVisible() || isTargetUndecorated()) {
int screenX = sB.x + sIn.left;
int screenY = sB.y + sIn.top;
if (newW > screenW) {
newW = screenW;
}
if (newH > screenH) {
newH = screenH;
}
if (newX < screenX) {
newX = screenX;
} else if (newX + newW > screenX + screenW) {
newX = screenX + screenW - newW;
}
if (newY < screenY) {
newY = screenY;
} else if (newY + newH > screenY + screenH) {
newY = screenY + screenH - newH;
}
} else {
int maxW = Math.max(screenW, sysW);
int maxH = Math.max(screenH, sysH);
if (newW > maxW) {
newW = maxW;
}
if (newH > maxH) {
newH = maxH;
}
}
return new Rectangle(newX, newY, newW, newH);
}
public void setBounds(int x, int y, int width, int height, int op) {
Rectangle newBounds = constrainBounds(x, y, width, height);
super.setBounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height, op);
}
@Override
public void setOpacity(float opacity) {
}
@Override
public void setOpaque(boolean isOpaque) {
}
@Override
public void updateWindow() {
}
public void repositionSecurityWarning() {
}
}