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