001: /*
002: * @(#)MWToolkit.java 1.23 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.awt;
029:
030: import java.awt.image.ColorModel;
031: import java.awt.image.ImageObserver;
032: import java.awt.image.ImageProducer;
033: import java.net.URL;
034: import java.util.Properties;
035: import java.util.Hashtable;
036: import java.util.Map;
037: import java.util.Iterator;
038: import sun.awt.image.ByteArrayImageSource;
039: import sun.awt.image.FileImageSource;
040: import sun.awt.image.URLImageSource;
041:
042: /** The toolkit used by this AWT implementation based on the MicroWindows library.
043: @version 1.8, 11/27/01
044: */
045:
046: class MWToolkit extends Toolkit {
047: private EventQueue eventQueue = new EventQueue();
048: private MWGraphicsEnvironment localEnv = new MWGraphicsEnvironment();
049: private MWGraphicsConfiguration defaultGC = (MWGraphicsConfiguration) localEnv.defaultScreenDevice
050: .getDefaultConfiguration();
051: private static Hashtable cachedImages = new Hashtable();
052:
053: public MWToolkit() {
054: }
055:
056: /**
057: * Gets the size of the screen.
058: * @return the size of this toolkit's screen, in pixels.
059: * @since JDK1.0
060: */
061: public Dimension getScreenSize() {
062: Rectangle dims = GraphicsEnvironment
063: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
064: .getDefaultConfiguration().getBounds();
065: return new Dimension(dims.width, dims.height);
066: }
067:
068: Graphics getGraphics(Window window) {
069: return new MWGraphics(window);
070: }
071:
072: GraphicsEnvironment getLocalGraphicsEnvironment() {
073: return localEnv;
074: }
075:
076: /**
077: * Returns the screen resolution in dots-per-inch.
078: * @return this toolkit's screen resolution, in dots-per-inch.
079: * @since JDK1.0
080: */
081: public native int getScreenResolution();
082:
083: /**
084: * Determines the color model of this toolkit's screen.
085: * <p>
086: * <code>ColorModel</code> is an class that
087: * encapsulates the ability to translate between the
088: * pixel values of an image and its red, green, blue,
089: * and alpha components.
090: * <p>
091: * This toolkit method is called by the
092: * <code>getColorModel</code> method
093: * of the <code>Component</code> class.
094: * @return the color model of this toolkit's screen.
095: * @see java.awt.image.ColorModel
096: * @see java.awt.Component#getColorModel
097: * @since JDK1.0
098: */
099: public ColorModel getColorModel() {
100: return GraphicsEnvironment.getLocalGraphicsEnvironment()
101: .getDefaultScreenDevice().getDefaultConfiguration()
102: .getColorModel();
103: }
104:
105: /**
106: * Returns the names of the available fonts in this toolkit.<p>
107: * For 1.1, the following font names are deprecated (the replacement
108: * name follows):
109: * <ul>
110: * <li>TimesRoman (use Serif)
111: * <li>Helvetica (use SansSerif)
112: * <li>Courier (use Monospaced)
113: * </ul><p>
114: * The ZapfDingbats font is also deprecated in 1.1, but only as a
115: * separate fontname. Unicode defines the ZapfDingbat characters
116: * starting at \u2700, and as of 1.1 Java supports those characters.
117: * @return the names of the available fonts in this toolkit.
118: * @since JDK1.0
119: */
120: public String[] getFontList() {
121: return MWFontMetrics.getFontList();
122: }
123:
124: /**
125: * Gets the screen metrics of the font.
126: * @param font a font.
127: * @return the screen metrics of the specified font in this toolkit.
128: * @since JDK1.0
129: */
130: public FontMetrics getFontMetrics(Font font) {
131: return MWFontMetrics.getFontMetrics(font);
132: }
133:
134: /**
135: * Synchronizes this toolkit's graphics state. Some window systems
136: * may do buffering of graphics events.
137: * <p>
138: * This method ensures that the display is up-to-date. It is useful
139: * for animation.
140: * @since JDK1.0
141: */
142: public void sync() {
143: }
144:
145: static void clearCache(MWImage image) {
146: synchronized (cachedImages) {
147: Iterator i = cachedImages.entrySet().iterator();
148: while (i.hasNext()) {
149: Map.Entry entry = (Map.Entry) i.next();
150: if (entry.getValue() == image) {
151: i.remove();
152: return;
153: }
154: }
155: }
156: }
157:
158: /**
159: * Returns an image which gets pixel data from the specified file.
160: * The underlying toolkit attempts to resolve multiple requests
161: * with the same filename to the same returned Image.
162: * Since the mechanism required to facilitate this sharing of
163: * Image objects may continue to hold onto images that are no
164: * longer of use for an indefinite period of time, developers
165: * are encouraged to implement their own caching of images by
166: * using the createImage variant wherever available.
167: * <h3>Compatibility</h3>
168: * PersonalJava does not require support of the PNG image file format.
169: * @param filename Filename must reference an image format that
170: * is recognized by this toolkit. The toolkit must be able
171: * to create images from the following image file formats:
172: * GIF, JPEG(JFIF), XBM, and PNG.
173: * @return an image which gets its pixel data from
174: * the specified file.
175: * @see java.awt.Image
176: * @see java.awt.Toolkit#createImage(java.lang.String)
177: */
178: public Image getImage(String filename) {
179: if (cachedImages.containsKey(filename))
180: return (Image) cachedImages.get(filename);
181: Image newImage = createImage(filename);
182: if (newImage != null)
183: cachedImages.put(filename, newImage);
184: return newImage;
185: }
186:
187: /**
188: * Returns an image which gets pixel data from the specified URL.
189: * The underlying toolkit attempts to resolve multiple requests
190: * with the same URL to the same returned Image.
191: * Since the mechanism required to facilitate this sharing of
192: * Image objects may continue to hold onto images that are no
193: * longer of use for an indefinite period of time, developers
194: * are encouraged to implement their own caching of images by
195: * using the createImage variant wherever available.
196: * <h3>Compatibility</h3>
197: * PersonalJava does not require support of the PNG image file format.
198: * @param url URL must reference an image format that
199: * is recognized by this toolkit. The toolkit must be
200: * able to create images from the following image file formats:
201: * GIF, JPEG(JFIF), XBM, and PNG.
202: * @return an image which gets its pixel data from
203: * the specified URL.
204: * @see java.awt.Image
205: * @see java.awt.Toolkit#createImage(java.net.URL)
206: */
207: public Image getImage(URL url) {
208: if (cachedImages.containsKey(url))
209: return (Image) cachedImages.get(url);
210: Image newImage = createImage(url);
211: if (newImage != null)
212: cachedImages.put(url, newImage);
213: return newImage;
214: }
215:
216: /**
217: * Returns an image which gets pixel data from the specified file.
218: * The returned Image is a new object which will not be shared
219: * with any other caller of this method or its getImage variant.
220: * @param filename the name of a file containing pixel data
221: * in a recognized file format.
222: * @return an image which gets its pixel data from
223: * the specified file.
224: * @see java.awt.Toolkit#getImage(java.lang.String)
225: */
226:
227: public Image createImage(String filename) {
228: ImageProducer ip = new FileImageSource(filename);
229: Image newImage = createImage(ip);
230: return newImage;
231: }
232:
233: /**
234: * Returns an image which gets pixel data from the specified URL.
235: * The returned Image is a new object which will not be shared
236: * with any other caller of this method or its getImage variant.
237: * @param url the URL to use in fetching the pixel data.
238: * @return an image which gets its pixel data from
239: * the specified URL.
240: * @see java.awt.Toolkit#getImage(java.net.URL)
241: */
242:
243: public Image createImage(URL url) {
244: ImageProducer ip = new URLImageSource(url);
245: Image newImage = createImage(ip);
246: return newImage;
247: }
248:
249: /**
250: * Prepares an image for rendering.
251: * <p>
252: * If the values of the width and height arguments are both
253: * <code>-1</code>, this method prepares the image for rendering
254: * on the default screen; otherwise, this method prepares an image
255: * for rendering on the default screen at the specified width and height.
256: * <p>
257: * The image data is downloaded asynchronously in another thread,
258: * and an appropriately scaled screen representation of the image is
259: * generated.
260: * <p>
261: * This method is called by components <code>prepareImage</code>
262: * methods.
263: * <p>
264: * Information on the flags returned by this method can be found
265: * with the definition of the <code>ImageObserver</code> interface.
266:
267: * @param image the image for which to prepare a
268: * screen representation.
269: * @param width the width of the desired screen
270: * representation, or <code>-1</code>.
271: * @param height the height of the desired screen
272: * representation, or <code>-1</code>.
273: * @param observer the <code>ImageObserver</code>
274: * object to be notified as the
275: * image is being prepared.
276: * @return <code>true</code> if the image has already been
277: * fully prepared; <code>false</code> otherwise.
278: * @see java.awt.Component#prepareImage(java.awt.Image,
279: * java.awt.image.ImageObserver)
280: * @see java.awt.Component#prepareImage(java.awt.Image,
281: * int, int, java.awt.image.ImageObserver)
282: * @see java.awt.image.ImageObserver
283: * @since JDK1.0
284: */
285: public boolean prepareImage(Image image, int width, int height,
286: ImageObserver observer) {
287: if (!(image instanceof MWImage)) {
288: return true;
289: }
290: MWImage mwimg = (MWImage) image;
291: return mwimg.prepareImage(width, height, observer);
292: }
293:
294: /**
295: * Indicates the construction status of a specified image that is
296: * being prepared for display.
297: * <p>
298: * If the values of the width and height arguments are both
299: * <code>-1</code>, this method returns the construction status of
300: * a screen representation of the specified image in this toolkit.
301: * Otherwise, this method returns the construction status of a
302: * scaled representation of the image at the specified width
303: * and height.
304: * <p>
305: * This method does not cause the image to begin loading.
306: * An application must call <code>prepareImage</code> to force
307: * the loading of an image.
308: * <p>
309: * This method is called by the component's <code>checkImage</code>
310: * methods.
311: * <p>
312: * Information on the flags returned by this method can be found
313: * with the definition of the <code>ImageObserver</code> interface.
314: * @param image the image whose status is being checked.
315: * @param width the width of the scaled version whose status is
316: * being checked, or <code>-1</code>.
317: * @param height the height of the scaled version whose status
318: * is being checked, or <code>-1</code>.
319: * @param observer the <code>ImageObserver</code> object to be
320: * notified as the image is being prepared.
321: * @return the bitwise inclusive <strong>OR</strong> of the
322: * <code>ImageObserver</code> flags for the
323: * image data that is currently available.
324: * @see java.awt.Toolkit#prepareImage(java.awt.Image,
325: * int, int, java.awt.image.ImageObserver)
326: * @see java.awt.Component#checkImage(java.awt.Image,
327: * java.awt.image.ImageObserver)
328: * @see java.awt.Component#checkImage(java.awt.Image,
329: * int, int, java.awt.image.ImageObserver)
330: * @see java.awt.image.ImageObserver
331: * @since JDK1.0
332: */
333: public int checkImage(Image image, int width, int height,
334: ImageObserver observer) {
335: if (!(image instanceof MWImage)) {
336: return ImageObserver.ALLBITS;
337: }
338: MWImage mwimg = (MWImage) image;
339: return mwimg.getStatus(observer);
340: }
341:
342: /**
343: * Creates an image with the specified image producer.
344: * @param producer the image producer to be used.
345: * @return an image with the specified image producer.
346: * @see java.awt.Image
347: * @see java.awt.image.ImageProducer
348: * @see java.awt.Component#createImage(java.awt.image.ImageProducer)
349: * @since JDK1.0
350: */
351: public Image createImage(ImageProducer producer) {
352: return new MWImage(producer);
353: }
354:
355: /**
356: * Creates an image which decodes the image stored in the specified
357: * byte array, and at the specified offset and length.
358: * The data must be in some image format, such as GIF or JPEG,
359: * that is supported by this toolkit.
360: * @param imagedata an array of bytes, representing
361: * image data in a supported image format.
362: * @param imageoffset the offset of the beginning
363: * of the data in the array.
364: * @param imagelength the length of the data in the array.
365: * @return an image.
366: * @since JDK1.1
367: */
368: public Image createImage(byte[] imagedata, int imageoffset,
369: int imagelength) {
370: ImageProducer ip = new ByteArrayImageSource(imagedata,
371: imageoffset, imagelength);
372: Image newImage = createImage(ip);
373: return newImage;
374: }
375:
376: Image createImage(Component component, int width, int height) {
377: float size = (float) width * height;
378: if (width <= 0 || height <= 0) {
379: throw new IllegalArgumentException("Width (" + width
380: + ") and height (" + height + ") must be > 0");
381: }
382: if (size >= Integer.MAX_VALUE) {
383: throw new IllegalArgumentException("Dimensions (width="
384: + width + " height=" + height + ") are too large");
385: }
386: return new MWOffscreenImage(component, width, height, defaultGC);
387: }
388:
389: /**
390: * Emits an audio beep.
391: * @since JDK1.1
392: */
393: public native void beep();
394:
395: /*
396: * Get the application's or applet's EventQueue instance, without
397: * checking access. For security reasons, this can only be called
398: * from a Toolkit subclass. Implementations wishing to modify
399: * the default EventQueue support should subclass this method.
400: */
401: protected EventQueue getSystemEventQueueImpl() {
402: return eventQueue;
403: }
404: }
|