2003/07/05 22:11:55
[org.ibex.core.git] / src / org / xwt / plat / Carbon.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
2 package org.xwt.plat;
3
4 import gnu.gcj.RawData;
5 import java.net.*;
6 import java.lang.reflect.*;
7 import java.io.*;
8 import java.util.*;
9 import org.xwt.util.*;
10 import org.xwt.*;
11
12 /** Platform implementation for Carbon UI on a POSIX-compliant OS (ie Mac OS X) */
13 public class Carbon extends POSIX {
14
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();
18
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();
22
23         /** List of all XWT font specs. Initialized by init(). */
24         static String[] fontList = null;
25
26     // General Methods ///////////////////////////////////////////////////////
27
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; }
44
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;
52
53                 Platform.ParsedFont pf = new Platform.ParsedFont( fontSpec );
54
55                 // Find the font
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";
61
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" );
67                                 
68                 // Create the ATSUStyle object
69                 ret = new WrappedRawData( _createATSUStyle( fontRef.wrapee, pf.size, pf.bold, pf.italic, pf.underline ) );
70
71                 // Map this font spec to the ATSFontRef to optimize future requests
72                 atsuStyleCache.put( fontSpec, ret );
73
74         return ret.wrapee;
75         }
76
77         /** Creates an ATSUStyle object with the specified attributes. */
78         protected native RawData _createATSUStyle( RawData fontRef, int fontSize, boolean isBold, boolean isItalic, boolean isUnderline );
79
80         /** Called once XWT is initialized and the application is running. On Mac OS X this calls
81         *       RunApplicationEventLoop(). */
82         protected native void _running();
83         
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);
88
89     public Carbon() { }
90
91     public void init() {
92                 natInit();
93
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";
100                         
101                         fontList[i] = fontName;
102                         fontList[i+1] = fontName + "i";
103                         fontList[i+2] = fontName + "b";
104                         fontList[i+3] = fontName + "bi";
105                 }
106
107                 // Make sure that the default font exists
108                 if ( _getATSUStyle( _getDefaultFont() ) == null ) throw new Error( "Default font does not exist" );
109         }
110         private native void natInit();
111
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; }
116     }
117
118     // CarbonSurface /////////////////////////////////////////////////////
119
120     /** Implements a Surface as an Carbon Window */
121     public static class CarbonSurface extends Surface {
122
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;
127         
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);
142
143         public CarbonSurface(Box root, boolean framed) { super(root); natInit( framed ); }
144
145     }
146
147
148     // Our Subclass of Picture ///////////////////////////////////////////////
149
150     /** Implements a Picture */
151     public static class CarbonPicture extends Picture {
152         int width;
153         int height;
154         int[] data = null;
155
156                 /** A CGImageRef of the picture. */
157                 RawData image = null;
158                 
159         public int getWidth() { return width; }
160         public int getHeight() { return height; }
161
162                 public native void natInit();
163                 public native void finalize();
164                 
165         public CarbonPicture(int[] data, int w, int h) {
166             this.data = data;
167             this.width = w;
168             this.height = h;
169                         natInit();
170         }
171
172     }
173
174     /** A Carbon DoubleBuffer */
175     public static class CarbonDoubleBuffer extends DoubleBuffer {
176         int width;
177         int height;
178
179                 /** A pointer to the raw bitmap data. */
180                 RawData bitmapData;
181                 /** A CGBitmapContextRef. */
182                 RawData gc;
183                 /** A CGImageRef which represents the CGBitmapContext. */
184                 RawData image;
185                 
186         public int getWidth() { return width; }
187         public int getHeight() { return height; }
188
189         public CarbonDoubleBuffer(int w, int h) {
190                         this.width = w;
191                         this.height = h;
192                         natInit();
193                 }
194
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();
202     }
203
204 }