1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.bouncycastle.util.encoders.Base64;
11 * A Surface, as described in the XWT Reference.
13 * Platform subclasses should include an inner class subclass of
14 * Surface to return from the Platform._createSurface() method
16 * Note that the members in the section 'state variables' are either
17 * in real-time (the actual size/position/state), or in
18 * Scheduler-time (the size/position/state at the time that the
19 * now-executing message was enqueued). This distinction is important.
21 public abstract class Surface extends PixelBuffer implements Scheduler.Task {
23 // Static Data ////////////////////////////////////////////////////////////////////////////////
25 private static Boolean T = Boolean.TRUE;
26 private static Boolean F = Boolean.FALSE;
28 /** all instances of Surface which need to be refreshed by the Scheduler */
29 public static Vec allSurfaces = new Vec();
31 /** When set to true, render() should abort as soon as possible and restart the rendering process */
32 static volatile boolean abort = false;
34 public static boolean alt = false; ///< true iff the alt button is pressed down, in real time
35 public static boolean control = false; ///< true iff the control button is pressed down, in real time
36 public static boolean shift = false; ///< true iff the shift button is pressed down, in real time
37 public static boolean button1 = false; ///< true iff button 1 is depressed, in Scheduler-time
38 public static boolean button2 = false; ///< true iff button 2 is depressed, in Scheduler-time
39 public static boolean button3 = false; ///< true iff button 3 is depressed, in Scheduler-tiem
43 // Instance Data ///////////////////////////////////////////////////////////////////////
45 Vec keywatchers = new Vec();
47 public Box root; ///< The Box at the root of this surface
48 public String cursor = "default"; ///< The active cursor to switch to when syncCursor() is called
49 public int mousex; ///< x position of the mouse, in Scheduler-time
50 public int mousey; ///< y position of the mouse, in Scheduler-time
51 public int newmousex = -1; ///< x position of the mouse, in realtime
52 public int newmousey = -1; ///< y position of the mouse, in realtime
53 public boolean minimized = false; ///< True iff this surface is minimized, in real time
54 public boolean maximized = false; ///< True iff this surface is maximized, in real time
55 private DirtyList dirtyRegions = new DirtyList(); ///< Dirty regions on the *screen*
57 // Used For Simulating Clicks and DoubleClicks /////////////////////////////////////////////////
59 int last_press_x = Integer.MAX_VALUE; ///< the x-position of the mouse the last time a Press message was enqueued
60 int last_press_y = Integer.MAX_VALUE; ///< the y-position of the mouse the last time a Press message was enqueued
61 static int lastClickButton = 0; ///< the last button to recieve a Click message; used for simulating DoubleClick's
62 static long lastClickTime = 0; ///< the last time a Click message was processed; used for simulating DoubleClick's
65 // Methods to be overridden by subclasses ///////////////////////////////////////////////////////
67 public abstract void toBack(); ///< should push surface to the back of the stacking order
68 public abstract void toFront(); ///< should pull surface to the front of the stacking order
69 public abstract void syncCursor(); ///< set the actual cursor to this.cursor if they do not match
70 public abstract void setInvisible(boolean b); ///< If <tt>b</tt>, make window invisible; otherwise, make it non-invisible.
71 protected abstract void _setMaximized(boolean b); ///< If <tt>b</tt>, maximize the surface; otherwise, un-maximize it.
72 protected abstract void _setMinimized(boolean b); ///< If <tt>b</tt>, minimize the surface; otherwise, un-minimize it.
73 public abstract void setLocation(); ///< Set the surface's x/y position to that of the root box
74 protected abstract void _setSize(int w, int h); ///< set the actual size of the surface
75 public abstract void setTitleBarText(String s); ///< Sets the surface's title bar text, if applicable
76 public abstract void setIcon(Picture i); ///< Sets the surface's title bar text, if applicable
77 public abstract void _dispose(); ///< Destroy the surface
78 public void setLimits(int min_width, int min_height, int max_width, int max_height) { /* FIXME */ }
81 // Helper methods for subclasses ////////////////////////////////////////////////////////////
83 protected final void Press(final int button) {
84 last_press_x = mousex;
85 last_press_y = mousey;
87 if (button == 1) button1 = true;
88 else if (button == 2) button2 = true;
89 else if (button == 3) button3 = true;
91 if (button == 1) new SimpleMessage("Press1", T, Box.whoIs(root, mousex, mousey));
92 else if (button == 2) new SimpleMessage("Press2", T, Box.whoIs(root, mousex, mousey));
93 else if (button == 3) {
94 final Box who = Box.whoIs(root, mousex, mousey);
95 Scheduler.add(new Scheduler.Task() { public void perform() {
96 Platform.clipboardReadEnabled = true;
97 root.putAndTriggerTraps("Press3", T);
98 Platform.clipboardReadEnabled = false;
103 protected final void Release(int button) {
104 if (button == 1) button1 = false;
105 else if (button == 2) button2 = false;
106 else if (button == 3) button3 = false;
108 if (button == 1) new SimpleMessage("Release1", T, Box.whoIs(root, mousex, mousey));
109 else if (button == 2) new SimpleMessage("Release2", T, Box.whoIs(root, mousex, mousey));
110 else if (button == 3) new SimpleMessage("Release3", T, Box.whoIs(root, mousex, mousey));
112 if (Platform.needsAutoClick() && Math.abs(last_press_x - mousex) < 5 && Math.abs(last_press_y - mousey) < 5) Click(button);
113 last_press_x = Integer.MAX_VALUE;
114 last_press_y = Integer.MAX_VALUE;
117 protected final void Click(int button) {
118 if (button == 1) new SimpleMessage("Click1", T, Box.whoIs(root, mousex, mousey));
119 else if (button == 2) new SimpleMessage("Click2", T, Box.whoIs(root, mousex, mousey));
120 else if (button == 3) new SimpleMessage("Click3", T, Box.whoIs(root, mousex, mousey));
121 if (Platform.needsAutoDoubleClick()) {
122 long now = System.currentTimeMillis();
123 if (lastClickButton == button && now - lastClickTime < 350) DoubleClick(button);
124 lastClickButton = button;
129 protected final void DoubleClick(int button) {
130 if (button == 1) new SimpleMessage("DoubleClick1", T, Box.whoIs(root, mousex, mousey));
131 else if (button == 2) new SimpleMessage("DoubleClick2", T, Box.whoIs(root, mousex, mousey));
132 else if (button == 3) new SimpleMessage("DoubleClick3", T, Box.whoIs(root, mousex, mousey));
135 /** Send a KeyPressed message; subclasses should not add the C- or A- prefixes or should they capitalize alphabet chars */
136 protected final void KeyPressed(String key) {
137 if (key == null) return;
138 if (key.toLowerCase().endsWith("shift")) shift = true; else if (shift) key = key.toUpperCase();
139 if (key.toLowerCase().equals("alt")) alt = true; else if (alt) key = "A-" + key;
140 if (key.toLowerCase().endsWith("control")) control = true; else if (control) key = "C-" + key;
141 Scheduler.add(new KMessage(key));
144 // This is implemented as a private static class instead of an anonymous class to work around a GCJ bug
145 private class KMessage implements Scheduler.Task {
147 public KMessage(String k) { key = k; }
148 public void perform() {
149 if (key.equals("C-v") || key.equals("A-v")) Platform.clipboardReadEnabled = true;
150 outer: for(int i=0; i<keywatchers.size(); i++) {
151 Box b = (Box)keywatchers.elementAt(i);
152 for(Box cur = b; cur != null; cur = cur.parent)
153 if (!cur.test(cur.VISIBLE)) continue outer;
154 b.putAndTriggerTraps("KeyPressed", key);
156 Platform.clipboardReadEnabled = false;
160 /** sends a KeyReleased message; subclasses should not add the C- or A- prefixes or capitalize alpha chars */
161 protected final void KeyReleased(final String key) {
162 if (key == null) return;
163 if (key.toLowerCase().equals("alt")) alt = false;
164 else if (key.toLowerCase().equals("control")) control = false;
165 else if (key.toLowerCase().equals("shift")) shift = false;
166 Scheduler.add(new Scheduler.Task() { public void perform() {
167 outer: for(int i=0; i<keywatchers.size(); i++) {
168 Box b = (Box)keywatchers.elementAt(i);
169 for(Box cur = b; cur != null; cur = cur.parent)
170 if (!cur.test(cur.VISIBLE)) continue outer;
171 b.putAndTriggerTraps("KeyReleased", key);
176 /** we enqueue ourselves in the Scheduler when we have a Move message to deal with */
177 public void perform() {
178 if (mousex == newmousex && mousey == newmousey) return;
179 int oldmousex = mousex; mousex = newmousex;
180 int oldmousey = mousey; mousey = newmousey;
181 String oldcursor = cursor; cursor = "default";
182 // Root gets motion events outside itself (if trapped)
183 if (!root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
184 root.putAndTriggerTraps("Move", T);
185 root.Move(oldmousex, oldmousey, mousex, mousey);
186 if (!cursor.equals(oldcursor)) syncCursor();
190 * Notify XWT that the mouse has moved. If the mouse leaves the
191 * surface, but the host windowing system does not provide its new
192 * position (for example, a Java MouseListener.mouseExited()
193 * message), the subclass should use (-1,-1).
195 protected final void Move(final int newmousex, final int newmousey) {
196 this.newmousex = newmousex;
197 this.newmousey = newmousey;
201 // FEATURE: can we avoid creating objects here?
202 protected final void SizeChange(final int width, final int height) {
203 Scheduler.addAtFront(new Scheduler.Task() { public void perform() {
204 // dirty the place where the scar used to be
205 root.dirty(0, root.maxheight - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
206 root.setMaxWidth(JS.N(width));
207 root.setMaxHeight(JS.N(height));
208 root.set(root.REFLOW);
213 // FEATURE: can we avoid creating objects here?
214 protected final void PosChange(final int x, final int y) {
215 Scheduler.add(new Scheduler.Task() { public void perform() {
218 root.putAndTriggerTraps("PosChange", T);
222 protected final void Close() { new SimpleMessage("Close", T, root); }
223 protected final void Minimized(boolean b) { minimized = b; new SimpleMessage("Minimized", b ? T : F, root); }
224 protected final void Maximized(boolean b) { maximized = b; new SimpleMessage("Maximized", b ? T : F, root); }
225 protected final void Focused(boolean b) { new SimpleMessage("Focused", b ? T : F, root); }
226 public static void Refresh() { Scheduler.add(renderAll); }
228 public static final Scheduler.Task renderAll = new Scheduler.Task() { public void perform() {
229 for(int i=0; i<allSurfaces.size(); i++)
230 ((Surface)allSurfaces.elementAt(i)).render();
233 public final void setMaximized(boolean b) { if (b != maximized) _setMaximized(maximized = b); }
234 public final void setMinimized(boolean b) { if (b != minimized) _setMinimized(minimized = b); }
237 // Other Methods ///////////////////////////////////////////////////////////////////////////////
239 /** Indicates that the Surface is no longer needed */
240 public final void dispose(boolean quitIfAllSurfacesGone) {
241 if (Log.on) Log.log(this, "disposing " + this);
242 allSurfaces.removeElement(this);
244 if (allSurfaces.size() == 0) {
245 if (Log.on) Log.log(this, "exiting because last surface was destroyed");
250 public void dirty(int x, int y, int w, int h) {
251 dirtyRegions.dirty(x, y, w, h);
255 public static Surface fromBox(Box b) {
256 for(int i=0; i<allSurfaces.size(); i++) {
257 Surface s = (Surface)allSurfaces.elementAt(i);
258 if (s.root == b) return s;
263 public Surface(Box root) {
265 root.setMaxWidth(JS.N(Math.min(Platform.getScreenWidth(), root.maxwidth)));
266 root.setMaxHeight(JS.N(Math.min(Platform.getScreenHeight(), root.maxheight)));
267 Surface old = fromBox(root);
268 if (old != null) old.dispose(false);
269 else root.removeSelf();
273 private static VectorGraphics.Affine identity = VectorGraphics.Affine.identity();
275 /** runs the prerender() and render() pipelines in the root Box to regenerate the backbuffer, then blits it to the screen */
276 public synchronized void render() {
278 // dirty the place where the scar used to be in case the root window size was programmatically changed
279 if (root.maxwidth != root.width || root.maxheight != root.height)
280 root.dirty(0, root.height - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
282 // make sure the root is properly sized
286 root.resize(root.x, root.y, root.maxwidth, root.maxheight);
287 root.resize_children();
288 _setSize(root.width, root.height);
289 String oldcursor = cursor;
291 root.Move(mousex, mousey, mousex, mousey);
292 if (!cursor.equals(oldcursor)) syncCursor();
295 int[][] dirt = dirtyRegions.flush();
296 for(int i = 0; dirt != null && i < dirt.length; i++) {
297 if (dirt[i] == null) continue;
298 int x = dirt[i][0], y = dirt[i][1], w = dirt[i][2], h = dirt[i][3];
301 if (x+w > root.width) w = root.width - x;
302 if (y+h > root.height) h = root.height - y;
303 if (w <= 0 || h <= 0) continue;
305 root.render(0, 0, x, y, x + w, y + h, this, identity);
306 drawPicture(Main.scarImage, 0, root.height - Main.scarImage.height, x, y, w, h);
310 // x,y,w,h is only partially reconstructed, so we must be careful not to re-blit it
311 dirtyRegions.dirty(x, y, w, h);
313 // put back all the dirty regions we haven't yet processed (including the current one)
314 for(int j=i; j<dirt.length; j++)
316 dirtyRegions.dirty(dirt[j][0], dirt[j][1], dirt[j][2], dirt[j][3]);
325 // FEATURE: reinstate recycler
326 public class SimpleMessage implements Scheduler.Task {
328 private Box boxContainingMouse;
329 private Object value;
332 SimpleMessage(String name, Object value, Box boxContainingMouse) {
333 this.boxContainingMouse = boxContainingMouse;
339 public void perform() { boxContainingMouse.putAndTriggerTraps(name, value); }
340 public String toString() { return "SimpleMessage [name=" + name + ", value=" + value + "]"; }
345 // Default PixelBuffer implementation /////////////////////////////////////////////////////////
347 public static abstract class DoubleBufferedSurface extends Surface {
349 public DoubleBufferedSurface(Box root) { super(root); }
350 PixelBuffer backbuffer = Platform.createPixelBuffer(Platform.getScreenWidth(), Platform.getScreenHeight(), this);
351 DirtyList screenDirtyRegions = new DirtyList();
353 public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
354 screenDirtyRegions.dirty(cx1, cy1, cx2 - cx1, cy2 - cy1);
355 backbuffer.drawPicture(source, dx, dy, cx1, cy1, cx2, cy2);
358 public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int argb) {
359 screenDirtyRegions.dirty(cx1, cy1, cx2 - cx1, cy2 - cy1);
360 backbuffer.drawGlyph(source, dx, dy, cx1, cy1, cx2, cy2, argb);
363 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
364 screenDirtyRegions.dirty(Math.min(x1, x3), y1, Math.max(x2, x4) - Math.min(x1, x3), y2 - y1);
365 backbuffer.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
368 public void render() {
373 public void render_() {
374 int[][] dirt = screenDirtyRegions.flush();
375 for(int i = 0; dirt != null && i < dirt.length; i++) {
376 if (dirt[i] == null) continue;
383 if (x+w > root.width) w = root.width - x;
384 if (y+h > root.height) h = root.height - y;
385 if (w <= 0 || h <= 0) continue;
386 blit(backbuffer, x, y, x, y, w + x, h + y);
390 /** This is how subclasses signal a 'shallow dirty', indicating that although the backbuffer is valid, the screen is not */
391 public final void Dirty(int x, int y, int w, int h) {
392 screenDirtyRegions.dirty(x, y, w, h);
396 /** copies a region from the doublebuffer to this surface */
397 public abstract void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2);