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 /** Called once XWT is initialized and the application is running. On Win32, we need to block the main thread
88 * on a semaphore because if the main thread exits, the whole application quits. */
89 protected void _running() {
90 // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
91 if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
92 new org.xwt.util.Semaphore().block();
95 /** takes a parsed font and finds the closest platform-specific font */
96 static native Win32Font mapFont(Platform.ParsedFont pf);
98 /** takes an unparsed font and finds the closest platform-specific font */
99 static Win32Font getFont(String font) {
100 Win32Font ret = (Win32Font)fontCache.get(font);
101 if (ret != null) return ret;
103 Platform.ParsedFont pf = new Platform.ParsedFont(font);
104 if (pf.name.equals("serif")) pf.name = "Times New Roman";
105 else if (pf.name.equals("sansserif")) pf.name = "Arial";
106 else if (pf.name.equals("monospace")) pf.name = "Courier New";
107 else if (pf.name.equals("dialog")) pf.name = "MS Sans Serif";
108 else if (pf.name.equals("tty")) pf.name = "FixedSys";
111 fontCache.put(font, ret);
116 // Implementation of Platform methods /////////////////////////////////////////////////////////
118 protected native String _getEnv(String key);
119 protected boolean _needsAutoClick() { return true; }
120 protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
121 protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
122 protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Win32DoubleBuffer(w, h, (Win32Surface)owner); }
123 protected Picture _createPicture(int[] b, int w, int h) { return new Win32Picture(b, w, h); }
124 protected native int _getScreenWidth();
125 protected native int _getScreenHeight();
126 protected boolean _supressDirtyOnResize() { return false; }
127 protected native void _criticalAbort(String message);
128 protected native String _getClipBoard();
129 protected native void _setClipBoard(String s);
130 protected boolean _isCaseSensitive() { return false; }
132 private native void __detectProxy(String[] container);
134 protected synchronized Proxy _detectProxy() {
136 String[] container = new String[] { null, null, null };
137 if (Log.on) Log.log(this, "accessing Win32 registry");
138 __detectProxy(container);
139 if (container[2] == null && container[0] == null) {
140 if (Log.on) Log.log(this, "no proxy settings in the Win32 registry");
144 if (Log.on) Log.log(this, "PAC Script URL: " + container[2]);
145 if (Log.on) Log.log(this, "Proxy Server String: " + container[0]);
146 if (Log.on) Log.log(this, "Proxy Override String: " + container[1]);
148 Proxy ret = new Proxy();
149 if (container[2] != null) {
150 ret.proxyAutoConfigFunction = Proxy.getProxyAutoConfigFunction(container[2]);
151 if (ret.proxyAutoConfigFunction != null) return ret;
154 if (container[0] == null) return null;
155 StringTokenizer st = new StringTokenizer(container[0], ";", false);
156 while(st.hasMoreTokens()) try {
157 String s = st.nextToken().trim();
158 String protocol, host;
159 if (s.indexOf(':') == -1) {
161 } else if (s.indexOf("://") != -1) {
162 protocol = s.substring(0, s.indexOf("://"));
163 s = s.substring(s.indexOf("://") + 3);
164 host = s.substring(0, s.indexOf(':'));
165 } else if (s.indexOf('=') == -1) {
167 host = s.substring(0, s.indexOf(':'));
169 protocol = s.substring(0, s.indexOf('='));
170 host = s.substring(s.indexOf('=') + 1, s.indexOf(':'));
172 int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
173 if (protocol.equals("http")) {
174 ret.httpProxyHost = host;
175 ret.httpProxyPort = port;
176 } else if (protocol.equals("https")) {
177 ret.httpsProxyHost = host;
178 ret.httpsProxyPort = port;
179 } else if (protocol.equals("socks")) {
180 ret.socksProxyHost = host;
181 ret.socksProxyPort = port;
183 } catch (NumberFormatException nfe) { }
185 if (container[1] != null) {
186 st = new StringTokenizer(container[1], ";", false);
187 ret.excluded = new String[st.countTokens()];
188 for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
193 protected native boolean _newBrowserWindow_(String url);
194 protected void _newBrowserWindow(String url) {
195 if (!_newBrowserWindow_(url))
196 if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
199 // Win32Surface ////////////////////////////////////////////////////////////////////////////
201 public static class Win32Surface extends Surface {
203 /** used to block while waiting for the message pump thread to create a hwnd for us */
204 public Semaphore hwndCreated = new Semaphore();
206 /** nothing more than a method version of WndProc, so we can access instance members/methods */
207 public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
209 /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
210 boolean inside = false;
212 /** true iff we have 'captured' the mouse with SetCapture() */
213 boolean captured = false;
215 public int hwnd = -1;
218 public int current_cursor = default_cursor;
220 /** used to restore the cursor immediately before ReleaseCapture() */
221 public int previous_cursor = 0;
223 public native void natInit(boolean framed);
224 public Win32Surface(Box b, final boolean framed) {
227 hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
230 public void syncCursor() {
231 if (cursor.equals("default")) current_cursor = default_cursor;
232 else if (cursor.equals("wait")) current_cursor = wait_cursor;
233 else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
234 else if (cursor.equals("text")) current_cursor = text_cursor;
235 else if (cursor.equals("move")) current_cursor = move_cursor;
236 else if (cursor.equals("hand")) current_cursor = hand_cursor;
237 else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
238 else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
239 else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
240 else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
244 public native void finalize();
245 public native void postCursorChange();
246 public native void toBack();
247 public native void toFront();
248 public native void _setMinimized(boolean m);
249 public native void setInvisible(boolean i);
250 public native void _setMaximized(boolean m);
251 public native void setSize(int w, int h);
252 public native void setLocation(int x, int y);
253 public native void setTitleBarText(String s);
254 public native void setIcon(Picture p);
255 public native void _dispose();
256 public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
260 // Win32Picture ////////////////////////////////////////////////////////////////////////////
262 public static class Win32Picture extends Picture {
266 /** the Win32 bitmap version of this Picture */
269 /** dc of the bitmap */
272 /** true iff this Picture has translucent regions */
273 boolean hasalpha = false;
275 /** true iff this Picture has transparent regions but no translucent regions */
276 boolean hasmask = true;
278 /** if hasmask, this mask indicates which regions are transparent */
281 /** dc of the mask */
284 public int getWidth() { return w; };
285 public int getHeight() { return h; };
286 public int[] getData() { return data; }
287 public native void natInit();
288 public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
292 // Win32DoubleBuffer //////////////////////////////////////////////////////////////////////////
294 public static class Win32DoubleBuffer extends DoubleBuffer {
307 public int getHeight() { return h; }
308 public int getWidth() { return w; }
310 public native void natInit();
311 public Win32DoubleBuffer(int w, int h, Win32Surface owner) {
319 public native void setClip(int x, int y, int x2, int y2);
320 public native void fillRect(int x, int y, int x2, int y2, int color);
321 public native void drawString(String font, String text, int x, int y, int color);
322 public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
323 public native void finalize();
324 public void drawPicture(Picture source, int x, int y) {
325 drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());