1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
7 import org.ibex.util.*;
10 import org.ibex.graphics.*;
11 import org.ibex.core.*;
12 import org.ibex.net.*;
14 /** Platform specific code for GCJ-compiled Win32 binaries */
15 public class Win32 extends GCJ {
16 // Initialization ////////////////////////////////////////////////////////////////////////////
18 // Win32 often asks for a DC/Handle when it doesn't really need one
19 static int desktop_handle = 0;
20 static int desktop_dc = 0;
23 static int wait_cursor = 0;
24 static int default_cursor = 0;
25 static int crosshair_cursor = 0;
26 static int text_cursor = 0;
27 static int move_cursor = 0;
28 static int sizenesw_cursor = 0;
29 static int sizens_cursor = 0;
30 static int sizenwse_cursor = 0;
31 static int sizewe_cursor = 0;
32 static int hand_cursor = 0;
34 /** reverse lookup from hwnd to Win32Surface */
35 public static Hashtable hwndToWin32SurfaceMap = new Hashtable();
37 /** lets us know that natInit() is finished */
38 public static Semaphore messagePumpStarted = new Semaphore();
40 /** ThreadId of the message pump thread, used to send it messages */
41 public static int messagePumpThread = 0;
43 public static native String getTempPath();
44 public static native void natInit();
45 public static native void natPreInit();
47 protected native String _fileDialog(String suggestedFileName, boolean write);
49 public Win32() { natPreInit(); }
51 public void postInit() {
52 new Thread() { public void run() { natInit(); } }.start();
53 messagePumpStarted.block();
57 // Implementation of Platform methods /////////////////////////////////////////////////////////
59 protected native String _getEnv(String key);
60 protected boolean _needsAutoClick() { return true; }
61 protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
62 protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
63 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Win32PixelBuffer(w, h, (Win32Surface)owner); }
64 protected Picture _createPicture(JS r) { return new Win32Picture(r); }
65 protected native int _getScreenWidth();
66 protected native int _getScreenHeight();
67 protected boolean _supressDirtyOnResize() { return false; }
68 protected native void _criticalAbort(String message);
69 protected native String _getClipBoard();
70 protected native void _setClipBoard(String s);
71 protected boolean _isCaseSensitive() { return false; }
73 private native void __detectProxy(String[] container);
75 protected synchronized HTTP.Proxy _detectProxy() {
77 String[] container = new String[] { null, null, null };
78 if (Log.on) Log.info(this, "accessing Win32 registry");
79 __detectProxy(container);
80 if (container[2] == null && container[0] == null) {
81 if (Log.on) Log.info(this, "no proxy settings in the Win32 registry");
85 if (Log.on) Log.info(this, "PAC Script URL: " + container[2]);
86 if (Log.on) Log.info(this, "Proxy Server String: " + container[0]);
87 if (Log.on) Log.info(this, "Proxy Override String: " + container[1]);
89 HTTP.Proxy ret = new HTTP.Proxy();
90 if (container[2] != null) {
92 //ret.proxyAutoConfigFunction = HTTP.Proxy.getProxyAutoConfigFunction(container[2]);
93 if (ret.proxyAutoConfigFunction != null) return ret;
96 if (container[0] == null) return null;
97 StringTokenizer st = new StringTokenizer(container[0], ";", false);
98 while(st.hasMoreTokens()) try {
99 String s = st.nextToken().trim();
100 String protocol, host;
101 if (s.indexOf(':') == -1) {
103 } else if (s.indexOf("://") != -1) {
104 protocol = s.substring(0, s.indexOf("://"));
105 s = s.substring(s.indexOf("://") + 3);
106 host = s.substring(0, s.indexOf(':'));
107 } else if (s.indexOf('=') == -1) {
109 host = s.substring(0, s.indexOf(':'));
111 protocol = s.substring(0, s.indexOf('='));
112 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
114 int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
115 if (protocol.equals("http")) {
116 ret.httpProxyHost = host;
117 ret.httpProxyPort = port;
118 } else if (protocol.equals("https")) {
119 ret.httpsProxyHost = host;
120 ret.httpsProxyPort = port;
121 } else if (protocol.equals("socks")) {
122 ret.socksProxyHost = host;
123 ret.socksProxyPort = port;
125 } catch (NumberFormatException nfe) { }
127 if (container[1] != null) {
128 st = new StringTokenizer(container[1], ";", false);
129 ret.excluded = new String[st.countTokens()];
130 for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
135 protected native boolean _newBrowserWindow_(String url);
136 protected void _newBrowserWindow(String url) {
137 if (!_newBrowserWindow_(url))
138 if (Log.on) Log.info(this, "ShellExecuteEx() failed trying to open url " + url);
141 // Win32Surface ////////////////////////////////////////////////////////////////////////////
143 public static class Win32Surface extends Surface.DoubleBufferedSurface {
145 /** used to block while waiting for the message pump thread to create a hwnd for us */
146 public Semaphore hwndCreated = new Semaphore();
148 /** nothing more than a method version of WndProc, so we can access instance members/methods */
149 public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
151 /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
152 boolean inside = false;
154 /** true iff we have 'captured' the mouse with SetCapture() */
155 boolean captured = false;
157 public int hwnd = -1;
160 public int current_cursor = default_cursor;
162 /** used to restore the cursor immediately before ReleaseCapture() */
163 public int previous_cursor = 0;
165 public native void natInit(boolean framed);
166 public Win32Surface(Box b, final boolean framed) {
169 hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
172 public void syncCursor() {
173 if (cursor.equals("default")) current_cursor = default_cursor;
174 else if (cursor.equals("wait")) current_cursor = wait_cursor;
175 else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
176 else if (cursor.equals("text")) current_cursor = text_cursor;
177 else if (cursor.equals("move")) current_cursor = move_cursor;
178 else if (cursor.equals("hand")) current_cursor = hand_cursor;
179 else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
180 else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
181 else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
182 else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
186 public native void finalize();
187 public native void postCursorChange();
188 public native void toBack();
189 public native void toFront();
190 public native void _setMinimized(boolean m);
191 public native void setInvisible(boolean i);
192 public native void _setMaximized(boolean m);
193 public native void _setSize(int w, int h);
194 public native void setLocation();
195 public native void setTitleBarText(String s);
196 public native void setIcon(Picture p);
197 public native void _dispose();
198 public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
202 // Win32Picture ////////////////////////////////////////////////////////////////////////////
204 public static class Win32Picture extends Picture {
206 /** the Win32 bitmap version of this Picture */
209 /** dc of the bitmap */
212 /** true iff this Picture has translucent regions */
213 boolean hasalpha = false;
215 /** true iff this Picture has transparent regions but no translucent regions */
216 boolean hasmask = true;
218 /** if hasmask, this mask indicates which regions are transparent */
221 /** dc of the mask */
224 public Win32Picture(JS r) { super(r); }
225 public int getWidth() { return width; };
226 public int getHeight() { return height; };
227 public int[] getData() { return data; }
228 boolean initialized = false;
229 public void init() { if (!initialized && isLoaded) natInit(); initialized = true; }
230 public native void natInit();
234 // Win32PixelBuffer //////////////////////////////////////////////////////////////////////////
236 public static class Win32PixelBuffer extends PixelBuffer {
249 public int getHeight() { return h; }
250 public int getWidth() { return w; }
252 public native void natInit();
253 public Win32PixelBuffer(int w, int h, Win32Surface owner) {
261 public native void fillRect(int x, int y, int x2, int y2, int color);
262 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
263 ((Win32Picture)source).init();
264 drawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
266 public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
267 Win32Picture p = ((Win32Picture)((Platform.DefaultGlyph)source).getPicture());
269 drawPicture(p, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
271 public native void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
272 int rgb, boolean alphaOnly);
274 public native void finalize();
276 // FIXME: try to use os acceleration
277 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
278 if (x1 == x3 && x2 == x4) {
279 fillRect(x1, y1, x4, y2, argb);
280 } else for(int y=y1; y<y2; y++) {
281 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
282 int _y1 = (int)Math.floor(y);
283 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
284 int _y2 = (int)Math.floor(y) + 1;
285 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
286 fillRect(_x1, _y1, _x2, _y2, argb);