2002/04/28 04:36:08
[org.ibex.core.git] / src / org / xwt / plat / Win32.java
1 // Copyright 2002 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     public Win32() { }
43
44     public void init() {
45         String logfile = getTempPath() + "xwt-log.txt";
46         try {
47             PrintStream ps = new PrintStream(new FileOutputStream(logfile));
48             System.setOut(ps);
49             System.setErr(ps);
50         } catch (Throwable e) {
51             criticalAbort("Exception while attempting to redirect logging to " + logfile + " -- " + e);
52         }
53
54         new Thread() { public void run() { natInit(); } }.start();
55         messagePumpStarted.block();
56         fontList = new String[fontListVec.size()];
57         fontListVec.toArray(fontList);
58         fontListVec = null;
59     }
60
61
62     // Font Handling ////////////////////////////////////////////////////////////////////////////
63
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);
69
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" : ""));
76     }
77
78     static Hash fontCache = new Hash();
79     public static class Win32Font {
80         int hfont;
81         int maxAscent;
82         int maxDescent;
83     }
84
85     /** takes a parsed font and finds the closest platform-specific font */
86     static native Win32Font mapFont(Platform.ParsedFont pf);
87
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;
92
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";
99
100         ret = mapFont(pf);
101         fontCache.put(font, ret);
102         return ret;
103     }
104
105
106     // Implementation of Platform methods /////////////////////////////////////////////////////////
107
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);
119
120     protected native boolean _newBrowserWindow_(String url);
121     protected void _newBrowserWindow(String url) {
122         if (!_newBrowserWindow_(url))
123             if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
124     }
125
126     // Win32Surface ////////////////////////////////////////////////////////////////////////////
127
128     public static class Win32Surface extends Surface {
129
130         /** used to block while waiting for the message pump thread to create a hwnd for us */
131         public Semaphore hwndCreated = new Semaphore();
132
133         /** nothing more than a method version of WndProc, so we can access instance members/methods */
134         public native int WndProc(int hwnd, int imsg, int wparam, int lparam);
135
136         /** true iff the mouse is inside this window; used to determine if we should capture the mouse */
137         boolean inside = false;
138
139         /** true iff we have 'captured' the mouse with SetCapture() */
140         boolean captured = false;
141
142         public int hwnd = -1;
143         public int hdc = -1;
144
145         public int current_cursor = default_cursor;
146
147         /** used to restore the cursor immediately before ReleaseCapture() */
148         public int previous_cursor = 0;
149
150         public native void natInit(boolean framed);
151         public Win32Surface(Box b, final boolean framed) {
152             super(b);
153             if (Log.on) Log.log(this, "before, hwnd = " + hwnd);
154             if (Log.on) Log.log(this, "before, hdc = " + hdc);
155             natInit(framed);
156             if (Log.on) Log.log(this, "after, hwnd = " + hwnd);
157             if (Log.on) Log.log(this, "after, hdc = " + hdc);
158             hwndToWin32SurfaceMap.put(new Integer(hwnd), this);
159         }
160
161         public void syncCursor() {
162             if (cursor.equals("default")) current_cursor = default_cursor;
163             else if (cursor.equals("wait")) current_cursor = wait_cursor;
164             else if (cursor.equals("crosshair")) current_cursor = crosshair_cursor;
165             else if (cursor.equals("text")) current_cursor = text_cursor;
166             else if (cursor.equals("move")) current_cursor = move_cursor;
167             else if (cursor.equals("hand")) current_cursor = hand_cursor;
168             else if (cursor.equals("east") || cursor.equals("west")) current_cursor = sizewe_cursor;
169             else if (cursor.equals("north") || cursor.equals("south")) current_cursor = sizens_cursor;
170             else if (cursor.equals("northwest") || cursor.equals("southeast")) current_cursor = sizenwse_cursor;
171             else if (cursor.equals("northeast") || cursor.equals("southwest")) current_cursor = sizenesw_cursor;
172             postCursorChange();
173         }
174
175         public native void finalize();
176         public native void postCursorChange();
177         public native void toBack();
178         public native void toFront();
179         public native void _setMinimized(boolean m);
180         public native void setInvisible(boolean i);
181         public native void _setMaximized(boolean m);
182         public native void setSize(int w, int h);
183         public native void setLocation(int x, int y);
184         public native void setTitleBarText(String s);
185         public native void setIcon(Picture p);
186         public native void _dispose();
187         public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
188     }
189
190
191     // Win32Picture ////////////////////////////////////////////////////////////////////////////
192
193     public static class Win32Picture implements Picture {
194         int w = 0, h = 0;
195         int[] data = null;
196
197         /** the Win32 bitmap version of this Picture */
198         int hbitmap = -1;
199
200         /** dc of the bitmap */
201         int hdc = -1;
202
203         /** true iff this Picture has translucent regions */
204         boolean hasalpha = false;
205
206         /** true iff this Picture has transparent regions but no translucent regions */
207         boolean hasmask = true;
208
209         /** if hasmask, this mask indicates which regions are transparent */
210         int hmask = -1;
211
212         /** dc of the mask */
213         int maskdc = -1;
214
215         public int getWidth() { return w; };
216         public int getHeight() { return h; };
217         public int[] getData() { return data; }
218         public native void natInit();
219         public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
220     }
221     
222
223     // Win32DoubleBuffer //////////////////////////////////////////////////////////////////////////
224
225     public static class Win32DoubleBuffer implements DoubleBuffer {
226
227         int w = 0;
228         int h = 0;
229
230         int clipx1 = 0;
231         int clipy1 = 0;
232         int clipx2 = 0;
233         int clipy2 = 0;
234
235         int hdc = -1;
236         int hbitmap = -1;
237
238         public int getHeight() { return h; }
239         public int getWidth() { return w; }
240
241         public native void natInit();
242         public Win32DoubleBuffer(int w, int h, Win32Surface owner) {
243             this.w = w;
244             this.h = h;
245             clipx2 = w;
246             clipy2 = h;
247             natInit();
248         }
249
250         public native void setClip(int x, int y, int x2, int y2);
251         public native void fillRect(int x, int y, int x2, int y2, int color);
252         public native void drawString(String font, String text, int x, int y, int color);
253         public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
254         public native void finalize();
255         public void drawPicture(Picture source, int x, int y) {
256             drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
257         }
258
259     }
260
261 }
262