removed import org.ibex (breaks gcj)
[org.ibex.core.git] / src / org / ibex / plat / Win32.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
2 package org.ibex.plat;
3
4 import org.ibex.util.*;
5 import java.util.*;
6 import org.ibex.js.*;
7 import org.ibex.graphics.*;
8 import org.ibex.core.*;
9 import org.ibex.net.*;
10
11 /** Platform specific code for GCJ-compiled Win32 binaries */
12 public class Win32 extends GCJ {
13     // Initialization ////////////////////////////////////////////////////////////////////////////
14
15     // Win32 often asks for a DC/Handle when it doesn't really need one
16     static int desktop_handle = 0;
17     static int desktop_dc = 0;
18
19     // Cursors
20     static int wait_cursor = 0;
21     static int default_cursor = 0;
22     static int crosshair_cursor = 0;
23     static int text_cursor = 0;
24     static int move_cursor = 0;
25     static int sizenesw_cursor = 0;
26     static int sizens_cursor = 0;
27     static int sizenwse_cursor = 0;
28     static int sizewe_cursor = 0;
29     static int hand_cursor = 0;
30     
31     /** reverse lookup from hwnd to Win32Surface */
32     public static Hashtable hwndToWin32SurfaceMap = new Hashtable();
33
34     /** lets us know that natInit() is finished */
35     public static Semaphore messagePumpStarted = new Semaphore();
36
37     /** ThreadId of the message pump thread, used to send it messages */
38     public static int messagePumpThread = 0;
39
40     public static native String getTempPath();
41     public static native void natInit();
42     public static native void natPreInit();
43
44     protected native String _fileDialog(String suggestedFileName, boolean write);
45
46     public Win32() { natPreInit(); }
47
48     public void postInit() {
49         new Thread() { public void run() { natInit(); } }.start();
50         messagePumpStarted.block();
51     }
52
53
54     // Implementation of Platform methods /////////////////////////////////////////////////////////
55
56     protected native String _getEnv(String key);
57     protected boolean _needsAutoClick() { return true; }
58     protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
59     protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
60     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Win32PixelBuffer(w, h, (Win32Surface)owner); }
61     protected Picture _createPicture(JS r) { return new Win32Picture(r); }
62     protected native int _getScreenWidth();
63     protected native int _getScreenHeight();
64     protected boolean _supressDirtyOnResize() { return false; }
65     protected native void _criticalAbort(String message);
66     protected native String _getClipBoard();
67     protected native void _setClipBoard(String s);
68     protected boolean _isCaseSensitive() { return false; }
69
70     private native void __detectProxy(String[] container);
71
72     protected synchronized HTTP.Proxy _detectProxy() {
73
74         String[] container = new String[] { null, null, null };
75         if (Log.on) Log.info(this, "accessing Win32 registry");
76         __detectProxy(container);
77         if (container[2] == null && container[0] == null) {
78             if (Log.on) Log.info(this, "no proxy settings in the Win32 registry");
79             return null;
80         }
81         
82         if (Log.on) Log.info(this, "PAC Script URL: " + container[2]);
83         if (Log.on) Log.info(this, "Proxy Server String: " + container[0]);
84         if (Log.on) Log.info(this, "Proxy Override String: " + container[1]);
85
86         HTTP.Proxy ret = new HTTP.Proxy();
87         if (container[2] != null) {
88             ret.proxyAutoConfigFunction = HTTP.Proxy.getProxyAutoConfigFunction(container[2]);
89             if (ret.proxyAutoConfigFunction != null) return ret;
90         }
91
92         if (container[0] == null) return null;
93         StringTokenizer st = new StringTokenizer(container[0], ";", false);
94         while(st.hasMoreTokens()) try {
95             String s = st.nextToken().trim();
96             String protocol, host;
97             if (s.indexOf(':') == -1) {
98                 continue;
99             } else if (s.indexOf("://") != -1) {
100                 protocol = s.substring(0, s.indexOf("://"));
101                 s = s.substring(s.indexOf("://") + 3);
102                 host = s.substring(0, s.indexOf(':'));
103             } else if (s.indexOf('=') == -1) {
104                 protocol = "http";
105                 host = s.substring(0, s.indexOf(':'));
106             } else {
107                 protocol = s.substring(0, s.indexOf('='));
108                 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
109             }
110             int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
111             if (protocol.equals("http")) {
112                 ret.httpProxyHost = host;
113                 ret.httpProxyPort = port;
114             } else if (protocol.equals("https")) {
115                 ret.httpsProxyHost = host;
116                 ret.httpsProxyPort = port;
117             } else if (protocol.equals("socks")) {
118                 ret.socksProxyHost = host;
119                 ret.socksProxyPort = port;
120             }
121         } catch (NumberFormatException nfe) { }
122
123         if (container[1] != null) {
124             st = new StringTokenizer(container[1], ";", false);
125             ret.excluded = new String[st.countTokens()];
126             for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
127         }
128         return ret;
129     }
130
131     protected native boolean _newBrowserWindow_(String url);
132     protected void _newBrowserWindow(String url) {
133         if (!_newBrowserWindow_(url))
134             if (Log.on) Log.info(this, "ShellExecuteEx() failed trying to open url " + url);
135     }
136
137     // Win32Surface ////////////////////////////////////////////////////////////////////////////
138
139     public static class Win32Surface extends Surface.DoubleBufferedSurface {
140
141         /** used to block while waiting for the message pump thread to create a hwnd for us */
142         public Semaphore hwndCreated = new Semaphore();
143
144         /** nothing more than a method version of WndProc, so we can access instance members/methods */
145         public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
146
147         /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
148         boolean inside = false;
149
150         /** true iff we have 'captured' the mouse with SetCapture() */
151         boolean captured = false;
152
153         public int hwnd = -1;
154         public int hdc = 0;
155
156         public int current_cursor = default_cursor;
157
158         /** used to restore the cursor immediately before ReleaseCapture() */
159         public int previous_cursor = 0;
160
161         public native void natInit(boolean framed);
162         public Win32Surface(Box b, final boolean framed) {
163             super(b);
164             natInit(framed);
165             hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
166         }
167
168         public void syncCursor() {
169             if (cursor.equals("default")) current_cursor = default_cursor;
170             else if (cursor.equals("wait")) current_cursor = wait_cursor;
171             else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
172             else if (cursor.equals("text")) current_cursor = text_cursor;
173             else if (cursor.equals("move")) current_cursor = move_cursor;
174             else if (cursor.equals("hand")) current_cursor = hand_cursor;
175             else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
176             else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
177             else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
178             else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
179             postCursorChange();
180         }
181
182         public native void finalize();
183         public native void postCursorChange();
184         public native void toBack();
185         public native void toFront();
186         public native void _setMinimized(boolean m);
187         public native void setInvisible(boolean i);
188         public native void _setMaximized(boolean m);
189         public native void _setSize(int w, int h);
190         public native void setLocation();
191         public native void setTitleBarText(String s);
192         public native void setIcon(Picture p);
193         public native void _dispose();
194         public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
195     }
196
197
198     // Win32Picture ////////////////////////////////////////////////////////////////////////////
199
200     public static class Win32Picture extends Picture {
201
202         /** the Win32 bitmap version of this Picture */
203         int hbitmap = -1;
204
205         /** dc of the bitmap */
206         int hdc = -1;
207
208         /** true iff this Picture has translucent regions */
209         boolean hasalpha = false;
210
211         /** true iff this Picture has transparent regions but no translucent regions */
212         boolean hasmask = true;
213
214         /** if hasmask, this mask indicates which regions are transparent */
215         int hmask = -1;
216
217         /** dc of the mask */
218         int maskdc = -1;
219
220         public Win32Picture(JS r) { super(r); }
221         public int getWidth() { return width; };
222         public int getHeight() { return height; };
223         public int[] getData() { return data; }
224         boolean initialized = false;
225         public void init() { if (!initialized && isLoaded) natInit(); initialized = true; }
226         public native void natInit();
227     }
228     
229
230     // Win32PixelBuffer //////////////////////////////////////////////////////////////////////////
231
232     public static class Win32PixelBuffer extends PixelBuffer {
233
234         int w = 0;
235         int h = 0;
236
237         int clipx1 = 0;
238         int clipy1 = 0;
239         int clipx2 = 0;
240         int clipy2 = 0;
241
242         int hdc = -1;
243         int hbitmap = -1;
244
245         public int getHeight() { return h; }
246         public int getWidth() { return w; }
247
248         public native void natInit();
249         public Win32PixelBuffer(int w, int h, Win32Surface owner) {
250             this.w = w;
251             this.h = h;
252             clipx2 = w;
253             clipy2 = h;
254             natInit();
255         }
256
257         public native void fillRect(int x, int y, int x2, int y2, int color);
258         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
259             ((Win32Picture)source).init();
260             drawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
261         }
262         public void drawGlyph(org.ibex.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
263             Win32Picture p = ((Win32Picture)((Platform.DefaultGlyph)source).getPicture());
264             p.init();
265             drawPicture(p, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
266         }
267         public native void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
268                                        int rgb, boolean alphaOnly);
269
270         public native void finalize();
271
272         // FIXME: try to use os acceleration
273         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
274             if (x1 == x3 && x2 == x4) {
275                 fillRect(x1, y1, x4, y2, argb);
276             } else for(int y=y1; y<y2; y++) {
277                 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
278                 int _y1 = (int)Math.floor(y);
279                 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
280                 int _y2 = (int)Math.floor(y) + 1;
281                 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
282                 fillRect(_x1, _y1, _x2, _y2, argb);
283             }
284         }
285     }
286
287 }
288