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