1
2 package org.xwt;
3
4 import org.bouncycastle.util.encoders.Base64;
5 import org.xwt.js.*;
6 import org.xwt.util.*;
7 import java.io.*;
8 import java.util.*;
9
10
16 public abstract class Surface extends PixelBuffer implements Scheduler.Task {
17
18
19
20 private static Boolean T = Boolean.TRUE;
21 private static Boolean F = Boolean.FALSE;
22
23
24 public static Vec allSurfaces = new Vec();
25
26
27 volatile boolean abort = false;
28
29
30 volatile boolean syncRootBoxToSurface = false;
31 volatile int pendingWidth = 0;
32 volatile int pendingHeight = 0;
33
34 public static boolean alt = false;
35 public static boolean control = false;
36 public static boolean shift = false;
37 public static boolean button1 = false;
38 public static boolean button2 = false;
39 public static boolean button3 = false;
40
41
42
43
44 public Box root;
45 public String cursor = "default";
46 public int mousex;
47 public int mousey;
48 public int _mousex;
49 public int _mousey;
50 public int newmousex = -1;
51 public int newmousey = -1;
52 public boolean minimized = false;
53 public boolean maximized = false;
54 DirtyList dirtyRegions = new DirtyList();
55
56
57
58 int last_press_x = Integer.MAX_VALUE;
59 int last_press_y = Integer.MAX_VALUE;
60 static int lastClickButton = 0;
61 static long lastClickTime = 0;
62
63
64
65
66 public abstract void toBack();
67 public abstract void toFront();
68 public abstract void syncCursor();
69 public abstract void setInvisible(boolean b);
70 protected abstract void _setMaximized(boolean b);
71 protected abstract void _setMinimized(boolean b);
72 public abstract void setLocation();
73 protected abstract void _setSize(int w, int h);
74 public abstract void setTitleBarText(String s);
75 public abstract void setIcon(Picture i);
76 public abstract void _dispose();
77 public void setMinimumSize(int minx, int miny, boolean resizable) { }
78 protected void setSize(int w, int h) { _setSize(w, h); }
79
80
81
82
83 protected final void Press(final int button) {
84 last_press_x = mousex;
85 last_press_y = mousey;
86
87 if (button == 1) button1 = true;
88 else if (button == 2) button2 = true;
89 else if (button == 3) button3 = true;
90
91 if (button == 1) new Message("_Press1", T, root);
92 else if (button == 2) new Message("_Press2", T, root);
93 else if (button == 3) {
94 final Box who = root;
95 Scheduler.add(new Scheduler.Task() { public void perform() throws JSExn {
96 Platform.clipboardReadEnabled = true;
97 try {
98 root.putAndTriggerTraps("_Press3", T);
99 } finally {
100 Platform.clipboardReadEnabled = false;
101 }
102 }});
103 }
104 }
105
106 protected final void Release(int button) {
107 if (button == 1) button1 = false;
108 else if (button == 2) button2 = false;
109 else if (button == 3) button3 = false;
110
111 if (button == 1) new Message("_Release1", T, root);
112 else if (button == 2) new Message("_Release2", T, root);
113 else if (button == 3) new Message("_Release3", T, root);
114
115 if (Platform.needsAutoClick() && Math.abs(last_press_x - mousex) < 5 && Math.abs(last_press_y - mousey) < 5) Click(button);
116 last_press_x = Integer.MAX_VALUE;
117 last_press_y = Integer.MAX_VALUE;
118 }
119
120 protected final void Click(int button) {
121 if (button == 1) new Message("_Click1", T, root);
122 else if (button == 2) new Message("_Click2", T, root);
123 else if (button == 3) new Message("_Click3", T, root);
124 if (Platform.needsAutoDoubleClick()) {
125 long now = System.currentTimeMillis();
126 if (lastClickButton == button && now - lastClickTime < 350) DoubleClick(button);
127 lastClickButton = button;
128 lastClickTime = now;
129 }
130 }
131
132
133 public void perform() {
134 if (mousex == newmousex && mousey == newmousey) return;
135 int oldmousex = mousex; mousex = newmousex;
136 int oldmousey = mousey; mousey = newmousey;
137 String oldcursor = cursor; cursor = "default";
138
139 if (!root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
140 root.putAndTriggerTrapsAndCatchExceptions("_Move", T);
141 if (!cursor.equals(oldcursor)) syncCursor();
142 }
143
144
150 protected final void Move(final int newmousex, final int newmousey) {
151 this.newmousex = newmousex;
152 this.newmousey = newmousey;
153 Scheduler.add(this);
154 }
155
156
157 protected final void SizeChange(final int width, final int height) {
158 if (pendingWidth == width && pendingHeight == height) return;
159 pendingWidth = width;
160 pendingHeight = height;
161 syncRootBoxToSurface = true;
162 abort = true;
163 Scheduler.renderAll();
164 }
165
166
167 protected final void PosChange(final int x, final int y) {
168 Scheduler.add(new Scheduler.Task() { public void perform() throws JSExn {
169 root.x = x;
170 root.y = y;
171 root.putAndTriggerTrapsAndCatchExceptions("PosChange", T);
172 }});
173 }
174
175 private final String[] doubleClick = new String[] { null, "_DoubleClick1", "_DoubleClick2", "_DoubleClick3" };
176 protected final void DoubleClick(int button) { new Message(doubleClick[button], T, root); }
177 protected final void KeyPressed(String key) { new Message("_KeyPressed", key, root); }
178 protected final void KeyReleased(String key) { new Message("_KeyReleased", key, root); }
179 protected final void Close() { new Message("Close", T, root); }
180 protected final void Minimized(boolean b) { minimized = b; new Message("Minimized", b ? T : F, root); }
181 protected final void Maximized(boolean b) { maximized = b; new Message("Maximized", b ? T : F, root); }
182 protected final void Focused(boolean b) { new Message("Focused", b ? T : F, root); }
183 public void Refresh() { Scheduler.add(new Scheduler.Task() { public void perform() { } }); }
184
185 public final void setMaximized(boolean b) { if (b != maximized) _setMaximized(maximized = b); }
186 public final void setMinimized(boolean b) { if (b != minimized) _setMinimized(minimized = b); }
187
188
189
190
191
192 public final void dispose(boolean quitIfAllSurfacesGone) {
193 if (Log.on) Log.info(this, "disposing " + this);
194 allSurfaces.removeElement(this);
195 _dispose();
196 if (allSurfaces.size() == 0) {
197 if (Log.on) Log.info(this, "exiting because last surface was destroyed");
198 System.exit(0);
199 }
200 }
201
202 public void dirty(int x, int y, int w, int h) {
203 dirtyRegions.dirty(x, y, w, h);
204 Refresh();
205 }
206
207 public static Surface fromBox(Box b) {
208
209 for(int i=0; i<allSurfaces.size(); i++) {
210 Surface s = (Surface)allSurfaces.elementAt(i);
211 if (s.root == b) return s;
212 }
213 return null;
214 }
215
216 public Surface(Box root) {
217 this.root = root;
218 root.setMaxWidth(JS.N(Math.min(Platform.getScreenWidth(), root.maxwidth)));
219 root.setMaxHeight(JS.N(Math.min(Platform.getScreenHeight(), root.maxheight)));
220 Surface old = fromBox(root);
221 if (old != null) old.dispose(false);
222 else root.removeSelf();
223 Refresh();
224 }
225
226 private static VectorGraphics.Affine identity = VectorGraphics.Affine.identity();
227
228
229 public synchronized void render() {
230
231
232 do {
233 abort = false;
234 root.repack();
235 if (syncRootBoxToSurface) {
236 root.setMaxWidth(JS.N(pendingWidth));
237 root.setMaxHeight(JS.N(pendingHeight));
238 syncRootBoxToSurface = false;
239 }
240 if (root.maxwidth != root.width || root.maxheight != root.height) {
241
242 dirty(0, root.height - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
243 dirty(0, root.maxheight - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
244 }
245 root.resize(root.x, root.y, root.maxwidth, root.maxheight);
246 root.resize_children();
247 setSize(root.width, root.height);
248 String oldcursor = cursor;
249 cursor = "default";
250 root.putAndTriggerTrapsAndCatchExceptions("_Move", JS.T);
251 if (!cursor.equals(oldcursor)) syncCursor();
252 } while(abort);
253
254 int[][] dirt = dirtyRegions.flush();
255 for(int i = 0; dirt != null && i < dirt.length; i++) {
256 if (dirt[i] == null) continue;
257 intintintintdirti], y = dirt[i][1], w = dirt[i][2], h = dirt[i][3];
258 if (x < 0) x = 0;
259 if (y < 0) y = 0;
260 if (x+w > root.width) w = root.width - x;
261 if (y+h > root.height) h = root.height - y;
262 if (w <= 0 || h <= 0) continue;
263
264 root.render(0, 0, x, y, x + w, y + h, this, identity);
265 drawPicture(Main.scarImage, 0, root.height - Main.scarImage.height, x, y, x+w, y+h);
266
267 if (abort) {
268
269 dirtyRegions.dirty(x, y, w, h);
270
271 for(int j=i; j<dirt.length; j++)
272 if (dirt[j] != null)
273 dirtyRegions.dirty(dirt[j][0], dirt[j][1], dirt[j][2], dirt[j][3]);
274 return;
275 }
276 }
277 }
278
279
280 public class Message implements Scheduler.Task {
281
282 private Box boxContainingMouse;
283 private Object value;
284 public String name;
285
286 Message(String name, Object value, Box boxContainingMouse) {
287 this.boxContainingMouse = boxContainingMouse;
288 this.name = name;
289 this.value = value;
290 Scheduler.add(this);
291 }
292
293 public void perform() {
294 if (name.equals("_KeyPressed")) {
295 String value = (String)this.value;
296 if (value.toLowerCase().endsWith("shift")) shift = true; else if (shift) value = value.toUpperCase();
297 if (value.toLowerCase().equals("alt")) alt = true; else if (alt) value = "A-" + value;
298 if (value.toLowerCase().endsWith("control")) control = true; else if (control) value = "C-" + value;
299 if (value.equals("C-v") || value.equals("A-v")) Platform.clipboardReadEnabled = true;
300 this.value = value;
301 } else if (name.equals("_KeyReleased")) {
302 String value = (String)this.value;
303 if (value.toLowerCase().equals("alt")) alt = false;
304 else if (value.toLowerCase().equals("control")) control = false;
305 else if (value.toLowerCase().equals("shift")) shift = false;
306 this.value = value;
307 }
308 try {
309 boxContainingMouse.putAndTriggerTrapsAndCatchExceptions(name, value);
310 } finally {
311 Platform.clipboardReadEnabled = false;
312 }
313 }
314 public String toString() { return "Message [name=" + name + ", value=" + value + "]"; }
315 }
316
317
318
319
320 public static abstract class DoubleBufferedSurface extends Surface {
321
322 public DoubleBufferedSurface(Box root) { super(root); }
323 PixelBuffer backbuffer = Platform.createPixelBuffer(Platform.getScreenWidth(), Platform.getScreenHeight(), this);
324 DirtyList screenDirtyRegions = new DirtyList();
325
326 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
327 screenDirtyRegions.dirty(cx1, cy1, cx2 - cx1, cy2 - cy1);
328 backbuffer.drawPicture(source, dx, dy, cx1, cy1, cx2, cy2);
329 }
330
331 public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int argb) {
332 screenDirtyRegions.dirty(cx1, cy1, cx2 - cx1, cy2 - cy1);
333 backbuffer.drawGlyph(source, dx, dy, cx1, cy1, cx2, cy2, argb);
334 }
335
336 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
337 screenDirtyRegions.dirty(Math.min(x1, x3), y1, Math.max(x2, x4) - Math.min(x1, x3), y2 - y1);
338 backbuffer.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
339 }
340
341 public void render() {
342 super.render();
343 if (abort) return;
344 int[][] dirt = screenDirtyRegions.flush();
345 for(int i = 0; dirt != null && i < dirt.length; i++) {
346 if (dirt[i] == null) continue;
347 int x = dirt[i][0];
348 int y = dirt[i][1];
349 int w = dirt[i][2];
350 int h = dirt[i][3];
351 if (x < 0) x = 0;
352 if (y < 0) y = 0;
353 if (x+w > root.width) w = root.width - x;
354 if (y+h > root.height) h = root.height - y;
355 if (w <= 0 || h <= 0) continue;
356 if (abort) return;
357 blit(backbuffer, x, y, x, y, w + x, h + y);
358 }
359 }
360
361
362 public final void Dirty(int x, int y, int w, int h) {
363 screenDirtyRegions.dirty(x, y, w, h);
364 Scheduler.renderAll();
365 }
366
367 public void dirty(int x, int y, int w, int h) {
368 screenDirtyRegions.dirty(x, y, w, h);
369 super.dirty(x, y, w, h);
370 }
371
372
373 public abstract void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2);
374
375 }
376
377 }
378