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();
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();
58 fontList = new String[fontListVec.size()];
59 fontListVec.toArray(fontList);
64 // Font Handling ////////////////////////////////////////////////////////////////////////////
66 // FEATURE: query the registry for the user's default font
67 protected String _getDefaultFont() { return "dialog8"; }
68 protected int _getMaxAscent(String font) { return getFont(font).maxAscent; }
69 protected int _getMaxDescent(String font) { return getFont(font).maxDescent; }
70 protected native int _stringWidth(String font, String text);
72 // methods/members used to enumerate platform fonts on startup
73 public static Vector fontListVec = new Vector();
74 public static String[] fontList = null;
75 protected String[] _listFonts() { return fontList; }
76 public static void addFont(String name, int height, boolean italic, boolean bold) {
77 fontListVec.addElement(name.replace(' ', '_').toLowerCase() + "" + height + (italic ? "i" : "") + (bold ? "b" : ""));
80 static Hash fontCache = new Hash();
81 public static class Win32Font {
87 /** takes a parsed font and finds the closest platform-specific font */
88 static native Win32Font mapFont(Platform.ParsedFont pf);
90 /** takes an unparsed font and finds the closest platform-specific font */
91 static Win32Font getFont(String font) {
92 Win32Font ret = (Win32Font)fontCache.get(font);
93 if (ret != null) return ret;
95 Platform.ParsedFont pf = new Platform.ParsedFont(font);
96 if (pf.name.equals("serif")) pf.name = "Times New Roman";
97 else if (pf.name.equals("sansserif")) pf.name = "Arial";
98 else if (pf.name.equals("monospace")) pf.name = "Courier New";
99 else if (pf.name.equals("dialog")) pf.name = "Arial";
100 else if (pf.name.equals("tty")) pf.name = "FixedSys";
103 fontCache.put(font, ret);
108 // Implementation of Platform methods /////////////////////////////////////////////////////////
110 protected boolean _needsAutoClick() { return true; }
111 protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
112 protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
113 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Win32DoubleBuffer(w, h, (Win32Surface)owner); }
114 protected Picture _createPicture(int[] b, int w, int h) { return new Win32Picture(b, w, h); }
115 protected native int _getScreenWidth();
116 protected native int _getScreenHeight();
117 protected boolean _supressDirtyOnResize() { return false; }
118 protected native void _criticalAbort(String message);
119 protected native String _getClipBoard();
120 protected native void _setClipBoard(String s);
121 protected boolean _isCaseSensitive() { return false; }
123 private native void __detectProxy(String[] container);
125 protected synchronized HTTP.ProxyInfo _detectProxy() {
127 String[] container = new String[] { null, null, null };
128 if (Log.on) Log.log(this, "accessing Win32 registry");
129 __detectProxy(container);
130 if (container[2] == null && container[0] == null) {
131 if (Log.on) Log.log(this, "no proxy settings in the Win32 registry");
135 if (Log.on) Log.log(this, "PAC Script URL: " + container[2]);
136 if (Log.on) Log.log(this, "Proxy Server String: " + container[0]);
137 if (Log.on) Log.log(this, "Proxy Override String: " + container[1]);
139 HTTP.ProxyInfo ret = new HTTP.ProxyInfo();
140 if (container[2] != null) {
141 ret.proxyAutoConfigFunction = HTTP.ProxyInfo.getProxyAutoConfigFunction(container[2]);
142 if (ret.proxyAutoConfigFunction != null) return ret;
145 if (container[0] == null) return null;
146 StringTokenizer st = new StringTokenizer(container[0], ";", false);
147 while(st.hasMoreTokens()) try {
148 String s = st.nextToken().trim();
149 if (s.indexOf(':') == -1) continue;
150 String protocol, host;
151 if (s.indexOf('=') == -1) {
153 host = s.substring(0, s.indexOf(':'));
155 protocol = s.substring(0, s.indexOf('='));
156 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
158 int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
159 if (protocol.equals("http")) {
160 ret.httpProxyHost = host;
161 ret.httpProxyPort = port;
162 } else if (protocol.equals("https")) {
163 ret.httpsProxyHost = host;
164 ret.httpsProxyPort = port;
165 } else if (protocol.equals("socks")) {
166 ret.socksProxyHost = host;
167 ret.socksProxyPort = port;
169 } catch (NumberFormatException nfe) { }
171 if (container[1] != null) {
172 st = new StringTokenizer(container[1], ";", false);
173 ret.excluded = new String[st.countTokens()];
174 for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
179 protected native boolean _newBrowserWindow_(String url);
180 protected void _newBrowserWindow(String url) {
181 if (!_newBrowserWindow_(url))
182 if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
185 // Win32Surface ////////////////////////////////////////////////////////////////////////////
187 public static class Win32Surface extends Surface {
189 /** used to block while waiting for the message pump thread to create a hwnd for us */
190 public Semaphore hwndCreated = new Semaphore();
192 /** nothing more than a method version of WndProc, so we can access instance members/methods */
193 public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
195 /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
196 boolean inside = false;
198 /** true iff we have 'captured' the mouse with SetCapture() */
199 boolean captured = false;
201 public int hwnd = -1;
204 public int current_cursor = default_cursor;
206 /** used to restore the cursor immediately before ReleaseCapture() */
207 public int previous_cursor = 0;
209 public native void natInit(boolean framed);
210 public Win32Surface(Box b, final boolean framed) {
213 hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
216 public void syncCursor() {
217 if (cursor.equals("default")) current_cursor = default_cursor;
218 else if (cursor.equals("wait")) current_cursor = wait_cursor;
219 else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
220 else if (cursor.equals("text")) current_cursor = text_cursor;
221 else if (cursor.equals("move")) current_cursor = move_cursor;
222 else if (cursor.equals("hand")) current_cursor = hand_cursor;
223 else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
224 else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
225 else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
226 else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
230 public native void finalize();
231 public native void postCursorChange();
232 public native void toBack();
233 public native void toFront();
234 public native void _setMinimized(boolean m);
235 public native void setInvisible(boolean i);
236 public native void _setMaximized(boolean m);
237 public native void setSize(int w, int h);
238 public native void setLocation(int x, int y);
239 public native void setTitleBarText(String s);
240 public native void setIcon(Picture p);
241 public native void _dispose();
242 public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
246 // Win32Picture ////////////////////////////////////////////////////////////////////////////
248 public static class Win32Picture implements Picture {
252 /** the Win32 bitmap version of this Picture */
255 /** dc of the bitmap */
258 /** true iff this Picture has translucent regions */
259 boolean hasalpha = false;
261 /** true iff this Picture has transparent regions but no translucent regions */
262 boolean hasmask = true;
264 /** if hasmask, this mask indicates which regions are transparent */
267 /** dc of the mask */
270 public int getWidth() { return w; };
271 public int getHeight() { return h; };
272 public int[] getData() { return data; }
273 public native void natInit();
274 public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
278 // Win32DoubleBuffer //////////////////////////////////////////////////////////////////////////
280 public static class Win32DoubleBuffer implements DoubleBuffer {
293 public int getHeight() { return h; }
294 public int getWidth() { return w; }
296 public native void natInit();
297 public Win32DoubleBuffer(int w, int h, Win32Surface owner) {
305 public native void setClip(int x, int y, int x2, int y2);
306 public native void fillRect(int x, int y, int x2, int y2, int color);
307 public native void drawString(String font, String text, int x, int y, int color);
308 public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
309 public native void finalize();
310 public void drawPicture(Picture source, int x, int y) {
311 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());