1 | /* Copyright (C) 2000, 2002 Free Software Foundation
|
---|
2 |
|
---|
3 | This file is part of libgcj.
|
---|
4 |
|
---|
5 | This software is copyrighted work licensed under the terms of the
|
---|
6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
7 | details. */
|
---|
8 |
|
---|
9 | package gnu.awt.xlib;
|
---|
10 |
|
---|
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;
|
---|
27 |
|
---|
28 | import java.awt.peer.*;
|
---|
29 | import java.awt.image.*;
|
---|
30 |
|
---|
31 | import java.awt.event.MouseListener;
|
---|
32 | import java.awt.event.PaintEvent;
|
---|
33 |
|
---|
34 | import java.util.EventListener;
|
---|
35 |
|
---|
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;
|
---|
43 |
|
---|
44 | import gnu.awt.j2d.*;
|
---|
45 |
|
---|
46 | public class XCanvasPeer implements CanvasPeer
|
---|
47 | {
|
---|
48 | static final Dimension MIN_SIZE = new Dimension(1, 1);
|
---|
49 |
|
---|
50 | public // temporary
|
---|
51 |
|
---|
52 | Window window;
|
---|
53 | Window parent;
|
---|
54 |
|
---|
55 | Component component;
|
---|
56 | XGraphicsConfiguration config;
|
---|
57 |
|
---|
58 | public XCanvasPeer(Component component)
|
---|
59 | {
|
---|
60 | this.component = component;
|
---|
61 |
|
---|
62 | // Set up graphics configuration (ie. screen + visual):
|
---|
63 |
|
---|
64 | config = (XGraphicsConfiguration)
|
---|
65 | component.getGraphicsConfiguration();
|
---|
66 |
|
---|
67 | if (config == null)
|
---|
68 | {
|
---|
69 | // This will usually only happen for toplevel windows
|
---|
70 | config = getXToolkit().getDefaultXGraphicsConfiguration();
|
---|
71 | }
|
---|
72 |
|
---|
73 | Rectangle bounds = component.getBounds();
|
---|
74 | parent = locateParentWindow(bounds);
|
---|
75 |
|
---|
76 | // Windows in X must atleast be of size 1x1
|
---|
77 | boolean boundsChanged = false;
|
---|
78 | if (bounds.width < 1)
|
---|
79 | {
|
---|
80 | boundsChanged = true;
|
---|
81 | bounds.width = 1;
|
---|
82 | }
|
---|
83 | if (bounds.height < 1)
|
---|
84 | {
|
---|
85 | boundsChanged = true;
|
---|
86 | bounds.height = 1;
|
---|
87 | }
|
---|
88 |
|
---|
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);
|
---|
93 |
|
---|
94 | WindowAttributes attributes = new WindowAttributes();
|
---|
95 |
|
---|
96 | /* Set background color */
|
---|
97 | Color bg = component.getBackground();
|
---|
98 | if (bg != null)
|
---|
99 | {
|
---|
100 | int[] components =
|
---|
101 | {
|
---|
102 | bg.getRed(),
|
---|
103 | bg.getGreen(),
|
---|
104 | bg.getBlue(),
|
---|
105 | 0xff
|
---|
106 | };
|
---|
107 |
|
---|
108 | ColorModel cm = config.getColorModel();
|
---|
109 | long pixel = cm.getDataElement(components, 0);
|
---|
110 | attributes.setBackground(pixel);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* Set exposure mask so that we get exposure events
|
---|
114 | that can be translated into paint() calls. */
|
---|
115 | long eventMask = WindowAttributes.MASK_EXPOSURE;
|
---|
116 |
|
---|
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. */
|
---|
126 |
|
---|
127 | attributes.setEventMask(eventMask);
|
---|
128 |
|
---|
129 |
|
---|
130 | // TODO: set more window attributes?
|
---|
131 |
|
---|
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)
|
---|
136 | {
|
---|
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. */
|
---|
141 | }
|
---|
142 |
|
---|
143 | initWindowProperties();
|
---|
144 |
|
---|
145 | if (component.isVisible())
|
---|
146 | EventQueue.invokeLater(new DoMap(window));
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Override this in subclasses to implement other ways of obtaining
|
---|
151 | * parent windows. Toplevel windows will typically have a different
|
---|
152 | * implementation.
|
---|
153 | */
|
---|
154 | gnu.gcj.xlib.Window locateParentWindow(Rectangle bounds)
|
---|
155 | {
|
---|
156 | Container parent = component.getParent();
|
---|
157 | while (parent.isLightweight())
|
---|
158 | {
|
---|
159 | bounds.x += parent.getX();
|
---|
160 | bounds.y += parent.getY();
|
---|
161 | parent = parent.getParent();
|
---|
162 | // a null pointer here is a genuine error
|
---|
163 | }
|
---|
164 |
|
---|
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;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Template method to allow subclasses to apply properties to X11
|
---|
178 | * window right after creation.
|
---|
179 | */
|
---|
180 | void initWindowProperties()
|
---|
181 | {
|
---|
182 | }
|
---|
183 |
|
---|
184 | XToolkit getXToolkit()
|
---|
185 | {
|
---|
186 | return XToolkit.INSTANCE;
|
---|
187 | }
|
---|
188 |
|
---|
189 | protected void ensureFlush()
|
---|
190 | {
|
---|
191 | getXToolkit().flushIfIdle();
|
---|
192 | }
|
---|
193 |
|
---|
194 | public Component getComponent()
|
---|
195 | {
|
---|
196 | return component;
|
---|
197 | }
|
---|
198 |
|
---|
199 | long getBasicEventMask()
|
---|
200 | {
|
---|
201 | return WindowAttributes.MASK_EXPOSURE;
|
---|
202 | }
|
---|
203 |
|
---|
204 | // -------- java.awt.peer.ComponentPeer implementation
|
---|
205 |
|
---|
206 | public int checkImage(Image img, int width, int height, ImageObserver o)
|
---|
207 | {
|
---|
208 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
209 | }
|
---|
210 | public Image createImage(ImageProducer prod)
|
---|
211 | {
|
---|
212 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
213 | }
|
---|
214 | public Image createImage(int width, int height)
|
---|
215 | {
|
---|
216 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
217 | }
|
---|
218 | public void dispose()
|
---|
219 | {
|
---|
220 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
221 | }
|
---|
222 |
|
---|
223 | public GraphicsConfiguration getGraphicsConfiguration()
|
---|
224 | {
|
---|
225 | return config;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public FontMetrics getFontMetrics(Font f)
|
---|
229 | {
|
---|
230 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
231 | }
|
---|
232 |
|
---|
233 | public ColorModel getColorModel ()
|
---|
234 | {
|
---|
235 | return null;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public Graphics getGraphics()
|
---|
239 | {
|
---|
240 | DirectRasterGraphics gfxDevice = new XGraphics(window, config);
|
---|
241 | IntegerGraphicsState igState = new IntegerGraphicsState(gfxDevice);
|
---|
242 | Graphics2DImpl gfx2d = new Graphics2DImpl(config);
|
---|
243 |
|
---|
244 | gfx2d.setState(igState);
|
---|
245 | gfx2d.setColor(component.getBackground());
|
---|
246 | return gfx2d;
|
---|
247 | }
|
---|
248 |
|
---|
249 | public Point getLocationOnScreen()
|
---|
250 | {
|
---|
251 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
252 | }
|
---|
253 |
|
---|
254 | public Dimension getMinimumSize ()
|
---|
255 | {
|
---|
256 | return MIN_SIZE;
|
---|
257 | }
|
---|
258 |
|
---|
259 | public Dimension minimumSize ()
|
---|
260 | {
|
---|
261 | return getMinimumSize ();
|
---|
262 | }
|
---|
263 |
|
---|
264 | public Dimension getPreferredSize ()
|
---|
265 | {
|
---|
266 | return component.getSize();
|
---|
267 | }
|
---|
268 |
|
---|
269 | public Dimension preferredSize ()
|
---|
270 | {
|
---|
271 | return getPreferredSize();
|
---|
272 | }
|
---|
273 |
|
---|
274 | public Toolkit getToolkit()
|
---|
275 | {
|
---|
276 | return getXToolkit();
|
---|
277 | }
|
---|
278 |
|
---|
279 | public void handleEvent(AWTEvent event)
|
---|
280 | {
|
---|
281 | }
|
---|
282 |
|
---|
283 | public boolean isFocusTraversable()
|
---|
284 | {
|
---|
285 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
286 | }
|
---|
287 |
|
---|
288 | public void paint(Graphics gfx)
|
---|
289 | {
|
---|
290 | // do nothing by default
|
---|
291 | }
|
---|
292 |
|
---|
293 | public boolean prepareImage(Image img, int width, int height,
|
---|
294 | ImageObserver o)
|
---|
295 | {
|
---|
296 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
297 | }
|
---|
298 |
|
---|
299 | public void print(Graphics graphics)
|
---|
300 | {
|
---|
301 | paint(graphics);
|
---|
302 | }
|
---|
303 |
|
---|
304 | public void repaint(long tm, int x, int y, int w, int h)
|
---|
305 | {
|
---|
306 | /* TODO?
|
---|
307 |
|
---|
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. */
|
---|
311 |
|
---|
312 | PaintEvent updateEvent = new PaintEvent(component,
|
---|
313 | PaintEvent.UPDATE,
|
---|
314 | new Rectangle(x, y, w, h));
|
---|
315 | getXToolkit().queue.postEvent(updateEvent);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public void requestFocus()
|
---|
319 | {
|
---|
320 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
321 | }
|
---|
322 |
|
---|
323 | public void setBackground(Color color)
|
---|
324 | {
|
---|
325 | throw new UnsupportedOperationException("not implemented");
|
---|
326 | }
|
---|
327 |
|
---|
328 | public void setBounds(int x, int y, int width, int height)
|
---|
329 | {
|
---|
330 | width = Math.max(width, 1);
|
---|
331 | height = Math.max(height, 1);
|
---|
332 | window.setBounds(x, y, width, height);
|
---|
333 | ensureFlush();
|
---|
334 | }
|
---|
335 |
|
---|
336 | public void reshape (int x, int y, int width, int height)
|
---|
337 | {
|
---|
338 | setBounds (x, y, width, height);
|
---|
339 | }
|
---|
340 |
|
---|
341 | public void setCursor(Cursor cursor)
|
---|
342 | {
|
---|
343 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
344 | }
|
---|
345 |
|
---|
346 | public void setEnabled(boolean enabled)
|
---|
347 | {
|
---|
348 | throw new UnsupportedOperationException("FIXME, not implemented");
|
---|
349 | }
|
---|
350 |
|
---|
351 | public void enable ()
|
---|
352 | {
|
---|
353 | setEnabled (true);
|
---|
354 | }
|
---|
355 |
|
---|
356 | public void disable ()
|
---|
357 | {
|
---|
358 | setEnabled (false);
|
---|
359 | }
|
---|
360 |
|
---|
361 | public void setEventMask(long eventMask)
|
---|
362 | {
|
---|
363 | WindowAttributes attributes = new WindowAttributes();
|
---|
364 |
|
---|
365 | long xEventMask = getBasicEventMask();
|
---|
366 |
|
---|
367 | if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)
|
---|
368 | {
|
---|
369 | xEventMask |=
|
---|
370 | WindowAttributes.MASK_BUTTON_PRESS |
|
---|
371 | WindowAttributes.MASK_BUTTON_RELEASE;
|
---|
372 | }
|
---|
373 |
|
---|
374 | attributes.setEventMask(xEventMask);
|
---|
375 | window.setAttributes(attributes);
|
---|
376 | ensureFlush();
|
---|
377 | }
|
---|
378 |
|
---|
379 | public void setFont(Font font)
|
---|
380 | {
|
---|
381 | /* default canvas peer does keep track of font, since it won't
|
---|
382 | write anything. */
|
---|
383 | }
|
---|
384 |
|
---|
385 | public void setForeground(Color color)
|
---|
386 | {
|
---|
387 | /* default canvas peer does keep track of foreground, since it won't
|
---|
388 | paint anything. */
|
---|
389 | }
|
---|
390 |
|
---|
391 | public void setVisible(boolean visible)
|
---|
392 | {
|
---|
393 | if (visible)
|
---|
394 | {
|
---|
395 | window.map();
|
---|
396 | ensureFlush();
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | throw new UnsupportedOperationException("unmap not implemented");
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | public void show ()
|
---|
405 | {
|
---|
406 | setVisible (true);
|
---|
407 | }
|
---|
408 |
|
---|
409 | public void hide ()
|
---|
410 | {
|
---|
411 | setVisible (false);
|
---|
412 | }
|
---|
413 |
|
---|
414 | static class DoMap implements Runnable
|
---|
415 | {
|
---|
416 | Window window;
|
---|
417 | public DoMap(Window w)
|
---|
418 | {
|
---|
419 | this.window = w;
|
---|
420 | }
|
---|
421 |
|
---|
422 | public void run()
|
---|
423 | {
|
---|
424 | window.map();
|
---|
425 | }
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|