1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
9 /** Platform specific code for GCJ-compiled Win32 binaries */
10 public class Win32 extends GCJ {
12 // Initialization ////////////////////////////////////////////////////////////////////////////
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;
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;
30 /** reverse lookup from hwnd to Win32Surface */
31 public static Hashtable hwndToWin32SurfaceMap = new Hashtable();
33 /** lets us know that natInit() is finished */
34 public static Semaphore messagePumpStarted = new Semaphore();
36 /** ThreadId of the message pump thread, used to send it messages */
37 public static int messagePumpThread = 0;
39 public static native String getTempPath();
40 public static native void natInit();
42 protected native String _fileDialog(String suggestedFileName, boolean write);
47 String logfile = getTempPath() + "xwt-log.txt";
49 PrintStream ps = new PrintStream(new FileOutputStream(logfile));
52 } catch (Throwable e) {
53 criticalAbort("Exception while attempting to redirect logging to " + logfile + " -- " + e);
56 new Thread() { public void run() { natInit(); } }.start();
57 messagePumpStarted.block();
61 // Implementation of Platform methods /////////////////////////////////////////////////////////
63 protected native String _getEnv(String key);
64 protected boolean _needsAutoClick() { return true; }
65 protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
66 protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
67 protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Win32PixelBuffer(w, h, (Win32Surface)owner); }
68 protected Picture _createPicture() { return new Win32Picture(); }
69 protected native int _getScreenWidth();
70 protected native int _getScreenHeight();
71 protected boolean _supressDirtyOnResize() { return false; }
72 protected native void _criticalAbort(String message);
73 protected native String _getClipBoard();
74 protected native void _setClipBoard(String s);
75 protected boolean _isCaseSensitive() { return false; }
77 private native void __detectProxy(String[] container);
79 protected synchronized HTTP.Proxy _detectProxy() {
81 String[] container = new String[] { null, null, null };
82 if (Log.on) Log.log(this, "accessing Win32 registry");
83 __detectProxy(container);
84 if (container[2] == null && container[0] == null) {
85 if (Log.on) Log.log(this, "no proxy settings in the Win32 registry");
89 if (Log.on) Log.log(this, "PAC Script URL: " + container[2]);
90 if (Log.on) Log.log(this, "Proxy Server String: " + container[0]);
91 if (Log.on) Log.log(this, "Proxy Override String: " + container[1]);
93 HTTP.Proxy ret = new HTTP.Proxy();
94 if (container[2] != null) {
95 ret.proxyAutoConfigJSFunction = HTTP.Proxy.getProxyAutoConfigJSFunction(container[2]);
96 if (ret.proxyAutoConfigJSFunction != null) return ret;
99 if (container[0] == null) return null;
100 StringTokenizer st = new StringTokenizer(container[0], ";", false);
101 while(st.hasMoreTokens()) try {
102 String s = st.nextToken().trim();
103 String protocol, host;
104 if (s.indexOf(':') == -1) {
106 } else if (s.indexOf("://") != -1) {
107 protocol = s.substring(0, s.indexOf("://"));
108 s = s.substring(s.indexOf("://") + 3);
109 host = s.substring(0, s.indexOf(':'));
110 } else if (s.indexOf('=') == -1) {
112 host = s.substring(0, s.indexOf(':'));
114 protocol = s.substring(0, s.indexOf('='));
115 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
117 int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
118 if (protocol.equals("http")) {
119 ret.httpProxyHost = host;
120 ret.httpProxyPort = port;
121 } else if (protocol.equals("https")) {
122 ret.httpsProxyHost = host;
123 ret.httpsProxyPort = port;
124 } else if (protocol.equals("socks")) {
125 ret.socksProxyHost = host;
126 ret.socksProxyPort = port;
128 } catch (NumberFormatException nfe) { }
130 if (container[1] != null) {
131 st = new StringTokenizer(container[1], ";", false);
132 ret.excluded = new String[st.countTokens()];
133 for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
138 protected native boolean _newBrowserWindow_(String url);
139 protected void _newBrowserWindow(String url) {
140 if (!_newBrowserWindow_(url))
141 if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
144 // Win32Surface ////////////////////////////////////////////////////////////////////////////
146 public static class Win32Surface extends Surface.DoubleBufferedSurface {
148 /** used to block while waiting for the message pump thread to create a hwnd for us */
149 public Semaphore hwndCreated = new Semaphore();
151 /** nothing more than a method version of WndProc, so we can access instance members/methods */
152 public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
154 /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
155 boolean inside = false;
157 /** true iff we have 'captured' the mouse with SetCapture() */
158 boolean captured = false;
160 public int hwnd = -1;
163 public int current_cursor = default_cursor;
165 /** used to restore the cursor immediately before ReleaseCapture() */
166 public int previous_cursor = 0;
168 public native void natInit(boolean framed);
169 public Win32Surface(Box b, final boolean framed) {
172 hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
175 public void syncCursor() {
176 if (cursor.equals("default")) current_cursor = default_cursor;
177 else if (cursor.equals("wait")) current_cursor = wait_cursor;
178 else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
179 else if (cursor.equals("text")) current_cursor = text_cursor;
180 else if (cursor.equals("move")) current_cursor = move_cursor;
181 else if (cursor.equals("hand")) current_cursor = hand_cursor;
182 else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
183 else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
184 else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
185 else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
189 public native void finalize();
190 public native void postCursorChange();
191 public native void toBack();
192 public native void toFront();
193 public native void _setMinimized(boolean m);
194 public native void setInvisible(boolean i);
195 public native void _setMaximized(boolean m);
196 public native void _setSize(int w, int h);
197 public native void setLocation();
198 public native void setTitleBarText(String s);
199 public native void setIcon(Picture p);
200 public native void _dispose();
201 public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
205 // Win32Picture ////////////////////////////////////////////////////////////////////////////
207 public static class Win32Picture extends Picture {
211 /** the Win32 bitmap version of this Picture */
214 /** dc of the bitmap */
217 /** true iff this Picture has translucent regions */
218 boolean hasalpha = false;
220 /** true iff this Picture has transparent regions but no translucent regions */
221 boolean hasmask = true;
223 /** if hasmask, this mask indicates which regions are transparent */
226 /** dc of the mask */
229 public int getWidth() { return w; };
230 public int getHeight() { return h; };
231 public int[] getData() { return data; }
232 boolean initialized = false;
233 public void init() { if (!initialized && isLoaded) natInit(); initialized = true; }
234 public native void natInit();
238 // Win32PixelBuffer //////////////////////////////////////////////////////////////////////////
240 public static class Win32PixelBuffer extends PixelBuffer {
253 public int getHeight() { return h; }
254 public int getWidth() { return w; }
256 public native void natInit();
257 public Win32PixelBuffer(int w, int h, Win32Surface owner) {
265 public native void fillRect(int x, int y, int x2, int y2, int color);
266 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
267 ((Win32Picture)source).init();
268 drawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
270 public void drawGlyph(org.xwt.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
271 Win32Picture p = ((Win32Picture)((DefaultGlyph)source).getPicture());
273 drawPicture(p, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
275 public native void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
276 int rgb, boolean alphaOnly);
278 public native void finalize();
280 // FIXME: try to use os acceleration
281 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
282 if (x1 == x3 && x2 == x4) {
283 fillRect(x1, y1, x4, y2, argb);
284 } else for(int y=y1; y<y2; y++) {
285 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
286 int _y1 = (int)Math.floor(y);
287 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
288 int _y2 = (int)Math.floor(y) + 1;
289 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
290 fillRect(_x1, _y1, _x2, _y2, argb);