1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
5 import java.awt.image.*;
6 import gnu.gcj.RawData;
8 import java.lang.reflect.*;
11 import java.awt.peer.*;
12 import org.xwt.util.*;
15 /** Platform implementation for Carbon UI on a POSIX-compliant OS (ie Mac OS X) */
16 public class Carbon extends POSIX {
18 /** hashtable of all OS X fonts; key is an XWT font name, value is WrappedRawData which stores an ATSFontRef.
19 * Initialized by natInit(). */
20 static Hashtable nativeFontCache = new Hashtable();
22 /** Cache of ATSUStyle objects; key is an XWT font spec, value is WrappedRawData which stores an ATSUStyle.
23 * According to an Apple technote, caching the style bjects can really increase performance. */
24 static Hashtable atsuStyleCache = new Hashtable();
26 /** List of all XWT font specs. Initialized by init(). */
27 static String[] fontList = null;
29 // General Methods ///////////////////////////////////////////////////////
31 protected String _getAltKeyName() { return "option"; }
32 protected String[] _listFonts() { return fontList; }
33 protected Picture _createPicture(int[] data, int w, int h) { return new CarbonPicture(data, w, h); }
34 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new CarbonDoubleBuffer(w, h); }
35 protected Surface _createSurface(Box b, boolean framed) { return new CarbonSurface(b, framed); }
36 protected boolean _needsAutoClick() { return false; }
37 protected native int _getScreenWidth();
38 protected native int _getScreenHeight();
39 protected native String _getClipBoard();
40 protected native void _setClipBoard(String s);
41 static String defaultFontName = "lucida_grande";
42 protected String _getDefaultFont() { return defaultFontName + "13"; }
43 protected native int _stringWidth(String fontSpec, String text);
44 protected native int _getMaxAscent(String font);
45 protected native int _getMaxDescent(String font);
46 protected boolean _needsAutoDoubleClick() { return false; }
48 /** Returns the ATSUStyle associated with the given XWT font spec.
49 * This method first checks its internal cache before creating the
50 * ATSUStyle object from scratch. */
51 protected RawData _getATSUStyle( String fontSpec ) {
52 WrappedRawData ret = null;
53 ret = (WrappedRawData) atsuStyleCache.get( fontSpec );
54 if (ret != null) return ret.wrapee;
56 Platform.ParsedFont pf = new Platform.ParsedFont( fontSpec );
59 if (pf.name.equals("serif")) pf.name = "lucida_grande";
60 else if (pf.name.equals("sansserif")) pf.name = "helvetica";
61 else if (pf.name.equals("monospace")) pf.name = "courier";
62 else if (pf.name.equals("dialog")) pf.name = "lucida_grande";
63 else if (pf.name.equals("tty")) pf.name = "courier";
65 // Find the ATSFontRef
66 WrappedRawData fontRef = (WrappedRawData) nativeFontCache.get( pf.name );
67 // If we couldn't find the font, use the default font
68 if ( fontRef == null ) fontRef = (WrappedRawData) nativeFontCache.get( defaultFontName );
69 if ( fontRef == null ) throw new Error( "Default font cannot be found" );
71 // Create the ATSUStyle object
72 ret = new WrappedRawData( _createATSUStyle( fontRef.wrapee, pf.size, pf.bold, pf.italic, pf.underline ) );
74 // Map this font spec to the ATSFontRef to optimize future requests
75 atsuStyleCache.put( fontSpec, ret );
80 /** Creates an ATSUStyle object with the specified attributes. */
81 protected native RawData _createATSUStyle( RawData fontRef, int fontSize, boolean isBold, boolean isItalic, boolean isUnderline );
83 /** Called once XWT is initialized and the application is running. On Mac OS X this calls
84 * RunApplicationEventLoop(). */
85 protected native void _running();
87 /** dumps a list of Mac OS X font strings. TODO: Will this be sufficient? */
88 //private native String[] listNativeFonts();
89 /** translates a font string into an ATSUFontRef? TODO: Will this be sufficient? */
90 //public static native gnu.gcj.RawData fontStringToStruct(String s);
97 // nativeFontCache contains font NAMES. Each font exists as an outline font
98 // which can be any size, plus can have real or simulated bold or italic
99 fontList = new String[nativeFontCache.size()*4];
100 Enumeration e = nativeFontCache.keys();
101 for(int i=0; e.hasMoreElements(); i+=4) {
102 String fontName = (String)e.nextElement() + "0";
104 fontList[i] = fontName;
105 fontList[i+1] = fontName + "i";
106 fontList[i+2] = fontName + "b";
107 fontList[i+3] = fontName + "bi";
110 // Make sure that the default font exists
111 if ( _getATSUStyle( _getDefaultFont() ) == null ) throw new Error( "Default font does not exist" );
113 private native void natInit();
115 /** so we can put ATSUStyles and ATSFontRefs into Hashtables */
116 private static class WrappedRawData {
117 public RawData wrapee = null;
118 public WrappedRawData(RawData r) { wrapee = r; }
121 // CarbonSurface /////////////////////////////////////////////////////
123 /** Implements a Surface as an Carbon Window */
124 public static class CarbonSurface extends Surface {
126 /** The WindowRef that implements this Surface. */
127 gnu.gcj.RawData window = null;
128 /** The CGContextRef. TODO: How do we get this??? */
129 gnu.gcj.RawData gc = null;
131 public native void setInvisible(boolean i);
132 public native void _setMaximized(boolean m);
133 public native void setIcon(Picture p);
134 public native void _setMinimized(boolean b);
135 public native void setTitleBarText(String s);
136 public native void setSize(int w, int h);
137 public native void setLocation(int x, int y);
138 public native void natInit(boolean framed);
139 public native void toFront();
140 public native void toBack();
141 public native void syncCursor();
142 public native void _dispose();
143 //public native void setLimits(int minw, int minh, int maxw, int maxh);
144 public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
146 public CarbonSurface(Box root, boolean framed) { super(root); natInit( framed ); }
151 // Our Subclass of Picture ///////////////////////////////////////////////
153 /** Implements a Picture */
154 public static class CarbonPicture extends Picture {
159 /** A CGImageRef of the picture. */
160 RawData image = null;
162 public int getWidth() { return width; }
163 public int getHeight() { return height; }
165 public native void natInit();
166 public native void finalize();
168 public CarbonPicture(int[] data, int w, int h) {
177 /** A Carbon DoubleBuffer */
178 public static class CarbonDoubleBuffer extends DoubleBuffer {
182 /** A pointer to the raw bitmap data. */
184 /** A CGBitmapContextRef. */
186 /** A CGImageRef which represents the CGBitmapContext. */
189 public int getWidth() { return width; }
190 public int getHeight() { return height; }
192 public CarbonDoubleBuffer(int w, int h) {
198 public native void setClip(int x, int y, int x2, int y2);
199 public native void drawPicture(Picture source, int x, int y);
200 public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
201 public native void fillRect(int x, int y, int x2, int y2, int color);
202 public native void drawString(String font, String text, int x, int y, int color);
203 public native void natInit();
204 public native void finalize();