2003/10/01 03:08:32
[org.ibex.core.git] / src / org / xwt / plat / Win32.java
1 // Copyright 2003 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     }
59
60
61     // Implementation of Platform methods /////////////////////////////////////////////////////////
62
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(int[] b, int w, int h) { return new Win32Picture(b, w, h); }
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; }
76
77     private native void __detectProxy(String[] container);
78
79     protected synchronized HTTP.Proxy _detectProxy() {
80
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");
86             return null;
87         }
88         
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]);
92
93         HTTP.Proxy ret = new HTTP.Proxy();
94         if (container[2] != null) {
95             ret.proxyAutoConfigFunction = HTTP.Proxy.getProxyAutoConfigFunction(container[2]);
96             if (ret.proxyAutoConfigFunction != null) return ret;
97         }
98
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) {
105                 continue;
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) {
111                 protocol = "http";
112                 host = s.substring(0, s.indexOf(':'));
113             } else {
114                 protocol = s.substring(0, s.indexOf('='));
115                 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
116             }
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;
127             }
128         } catch (NumberFormatException nfe) { }
129
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();
134         }
135         return ret;
136     }
137
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);
142     }
143
144     // Win32Surface ////////////////////////////////////////////////////////////////////////////
145
146     public static class Win32Surface extends Surface.DoubleBufferedSurface {
147
148         /** used to block while waiting for the message pump thread to create a hwnd for us */
149         public Semaphore hwndCreated = new Semaphore();
150
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);
153
154         /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
155         boolean inside = false;
156
157         /** true iff we have 'captured' the mouse with SetCapture() */
158         boolean captured = false;
159
160         public int hwnd = -1;
161         public int hdc = 0;
162
163         public int current_cursor = default_cursor;
164
165         /** used to restore the cursor immediately before ReleaseCapture() */
166         public int previous_cursor = 0;
167
168         public native void natInit(boolean framed);
169         public Win32Surface(Box b, final boolean framed) {
170             super(b);
171             natInit(framed);
172             hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
173         }
174
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;
186             postCursorChange();
187         }
188
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);
202     }
203
204
205     // Win32Picture ////////////////////////////////////////////////////////////////////////////
206
207     public static class Win32Picture extends Picture {
208         int w = 0, h = 0;
209         int[] data = null;
210
211         /** the Win32 bitmap version of this Picture */
212         int hbitmap = -1;
213
214         /** dc of the bitmap */
215         int hdc = -1;
216
217         /** true iff this Picture has translucent regions */
218         boolean hasalpha = false;
219
220         /** true iff this Picture has transparent regions but no translucent regions */
221         boolean hasmask = true;
222
223         /** if hasmask, this mask indicates which regions are transparent */
224         int hmask = -1;
225
226         /** dc of the mask */
227         int maskdc = -1;
228
229         public int getWidth() { return w; };
230         public int getHeight() { return h; };
231         public int[] getData() { return data; }
232         public native void natInit();
233         public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
234     }
235     
236
237     // Win32PixelBuffer //////////////////////////////////////////////////////////////////////////
238
239     public static class Win32PixelBuffer extends PixelBuffer {
240
241         int w = 0;
242         int h = 0;
243
244         int clipx1 = 0;
245         int clipy1 = 0;
246         int clipx2 = 0;
247         int clipy2 = 0;
248
249         int hdc = -1;
250         int hbitmap = -1;
251
252         public int getHeight() { return h; }
253         public int getWidth() { return w; }
254
255         public native void natInit();
256         public Win32PixelBuffer(int w, int h, Win32Surface owner) {
257             this.w = w;
258             this.h = h;
259             clipx2 = w;
260             clipy2 = h;
261             natInit();
262         }
263
264         public native void setClip(int x, int y, int x2, int y2);
265         public native void fillRect(int x, int y, int x2, int y2, int color);
266         public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
267         public native void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2,
268                                                 int sx1, int sy1, int sx2, int sy2, int rgb);
269         public native void finalize();
270         public void drawPicture(Picture source, int x, int y) {
271             drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
272         }
273
274         // FIXME: try to use os acceleration
275         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
276             if (x1 == x3 && x2 == x4) {
277                 fillRect(x1, y1, x4, y2, argb);
278             } else for(int y=y1; y<y2; y++) {
279                 int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
280                 int _y1 = (int)Math.floor(y);
281                 int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
282                 int _y2 = (int)Math.floor(y) + 1;
283                 if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
284                 fillRect(_x1, _y1, _x2, _y2, argb);
285             }
286         }
287     }
288
289 }
290