FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / awt / xlib / XCanvasPeer.java
bloba208be5c10387605d47bc2a938a5bccc68c38660
1 /* Copyright (C) 2000, 2002 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.awt.xlib;
11 import java.awt.Dimension;
12 import java.awt.Component;
13 import java.awt.EventQueue;
14 import java.awt.Rectangle;
15 import java.awt.Color;
16 import java.awt.Container;
17 import java.awt.Image;
18 import java.awt.GraphicsConfiguration;
19 import java.awt.Font;
20 import java.awt.FontMetrics;
21 import java.awt.Graphics;
22 import java.awt.Point;
23 import java.awt.Toolkit;
24 import java.awt.AWTEvent;
25 import java.awt.Cursor;
26 import java.awt.Shape;
28 import java.awt.peer.*;
29 import java.awt.image.*;
31 import java.awt.event.MouseListener;
32 import java.awt.event.PaintEvent;
34 import java.util.EventListener;
36 import gnu.gcj.xlib.WMSizeHints;
37 import gnu.gcj.xlib.Window;
38 import gnu.gcj.xlib.WindowAttributes;
39 import gnu.gcj.xlib.Display;
40 import gnu.gcj.xlib.Visual;
41 import gnu.gcj.xlib.Screen;
42 import gnu.gcj.xlib.XImage;
44 import gnu.awt.j2d.*;
46 public class XCanvasPeer implements CanvasPeer
48 static final Dimension MIN_SIZE = new Dimension(1, 1);
50 public // temporary
52 Window window;
53 Window parent;
55 Component component;
56 XGraphicsConfiguration config;
58 public XCanvasPeer(Component component)
60 this.component = component;
62 // Set up graphics configuration (ie. screen + visual):
64 config = (XGraphicsConfiguration)
65 component.getGraphicsConfiguration();
67 if (config == null)
69 // This will usually only happen for toplevel windows
70 config = getXToolkit().getDefaultXGraphicsConfiguration();
73 Rectangle bounds = component.getBounds();
74 parent = locateParentWindow(bounds);
76 // Windows in X must atleast be of size 1x1
77 boolean boundsChanged = false;
78 if (bounds.width < 1)
80 boundsChanged = true;
81 bounds.width = 1;
83 if (bounds.height < 1)
85 boundsChanged = true;
86 bounds.height = 1;
89 /* don't worry about this calling back to us, since the real
90 component object has not yet received a reference to this peer
91 object. */
92 component.setBounds(bounds);
94 WindowAttributes attributes = new WindowAttributes();
96 /* Set background color */
97 Color bg = component.getBackground();
98 if (bg != null)
100 int[] components =
102 bg.getRed(),
103 bg.getGreen(),
104 bg.getBlue(),
105 0xff
108 ColorModel cm = config.getColorModel();
109 long pixel = cm.getDataElement(components, 0);
110 attributes.setBackground(pixel);
113 /* Set exposure mask so that we get exposure events
114 that can be translated into paint() calls. */
115 long eventMask = WindowAttributes.MASK_EXPOSURE;
117 /* It would be nice to set up all other required events here, but
118 it is not possible to do so before after all the children of
119 this component has been realized. The reason is that it is not
120 determined whether a component is lightweight before after the
121 addNotify() method has been called. Thus, it is not possible
122 for parent component to determine what events it needs to
123 furnish for lightweight children. Instead, we currently rely
124 on the component calling our setEventMask() method after the
125 correct event mask has been determined. */
127 attributes.setEventMask(eventMask);
130 // TODO: set more window attributes?
132 /* don't allow event queue to process events from the newly
133 created window before this peer has been registered as client
134 data. */
135 synchronized (getXToolkit().eventLoop)
137 window = new gnu.gcj.xlib.Window(parent, bounds, attributes);
138 window.setClientData(this); /* make it possible to find back
139 to this peer object. Used by
140 XEventQueue. */
143 initWindowProperties();
145 if (component.isVisible())
146 EventQueue.invokeLater(new DoMap(window));
150 * Override this in subclasses to implement other ways of obtaining
151 * parent windows. Toplevel windows will typically have a different
152 * implementation.
154 gnu.gcj.xlib.Window locateParentWindow(Rectangle bounds)
156 Container parent = component.getParent();
157 while (parent.isLightweight())
159 bounds.x += parent.getX();
160 bounds.y += parent.getY();
161 parent = parent.getParent();
162 // a null pointer here is a genuine error
165 XCanvasPeer parentPeer = (XCanvasPeer) parent.getPeer();
166 if (parentPeer == null)
167 throw new NullPointerException("Parent has no peer. This should " +
168 "not be possible, since the " +
169 "calls leading here should come " +
170 "from parent, after it has " +
171 "set the parent peer.");
172 return parentPeer.window;
176 /**
177 * Template method to allow subclasses to apply properties to X11
178 * window right after creation.
180 void initWindowProperties()
184 XToolkit getXToolkit()
186 return XToolkit.INSTANCE;
189 protected void ensureFlush()
191 getXToolkit().flushIfIdle();
194 public Component getComponent()
196 return component;
199 long getBasicEventMask()
201 return WindowAttributes.MASK_EXPOSURE;
204 // -------- java.awt.peer.ComponentPeer implementation
206 public int checkImage(Image img, int width, int height, ImageObserver o)
208 throw new UnsupportedOperationException("FIXME, not implemented");
210 public Image createImage(ImageProducer prod)
212 throw new UnsupportedOperationException("FIXME, not implemented");
214 public Image createImage(int width, int height)
216 throw new UnsupportedOperationException("FIXME, not implemented");
218 public void dispose()
220 throw new UnsupportedOperationException("FIXME, not implemented");
223 public GraphicsConfiguration getGraphicsConfiguration()
225 return config;
228 public FontMetrics getFontMetrics(Font f)
230 throw new UnsupportedOperationException("FIXME, not implemented");
233 public ColorModel getColorModel ()
235 return null;
238 public Graphics getGraphics()
240 DirectRasterGraphics gfxDevice = new XGraphics(window, config);
241 IntegerGraphicsState igState = new IntegerGraphicsState(gfxDevice);
242 Graphics2DImpl gfx2d = new Graphics2DImpl(config);
244 gfx2d.setState(igState);
245 gfx2d.setColor(component.getBackground());
246 return gfx2d;
249 public Point getLocationOnScreen()
251 throw new UnsupportedOperationException("FIXME, not implemented");
254 public Dimension getMinimumSize ()
256 return MIN_SIZE;
259 public Dimension minimumSize ()
261 return getMinimumSize ();
264 public Dimension getPreferredSize ()
266 return component.getSize();
269 public Dimension preferredSize ()
271 return getPreferredSize();
274 public Toolkit getToolkit()
276 return getXToolkit();
279 public void handleEvent(AWTEvent event)
283 public boolean isFocusTraversable()
285 throw new UnsupportedOperationException("FIXME, not implemented");
288 public void paint(Graphics gfx)
290 // do nothing by default
293 public boolean prepareImage(Image img, int width, int height,
294 ImageObserver o)
296 throw new UnsupportedOperationException("FIXME, not implemented");
299 public void print(Graphics graphics)
301 paint(graphics);
304 public void repaint(long tm, int x, int y, int w, int h)
306 /* TODO?
308 X allows intelligent X servers to do smart
309 refreshing. Perhaps involve X in repainting of components,
310 rather that keeping it all within the local event queue. */
312 PaintEvent updateEvent = new PaintEvent(component,
313 PaintEvent.UPDATE,
314 new Rectangle(x, y, w, h));
315 getXToolkit().queue.postEvent(updateEvent);
318 public void requestFocus()
320 throw new UnsupportedOperationException("FIXME, not implemented");
323 public void setBackground(Color color)
325 throw new UnsupportedOperationException("not implemented");
328 public void setBounds(int x, int y, int width, int height)
330 width = Math.max(width, 1);
331 height = Math.max(height, 1);
332 window.setBounds(x, y, width, height);
333 ensureFlush();
336 public void reshape (int x, int y, int width, int height)
338 setBounds (x, y, width, height);
341 public void setCursor(Cursor cursor)
343 throw new UnsupportedOperationException("FIXME, not implemented");
346 public void setEnabled(boolean enabled)
348 throw new UnsupportedOperationException("FIXME, not implemented");
351 public void enable ()
353 setEnabled (true);
356 public void disable ()
358 setEnabled (false);
361 public void setEventMask(long eventMask)
363 WindowAttributes attributes = new WindowAttributes();
365 long xEventMask = getBasicEventMask();
367 if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)
369 xEventMask |=
370 WindowAttributes.MASK_BUTTON_PRESS |
371 WindowAttributes.MASK_BUTTON_RELEASE;
374 attributes.setEventMask(xEventMask);
375 window.setAttributes(attributes);
376 ensureFlush();
379 public void setFont(Font font)
381 /* default canvas peer does keep track of font, since it won't
382 write anything. */
385 public void setForeground(Color color)
387 /* default canvas peer does keep track of foreground, since it won't
388 paint anything. */
391 public void setVisible(boolean visible)
393 if (visible)
395 window.map();
396 ensureFlush();
398 else
400 throw new UnsupportedOperationException("unmap not implemented");
404 public void show ()
406 setVisible (true);
409 public void hide ()
411 setVisible (false);
414 static class DoMap implements Runnable
416 Window window;
417 public DoMap(Window w)
419 this.window = w;
422 public void run()
424 window.map();