1 // Copyright 2002 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();
45 String logfile = getTempPath() + "xwt-log.txt";
47 PrintStream ps = new PrintStream(new FileOutputStream(logfile));
50 } catch (Throwable e) {
51 criticalAbort("Exception while attempting to redirect logging to " + logfile + " -- " + e);
54 new Thread() { public void run() { natInit(); } }.start();
55 messagePumpStarted.block();
56 fontList = new String[fontListVec.size()];
57 fontListVec.toArray(fontList);
62 // Font Handling ////////////////////////////////////////////////////////////////////////////
64 // FEATURE: query the registry for the user's default font
65 protected String _getDefaultFont() { return "dialog8"; }
66 protected int _getMaxAscent(String font) { return getFont(font).maxAscent; }
67 protected int _getMaxDescent(String font) { return getFont(font).maxDescent; }
68 protected native int _stringWidth(String font, String text);
70 // methods/members used to enumerate platform fonts on startup
71 public static Vector fontListVec = new Vector();
72 public static String[] fontList = null;
73 protected String[] _listFonts() { return fontList; }
74 public static void addFont(String name, int height, boolean italic, boolean bold) {
75 fontListVec.addElement(name.replace(' ', '_').toLowerCase() + "" + height + (italic ? "i" : "") + (bold ? "b" : ""));
78 static Hash fontCache = new Hash();
79 public static class Win32Font {
85 /** takes a parsed font and finds the closest platform-specific font */
86 static native Win32Font mapFont(Platform.ParsedFont pf);
88 /** takes an unparsed font and finds the closest platform-specific font */
89 static Win32Font getFont(String font) {
90 Win32Font ret = (Win32Font)fontCache.get(font);
91 if (ret != null) return ret;
93 Platform.ParsedFont pf = new Platform.ParsedFont(font);
94 if (pf.name.equals("serif")) pf.name = "Times New Roman";
95 else if (pf.name.equals("sansserif")) pf.name = "Arial";
96 else if (pf.name.equals("monospace")) pf.name = "Courier New";
97 else if (pf.name.equals("dialog")) pf.name = "Arial";
98 else if (pf.name.equals("tty")) pf.name = "FixedSys";
101 fontCache.put(font, ret);
106 // Implementation of Platform methods /////////////////////////////////////////////////////////
108 protected boolean _needsAutoClick() { return true; }
109 protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
110 protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
111 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Win32DoubleBuffer(w, h, (Win32Surface)owner); }
112 protected Picture _createPicture(int[] b, int w, int h) { return new Win32Picture(b, w, h); }
113 protected native int _getScreenWidth();
114 protected native int _getScreenHeight();
115 protected boolean _supressDirtyOnResize() { return false; }
116 protected native void _criticalAbort(String message);
117 protected native String _getClipBoard();
118 protected native void _setClipBoard(String s);
120 private native void __detectProxy(String[] container);
122 protected synchronized HTTP.ProxyInfo _detectProxy() {
124 String[] container = new String[] { null, null, null };
125 if (Log.on) Log.log(this, "accessing Win32 registry");
126 __detectProxy(container);
127 if (container[2] == null && container[0] == null) {
128 if (Log.on) Log.log(this, "no proxy settings in the Win32 registry");
132 if (Log.on) Log.log(this, "PAC Script URL: " + container[2]);
133 if (Log.on) Log.log(this, "Proxy Server String: " + container[0]);
134 if (Log.on) Log.log(this, "Proxy Override String: " + container[1]);
136 HTTP.ProxyInfo ret = new HTTP.ProxyInfo();
137 if (container[2] != null) {
138 ret.proxyAutoConfigFunction = HTTP.ProxyInfo.getProxyAutoConfigFunction(container[2]);
139 if (ret.proxyAutoConfigFunction != null) return ret;
142 if (container[0] == null) return null;
143 StringTokenizer st = new StringTokenizer(container[0], ";", false);
144 while(st.hasMoreTokens()) try {
145 String s = st.nextToken().trim();
146 if (s.indexOf('=') == -1) continue;
147 if (s.indexOf(':') == -1) continue;
148 String protocol = s.substring(0, s.indexOf('='));
149 String host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
150 int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
151 if (protocol.equals("http")) {
152 ret.httpProxyHost = host;
153 ret.httpProxyPort = port;
154 } else if (protocol.equals("https")) {
155 ret.httpsProxyHost = host;
156 ret.httpsProxyPort = port;
157 } else if (protocol.equals("socks")) {
158 ret.socksProxyHost = host;
159 ret.socksProxyPort = port;
161 } catch (NumberFormatException nfe) { }
163 if (container[1] != null) {
164 st = new StringTokenizer(container[1], ";", false);
165 ret.excluded = new String[st.countTokens()];
166 for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
171 protected native boolean _newBrowserWindow_(String url);
172 protected void _newBrowserWindow(String url) {
173 if (!_newBrowserWindow_(url))
174 if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
177 // Win32Surface ////////////////////////////////////////////////////////////////////////////
179 public static class Win32Surface extends Surface {
181 /** used to block while waiting for the message pump thread to create a hwnd for us */
182 public Semaphore hwndCreated = new Semaphore();
184 /** nothing more than a method version of WndProc, so we can access instance members/methods */
185 public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
187 /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
188 boolean inside = false;
190 /** true iff we have 'captured' the mouse with SetCapture() */
191 boolean captured = false;
193 public int hwnd = -1;
196 public int current_cursor = default_cursor;
198 /** used to restore the cursor immediately before ReleaseCapture() */
199 public int previous_cursor = 0;
201 public native void natInit(boolean framed);
202 public Win32Surface(Box b, final boolean framed) {
205 hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
208 public void syncCursor() {
209 if (cursor.equals("default")) current_cursor = default_cursor;
210 else if (cursor.equals("wait")) current_cursor = wait_cursor;
211 else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
212 else if (cursor.equals("text")) current_cursor = text_cursor;
213 else if (cursor.equals("move")) current_cursor = move_cursor;
214 else if (cursor.equals("hand")) current_cursor = hand_cursor;
215 else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
216 else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
217 else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
218 else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
222 public native void finalize();
223 public native void postCursorChange();
224 public native void toBack();
225 public native void toFront();
226 public native void _setMinimized(boolean m);
227 public native void setInvisible(boolean i);
228 public native void _setMaximized(boolean m);
229 public native void setSize(int w, int h);
230 public native void setLocation(int x, int y);
231 public native void setTitleBarText(String s);
232 public native void setIcon(Picture p);
233 public native void _dispose();
234 public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
238 // Win32Picture ////////////////////////////////////////////////////////////////////////////
240 public static class Win32Picture implements Picture {
244 /** the Win32 bitmap version of this Picture */
247 /** dc of the bitmap */
250 /** true iff this Picture has translucent regions */
251 boolean hasalpha = false;
253 /** true iff this Picture has transparent regions but no translucent regions */
254 boolean hasmask = true;
256 /** if hasmask, this mask indicates which regions are transparent */
259 /** dc of the mask */
262 public int getWidth() { return w; };
263 public int getHeight() { return h; };
264 public int[] getData() { return data; }
265 public native void natInit();
266 public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
270 // Win32DoubleBuffer //////////////////////////////////////////////////////////////////////////
272 public static class Win32DoubleBuffer implements DoubleBuffer {
285 public int getHeight() { return h; }
286 public int getWidth() { return w; }
288 public native void natInit();
289 public Win32DoubleBuffer(int w, int h, Win32Surface owner) {
297 public native void setClip(int x, int y, int x2, int y2);
298 public native void fillRect(int x, int y, int x2, int y2, int color);
299 public native void drawString(String font, String text, int x, int y, int color);
300 public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
301 public native void finalize();
302 public void drawPicture(Picture source, int x, int y) {
303 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());