1 /* Toolkit.java -- AWT Toolkit superclass
2 Copyright (C) 1999, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, 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. */
41 import java.awt.event.*;
42 import java.awt.peer.*;
43 import java.awt.image.*;
44 import java.awt.datatransfer.Clipboard;
45 import java.util.Properties;
50 * The AWT system uses a set of native peer objects to implement its
51 * widgets. These peers are provided by a peer toolkit, that is accessed
52 * via a subclass of this superclass. The system toolkit is retrieved
53 * by the static methods <code>getDefaultToolkit</code>. This method
54 * determines the system toolkit by examining the system property
55 * <code>awt.toolkit</code>. That property is set to the name of the
56 * <code>Toolkit</code> subclass for the specified peer set. If the
57 * <code>awt.toolkit</code> property is not set, then the default
58 * toolkit <code>gnu.java.awt.peer.gtk.GtkToolkit</code> is used. This
59 * toolkit creates its peers using the GTK+ toolkit.
61 * @author Aaron M. Renn (arenn@urbanophile.com)
63 public abstract class Toolkit
70 // The default toolkit name.
71 private static String default_toolkit_name =
72 "gnu.java.awt.peer.gtk.GtkToolkit";
74 // The toolkit in use. Once we load it, we don't ever change it
75 // if the awt.toolkit propert is set.
76 private static Toolkit toolkit;
78 // The toolkit properties
79 private static Properties props = new Properties();
81 private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
83 private Properties desktopProperties = new Properties();
85 /*************************************************************************/
92 * Returns an instance of the default toolkit. The default toolkit is
93 * the subclass of <code>Toolkit</code> specified in the system property
94 * <code>awt.toolkit</code>, or <code>gnu.java.awt.peer.gtk.GtkToolkit</code>
95 * if the property is not set.
97 * @return An instance of the system default toolkit.
99 * @error AWTError If the toolkit cannot be loaded.
101 public static Toolkit
107 String toolkit_name = System.getProperty("awt.toolkit",
108 default_toolkit_name);
112 Class cls = Class.forName(toolkit_name);
113 Object obj = cls.newInstance();
115 if (!(obj instanceof Toolkit))
116 throw new AWTError(toolkit_name + " is not a subclass of " +
119 toolkit = (Toolkit)obj;
124 throw new AWTError("Cannot load AWT toolkit: " + e.getMessage());
128 /*************************************************************************/
131 * Returns the value of the property with the specified name, or the
132 * default value if the property does not exist.
134 * @param key The name of the property to retrieve.
135 * @param defThe default value of the property.
138 getProperty(String key, String def)
140 return(props.getProperty(key, def));
143 /*************************************************************************/
146 * Returns the native container object of the specified component. This
147 * method is necessary because the parent component might be a lightweight
150 * @param component The component to fetch the native container for.
152 * @return The native container object for this component.
154 protected static Container
155 getNativeContainer(Component component)
157 component = component.getParent();
161 if (component == null)
164 if (!(component instanceof Container))
166 component = component.getParent();
170 if (component.getPeer() instanceof LightweightPeer)
172 component = component.getParent();
176 return((Container)component);
180 /*************************************************************************/
187 * Default constructor for subclasses.
194 /*************************************************************************/
201 * Creates a peer object for the specified <code>Button</code>.
203 * @param target The <code>Button</code> to create the peer for.
205 * @return The peer for the specified <code>Button</code> object.
207 protected abstract ButtonPeer
208 createButton(Button target);
210 /*************************************************************************/
213 * Creates a peer object for the specified <code>TextField</code>.
215 * @param target The <code>TextField</code> to create the peer for.
217 * @return The peer for the specified <code>TextField</code> object.
219 protected abstract TextFieldPeer
220 createTextField(TextField target);
222 /*************************************************************************/
225 * Creates a peer object for the specified <code>Label</code>.
227 * @param target The <code>Label</code> to create the peer for.
229 * @return The peer for the specified <code>Label</code> object.
231 protected abstract LabelPeer
232 createLabel(Label target);
234 /*************************************************************************/
237 * Creates a peer object for the specified <code>List</code>.
239 * @param target The <code>List</code> to create the peer for.
241 * @return The peer for the specified <code>List</code> object.
243 protected abstract ListPeer
244 createList(List target);
246 /*************************************************************************/
249 * Creates a peer object for the specified <code>Checkbox</code>.
251 * @param target The <code>Checkbox</code> to create the peer for.
253 * @return The peer for the specified <code>Checkbox</code> object.
255 protected abstract CheckboxPeer
256 createCheckbox(Checkbox target);
258 /*************************************************************************/
261 * Creates a peer object for the specified <code>Scrollbar</code>.
263 * @param target The <code>Scrollbar</code> to create the peer for.
265 * @return The peer for the specified <code>Scrollbar</code> object.
267 protected abstract ScrollbarPeer
268 createScrollbar(Scrollbar target);
270 /*************************************************************************/
273 * Creates a peer object for the specified <code>ScrollPane</code>.
275 * @param target The <code>ScrollPane</code> to create the peer for.
277 * @return The peer for the specified <code>ScrollPane</code> object.
279 protected abstract ScrollPanePeer
280 createScrollPane(ScrollPane target);
282 /*************************************************************************/
285 * Creates a peer object for the specified <code>TextArea</code>.
287 * @param target The <code>TextArea</code> to create the peer for.
289 * @return The peer for the specified <code>TextArea</code> object.
291 protected abstract TextAreaPeer
292 createTextArea(TextArea target);
294 /*************************************************************************/
297 * Creates a peer object for the specified <code>Choice</code>.
299 * @param target The <code>Choice</code> to create the peer for.
301 * @return The peer for the specified <code>Choice</code> object.
303 protected abstract ChoicePeer
304 createChoice(Choice target);
306 /*************************************************************************/
309 * Creates a peer object for the specified <code>Frame</code>.
311 * @param target The <code>Frame</code> to create the peer for.
313 * @return The peer for the specified <code>Frame</code> object.
315 protected abstract FramePeer
316 createFrame(Frame target);
318 /*************************************************************************/
321 * Creates a peer object for the specified <code>Canvas</code>.
323 * @param target The <code>Canvas</code> to create the peer for.
325 * @return The peer for the specified <code>Canvas</code> object.
327 protected abstract CanvasPeer
328 createCanvas(Canvas target);
330 /*************************************************************************/
333 * Creates a peer object for the specified <code>Panel</code>.
335 * @param target The <code>Panel</code> to create the peer for.
337 * @return The peer for the specified <code>Panel</code> object.
339 protected abstract PanelPeer
340 createPanel(Panel target);
342 /*************************************************************************/
345 * Creates a peer object for the specified <code>Window</code>.
347 * @param target The <code>Window</code> to create the peer for.
349 * @return The peer for the specified <code>Window</code> object.
351 protected abstract WindowPeer
352 createWindow(Window target);
354 /*************************************************************************/
357 * Creates a peer object for the specified <code>Dialog</code>.
359 * @param target The dialog to create the peer for
361 * @return The peer for the specified font name.
363 protected abstract DialogPeer
364 createDialog(Dialog target);
366 /*************************************************************************/
369 * Creates a peer object for the specified <code>MenuBar</code>.
371 * @param target The <code>MenuBar</code> to create the peer for.
373 * @return The peer for the specified <code>MenuBar</code> object.
375 protected abstract MenuBarPeer
376 createMenuBar(MenuBar target);
378 /*************************************************************************/
381 * Creates a peer object for the specified <code>Menu</code>.
383 * @param target The <code>Menu</code> to create the peer for.
385 * @return The peer for the specified <code>Menu</code> object.
387 protected abstract MenuPeer
388 createMenu(Menu target);
390 /*************************************************************************/
393 * Creates a peer object for the specified <code>PopupMenu</code>.
395 * @param target The <code>PopupMenu</code> to create the peer for.
397 * @return The peer for the specified <code>PopupMenu</code> object.
399 protected abstract PopupMenuPeer
400 createPopupMenu(PopupMenu target);
402 /*************************************************************************/
405 * Creates a peer object for the specified <code>MenuItem</code>.
407 * @param target The <code>MenuItem</code> to create the peer for.
409 * @return The peer for the specified <code>MenuItem</code> object.
411 protected abstract MenuItemPeer
412 createMenuItem(MenuItem target);
414 /*************************************************************************/
417 * Creates a peer object for the specified <code>FileDialog</code>.
419 * @param target The <code>FileDialog</code> to create the peer for.
421 * @return The peer for the specified <code>FileDialog</code> object.
423 protected abstract FileDialogPeer
424 createFileDialog(FileDialog target);
426 /*************************************************************************/
429 * Creates a peer object for the specified <code>CheckboxMenuItem</code>.
431 * @param target The <code>CheckboxMenuItem</code> to create the peer for.
433 * @return The peer for the specified <code>CheckboxMenuItem</code> object.
435 protected abstract CheckboxMenuItemPeer
436 createCheckboxMenuItem(CheckboxMenuItem target);
438 /*************************************************************************/
441 * Creates a peer object for the specified <code>Component</code>. The
442 * peer returned by this method is not a native windowing system peer
443 * with its own native window. Instead, this method allows the component
444 * to draw on its parent window as a "lightweight" widget.
448 * @param target The <code>Component</code> to create the peer for.
450 * @return The peer for the specified <code>Component</code> object.
452 protected LightweightPeer
453 createComponent(Component target)
458 /*************************************************************************/
461 * Creates a peer object for the specified font name.
463 * @param name The font to create the peer for.
464 * @param style The font style to create the peer for.
466 * @return The peer for the specified font name.
468 protected abstract FontPeer
469 getFontPeer(String name, int style);
471 /*************************************************************************/
474 * Copies the current system colors into the specified array. This is
475 * the interface used by the <code>SystemColors</code> class.
477 * @param colors The array to copy the system colors into.
480 loadSystemColors(int systemColors[])
484 /*************************************************************************/
487 * Returns the dimensions of the screen in pixels.
489 * @return The dimensions of the screen in pixels.
491 public abstract Dimension
494 /*************************************************************************/
497 * Returns the screen resolution in dots per square inch.
499 * @return The screen resolution in dots per square inch.
502 getScreenResolution();
504 /*************************************************************************/
507 * Returns the color model of the screen.
509 * @return The color model of the screen.
511 public abstract ColorModel
514 /*************************************************************************/
517 * Returns the names of the available fonts.
519 * @return The names of the available fonts.
521 public abstract String[]
524 /*************************************************************************/
527 * Return the font metrics for the specified font
529 * @param name The name of the font to return metrics for.
531 * @return The requested font metrics.
533 public abstract FontMetrics
534 getFontMetrics(Font name);
536 /*************************************************************************/
539 * Flushes any buffered data to the screen so that it is in sync with
540 * what the AWT system has drawn to it.
545 /*************************************************************************/
548 * Returns an image from the specified file, which must be in a
549 * recognized format. Supported formats vary from toolkit to toolkit.
551 * @return name The name of the file to read the image from.
553 public abstract Image
554 getImage(String name);
556 /*************************************************************************/
559 * Returns an image from the specified URL, which must be in a
560 * recognized format. Supported formats vary from toolkit to toolkit.
562 * @return url The URl to read the image from.
564 public abstract Image
567 /*************************************************************************/
570 * Readies an image to be rendered on the screen. The width and height
571 * values can be set to the default sizes for the image by passing -1
572 * in those parameters.
574 * @param image The image to prepare for rendering.
575 * @param width The width of the image.
576 * @param height The height of the image.
577 * @param observer The observer to receive events about the preparation
580 * @return <code>true</code> if the image is already prepared for rendering,
581 * <code>false</code> otherwise.
583 public abstract boolean
584 prepareImage(Image image, int width, int height, ImageObserver observer);
586 /*************************************************************************/
589 * Checks the status of specified image as it is being readied for
592 * @param image The image to prepare for rendering.
593 * @param width The width of the image.
594 * @param height The height of the image.
595 * @param observer The observer to receive events about the preparation
598 * @return A union of the bitmasks from
599 * <code>java.awt.image.ImageObserver</code> that indicates the current
600 * state of the imaging readying process.
603 checkImage(Image image, int width, int height, ImageObserver observer);
605 /*************************************************************************/
608 * Creates an image using the specified <code>ImageProducer</code>
610 * @param producer The <code>ImageProducer</code> to create the image from.
612 * @return The created image.
614 public abstract Image
615 createImage(ImageProducer producer);
617 /*************************************************************************/
620 * Creates an image from the specified portion of the byte array passed.
621 * The array must be in a recognized format. Supported formats vary from
622 * toolkit to toolkit.
624 * @param data The raw image data.
625 * @param offset The offset into the data where the image data starts.
626 * @param len The length of the image data.
628 * @return The created image.
630 public abstract Image
631 createImage(byte[] data, int offset, int len);
633 /*************************************************************************/
636 * Creates an image from the specified byte array. The array must be in
637 * a recognized format. Supported formats vary from toolkit to toolkit.
639 * @param data The raw image data.
641 * @return The created image.
644 createImage(byte[] data)
646 return(createImage(data, 0, data.length));
649 public abstract Image
650 createImage(String filename);
652 public abstract Image
653 createImage(URL url);
656 /*************************************************************************/
659 * Returns a instance of <code>PrintJob</code> for the specified
662 * @param frame The window initiating the print job.
663 * @param title The print job title.
664 * @param props The print job properties.
666 * @return The requested print job, or <code>null</code> if the job
669 public abstract PrintJob
670 getPrintJob(Frame frame, String title, Properties props);
672 /*************************************************************************/
675 * Returns the system clipboard.
677 * @return THe system clipboard.
679 public abstract Clipboard
680 getSystemClipboard();
682 /*************************************************************************/
685 * Returns the accelerator key mask for menu shortcuts. The default is
686 * <code>Event.CTRL_MASK</code>. A toolkit must override this method
687 * to change the default.
689 * @return The key mask for the menu accelerator key.
692 getMenuShortcutKeyMask()
694 return Event.CTRL_MASK;
698 getLockingKeyState(int keyCode)
700 if (keyCode != KeyEvent.VK_CAPS_LOCK
701 && keyCode != KeyEvent.VK_NUM_LOCK
702 && keyCode != KeyEvent.VK_SCROLL_LOCK)
703 throw new IllegalArgumentException();
705 throw new UnsupportedOperationException();
709 setLockingKeyState(int keyCode, boolean on)
711 if (keyCode != KeyEvent.VK_CAPS_LOCK
712 && keyCode != KeyEvent.VK_NUM_LOCK
713 && keyCode != KeyEvent.VK_SCROLL_LOCK)
714 throw new IllegalArgumentException();
716 throw new UnsupportedOperationException();
719 /*************************************************************************/
722 * Returns the event queue for the applet. Despite the word "System"
723 * in the name of this method, there is no guarantee that the same queue
724 * is shared system wide.
726 * @return The event queue for this applet (or application)
728 public final EventQueue
729 getSystemEventQueue()
731 return(getSystemEventQueueImpl());
734 /*************************************************************************/
737 * // FIXME: What does this do?
739 protected abstract EventQueue
740 getSystemEventQueueImpl();
742 /*************************************************************************/
745 * Causes a "beep" tone to be generated.
751 createCustomCursor(Image cursor, Point hotSpot, String name)
752 throws IndexOutOfBoundsException
754 // Presumably the only reason this isn't abstract is for backwards
755 // compatibility? FIXME?
760 getBestCursorSize(int preferredWidth, int preferredHeight)
762 return new Dimension (0,0);
766 getMaximumCursorColors()
772 getDesktopProperty(String propertyName)
774 return desktopProperties.get(propertyName);
778 setDesktopProperty(String name, Object newValue)
780 Object oldValue = getDesktopProperty(name);
781 desktopProperties.put(name, newValue);
782 changeSupport.firePropertyChange(name, oldValue, newValue);
785 lazilyLoadDesktopProperty(String name)
787 // FIXME - what is this??
792 initializeDesktopProperties()
794 // Overridden by toolkit implementation?
798 addPropertyChangeListener(String name, PropertyChangeListener pcl)
800 changeSupport.addPropertyChangeListener(name, pcl);
804 removePropertyChangeListener(String name, PropertyChangeListener pcl)
806 changeSupport.removePropertyChangeListener(name, pcl);
810 addAWTEventListener(AWTEventListener listener, long eventMask)
812 // SecurityManager s = System.getSecurityManager();
814 // s.checkPermission(AWTPermission("listenToAllAWTEvents"));
820 removeAWTEventListener(AWTEventListener listener)