1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.bouncycastle.util.encoders.Base64;
10 * A Surface, as described in the XWT Reference.
12 * Platform subclasses should include an inner class subclass of
13 * Surface to return from the Platform._createSurface() method
15 * Note that the members in the section 'state variables' are either
16 * in real-time (the actual size/position/state), or in
17 * MessageQueue-time (the size/position/state at the time that the
18 * now-executing message was enqueued). This distinction is important.
20 public abstract class Surface {
22 // Static Data ////////////////////////////////////////////////////////////////////////////////
24 /** true iff a user-created surface was created */
25 static boolean refreshableSurfaceWasCreated = false;
27 /** a reference to the most recently enqueued Move message; used to throttle the message rate */
28 private static Message lastMoveMessage = null;
30 /** all instances of Surface which need to be refreshed by the MessageQueue */
31 public static Vec allSurfaces = new Vec();
33 /** true iff the alt button is pressed down, in real time */
34 public static boolean alt = false;
36 /** true iff the control button is pressed down, in real time */
37 public static boolean control = false;
39 /** true iff the shift button is pressed down, in real time */
40 public static boolean shift = false;
42 /** true iff button 1 is depressed, in MessageQueue-time */
43 public static boolean button1 = false;
45 /** true iff button 2 is depressed, in MessageQueue-time */
46 public static boolean button2 = false;
48 /** true iff button 3 is depressed, in MessageQueue-time */
49 public static boolean button3 = false;
51 /** true iff all surfaces created from now on should be scarred */
52 public static boolean scarAllSurfacesFromNowOn = false;
54 /** false if the surface has never been rendered; used to determine if the surface should be repositioned to be centered on the screen */
55 public boolean centerSurfaceOnRender = true;
57 /** the x position of the mouse, relative to this Surface, in MessageQueue-time */
60 /** the y position of the mouse, relative to this Surface, in MessageQueue-time */
63 /** True iff this surface is minimized, in real time */
64 public boolean minimized = false;
66 /** True iff this surface is maximized, in real time */
67 public boolean maximized = false;
69 /** The name of the cursor on this surface -- this value fluctuates during rendering, so it may not be accurate;
70 * syncCursor() is called once the value is stable, to prevent the "flickering cursor" phenomenon
72 public String cursor = "default";
74 /** The Box at the root of this surface */
77 /** The number of SizeChange/PosChange traps triggered since the last successful render -- used to detect infinite loops */
78 public int sizePosChangesSinceLastRender = 0;
81 // Used For Simulating Clicks and DoubleClicks /////////////////////////////////////////////////
83 /** the x-position of the mouse the last time a Press message was enqueued */
84 int last_press_x = Integer.MAX_VALUE;
86 /** the y-position of the mouse the last time a Press message was enqueued */
87 int last_press_y = Integer.MAX_VALUE;
89 /** the last button to recieve a Click message; used for simulating DoubleClick's */
90 static int lastClickButton = 0;
92 /** the last time a Click message was processed; used for simulating DoubleClick's */
93 static long lastClickTime = 0;
96 // Methods to be overridden by subclasses ///////////////////////////////////////////////////////
98 /** when this method is invoked, the surface should push itself to the back of the stacking order */
99 public abstract void toBack();
101 /** when this method is invoked, the surface should pull itself to the front of the stacking order */
102 public abstract void toFront();
104 /** sets the <i>actual</i> cursor for this surface to the cursor referenced by <tt>cursor</tt> */
105 public abstract void syncCursor();
107 /** If <tt>b == true</tt>, make the window invisible; otherwise, make it non-invisible. */
108 public abstract void setInvisible(boolean b);
110 /** If <tt>b == true</tt>, maximize the surface; otherwise, un-maximize it. */
111 protected abstract void _setMaximized(boolean b);
113 /** If <tt>b == true</tt>, minimize the surface; otherwise, un-minimize it. */
114 protected abstract void _setMinimized(boolean b);
116 /** Sets the surface's width and height. */
117 protected abstract void setSize(int width, int height);
119 /** Sets the surface's x and y position. */
120 public abstract void setLocation(int x, int y);
121 public void setLocation() { setLocation(root.x, root.y); }
123 /** Sets the surface's title bar text, if applicable */
124 public abstract void setTitleBarText(String s);
126 /** Sets the surface's title bar text, if applicable */
127 public abstract void setIcon(Picture i);
129 /** copies a region from the doublebuffer to this surface */
130 public abstract void blit(DoubleBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2);
132 /** Destroy the surface */
133 public abstract void _dispose();
135 /** Notifies the surface that limits have been imposed on the surface's size */
136 public void setLimits(int min_width, int min_height, int max_width, int max_height) { }
139 // Helper methods for subclasses ////////////////////////////////////////////////////////////
141 protected final void Press(final int button) {
142 last_press_x = mousex;
143 last_press_y = mousey;
145 if (button == 1) button1 = true;
146 else if (button == 2) button2 = true;
147 else if (button == 3) button3 = true;
149 if (button == 1) new SimpleMessage("Press1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
150 else if (button == 2) new SimpleMessage("Press2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
151 else if (button == 3) {
152 final Box who = Box.whoIs(root, mousex, mousey);
153 MessageQueue.add(new Message() { public void perform() {
154 Platform.clipboardReadEnabled = true;
155 root.put("Press3", Boolean.TRUE);
156 Platform.clipboardReadEnabled = false;
161 protected final void Release(int button) {
162 if (button == 1) button1 = false;
163 else if (button == 2) button2 = false;
164 else if (button == 3) button3 = false;
166 if (button == 1) new SimpleMessage("Release1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
167 else if (button == 2) new SimpleMessage("Release2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
168 else if (button == 3) new SimpleMessage("Release3", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
170 if (Platform.needsAutoClick() && Math.abs(last_press_x - mousex) < 5 && Math.abs(last_press_y - mousey) < 5) Click(button);
171 last_press_x = Integer.MAX_VALUE;
172 last_press_y = Integer.MAX_VALUE;
175 protected final void Click(int button) {
176 if (button == 1) new SimpleMessage("Click1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
177 else if (button == 2) new SimpleMessage("Click2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
178 else if (button == 3) new SimpleMessage("Click3", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
179 if (Platform.needsAutoDoubleClick()) {
180 long now = System.currentTimeMillis();
181 if (lastClickButton == button && now - lastClickTime < 350) DoubleClick(button);
182 lastClickButton = button;
187 protected final void DoubleClick(int button) {
188 if (button == 1) new SimpleMessage("DoubleClick1", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
189 else if (button == 2) new SimpleMessage("DoubleClick2", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
190 else if (button == 3) new SimpleMessage("DoubleClick3", Boolean.TRUE, Box.whoIs(root, mousex, mousey));
193 /** sends a KeyPressed message; subclasses should not add the C- or A- prefixes, nor should they capitalize alphabet characters */
194 protected final void KeyPressed(String key) {
195 if (key == null) return;
197 if (key.toLowerCase().endsWith("shift")) shift = true;
198 else if (shift) key = key.toUpperCase();
200 if (key.toLowerCase().equals("alt")) alt = true;
201 else if (alt) key = "A-" + key;
203 if (key.toLowerCase().endsWith("control")) control = true;
204 else if (control) key = "C-" + key;
206 final String fkey = key;
207 MessageQueue.add(new KMessage(key));
210 // This is implemented as a private static class instead of an anonymous class to work around a GCJ bug
211 private class KMessage implements Message {
213 public KMessage(String k) { key = k; }
214 public void perform() {
215 if (key.equals("C-v") || key.equals("A-v")) Platform.clipboardReadEnabled = true;
216 outer: for(int i=0; i<keywatchers.size(); i++) {
217 Box b = (Box)keywatchers.elementAt(i);
218 for(Box cur = b; cur != null; cur = cur.getParent())
219 if (cur.invisible) continue outer;
220 b.put("KeyPressed", key);
222 Platform.clipboardReadEnabled = false;
226 /** sends a KeyReleased message; subclasses should not add the C- or A- prefixes, nor should they capitalize alphabet characters */
227 protected final void KeyReleased(final String key) {
228 if (key == null) return;
229 if (key.toLowerCase().equals("alt")) alt = false;
230 else if (key.toLowerCase().equals("control")) control = false;
231 else if (key.toLowerCase().equals("shift")) shift = false;
232 MessageQueue.add(new Message() { public void perform() {
233 outer: for(int i=0; i<keywatchers.size(); i++) {
234 Box b = (Box)keywatchers.elementAt(i);
235 for(Box cur = b; cur != null; cur = cur.getParent())
236 if (cur.invisible) continue outer;
237 b.put("KeyReleased", key);
243 * Notify XWT that the mouse has moved. If the mouse leaves the
244 * surface, but the host windowing system does not provide its new
245 * position (for example, a Java MouseListener.mouseExited()
246 * message), the subclass should use (-1,-1).
248 protected final void Move(final int newmousex, final int newmousey) {
249 MessageQueue.add(lastMoveMessage = new Message() { public void perform() {
250 synchronized(Surface.this) {
252 // if move messages are arriving faster than we can process them, we just start ignoring them
253 if (lastMoveMessage != this) return;
255 int oldmousex = mousex;
256 int oldmousey = mousey;
260 String oldcursor = cursor;
263 // Root gets motion events outside itself (if trapped, of course)
264 if (!root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
265 root.put("Move", Boolean.TRUE);
267 root.Move(oldmousex, oldmousey, mousex, mousey);
268 if (!cursor.equals(oldcursor)) syncCursor();
273 protected final void SizeChange(final int width, final int height) {
274 MessageQueue.add(new Message() { public void perform() {
275 if (width == root.width && height == root.height) return;
277 root.height = height;
278 root.needs_reflow = true;
283 protected final void PosChange(final int x, final int y) {
284 MessageQueue.add(new Message() { public void perform() {
285 root.put("x", new Integer(x));
286 root.put("y", new Integer(y));
290 protected final void Close() { new SimpleMessage("Close", Boolean.TRUE, root); }
291 protected final void Minimized(boolean b) { minimized = b; new SimpleMessage("Minimized", b ? Boolean.TRUE : Boolean.FALSE, root); }
292 protected final void Maximized(boolean b) { maximized = b; new SimpleMessage("Maximized", b ? Boolean.TRUE : Boolean.FALSE, root); }
293 protected final void Focused(boolean b) { new SimpleMessage("Focused", b ? Boolean.TRUE : Boolean.FALSE, root); }
294 public static void Refresh() { MessageQueue.refresh(); }
296 // the following value is split into two int's to work around GCJ bug java/6393
298 /** This is how subclasses signal a 'shallow dirty', indicating that although the backbuffer is valid, the screen is not */
299 public final void Dirty(int x, int y, int w, int h) {
300 screenDirtyRegions.dirty(x, y, w, h);
305 // Private Instance Data /////////////////////////////////////////////////////////////////////////////////////////////
307 /** The automatic double buffer for the root box */
308 DoubleBuffer backbuffer = null;
310 /** Dirty regions on the backbuffer which need to be rebuilt using Box.render() */
311 private DirtyList backbufferDirtyRegions = new DirtyList();
313 /** Dirty regions on the screen which need to be rebuilt using Surface.blit() */
314 private DirtyList screenDirtyRegions = new DirtyList();
316 /** A list of all the Boxes on this Surface that should be notified of keyboard events */
317 Vec keywatchers = new Vec();
319 /** When set to true, render() should abort as soon as possible and restart the rendering process */
320 static volatile boolean abort = false;
322 /** a solid red 10x10 double buffer */
323 private DoubleBuffer showRenderBuf = null;
325 /** a striped 100x100 double buffer */
326 private DoubleBuffer showRenderBuf2 = null;
328 /** true iff this window should be scarred */
329 private boolean scarred = true;
332 // Other Methods ///////////////////////////////////////////////////////////////////////////////
334 /** If <tt>b == true</tt>, maximize the surface; otherwise, un-maximize it. */
335 public final void setMaximized(boolean b) {
336 if (b == maximized) return;
341 /** If <tt>b == true</tt>, minimize the surface; otherwise, un-minimize it. */
342 public final void setMinimized(boolean b) {
343 if (b == minimized) return;
348 /** wrapper for setSize() which makes sure to dirty the place where the scar used to be */
351 root.width = Math.max(root.width, scarPicture.getWidth());
352 root.height = Math.max(root.height, scarPicture.getHeight());
354 root.height - vscar - scarPicture.getHeight(),
355 scarPicture.getWidth(),
356 scarPicture.getHeight());
358 setSize(root.width, root.height);
361 /** Indicates that the Surface is no longer needed */
362 public final void dispose(boolean quitIfAllSurfacesGone) {
363 if (root == null) return;
364 if (Log.on) Log.log(this, "disposing " + this);
365 allSurfaces.removeElement(this);
368 // quit when all windows are closed
369 if (allSurfaces.size() == 0 && quitIfAllSurfacesGone && Main.doneInitializing) {
371 if (refreshableSurfaceWasCreated) Log.log(this, "exiting because last remaining surface was disposed");
372 else Log.log(this, "exiting because no surface was ever created");
378 /** Indicates that the backbuffer region x,y,w,h is no longer correct and must be regenerated */
379 public void dirty(int x, int y, int w, int h) {
380 backbufferDirtyRegions.dirty(x, y, w, h);
384 public Surface(Box root) {
385 this.scarred = scarAllSurfacesFromNowOn;
386 scarAllSurfacesFromNowOn = true;
388 if (root.surface != null && root.surface.root == root) {
389 root.surface.dispose(false);
395 // make sure the root is properly sized
401 // this is a bit dangerous since we're passing ourselves to another method before subclasses' ctors have run...
402 backbuffer = Platform.createDoubleBuffer(Platform.getScreenWidth(), Platform.getScreenHeight(), this);
408 /** runs the prerender() and render() pipelines in the root Box to regenerate the backbuffer, then blits it to the screen */
409 public synchronized void render() {
411 // make sure the root is properly sized
415 // update mouseinside and trigger Enter/Leave as a result of box size/position changes
416 String oldcursor = cursor;
418 root.Move(mousex, mousey, mousex, mousey);
419 if (!cursor.equals(oldcursor)) syncCursor();
422 if (centerSurfaceOnRender) {
423 centerSurfaceOnRender = false;
424 int x = (Platform.getScreenWidth() - root.width) / 2;
425 int y = (Platform.getScreenHeight() - root.height) / 2;
431 sizePosChangesSinceLastRender = 0;
432 int[][] dirt = backbufferDirtyRegions.flush();
433 for(int i = 0; dirt != null && i < dirt.length; i++) {
434 if (dirt[i] == null) continue;
441 if (x+w > root.width) w = root.width - x;
442 if (y+h > root.height) h = root.height - y;
443 if (w <= 0 || h <= 0) continue;
445 root.render(0, 0, x, y, w, h, backbuffer);
447 // if any area under the scar was repainted, rescar that area
448 if (scarred && x < hscar + scarPicture.getWidth() &&
449 y + h > root.height - scarPicture.getHeight() - vscar) {
450 int _x1 = Math.max(x, hscar);
451 int _x2 = Math.min(x + w, hscar + scarPicture.getWidth());
452 int _y1 = Math.max(y, root.height - scarPicture.getHeight() - vscar);
453 int _y2 = Math.min(y + h, root.height - vscar);
455 backbuffer.drawPicture(scarPicture, _x1, _y1, _x2, _y2,
457 _y1 - (root.height - scarPicture.getHeight() - vscar),
459 _y2 - (root.height - scarPicture.getHeight() - vscar)
465 // x,y,w,h is only partially reconstructed, so we must be careful not to re-blit it
466 blitDirtyScreenRegions(x, y, w, h);
467 screenDirtyRegions.dirty(x, y, w, h);
469 // put back all the dirty regions we haven't yet processed (including the current one)
470 for(int j=i; j<dirt.length; j++)
472 backbufferDirtyRegions.dirty(dirt[j][0], dirt[j][1], dirt[j][2], dirt[j][3]);
479 // now that we've reconstructed this region in the backbuffer, queue it to be reblitted
480 screenDirtyRegions.dirty(x, y, w, h);
483 // blit out all the areas we've just reconstructed
484 blitDirtyScreenRegions();
487 /** blits from the backbuffer to the screen for all regions of the screen which have become dirty */
488 public synchronized void blitDirtyScreenRegions() { blitDirtyScreenRegions(-1, -1, 0, 0); }
490 /** same as blitDirtyScreenRegions(), except that it will skip any regions within a,b,c,d */
491 private synchronized void blitDirtyScreenRegions(int a, int b, int c, int d) {
493 int[][] dirt = screenDirtyRegions.flush();
494 if (Main.showRenders && dirt != null && dirt.length > 0 && a == -1)
495 blit(backbuffer, 0, 0, 0, 0, root.width, root.height);
497 for(int i = 0; dirt != null && i < dirt.length; i++) {
498 if (dirt[i] == null) continue;
505 if (x+w > root.width) w = root.width - x;
506 if (y+h > root.height) h = root.height - y;
507 if (w <= 0 || h <= 0) continue;
509 // if any part of this region falls within the "bad region", just skip it
510 boolean hhit = (x >= a && x <= a + c) || (x+w >= a && x+w <= a + c);
511 boolean vhit = (y >= b && y <= b + d) || (y+h >= b && y+h <= b + d);
513 screenDirtyRegions.dirty(x, y, w, h);
517 blit(backbuffer, x, y, x, y, w + x, h + y);
519 if (Main.showRenders) {
520 if (showRenderBuf == null) {
521 showRenderBuf = Platform.createDoubleBuffer(10, 10, this);
522 showRenderBuf.fillRect(0, 0, 10, 10, 0x00FF0000);
523 showRenderBuf2 = Platform.createDoubleBuffer(100, 100, this);
524 for(int y1 = 0; y1<100; y1++)
525 for(int x1 = 0; x1<100; x1++)
526 if ((x1 + y1) % 5 == 0)
527 showRenderBuf2.fillRect(x1, y1, x1 + 1, y1 + 1, 0x00FF0000);
529 for(int x1 = x; x1<x + w; x1 += 100)
530 for(int y1 = y; y1< y + h; y1 += 100) {
531 blit(showRenderBuf2, 0, 0, x1, y1, Math.min(x1 + 100, x + w), Math.min(y1 + 100, y + h));
533 for(int j=x; j<x + w; j += 10) {
534 blit(showRenderBuf, 0, 0, j, y, Math.min(j+ 10, x + w), y + 1);
535 blit(showRenderBuf, 0, 0, j, y + h, Math.min(j + 10, x + w), y + h + 1);
537 for(int j=y; j<y + h; j += 10) {
538 blit(showRenderBuf, 0, 0, x, j, x + 1, Math.min(j + 10, y + h));
539 blit(showRenderBuf, 0, 0, x + w, j, x + w + 1, Math.min(j + 10, y + h));
546 // FEATURE: reinstate recycler
547 public class SimpleMessage implements Message {
549 private Box boxContainingMouse;
550 private Object value;
553 SimpleMessage(String name, Object value, Box boxContainingMouse) {
554 this.boxContainingMouse = boxContainingMouse;
557 MessageQueue.add(this);
560 public void perform() { boxContainingMouse.put(name, value); }
561 public String toString() { return "SimpleMessage [name=" + name + ", value=" + value + "]"; }
565 // Scar-Related Stuff ////////////////////////////////////////////////////////////////////
567 /** The scar's horizontal offset */
570 /** The scar's vertical offset */
573 /** the scar image drawn on the bottom right hand corner */
574 static Picture scarPicture = null;