source: trunk/src/gcc/libjava/java/awt/Toolkit.java@ 1213

Last change on this file since 1213 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 22.5 KB
Line 
1/* Toolkit.java -- AWT Toolkit superclass
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.event.*;
42import java.awt.peer.*;
43import java.awt.image.*;
44import java.awt.datatransfer.Clipboard;
45import java.util.Properties;
46import java.net.URL;
47import java.beans.*;
48
49/**
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.
60 *
61 * @author Aaron M. Renn ([email protected])
62 */
63public abstract class Toolkit
64{
65
66/*
67 * Static Variables
68 */
69
70// The default toolkit name.
71private static String default_toolkit_name =
72 "gnu.java.awt.peer.gtk.GtkToolkit";
73
74// The toolkit in use. Once we load it, we don't ever change it
75// if the awt.toolkit propert is set.
76private static Toolkit toolkit;
77
78// The toolkit properties
79private static Properties props = new Properties();
80
81private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
82
83private Properties desktopProperties = new Properties();
84
85/*************************************************************************/
86
87/*
88 * Static Methods
89 */
90
91/**
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.
96 *
97 * @return An instance of the system default toolkit.
98 *
99 * @error AWTError If the toolkit cannot be loaded.
100 */
101public static Toolkit
102getDefaultToolkit()
103{
104 if (toolkit != null)
105 return(toolkit);
106
107 String toolkit_name = System.getProperty("awt.toolkit",
108 default_toolkit_name);
109
110 try
111 {
112 Class cls = Class.forName(toolkit_name);
113 Object obj = cls.newInstance();
114
115 if (!(obj instanceof Toolkit))
116 throw new AWTError(toolkit_name + " is not a subclass of " +
117 "java.awt.Toolkit");
118
119 toolkit = (Toolkit)obj;
120 return(toolkit);
121 }
122 catch(Exception e)
123 {
124 throw new AWTError("Cannot load AWT toolkit: " + e.getMessage());
125 }
126}
127
128/*************************************************************************/
129
130/**
131 * Returns the value of the property with the specified name, or the
132 * default value if the property does not exist.
133 *
134 * @param key The name of the property to retrieve.
135 * @param defThe default value of the property.
136 */
137public static String
138getProperty(String key, String def)
139{
140 return(props.getProperty(key, def));
141}
142
143/*************************************************************************/
144
145/**
146 * Returns the native container object of the specified component. This
147 * method is necessary because the parent component might be a lightweight
148 * component.
149 *
150 * @param component The component to fetch the native container for.
151 *
152 * @return The native container object for this component.
153 */
154protected static Container
155getNativeContainer(Component component)
156{
157 component = component.getParent();
158
159 for(;;)
160 {
161 if (component == null)
162 return(null);
163
164 if (!(component instanceof Container))
165 {
166 component = component.getParent();
167 continue;
168 }
169
170 if (component.getPeer() instanceof LightweightPeer)
171 {
172 component = component.getParent();
173 continue;
174 }
175
176 return((Container)component);
177 }
178}
179
180/*************************************************************************/
181
182/*
183 * Constructors
184 */
185
186/**
187 * Default constructor for subclasses.
188 */
189public
190Toolkit()
191{
192}
193
194/*************************************************************************/
195
196/*
197 * Instance Methods
198 */
199
200/**
201 * Creates a peer object for the specified <code>Button</code>.
202 *
203 * @param target The <code>Button</code> to create the peer for.
204 *
205 * @return The peer for the specified <code>Button</code> object.
206 */
207protected abstract ButtonPeer
208createButton(Button target);
209
210/*************************************************************************/
211
212/**
213 * Creates a peer object for the specified <code>TextField</code>.
214 *
215 * @param target The <code>TextField</code> to create the peer for.
216 *
217 * @return The peer for the specified <code>TextField</code> object.
218 */
219protected abstract TextFieldPeer
220createTextField(TextField target);
221
222/*************************************************************************/
223
224/**
225 * Creates a peer object for the specified <code>Label</code>.
226 *
227 * @param target The <code>Label</code> to create the peer for.
228 *
229 * @return The peer for the specified <code>Label</code> object.
230 */
231protected abstract LabelPeer
232createLabel(Label target);
233
234/*************************************************************************/
235
236/**
237 * Creates a peer object for the specified <code>List</code>.
238 *
239 * @param target The <code>List</code> to create the peer for.
240 *
241 * @return The peer for the specified <code>List</code> object.
242 */
243protected abstract ListPeer
244createList(List target);
245
246/*************************************************************************/
247
248/**
249 * Creates a peer object for the specified <code>Checkbox</code>.
250 *
251 * @param target The <code>Checkbox</code> to create the peer for.
252 *
253 * @return The peer for the specified <code>Checkbox</code> object.
254 */
255protected abstract CheckboxPeer
256createCheckbox(Checkbox target);
257
258/*************************************************************************/
259
260/**
261 * Creates a peer object for the specified <code>Scrollbar</code>.
262 *
263 * @param target The <code>Scrollbar</code> to create the peer for.
264 *
265 * @return The peer for the specified <code>Scrollbar</code> object.
266 */
267protected abstract ScrollbarPeer
268createScrollbar(Scrollbar target);
269
270/*************************************************************************/
271
272/**
273 * Creates a peer object for the specified <code>ScrollPane</code>.
274 *
275 * @param target The <code>ScrollPane</code> to create the peer for.
276 *
277 * @return The peer for the specified <code>ScrollPane</code> object.
278 */
279protected abstract ScrollPanePeer
280createScrollPane(ScrollPane target);
281
282/*************************************************************************/
283
284/**
285 * Creates a peer object for the specified <code>TextArea</code>.
286 *
287 * @param target The <code>TextArea</code> to create the peer for.
288 *
289 * @return The peer for the specified <code>TextArea</code> object.
290 */
291protected abstract TextAreaPeer
292createTextArea(TextArea target);
293
294/*************************************************************************/
295
296/**
297 * Creates a peer object for the specified <code>Choice</code>.
298 *
299 * @param target The <code>Choice</code> to create the peer for.
300 *
301 * @return The peer for the specified <code>Choice</code> object.
302 */
303protected abstract ChoicePeer
304createChoice(Choice target);
305
306/*************************************************************************/
307
308/**
309 * Creates a peer object for the specified <code>Frame</code>.
310 *
311 * @param target The <code>Frame</code> to create the peer for.
312 *
313 * @return The peer for the specified <code>Frame</code> object.
314 */
315protected abstract FramePeer
316createFrame(Frame target);
317
318/*************************************************************************/
319
320/**
321 * Creates a peer object for the specified <code>Canvas</code>.
322 *
323 * @param target The <code>Canvas</code> to create the peer for.
324 *
325 * @return The peer for the specified <code>Canvas</code> object.
326 */
327protected abstract CanvasPeer
328createCanvas(Canvas target);
329
330/*************************************************************************/
331
332/**
333 * Creates a peer object for the specified <code>Panel</code>.
334 *
335 * @param target The <code>Panel</code> to create the peer for.
336 *
337 * @return The peer for the specified <code>Panel</code> object.
338 */
339protected abstract PanelPeer
340createPanel(Panel target);
341
342/*************************************************************************/
343
344/**
345 * Creates a peer object for the specified <code>Window</code>.
346 *
347 * @param target The <code>Window</code> to create the peer for.
348 *
349 * @return The peer for the specified <code>Window</code> object.
350 */
351protected abstract WindowPeer
352createWindow(Window target);
353
354/*************************************************************************/
355
356/**
357 * Creates a peer object for the specified <code>Dialog</code>.
358 *
359 * @param target The dialog to create the peer for
360 *
361 * @return The peer for the specified font name.
362 */
363protected abstract DialogPeer
364createDialog(Dialog target);
365
366/*************************************************************************/
367
368/**
369 * Creates a peer object for the specified <code>MenuBar</code>.
370 *
371 * @param target The <code>MenuBar</code> to create the peer for.
372 *
373 * @return The peer for the specified <code>MenuBar</code> object.
374 */
375protected abstract MenuBarPeer
376createMenuBar(MenuBar target);
377
378/*************************************************************************/
379
380/**
381 * Creates a peer object for the specified <code>Menu</code>.
382 *
383 * @param target The <code>Menu</code> to create the peer for.
384 *
385 * @return The peer for the specified <code>Menu</code> object.
386 */
387protected abstract MenuPeer
388createMenu(Menu target);
389
390/*************************************************************************/
391
392/**
393 * Creates a peer object for the specified <code>PopupMenu</code>.
394 *
395 * @param target The <code>PopupMenu</code> to create the peer for.
396 *
397 * @return The peer for the specified <code>PopupMenu</code> object.
398 */
399protected abstract PopupMenuPeer
400createPopupMenu(PopupMenu target);
401
402/*************************************************************************/
403
404/**
405 * Creates a peer object for the specified <code>MenuItem</code>.
406 *
407 * @param target The <code>MenuItem</code> to create the peer for.
408 *
409 * @return The peer for the specified <code>MenuItem</code> object.
410 */
411protected abstract MenuItemPeer
412createMenuItem(MenuItem target);
413
414/*************************************************************************/
415
416/**
417 * Creates a peer object for the specified <code>FileDialog</code>.
418 *
419 * @param target The <code>FileDialog</code> to create the peer for.
420 *
421 * @return The peer for the specified <code>FileDialog</code> object.
422 */
423protected abstract FileDialogPeer
424createFileDialog(FileDialog target);
425
426/*************************************************************************/
427
428/**
429 * Creates a peer object for the specified <code>CheckboxMenuItem</code>.
430 *
431 * @param target The <code>CheckboxMenuItem</code> to create the peer for.
432 *
433 * @return The peer for the specified <code>CheckboxMenuItem</code> object.
434 */
435protected abstract CheckboxMenuItemPeer
436createCheckboxMenuItem(CheckboxMenuItem target);
437
438/*************************************************************************/
439
440/**
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.
445 *
446 * XXX: FIXME
447 *
448 * @param target The <code>Component</code> to create the peer for.
449 *
450 * @return The peer for the specified <code>Component</code> object.
451 */
452protected LightweightPeer
453createComponent(Component target)
454{
455 return null;
456}
457
458/*************************************************************************/
459
460/**
461 * Creates a peer object for the specified font name.
462 *
463 * @param name The font to create the peer for.
464 * @param style The font style to create the peer for.
465 *
466 * @return The peer for the specified font name.
467 */
468protected abstract FontPeer
469getFontPeer(String name, int style);
470
471/*************************************************************************/
472
473/**
474 * Copies the current system colors into the specified array. This is
475 * the interface used by the <code>SystemColors</code> class.
476 *
477 * @param colors The array to copy the system colors into.
478 */
479protected void
480loadSystemColors(int systemColors[])
481{
482}
483
484/*************************************************************************/
485
486/**
487 * Returns the dimensions of the screen in pixels.
488 *
489 * @return The dimensions of the screen in pixels.
490 */
491public abstract Dimension
492getScreenSize();
493
494/*************************************************************************/
495
496/**
497 * Returns the screen resolution in dots per square inch.
498 *
499 * @return The screen resolution in dots per square inch.
500 */
501public abstract int
502getScreenResolution();
503
504/*************************************************************************/
505
506/**
507 * Returns the color model of the screen.
508 *
509 * @return The color model of the screen.
510 */
511public abstract ColorModel
512getColorModel();
513
514/*************************************************************************/
515
516/**
517 * Returns the names of the available fonts.
518 *
519 * @return The names of the available fonts.
520 */
521public abstract String[]
522getFontList();
523
524/*************************************************************************/
525
526/**
527 * Return the font metrics for the specified font
528 *
529 * @param name The name of the font to return metrics for.
530 *
531 * @return The requested font metrics.
532 */
533public abstract FontMetrics
534getFontMetrics(Font name);
535
536/*************************************************************************/
537
538/**
539 * Flushes any buffered data to the screen so that it is in sync with
540 * what the AWT system has drawn to it.
541 */
542public abstract void
543sync();
544
545/*************************************************************************/
546
547/**
548 * Returns an image from the specified file, which must be in a
549 * recognized format. Supported formats vary from toolkit to toolkit.
550 *
551 * @return name The name of the file to read the image from.
552 */
553public abstract Image
554getImage(String name);
555
556/*************************************************************************/
557
558/**
559 * Returns an image from the specified URL, which must be in a
560 * recognized format. Supported formats vary from toolkit to toolkit.
561 *
562 * @return url The URl to read the image from.
563 */
564public abstract Image
565getImage(URL url);
566
567/*************************************************************************/
568
569/**
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.
573 *
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
578 * process.
579 *
580 * @return <code>true</code> if the image is already prepared for rendering,
581 * <code>false</code> otherwise.
582 */
583public abstract boolean
584prepareImage(Image image, int width, int height, ImageObserver observer);
585
586/*************************************************************************/
587
588/**
589 * Checks the status of specified image as it is being readied for
590 * rendering.
591 *
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
596 * process.
597 *
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.
601 */
602public abstract int
603checkImage(Image image, int width, int height, ImageObserver observer);
604
605/*************************************************************************/
606
607/**
608 * Creates an image using the specified <code>ImageProducer</code>
609 *
610 * @param producer The <code>ImageProducer</code> to create the image from.
611 *
612 * @return The created image.
613 */
614public abstract Image
615createImage(ImageProducer producer);
616
617/*************************************************************************/
618
619/**
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.
623 *
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.
627 *
628 * @return The created image.
629 */
630public abstract Image
631createImage(byte[] data, int offset, int len);
632
633/*************************************************************************/
634
635/**
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.
638 *
639 * @param data The raw image data.
640 *
641 * @return The created image.
642 */
643public Image
644createImage(byte[] data)
645{
646 return(createImage(data, 0, data.length));
647}
648
649public abstract Image
650createImage(String filename);
651
652public abstract Image
653createImage(URL url);
654
655
656/*************************************************************************/
657
658/**
659 * Returns a instance of <code>PrintJob</code> for the specified
660 * arguments.
661 *
662 * @param frame The window initiating the print job.
663 * @param title The print job title.
664 * @param props The print job properties.
665 *
666 * @return The requested print job, or <code>null</code> if the job
667 * was cancelled.
668 */
669public abstract PrintJob
670getPrintJob(Frame frame, String title, Properties props);
671
672/*************************************************************************/
673
674/**
675 * Returns the system clipboard.
676 *
677 * @return THe system clipboard.
678 */
679public abstract Clipboard
680getSystemClipboard();
681
682/*************************************************************************/
683
684/**
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.
688 *
689 * @return The key mask for the menu accelerator key.
690 */
691public int
692getMenuShortcutKeyMask()
693{
694 return Event.CTRL_MASK;
695}
696
697public boolean
698getLockingKeyState(int keyCode)
699{
700 if (keyCode != KeyEvent.VK_CAPS_LOCK
701 && keyCode != KeyEvent.VK_NUM_LOCK
702 && keyCode != KeyEvent.VK_SCROLL_LOCK)
703 throw new IllegalArgumentException();
704
705 throw new UnsupportedOperationException();
706}
707
708public void
709setLockingKeyState(int keyCode, boolean on)
710{
711 if (keyCode != KeyEvent.VK_CAPS_LOCK
712 && keyCode != KeyEvent.VK_NUM_LOCK
713 && keyCode != KeyEvent.VK_SCROLL_LOCK)
714 throw new IllegalArgumentException();
715
716 throw new UnsupportedOperationException();
717}
718
719/*************************************************************************/
720
721/**
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.
725 *
726 * @return The event queue for this applet (or application)
727 */
728public final EventQueue
729getSystemEventQueue()
730{
731 return(getSystemEventQueueImpl());
732}
733
734/*************************************************************************/
735
736/**
737 * // FIXME: What does this do?
738 */
739protected abstract EventQueue
740getSystemEventQueueImpl();
741
742/*************************************************************************/
743
744/**
745 * Causes a "beep" tone to be generated.
746 */
747public abstract void
748beep();
749
750public Cursor
751createCustomCursor(Image cursor, Point hotSpot, String name)
752 throws IndexOutOfBoundsException
753{
754 // Presumably the only reason this isn't abstract is for backwards
755 // compatibility? FIXME?
756 return null;
757}
758
759public Dimension
760getBestCursorSize(int preferredWidth, int preferredHeight)
761{
762 return new Dimension (0,0);
763}
764
765public int
766getMaximumCursorColors()
767{
768 return 0;
769}
770
771public final Object
772getDesktopProperty(String propertyName)
773{
774 return desktopProperties.get(propertyName);
775}
776
777protected final void
778setDesktopProperty(String name, Object newValue)
779{
780 Object oldValue = getDesktopProperty(name);
781 desktopProperties.put(name, newValue);
782 changeSupport.firePropertyChange(name, oldValue, newValue);
783}
784protected Object
785lazilyLoadDesktopProperty(String name)
786{
787 // FIXME - what is this??
788 return null;
789}
790
791protected void
792initializeDesktopProperties()
793{
794 // Overridden by toolkit implementation?
795}
796
797public void
798addPropertyChangeListener(String name, PropertyChangeListener pcl)
799{
800 changeSupport.addPropertyChangeListener(name, pcl);
801}
802
803public void
804removePropertyChangeListener(String name, PropertyChangeListener pcl)
805{
806 changeSupport.removePropertyChangeListener(name, pcl);
807}
808
809public void
810addAWTEventListener(AWTEventListener listener, long eventMask)
811{
812 // SecurityManager s = System.getSecurityManager();
813 // if (s != null)
814 // s.checkPermission(AWTPermission("listenToAllAWTEvents"));
815
816 // FIXME
817}
818
819public void
820removeAWTEventListener(AWTEventListener listener)
821{
822 // FIXME
823}
824
825} // class Toolkit
Note: See TracBrowser for help on using the repository browser.