📄 toolkit.java
字号:
package java.awt;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.Transferable;import java.awt.event.InputEvent;import java.awt.image.ColorModel;import java.awt.image.ImageObserver;import java.awt.image.ImageProducer;import java.awt.peer.ComponentPeer;import java.awt.peer.LightweightPeer;import java.awt.peer.WindowPeer;import java.io.File;import java.io.PrintStream;import java.net.URL;import java.util.Properties;import kaffe.util.Ptr;import kaffe.util.log.LogClient;import kaffe.util.log.LogStream;/** * Toolkit - used to be an abstract factory for peers, but since we don't have * peers, it is just a simple anchor for central mechanisms (like System- * EventQueue etc.) and a wrapper for all native methods. Of course, it is a * singleton. * * Copyright (c) 1998 * Transvirtual Technologies, Inc. All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. * * @author P.C.Mehlitz */class FlushThread extends Thread{ boolean stop; int flushInterval;FlushThread ( int interval ) { super( "AWT-Flusher"); flushInterval = interval; setPriority( Thread.MIN_PRIORITY + 1);}public void run () { while ( !stop ) { Toolkit.tlkFlush(); try { Thread.sleep( flushInterval); } catch ( Exception x ) {} }}void stopFlushing () { stop = true;}}class NativeCollector extends Thread{NativeCollector () { super( "AWT-native");}public void run () { // this does not return until the we shut down the system, since it // consititutes the native dispatcher loop. Don't be confused about // tlkInit being a sync method. It gives up the lock in the native // layer before falling into its dispatcher loop try { if ( !Toolkit.tlkInit( System.getProperty( "awt.display")) ) { throw new AWTError( "native layer init failed"); } } catch ( Throwable x ) { x.printStackTrace(); }}}public class Toolkit{ static Toolkit singleton; static Dimension screenSize; static int resolution; static EventQueue eventQueue; static EventDispatchThread eventThread; static NativeClipboard clipboard; static ColorModel colorModel; static LightweightPeer lightweightPeer = new LightweightPeer() {}; static WindowPeer windowPeer = new WindowPeer() {}; static FlushThread flushThread; static NativeCollector collectorThread; static int flags; final static int FAILED = -1; final static int IS_BLOCKING = 1; final static int IS_DISPATCH_EXCLUSIVE = 2; final static int NEEDS_FLUSH = 4; final static int NATIVE_DISPATCHER_LOOP = 8; final static int EXTERNAL_DECO = 16;static { System.loadLibrary( "awt"); flags = tlkProperties(); if ( (flags & NATIVE_DISPATCHER_LOOP) == 0 ) { if ( !tlkInit( System.getProperty( "awt.display")) ) { throw new AWTError( "native layer initialization failed"); } initToolkit(); } else { // Not much we can do here, we have to delegate the native init // to a own thread since tlkInit() doesn't return. Wait for this // thread to flag that initialization has been completed collectorThread = new NativeCollector(); collectorThread.start(); try { synchronized ( Toolkit.class ) { while ( singleton == null ) Toolkit.class.wait(); } } catch ( Exception x ) { x.printStackTrace(); } }}Toolkit () {}public void beep () { tlkBeep();}native static synchronized void cbdFreeClipboard ( Ptr cbdData );native static synchronized Transferable cbdGetContents( Ptr cbdData);native static synchronized Ptr cbdInitClipboard ();native static synchronized boolean cbdSetOwner ( Ptr cbdData );public int checkImage(Image image, int width, int height, ImageObserver observer) { return (image.checkImage( width, height, observer, false));}native static synchronized long clrBright ( int rgbValue );native static synchronized long clrDark ( int rgbValue );native static synchronized ColorModel clrGetColorModel();native static synchronized int clrGetPixelValue ( int rgb );native static synchronized int clrSetSystemColors ( int[] sysClrs );public Image createImage ( ImageProducer producer ) { return new Image( producer);}public Image createImage ( byte[] imageData ) { return createImage( imageData, 0, imageData.length);}public Image createImage ( byte[] imagedata, int imageoffset, int imagelength ) { return new Image( imagedata, imageoffset, imagelength);}ComponentPeer createLightweight ( Component c ) { // WARNING! this is just a dummy to enable checks like // "..getPeer() != null.. or ..peer instanceof LightweightPeer.. // see createWindow() return lightweightPeer;}static void createNative ( Component c ) { WMEvent e = null; synchronized ( Toolkit.class ) { // even if this could be done in a central location, we defer this // as much as possible because it might involve polling (for non-threaded // AWTs), slowing down the startup time if ( eventThread == null ) { startDispatch(); } } // do we need some kind of a context switch ? if ( (flags & IS_DISPATCH_EXCLUSIVE) != 0 ){ if ( (flags & NATIVE_DISPATCHER_LOOP) != 0 ){ if ( Thread.currentThread() != collectorThread ){ // this is beyond our capabilities (there is no Java message entry we can call // in the native collector), we have to revert to some native mechanism e = WMEvent.getEvent( c, WMEvent.WM_CREATE); evtSendWMEvent( e); } } else { if ( Thread.currentThread() != eventThread ){ // we can force the context switch by ourselves, no need to go native e = WMEvent.getEvent( c, WMEvent.WM_CREATE); eventQueue.postEvent( e); } } // Ok, we have a request out there, wait for it to be served if ( e != null ) { // we should check for nativeData because the event might // already be processed (depending on the thread system) while ( c.getNativeData() == null ) { synchronized ( e ) { try { e.wait(); } catch ( InterruptedException x ) {} } } return; } } else { // no need to switch threads, go native right away c.createNative(); }}protected WindowPeer createWindow ( Window w ) { // WARNING! this is just a dummy to enable checks like // "..getPeer() != null.. or ..peer instanceof LightweightPeer.. // it is NOT a real peer support. The peer field just exists to // check if a Component already passed its addNotify()/removeNotify(). // This most probably will be removed once the "isLightweightComponent()" // method gets official (1.2?) return windowPeer;}static void destroyNative ( Component c ) { WMEvent e = null; // do we need some kind of a context switch ? if ( (flags & IS_DISPATCH_EXCLUSIVE) != 0 ){ if ( (flags & NATIVE_DISPATCHER_LOOP) != 0 ){ if ( Thread.currentThread() != collectorThread ){ // this is beyond our capabilities (there is no Java message entry we can call // in the native collector), we have to revert to some native mechanism e = WMEvent.getEvent( c, WMEvent.WM_DESTROY); evtSendWMEvent( e); } } else { if ( Thread.currentThread() != eventThread ){ // we can force the context switch by ourselves, no need to go native e = WMEvent.getEvent( c, WMEvent.WM_DESTROY); eventQueue.postEvent( e); } } // Ok, we have a request out there, wait for it to be served if ( e != null ) { // we should check for nativeData because the event might // already be processed (depending on the thread system) while ( c.getNativeData() != null ) { synchronized ( e ) { try { e.wait(); } catch ( InterruptedException x ) {} } } return; } } else { // no need to switch threads, go native right away c.destroyNative(); }}native static synchronized AWTEvent evtGetNextEvent ();native static synchronized Component[] evtInit ();native static synchronized AWTEvent evtPeekEvent ();native static synchronized AWTEvent evtPeekEventId ( int eventId );native static synchronized int evtRegisterSource ( Ptr wndData );native static synchronized void evtSendWMEvent ( WMEvent e );native static synchronized int evtUnregisterSource ( Ptr wndData );native static synchronized void evtWakeup ();native static synchronized int fntBytesWidth ( Ptr fmData, byte[] data, int off, int len );native static synchronized int fntCharWidth ( Ptr fmData, char c );native static synchronized int fntCharsWidth ( Ptr fmData, char[] data, int off, int len );native static synchronized void fntFreeFont ( Ptr fontData );native static synchronized void fntFreeFontMetrics ( Ptr fmData );native static synchronized int fntGetAscent ( Ptr fmData);native static synchronized int fntGetDescent ( Ptr fmData);native static synchronized int fntGetFixedWidth ( Ptr fmData);native static synchronized int fntGetHeight ( Ptr fmData);native static synchronized int fntGetLeading ( Ptr fmData);native static synchronized int fntGetMaxAdvance ( Ptr fmData);native static synchronized int fntGetMaxAscent ( Ptr fmData);native static synchronized int fntGetMaxDescent ( Ptr fmData);native static synchronized int[] fntGetWidths ( Ptr fmData );native static synchronized Ptr fntInitFont ( String fntSpec, int style, int size );native static synchronized Ptr fntInitFontMetrics ( Ptr fontData);native static synchronized boolean fntIsWideFont ( Ptr fmData);native static synchronized int fntStringWidth ( Ptr fmData, String s );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -