1 /* XToolkit.java -- The central AWT Toolkit for the X peers
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.awt.peer.x;
41 import java.awt.AWTException;
42 import java.awt.Button;
43 import java.awt.Canvas;
44 import java.awt.Checkbox;
45 import java.awt.CheckboxMenuItem;
46 import java.awt.Choice;
47 import java.awt.Component;
48 import java.awt.Dialog;
49 import java.awt.Dimension;
50 import java.awt.EventQueue;
51 import java.awt.FileDialog;
53 import java.awt.FontMetrics;
54 import java.awt.Frame;
55 import java.awt.GraphicsDevice;
56 import java.awt.GraphicsEnvironment;
57 import java.awt.Image;
58 import java.awt.Label;
61 import java.awt.MenuBar;
62 import java.awt.MenuItem;
63 import java.awt.Panel;
64 import java.awt.PopupMenu;
65 import java.awt.PrintJob;
66 import java.awt.ScrollPane;
67 import java.awt.Scrollbar;
68 import java.awt.TextArea;
69 import java.awt.TextField;
70 import java.awt.Transparency;
71 import java.awt.Window;
72 import java.awt.datatransfer.Clipboard;
73 import java.awt.dnd.DragGestureEvent;
74 import java.awt.dnd.peer.DragSourceContextPeer;
75 import java.awt.im.InputMethodHighlight;
76 import java.awt.image.BufferedImage;
77 import java.awt.image.ColorModel;
78 import java.awt.image.DirectColorModel;
79 import java.awt.image.ImageObserver;
80 import java.awt.image.ImageProducer;
81 import java.awt.peer.ButtonPeer;
82 import java.awt.peer.CanvasPeer;
83 import java.awt.peer.CheckboxMenuItemPeer;
84 import java.awt.peer.CheckboxPeer;
85 import java.awt.peer.ChoicePeer;
86 import java.awt.peer.DialogPeer;
87 import java.awt.peer.FileDialogPeer;
88 import java.awt.peer.FontPeer;
89 import java.awt.peer.FramePeer;
90 import java.awt.peer.LabelPeer;
91 import java.awt.peer.LightweightPeer;
92 import java.awt.peer.ListPeer;
93 import java.awt.peer.MenuBarPeer;
94 import java.awt.peer.MenuItemPeer;
95 import java.awt.peer.MenuPeer;
96 import java.awt.peer.PanelPeer;
97 import java.awt.peer.PopupMenuPeer;
98 import java.awt.peer.RobotPeer;
99 import java.awt.peer.ScrollPanePeer;
100 import java.awt.peer.ScrollbarPeer;
101 import java.awt.peer.TextAreaPeer;
102 import java.awt.peer.TextFieldPeer;
103 import java.awt.peer.WindowPeer;
104 import java.io.ByteArrayInputStream;
106 import java.io.IOException;
107 import java.io.InputStream;
108 import java.net.MalformedURLException;
110 import java.util.HashMap;
111 import java.util.Map;
112 import java.util.Properties;
113 import java.util.WeakHashMap;
115 import javax.imageio.ImageIO;
117 import gnu.classpath.SystemProperties;
118 import gnu.java.awt.ClasspathToolkit;
119 import gnu.java.awt.EmbeddedWindow;
120 import gnu.java.awt.peer.ClasspathFontPeer;
121 import gnu.java.awt.peer.EmbeddedWindowPeer;
122 import gnu.java.awt.peer.swing.SwingCanvasPeer;
123 import gnu.java.awt.peer.swing.SwingLabelPeer;
124 import gnu.java.awt.peer.swing.SwingPanelPeer;
126 public class XToolkit
127 extends ClasspathToolkit
131 * Set to true to enable debug output.
133 static boolean DEBUG = false;
136 * Maps AWT colors to X colors.
138 HashMap colorMap = new HashMap();
141 * The system event queue.
143 private EventQueue eventQueue;
146 * The default color model of this toolkit.
148 private ColorModel colorModel;
151 * Maps image URLs to Image instances.
153 private HashMap imageCache = new HashMap();
158 private WeakHashMap fontCache = new WeakHashMap();
162 SystemProperties.setProperty("gnu.javax.swing.noGraphics2D", "true");
163 SystemProperties.setProperty("java.awt.graphicsenv",
164 "gnu.java.awt.peer.x.XGraphicsEnvironment");
167 public GraphicsEnvironment getLocalGraphicsEnvironment()
169 return new XGraphicsEnvironment();
173 * Returns the font peer for a font with the specified name and attributes.
175 * @param name the font name
176 * @param attrs the font attributes
178 * @return the font peer for a font with the specified name and attributes
180 public ClasspathFontPeer getClasspathFontPeer(String name, Map attrs)
182 String canonical = XFontPeer.encodeFont(name, attrs);
183 ClasspathFontPeer font;
184 if (!fontCache.containsKey(canonical))
187 SystemProperties.getProperty("gnu.xawt.graphics2d");
188 if (graphics2d != null && graphics2d.equals("gl"))
189 font = new XFontPeer2(name, attrs);
191 font = new XFontPeer(name, attrs);
192 fontCache.put(canonical, font);
196 font = (ClasspathFontPeer) fontCache.get(canonical);
201 public Font createFont(int format, InputStream stream)
206 public RobotPeer createRobot(GraphicsDevice screen) throws AWTException
208 // TODO: Implement this.
209 throw new UnsupportedOperationException("Not yet implemented.");
212 public EmbeddedWindowPeer createEmbeddedWindow(EmbeddedWindow w)
214 // TODO: Implement this.
215 throw new UnsupportedOperationException("Not yet implemented.");
218 protected ButtonPeer createButton(Button target)
220 // TODO: Implement this.
221 throw new UnsupportedOperationException("Not yet implemented.");
224 protected TextFieldPeer createTextField(TextField target)
226 // TODO: Implement this.
227 throw new UnsupportedOperationException("Not yet implemented.");
230 protected LabelPeer createLabel(Label target)
232 return new SwingLabelPeer(target);
235 protected ListPeer createList(List target)
237 // TODO: Implement this.
238 throw new UnsupportedOperationException("Not yet implemented.");
241 protected CheckboxPeer createCheckbox(Checkbox target)
243 // TODO: Implement this.
244 throw new UnsupportedOperationException("Not yet implemented.");
247 protected ScrollbarPeer createScrollbar(Scrollbar target)
249 // TODO: Implement this.
250 throw new UnsupportedOperationException("Not yet implemented.");
253 protected ScrollPanePeer createScrollPane(ScrollPane target)
255 // TODO: Implement this.
256 throw new UnsupportedOperationException("Not yet implemented.");
259 protected TextAreaPeer createTextArea(TextArea target)
261 // TODO: Implement this.
262 throw new UnsupportedOperationException("Not yet implemented.");
265 protected ChoicePeer createChoice(Choice target)
267 // TODO: Implement this.
268 throw new UnsupportedOperationException("Not yet implemented.");
271 protected FramePeer createFrame(Frame target)
273 XFramePeer frame = new XFramePeer(target);
277 protected CanvasPeer createCanvas(Canvas target)
279 return new SwingCanvasPeer(target);
282 protected PanelPeer createPanel(Panel target)
284 return new SwingPanelPeer(target);
287 protected WindowPeer createWindow(Window target)
289 return new XWindowPeer(target);
292 protected DialogPeer createDialog(Dialog target)
294 return new XDialogPeer(target);
297 protected MenuBarPeer createMenuBar(MenuBar target)
299 // TODO: Implement this.
300 throw new UnsupportedOperationException("Not yet implemented.");
303 protected MenuPeer createMenu(Menu target)
305 // TODO: Implement this.
306 throw new UnsupportedOperationException("Not yet implemented.");
309 protected PopupMenuPeer createPopupMenu(PopupMenu target)
311 // TODO: Implement this.
312 throw new UnsupportedOperationException("Not yet implemented.");
315 protected MenuItemPeer createMenuItem(MenuItem target)
317 // TODO: Implement this.
318 throw new UnsupportedOperationException("Not yet implemented.");
321 protected FileDialogPeer createFileDialog(FileDialog target)
323 // TODO: Implement this.
324 throw new UnsupportedOperationException("Not yet implemented.");
327 protected CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target)
329 // TODO: Implement this.
330 throw new UnsupportedOperationException("Not yet implemented.");
333 protected FontPeer getFontPeer(String name, int style)
335 // TODO: Implement this.
336 throw new UnsupportedOperationException("Not yet implemented.");
339 public Dimension getScreenSize()
341 // FIXME: This is only a hack to get some apps working.
342 return new Dimension(1024, 768);
345 public int getScreenResolution()
347 // TODO: Implement this.
348 throw new UnsupportedOperationException("Not yet implemented.");
352 * Returns the color model used by this toolkit.
354 * @return the color model used by this toolkit
356 public ColorModel getColorModel()
358 // TODO: I assume 24 bit depth here, we can do this better.
359 if (colorModel == null)
360 colorModel = new DirectColorModel(24, 0xFF0000, 0xFF00, 0xFF);
364 public String[] getFontList()
366 // TODO: Implement this.
367 throw new UnsupportedOperationException("Not yet implemented.");
370 public FontMetrics getFontMetrics(Font name)
372 ClasspathFontPeer peer = (ClasspathFontPeer) name.getPeer();
373 return peer.getFontMetrics(name);
378 // TODO: Implement this.
379 throw new UnsupportedOperationException("Not yet implemented.");
383 * Returns an image that has its pixel data loaded from a file with the
384 * specified name. If that file doesn't exist, an empty or error image
385 * is returned instead.
387 * @param name the filename of the file that contains the pixel data
391 public Image getImage(String name)
396 File file = new File(name);
397 image = getImage(file.toURL());
399 catch (MalformedURLException ex)
401 // TODO: Replace by a more meaningful error image instead.
408 * Returns an image that has its pixel data loaded from the specified URL.
409 * If the image cannot be loaded for some reason, an empty or error image
410 * is returned instead.
412 * @param url the URL to the image data
416 public Image getImage(URL url)
419 if (imageCache.containsKey(url))
421 image = (Image) imageCache.get(url);
425 image = createImage(url);
426 imageCache.put(url, image);
432 * Returns an image that has its pixel data loaded from a file with the
433 * specified name. If that file doesn't exist, an empty or error image
434 * is returned instead.
436 * @param filename the filename of the file that contains the pixel data
440 public Image createImage(String filename)
445 File file = new File(filename);
446 URL url = file.toURL();
447 im = createImage(url);
449 catch (MalformedURLException ex)
451 im = createErrorImage();
457 * Returns an image that has its pixel data loaded from the specified URL.
458 * If the image cannot be loaded for some reason, an empty or error image
459 * is returned instead.
461 * @param url the URL to the image data
465 public Image createImage(URL url)
470 image = createImage(url.openStream());
472 catch (IOException ex)
474 image = createErrorImage();
480 * Creates an image that is returned when calls to createImage() yields an
483 * @return an image that is returned when calls to createImage() yields an
486 private Image createErrorImage()
488 // TODO: Create better error image.
489 return new XImage(1, 1);
492 public boolean prepareImage(Image image, int width, int height, ImageObserver observer)
494 // Images are loaded synchronously, so we don't bother and return true.
498 public int checkImage(Image image, int width, int height, ImageObserver observer)
500 // TODO: Implement this.
501 throw new UnsupportedOperationException("Not yet implemented.");
504 public Image createImage(ImageProducer producer)
506 ImageConverter conv = new ImageConverter();
507 producer.startProduction(conv);
508 Image image = conv.getXImage();
512 public Image createImage(byte[] data, int offset, int len)
517 ByteArrayInputStream i = new ByteArrayInputStream(data, offset, len);
518 image = createImage(i);
520 catch (IOException ex)
522 image = createErrorImage();
527 private Image createImage(InputStream i)
531 BufferedImage buffered = ImageIO.read(i);
532 // If the bufferedimage is opaque, then we can copy it over to an
533 // X Pixmap for faster drawing.
534 if (buffered != null && buffered.getTransparency() == Transparency.OPAQUE)
536 ImageProducer source = buffered.getSource();
537 image = createImage(source);
539 else if (buffered != null)
545 image = createErrorImage();
550 public PrintJob getPrintJob(Frame frame, String title, Properties props)
552 // TODO: Implement this.
553 throw new UnsupportedOperationException("Not yet implemented.");
558 // TODO: Implement this.
559 throw new UnsupportedOperationException("Not yet implemented.");
562 public Clipboard getSystemClipboard()
564 // TODO: Implement this.
565 throw new UnsupportedOperationException("Not yet implemented.");
569 * Returns the eventqueue used by the XLib peers.
571 * @return the eventqueue used by the XLib peers
573 protected EventQueue getSystemEventQueueImpl()
575 if (eventQueue == null)
576 eventQueue = new EventQueue();
580 public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent e)
582 // TODO: Implement this.
583 throw new UnsupportedOperationException("Not yet implemented.");
586 public Map mapInputMethodHighlight(InputMethodHighlight highlight)
588 // TODO: Implement this.
589 throw new UnsupportedOperationException("Not yet implemented.");
593 * Helper method to quickly fetch the default device (X Display).
595 * @return the default XGraphicsDevice
597 static XGraphicsDevice getDefaultDevice()
599 XGraphicsEnvironment env = (XGraphicsEnvironment)
600 XGraphicsEnvironment.getLocalGraphicsEnvironment();
601 return (XGraphicsDevice) env.getDefaultScreenDevice();
604 protected LightweightPeer createComponent(Component c)
606 return new XLightweightPeer(c);