77d4dd90fa037ed1bb55ec9476b96213f519fc94
[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 java.awt.*;
5 import java.awt.image.*;
6 import gnu.gcj.RawData;
7 import java.net.*;
8 import java.lang.reflect.*;
9 import java.io.*;
10 import java.util.*;
11 import java.awt.peer.*;
12 import org.xwt.util.*;
13 import org.xwt.*;
14
15 /** Platform implementation for Carbon UI on a POSIX-compliant OS (ie Mac OS X) */
16 public class Carbon extends POSIX {
17
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();
21
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();
25
26         /** List of all XWT font specs. Initialized by init(). */
27         static String[] fontList = null;
28
29     // General Methods ///////////////////////////////////////////////////////
30
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; }
47
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;
55
56                 Platform.ParsedFont pf = new Platform.ParsedFont( fontSpec );
57
58                 // Find the font
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";
64
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" );
70                                 
71                 // Create the ATSUStyle object
72                 ret = new WrappedRawData( _createATSUStyle( fontRef.wrapee, pf.size, pf.bold, pf.italic, pf.underline ) );
73
74                 // Map this font spec to the ATSFontRef to optimize future requests
75                 atsuStyleCache.put( fontSpec, ret );
76
77         return ret.wrapee;
78         }
79
80         /** Creates an ATSUStyle object with the specified attributes. */
81         protected native RawData _createATSUStyle( RawData fontRef, int fontSize, boolean isBold, boolean isItalic, boolean isUnderline );
82
83         /** Called once XWT is initialized and the application is running. On Mac OS X this calls
84         *       RunApplicationEventLoop(). */
85         protected native void _running();
86         
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);
91
92     public Carbon() { }
93
94     public void init() {
95                 natInit();
96
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";
103                         
104                         fontList[i] = fontName;
105                         fontList[i+1] = fontName + "i";
106                         fontList[i+2] = fontName + "b";
107                         fontList[i+3] = fontName + "bi";
108                 }
109
110                 // Make sure that the default font exists
111                 if ( _getATSUStyle( _getDefaultFont() ) == null ) throw new Error( "Default font does not exist" );
112         }
113         private native void natInit();
114
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; }
119     }
120
121     // CarbonSurface /////////////////////////////////////////////////////
122
123     /** Implements a Surface as an Carbon Window */
124     public static class CarbonSurface extends Surface {
125
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;
130         
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);
145
146         public CarbonSurface(Box root, boolean framed) { super(root); natInit( framed ); }
147
148     }
149
150
151     // Our Subclass of Picture ///////////////////////////////////////////////
152
153     /** Implements a Picture */
154     public static class CarbonPicture extends Picture {
155         int width;
156         int height;
157         int[] data = null;
158
159                 /** A CGImageRef of the picture. */
160                 RawData image = null;
161                 
162         public int getWidth() { return width; }
163         public int getHeight() { return height; }
164
165                 public native void natInit();
166                 public native void finalize();
167                 
168         public CarbonPicture(int[] data, int w, int h) {
169             this.data = data;
170             this.width = w;
171             this.height = h;
172                         natInit();
173         }
174
175     }
176
177     /** A Carbon DoubleBuffer */
178     public static class CarbonDoubleBuffer extends DoubleBuffer {
179         int width;
180         int height;
181
182                 /** A pointer to the raw bitmap data. */
183                 RawData bitmapData;
184                 /** A CGBitmapContextRef. */
185                 RawData gc;
186                 /** A CGImageRef which represents the CGBitmapContext. */
187                 RawData image;
188                 
189         public int getWidth() { return width; }
190         public int getHeight() { return height; }
191
192         public CarbonDoubleBuffer(int w, int h) {
193                         this.width = w;
194                         this.height = h;
195                         natInit();
196                 }
197
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();
205     }
206
207 }