5485540eeda81922fe43be7bf3f1df0c8642ba6e
[org.ibex.core.git] / src / org / xwt / plat / Win32.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
2 package org.xwt.plat;
3
4 import org.xwt.*;
5 import org.xwt.util.*;
6 import java.util.*;
7 import java.io.*;
8
9 /** Platform specific code for GCJ-compiled Win32 binaries */
10 public class Win32 extends GCJ {
11
12     // Initialization ////////////////////////////////////////////////////////////////////////////
13
14     // Win32 often asks for a DC/Handle when it doesn't really need one
15     static int desktop_handle = 0;
16     static int desktop_dc = 0;
17
18     // Cursors
19     static int wait_cursor = 0;
20     static int default_cursor = 0;
21     static int crosshair_cursor = 0;
22     static int text_cursor = 0;
23     static int move_cursor = 0;
24     static int sizenesw_cursor = 0;
25     static int sizens_cursor = 0;
26     static int sizenwse_cursor = 0;
27     static int sizewe_cursor = 0;
28     static int hand_cursor = 0;
29     
30     /** reverse lookup from hwnd to Win32Surface */
31     public static Hashtable hwndToWin32SurfaceMap = new Hashtable();
32
33     /** lets us know that natInit() is finished */
34     public static Semaphore messagePumpStarted = new Semaphore();
35
36     /** ThreadId of the message pump thread, used to send it messages */
37     public static int messagePumpThread = 0;
38
39     public static native String getTempPath();
40     public static native void natInit();
41
42     protected native String _fileDialog(String suggestedFileName, boolean write);
43
44     public Win32() { }
45
46     public void init() {
47         String logfile = getTempPath() + "xwt-log.txt";
48         try {
49             PrintStream ps = new PrintStream(new FileOutputStream(logfile));
50             System.setOut(ps);
51             System.setErr(ps);
52         } catch (Throwable e) {
53             criticalAbort("Exception while attempting to redirect logging to " + logfile + " -- " + e);
54         }
55
56         new Thread() { public void run() { natInit(); } }.start();
57         messagePumpStarted.block();
58         fontList = new String[fontListVec.size()];
59         fontListVec.toArray(fontList);
60         fontListVec = null;
61     }
62
63
64     // Font Handling ////////////////////////////////////////////////////////////////////////////
65
66     // FEATURE: query the registry for the user's default font
67     protected String _getDefaultFont() { return "dialog8"; }
68     protected int _getMaxAscent(String font) { return getFont(font).maxAscent; }
69     protected int _getMaxDescent(String font) { return getFont(font).maxDescent; }
70     protected native int _stringWidth(String font, String text);
71
72     // methods/members used to enumerate platform fonts on startup
73     public static Vector fontListVec = new Vector();
74     public static String[] fontList = null;
75     protected String[] _listFonts() { return fontList; }
76     public static void addFont(String name, int height, boolean italic, boolean bold) {
77         fontListVec.addElement(name.replace(' ', '_').toLowerCase() + "" + height + (italic ? "i" : "") + (bold ? "b" : ""));
78     }
79
80     static Hash fontCache = new Hash();
81     public static class Win32Font {
82         int hfont;
83         int maxAscent;
84         int maxDescent;
85     }
86
87     /** takes a parsed font and finds the closest platform-specific font */
88     static native Win32Font mapFont(Platform.ParsedFont pf);
89
90     /** takes an unparsed font and finds the closest platform-specific font */
91     static Win32Font getFont(String font) {
92         Win32Font ret = (Win32Font)fontCache.get(font);
93         if (ret != null) return ret;
94
95         Platform.ParsedFont pf = new Platform.ParsedFont(font);
96         if (pf.name.equals("serif")) pf.name = "Times New Roman";
97         else if (pf.name.equals("sansserif")) pf.name = "Arial";
98         else if (pf.name.equals("monospace")) pf.name = "Courier New";
99         else if (pf.name.equals("dialog")) pf.name = "MS Sans Serif";
100         else if (pf.name.equals("tty")) pf.name = "FixedSys";
101
102         ret = mapFont(pf);
103         fontCache.put(font, ret);
104         return ret;
105     }
106
107
108     // Implementation of Platform methods /////////////////////////////////////////////////////////
109
110     protected native String _getEnv(String key);
111     protected boolean _needsAutoClick() { return true; }
112     protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
113     protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
114     protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Win32DoubleBuffer(w, h, (Win32Surface)owner); }
115     protected Picture _createPicture(int[] b, int w, int h) { return new Win32Picture(b, w, h); }
116     protected native int _getScreenWidth();
117     protected native int _getScreenHeight();
118     protected boolean _supressDirtyOnResize() { return false; }
119     protected native void _criticalAbort(String message);
120     protected native String _getClipBoard();
121     protected native void _setClipBoard(String s);
122     protected boolean _isCaseSensitive() { return false; }
123
124     private native void __detectProxy(String[] container);
125
126     protected synchronized HTTP.ProxyInfo _detectProxy() {
127
128         String[] container = new String[] { null, null, null };
129         if (Log.on) Log.log(this, "accessing Win32 registry");
130         __detectProxy(container);
131         if (container[2] == null && container[0] == null) {
132             if (Log.on) Log.log(this, "no proxy settings in the Win32 registry");
133             return null;
134         }
135         
136         if (Log.on) Log.log(this, "PAC Script URL: " + container[2]);
137         if (Log.on) Log.log(this, "Proxy Server String: " + container[0]);
138         if (Log.on) Log.log(this, "Proxy Override String: " + container[1]);
139
140         HTTP.ProxyInfo ret = new HTTP.ProxyInfo();
141         if (container[2] != null) {
142             ret.proxyAutoConfigFunction = HTTP.ProxyInfo.getProxyAutoConfigFunction(container[2]);
143             if (ret.proxyAutoConfigFunction != null) return ret;
144         }
145
146         if (container[0] == null) return null;
147         StringTokenizer st = new StringTokenizer(container[0], ";", false);
148         while(st.hasMoreTokens()) try {
149             String s = st.nextToken().trim();
150             String protocol, host;
151             if (s.indexOf(':') == -1) {
152                 continue;
153             } else if (s.indexOf("://") != -1) {
154                 protocol = s.substring(0, s.indexOf("://"));
155                 s = s.substring(s.indexOf("://") + 3);
156                 host = s.substring(0, s.indexOf(':'));
157             } else if (s.indexOf('=') == -1) {
158                 protocol = "http";
159                 host = s.substring(0, s.indexOf(':'));
160             } else {
161                 protocol = s.substring(0, s.indexOf('='));
162                 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
163             }
164             int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
165             if (protocol.equals("http")) {
166                 ret.httpProxyHost = host;
167                 ret.httpProxyPort = port;
168             } else if (protocol.equals("https")) {
169                 ret.httpsProxyHost = host;
170                 ret.httpsProxyPort = port;
171             } else if (protocol.equals("socks")) {
172                 ret.socksProxyHost = host;
173                 ret.socksProxyPort = port;
174             }
175         } catch (NumberFormatException nfe) { }
176
177         if (container[1] != null) {
178             st = new StringTokenizer(container[1], ";", false);
179             ret.excluded = new String[st.countTokens()];
180             for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
181         }
182         return ret;
183     }
184
185     protected native boolean _newBrowserWindow_(String url);
186     protected void _newBrowserWindow(String url) {
187         if (!_newBrowserWindow_(url))
188             if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
189     }
190
191     // Win32Surface ////////////////////////////////////////////////////////////////////////////
192
193     public static class Win32Surface extends Surface {
194
195         /** used to block while waiting for the message pump thread to create a hwnd for us */
196         public Semaphore hwndCreated = new Semaphore();
197
198         /** nothing more than a method version of WndProc, so we can access instance members/methods */
199         public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
200
201         /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
202         boolean inside = false;
203
204         /** true iff we have 'captured' the mouse with SetCapture() */
205         boolean captured = false;
206
207         public int hwnd = -1;
208         public int hdc = -1;
209
210         public int current_cursor = default_cursor;
211
212         /** used to restore the cursor immediately before ReleaseCapture() */
213         public int previous_cursor = 0;
214
215         public native void natInit(boolean framed);
216         public Win32Surface(Box b, final boolean framed) {
217             super(b);
218             natInit(framed);
219             hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
220         }
221
222         public void syncCursor() {
223             if (cursor.equals("default")) current_cursor = default_cursor;
224             else if (cursor.equals("wait")) current_cursor = wait_cursor;
225             else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
226             else if (cursor.equals("text")) current_cursor = text_cursor;
227             else if (cursor.equals("move")) current_cursor = move_cursor;
228             else if (cursor.equals("hand")) current_cursor = hand_cursor;
229             else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
230             else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
231             else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
232             else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
233             postCursorChange();
234         }
235
236         public native void finalize();
237         public native void postCursorChange();
238         public native void toBack();
239         public native void toFront();
240         public native void _setMinimized(boolean m);
241         public native void setInvisible(boolean i);
242         public native void _setMaximized(boolean m);
243         public native void setSize(int w, int h);
244         public native void setLocation(int x, int y);
245         public native void setTitleBarText(String s);
246         public native void setIcon(Picture p);
247         public native void _dispose();
248         public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
249     }
250
251
252     // Win32Picture ////////////////////////////////////////////////////////////////////////////
253
254     public static class Win32Picture implements Picture {
255         int w = 0, h = 0;
256         int[] data = null;
257
258         /** the Win32 bitmap version of this Picture */
259         int hbitmap = -1;
260
261         /** dc of the bitmap */
262         int hdc = -1;
263
264         /** true iff this Picture has translucent regions */
265         boolean hasalpha = false;
266
267         /** true iff this Picture has transparent regions but no translucent regions */
268         boolean hasmask = true;
269
270         /** if hasmask, this mask indicates which regions are transparent */
271         int hmask = -1;
272
273         /** dc of the mask */
274         int maskdc = -1;
275
276         public int getWidth() { return w; };
277         public int getHeight() { return h; };
278         public int[] getData() { return data; }
279         public native void natInit();
280         public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
281     }
282     
283
284     // Win32DoubleBuffer //////////////////////////////////////////////////////////////////////////
285
286     public static class Win32DoubleBuffer implements DoubleBuffer {
287
288         int w = 0;
289         int h = 0;
290
291         int clipx1 = 0;
292         int clipy1 = 0;
293         int clipx2 = 0;
294         int clipy2 = 0;
295
296         int hdc = -1;
297         int hbitmap = -1;
298
299         public int getHeight() { return h; }
300         public int getWidth() { return w; }
301
302         public native void natInit();
303         public Win32DoubleBuffer(int w, int h, Win32Surface owner) {
304             this.w = w;
305             this.h = h;
306             clipx2 = w;
307             clipy2 = h;
308             natInit();
309         }
310
311         public native void setClip(int x, int y, int x2, int y2);
312         public native void fillRect(int x, int y, int x2, int y2, int color);
313         public native void drawString(String font, String text, int x, int y, int color);
314         public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
315         public native void finalize();
316         public void drawPicture(Picture source, int x, int y) {
317             drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
318         }
319
320     }
321
322 }
323