package gnu.awt.xlib;
import java.awt.Dimension;
import java.awt.BufferCapabilities;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.GraphicsConfiguration;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.AWTEvent;
import java.awt.Cursor;
import java.awt.Shape;
import java.awt.peer.*;
import java.awt.image.*;
import java.awt.event.MouseListener;
import java.awt.event.PaintEvent;
import java.util.EventListener;
import gnu.gcj.xlib.WMSizeHints;
import gnu.gcj.xlib.Window;
import gnu.gcj.xlib.WindowAttributes;
import gnu.gcj.xlib.Display;
import gnu.gcj.xlib.Visual;
import gnu.gcj.xlib.Screen;
import gnu.gcj.xlib.XImage;
import gnu.awt.j2d.*;
public class XCanvasPeer implements CanvasPeer
{
static final Dimension MIN_SIZE = new Dimension(1, 1);
public
Window window;
Window parent;
Component component;
XGraphicsConfiguration config;
public XCanvasPeer(Component component)
{
this.component = component;
config = (XGraphicsConfiguration)
component.getGraphicsConfiguration();
if (config == null)
{
config = getXToolkit().getDefaultXGraphicsConfiguration();
}
Rectangle bounds = component.getBounds();
parent = locateParentWindow(bounds);
boolean boundsChanged = false;
if (bounds.width < 1)
{
boundsChanged = true;
bounds.width = 1;
}
if (bounds.height < 1)
{
boundsChanged = true;
bounds.height = 1;
}
component.setBounds(bounds);
WindowAttributes attributes = new WindowAttributes();
Color bg = component.getBackground();
if (bg != null)
{
int[] components =
{
bg.getRed(),
bg.getGreen(),
bg.getBlue(),
0xff
};
ColorModel cm = config.getColorModel();
long pixel = cm.getDataElement(components, 0);
attributes.setBackground(pixel);
}
long eventMask = WindowAttributes.MASK_EXPOSURE;
attributes.setEventMask(eventMask);
synchronized (getXToolkit().eventLoop)
{
window = new gnu.gcj.xlib.Window(parent, bounds, attributes);
window.setClientData(this);
}
initWindowProperties();
if (component.isVisible())
EventQueue.invokeLater(new DoMap(window));
}
gnu.gcj.xlib.Window locateParentWindow(Rectangle bounds)
{
Container parent = component.getParent();
while (parent.isLightweight())
{
bounds.x += parent.getX();
bounds.y += parent.getY();
parent = parent.getParent();
}
XCanvasPeer parentPeer = (XCanvasPeer) parent.getPeer();
if (parentPeer == null)
throw new NullPointerException("Parent has no peer. This should " +
"not be possible, since the " +
"calls leading here should come " +
"from parent, after it has " +
"set the parent peer.");
return parentPeer.window;
}
void initWindowProperties()
{
}
XToolkit getXToolkit()
{
return XToolkit.INSTANCE;
}
protected void ensureFlush()
{
getXToolkit().flushIfIdle();
}
public Component getComponent()
{
return component;
}
long getBasicEventMask()
{
return WindowAttributes.MASK_EXPOSURE;
}
public int checkImage(Image img, int width, int height, ImageObserver o)
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public Image createImage(ImageProducer prod)
{
return new XOffScreenImage (config, window, prod, config.getColorModel());
}
public Image createImage(int width, int height)
{
return new XOffScreenImage (config, window, width, height, config.getColorModel());
}
public void dispose()
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public GraphicsConfiguration getGraphicsConfiguration()
{
return config;
}
public FontMetrics getFontMetrics(Font f)
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public ColorModel getColorModel ()
{
return null;
}
public Graphics getGraphics()
{
DirectRasterGraphics gfxDevice = new XGraphics(window, config);
IntegerGraphicsState igState = new IntegerGraphicsState(gfxDevice);
Graphics2DImpl gfx2d = new Graphics2DImpl(config);
gfx2d.setState(igState);
gfx2d.setColor(component.getBackground());
return gfx2d;
}
public Point getLocationOnScreen()
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public Dimension getMinimumSize ()
{
return MIN_SIZE;
}
public Dimension minimumSize ()
{
return getMinimumSize ();
}
public Dimension getPreferredSize ()
{
return component.getSize();
}
public Dimension preferredSize ()
{
return getPreferredSize();
}
public Toolkit getToolkit()
{
return getXToolkit();
}
public void handleEvent(AWTEvent event)
{
int id = event.getID ();
switch (id)
{
case PaintEvent.PAINT:
case PaintEvent.UPDATE:
{
try
{
Graphics g = getGraphics ();
g.setClip (((PaintEvent)event).getUpdateRect ());
if (id == PaintEvent.PAINT)
component.paint (g);
else
component.update (g);
g.dispose ();
}
catch (InternalError e)
{
System.err.println (e);
}
}
break;
}
}
public boolean isFocusTraversable()
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public void paint(Graphics gfx)
{
}
public boolean prepareImage(Image img, int width, int height,
ImageObserver o)
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public void print(Graphics graphics)
{
paint(graphics);
}
public void repaint(long tm, int x, int y, int w, int h)
{
PaintEvent updateEvent = new PaintEvent(component,
PaintEvent.UPDATE,
new Rectangle(x, y, w, h));
getXToolkit().queue.postEvent(updateEvent);
}
public void requestFocus()
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public void setBackground(Color color)
{
throw new UnsupportedOperationException("not implemented");
}
public void setBounds(int x, int y, int width, int height)
{
width = Math.max(width, 1);
height = Math.max(height, 1);
window.setBounds(x, y, width, height);
ensureFlush();
}
public void reshape (int x, int y, int width, int height)
{
setBounds (x, y, width, height);
}
public void setCursor(Cursor cursor)
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public void setEnabled(boolean enabled)
{
throw new UnsupportedOperationException("FIXME, not implemented");
}
public void enable ()
{
setEnabled (true);
}
public void disable ()
{
setEnabled (false);
}
public void setEventMask(long eventMask)
{
WindowAttributes attributes = new WindowAttributes();
long xEventMask = getBasicEventMask();
if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)
{
xEventMask |=
WindowAttributes.MASK_BUTTON_PRESS |
WindowAttributes.MASK_BUTTON_RELEASE;
}
attributes.setEventMask(xEventMask);
window.setAttributes(attributes);
ensureFlush();
}
public void setFont(Font font)
{
}
public void setForeground(Color color)
{
}
public void setVisible(boolean visible)
{
if (visible)
{
window.map();
ensureFlush();
}
else
{
window.unmap();
ensureFlush();
}
}
public void show ()
{
setVisible (true);
}
public void hide ()
{
setVisible (false);
}
public boolean isFocusable ()
{
return false;
}
public boolean requestFocus (Component source, boolean b1,
boolean b2, long x)
{
return false;
}
public boolean isObscured ()
{
return false;
}
public boolean canDetermineObscurity ()
{
return false;
}
public void coalescePaintEvent (PaintEvent e)
{
}
public void updateCursorImmediately ()
{
}
public VolatileImage createVolatileImage (int width, int height)
{
return null;
}
public boolean handlesWheelScrolling ()
{
return false;
}
public void createBuffers (int x, BufferCapabilities capabilities)
throws java.awt.AWTException
{
}
public Image getBackBuffer ()
{
return null;
}
public void flip (BufferCapabilities.FlipContents contents)
{
}
public void destroyBuffers ()
{
}
static class DoMap implements Runnable
{
Window window;
public DoMap(Window w)
{
this.window = w;
}
public void run()
{
window.map();
}
}
}