home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / win32 / MToolkit.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  7.8 KB  |  292 lines

  1. /*
  2.  * @(#)MToolkit.java    1.35 96/03/28 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.awt.win32;
  21.  
  22. import java.util.Hashtable;
  23. import java.awt.*;
  24. import java.awt.image.*;
  25. import java.awt.peer.*;
  26. import java.io.*;
  27. import java.net.URL;
  28. import java.util.Properties;
  29. import sun.awt.image.FileImageSource;
  30. import sun.awt.image.URLImageSource;
  31. import sun.awt.image.ImageRepresentation;
  32.  
  33. public class MToolkit extends java.awt.Toolkit implements Runnable {
  34.     boolean serverThread = true;
  35.  
  36.     ThreadGroup getSystemThreadGroup()
  37.     {
  38.         ThreadGroup tg = Thread.currentThread().getThreadGroup();
  39.  
  40.         while (tg.getParent() != null)
  41.         {
  42.             tg = tg.getParent();
  43.         }
  44.        
  45.         return tg;
  46.     }
  47.     
  48.     public MToolkit() {
  49.         synchronized (this) {
  50.         new Thread(getSystemThreadGroup(), this, "AWT-Win32").start();
  51.             try {
  52.                 wait();
  53.             } catch (InterruptedException e) {
  54.                 System.err.println("Interrupted while starting " +
  55.                                    "AWT main thread, aborting.");
  56.                 System.exit(1);
  57.             }
  58.         }
  59.         serverThread = false;
  60.     new Thread(this, "AWT-Callback-Win32").start();
  61.     }
  62.  
  63.     public native void init(Thread t);
  64.  
  65.     public void run() {
  66.         if (serverThread) {
  67.         try {
  68.         System.loadLibrary("msawt");
  69.         init(Thread.currentThread());
  70.         } catch (Throwable e) {
  71.                 System.err.println("Exception received while initializing " +
  72.                                    "AWT toolkit, aborting.");
  73.         e.printStackTrace();
  74.         System.exit(1);
  75.         }
  76.         eventLoop();
  77.         } else {
  78.             callbackLoop();
  79.         }
  80.     }
  81.  
  82.     /* 
  83.      * Notifies the thread that started the server
  84.      * to indicate that initialization is complete.
  85.      * Then enters an infinite loop that retrieves and processes events.  
  86.      */
  87.     native void eventLoop();
  88.  
  89.     /* 
  90.      * A thread needs to be created to call this method.  This
  91.      * thread will be responsible for making all the callbacks
  92.      * from the window system to the java code.
  93.      */
  94.     native void callbackLoop();
  95.  
  96.     /*
  97.      * Create peer objects.
  98.      */
  99.  
  100.     public ButtonPeer createButton(Button target) {
  101.     return new MButtonPeer(target);
  102.     }
  103.  
  104.     public TextFieldPeer createTextField(TextField target) {
  105.     return new MTextFieldPeer(target);
  106.     }
  107.  
  108.     public LabelPeer createLabel(Label target) {
  109.     return new MLabelPeer(target);
  110.     }
  111.  
  112.     public ListPeer createList(List target) {
  113.     return new MListPeer(target);
  114.     }
  115.  
  116.     public CheckboxPeer createCheckbox(Checkbox target) {
  117.     return new MCheckboxPeer(target);
  118.     }
  119.  
  120.     public ScrollbarPeer createScrollbar(Scrollbar target) {
  121.     return new MScrollbarPeer(target);
  122.     }
  123.  
  124.     public TextAreaPeer createTextArea(TextArea target) {
  125.     return new MTextAreaPeer(target);
  126.     }
  127.  
  128.     public ChoicePeer createChoice(Choice target) {
  129.     return new MChoicePeer(target);
  130.     }
  131.  
  132.     public FramePeer  createFrame(Frame target) {
  133.     return new MFramePeer(target);
  134.     }
  135.  
  136.     public CanvasPeer createCanvas(Canvas target) {
  137.     return new MCanvasPeer(target);
  138.     }
  139.  
  140.     public PanelPeer createPanel(Panel target) {
  141.     return new MPanelPeer(target);
  142.     }
  143.  
  144.     public WindowPeer createWindow(Window target) {
  145.     return new MWindowPeer(target);
  146.     }
  147.  
  148.     public DialogPeer createDialog(Dialog target) {
  149.     return new MDialogPeer(target);
  150.     }
  151.  
  152.     public FileDialogPeer createFileDialog(FileDialog target) {
  153.     return new MFileDialogPeer(target);
  154.     }
  155.  
  156.     public MenuBarPeer createMenuBar(MenuBar target) {
  157.     return new MMenuBarPeer(target);
  158.     }
  159.  
  160.     public MenuPeer createMenu(Menu target) {
  161.     return new MMenuPeer(target);
  162.     }
  163.  
  164.     public MenuItemPeer createMenuItem(MenuItem target) {
  165.     return new MMenuItemPeer(target);
  166.     }
  167.  
  168.     public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
  169.     return new MCheckboxMenuItemPeer(target);
  170.     }
  171.  
  172.     static Hashtable imgHash = new Hashtable();
  173.  
  174.     static synchronized Image getImageFromHash(Toolkit tk, URL url) {
  175.     SecurityManager security = System.getSecurityManager();
  176.     if (security != null) {
  177.         security.checkConnect(url.getHost(), url.getPort());
  178.     }
  179.     Image img = (Image)imgHash.get(url);
  180.     if (img == null) {
  181.         try {    
  182.         img = tk.createImage(new URLImageSource(url));
  183.         imgHash.put(url, img);
  184.         } catch (Exception e) {
  185.         }
  186.     }
  187.     return img;
  188.     }
  189.  
  190.     static synchronized Image getImageFromHash(Toolkit tk, String filename) {
  191.     SecurityManager security = System.getSecurityManager();
  192.     if (security != null) {
  193.         security.checkRead(filename);
  194.     }
  195.     Image img = (Image)imgHash.get(filename);
  196.     if (img == null) {
  197.         try {    
  198.         img = tk.createImage(new FileImageSource(filename));
  199.         imgHash.put(filename, img);
  200.         } catch (Exception e) {
  201.         }
  202.     }
  203.     return img;
  204.     }
  205.  
  206.     public Image getImage(String filename) {
  207.     return getImageFromHash(this, filename);
  208.     }
  209.  
  210.     public Image getImage(URL url) {
  211.     return getImageFromHash(this, url);
  212.     }
  213.  
  214.     static boolean prepareScrImage(Image img, int w, int h, ImageObserver o) {
  215.     if (w == 0 || h == 0) {
  216.         return true;
  217.     }
  218.     Win32Image ximg = (Win32Image) img;
  219.     if (ximg.hasError()) {
  220.         if (o != null) {
  221.         o.imageUpdate(img, ImageObserver.ERROR|ImageObserver.ABORT,
  222.                   -1, -1, -1, -1);
  223.         }
  224.         return false;
  225.     }
  226.     if (w < 0) w = -1;
  227.     if (h < 0) h = -1;
  228.     ImageRepresentation ir = ximg.getImageRep(w, h);
  229.     return ir.prepare(o);
  230.     }
  231.  
  232.     static int checkScrImage(Image img, int w, int h, ImageObserver o) {
  233.     Win32Image ximg = (Win32Image) img;
  234.     int repbits;
  235.     if (w == 0 || h == 0) {
  236.         repbits = ImageObserver.ALLBITS;
  237.     } else {
  238.         if (w < 0) w = -1;
  239.         if (h < 0) h = -1;
  240.         repbits = ximg.getImageRep(w, h).check(o);
  241.     }
  242.     return ximg.check(o) | repbits;
  243.     }
  244.  
  245.     public int checkImage(Image img, int w, int h, ImageObserver o) {
  246.     return checkScrImage(img, w, h, o);
  247.     }
  248.  
  249.     public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
  250.     return prepareScrImage(img, w, h, o);
  251.     }
  252.  
  253.     public Image createImage(ImageProducer producer) {
  254.     return new Win32Image(producer);
  255.     }
  256.  
  257.     static native ColorModel makeColorModel();
  258.     static ColorModel screenmodel;
  259.  
  260.     static ColorModel getStaticColorModel() {
  261.     if (screenmodel == null) {
  262.         screenmodel = makeColorModel();
  263.     }
  264.     return screenmodel;
  265.     }
  266.  
  267.     public ColorModel getColorModel() {
  268.     return getStaticColorModel();
  269.     }
  270.  
  271.     public Dimension getScreenSize() {
  272.     return new Dimension(getScreenWidth(), getScreenHeight());
  273.     }
  274.  
  275.     public native int getScreenResolution();
  276.     native int getScreenWidth();
  277.     native int getScreenHeight();
  278.  
  279.     public String[] getFontList() {
  280.     // REMIND: return something useful
  281.     String list[] = {"Dialog", "Helvetica", "TimesRoman", "Courier", 
  282.                          "DialogInput", "ZapfDingbats"};
  283.     return list;
  284.     }
  285.  
  286.     public FontMetrics getFontMetrics(Font font) {
  287.     return Win32FontMetrics.getFontMetrics(font);
  288.     }
  289.  
  290.     public native void sync();
  291. }
  292.