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;
52 // Public Members and State Variables /////////////////////////////////////////////////////////
54 /** this is the box on this surface which the mouse was inside at the time that the currently-executing event was enqueued */
55 public Box boxContainingMouse = null;
57 /** false if the surface has never been rendered; used to determine if the surface should be repositioned to be centered on the screen */
58 public boolean centerSurfaceOnRender = true;
60 /** the x position of the mouse, relative to this Surface, in MessageQueue-time */
63 /** the y position of the mouse, relative to this Surface, in MessageQueue-time */
66 /** True iff this surface is minimized, in real time */
67 public boolean minimized = false;
69 /** True iff this surface is maximized, in real time */
70 public boolean maximized = false;
72 /** The name of the cursor on this surface -- this value fluctuates during rendering, so it may not be accurate;
73 * syncCursor() is called once the value is stable, to prevent the "flickering cursor" phenomenon
75 public String cursor = "default";
77 /** The width of the surface's drawable area, in real time */
80 /** The height of the surface's drawable area, in real time */
81 public int height = 0;
83 /** The Box at the root of this surface */
86 /** The number of SizeChange/PosChange traps triggered since the last successful render -- used to detect infinite loops */
87 public int sizePosChangesSinceLastRender = 0;
89 /** the x-position of the mouse the last time a Press message was enqueued */
90 int last_press_x = Integer.MAX_VALUE;
92 /** the y-position of the mouse the last time a Press message was enqueued */
93 int last_press_y = Integer.MAX_VALUE;
97 // Methods to be overridden by subclasses ///////////////////////////////////////////////////////
99 /** when this method is invoked, the surface should push itself to the back of the stacking order */
100 public abstract void toBack();
102 /** when this method is invoked, the surface should pull itself to the front of the stacking order */
103 public abstract void toFront();
105 /** sets the <i>actual</i> cursor for this surface to the cursor referenced by <tt>cursor</tt> */
106 public abstract void syncCursor();
108 /** If <tt>b == true</tt>, make the window invisible; otherwise, make it non-invisible. */
109 public abstract void setInvisible(boolean b);
111 /** If <tt>b == true</tt>, maximize the surface; otherwise, un-maximize it. */
112 protected abstract void _setMaximized(boolean b);
114 /** If <tt>b == true</tt>, minimize the surface; otherwise, un-minimize it. */
115 protected abstract void _setMinimized(boolean b);
117 /** Sets the surface's width and height. */
118 protected abstract void setSize(int width, int height);
120 /** Sets the surface's x and y position. */
121 public abstract void setLocation(int x, int 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, root.whoIs(mousex, mousey));
150 else if (button == 2) new SimpleMessage("Press2", Boolean.TRUE, root.whoIs(mousex, mousey));
151 else if (button == 3) {
152 final Box who = root.whoIs(mousex, mousey);
153 MessageQueue.add(new Message() { public void perform() {
154 Surface.this.boxContainingMouse = who;
155 Platform.clipboardReadEnabled = true;
156 root.put("Press1", null, Boolean.TRUE);
157 Platform.clipboardReadEnabled = false;
162 protected final void Release(int button) {
163 if (button == 1) button1 = false;
164 else if (button == 2) button2 = false;
165 else if (button == 3) button3 = false;
167 if (button == 1) new SimpleMessage("Release1", Boolean.TRUE, root.whoIs(mousex, mousey));
168 else if (button == 2) new SimpleMessage("Release2", Boolean.TRUE, root.whoIs(mousex, mousey));
169 else if (button == 3) new SimpleMessage("Release3", Boolean.TRUE, root.whoIs(mousex, mousey));
171 if (Platform.needsAutoClick() && Math.abs(last_press_x - mousex) < 5 && Math.abs(last_press_y - mousey) < 5) Click(button);
172 last_press_x = Integer.MAX_VALUE;
173 last_press_y = Integer.MAX_VALUE;
176 protected final void Click(int button) {
177 if (button == 1) new SimpleMessage("Click1", Boolean.TRUE, root.whoIs(mousex, mousey));
178 else if (button == 2) new SimpleMessage("Click2", Boolean.TRUE, root.whoIs(mousex, mousey));
179 else if (button == 3) new SimpleMessage("Click3", Boolean.TRUE, root.whoIs(mousex, mousey));
182 protected final void DoubleClick(int button) {
183 if (button == 1) new SimpleMessage("DoubleClick1", Boolean.TRUE, root.whoIs(mousex, mousey));
184 else if (button == 2) new SimpleMessage("DoubleClick2", Boolean.TRUE, root.whoIs(mousex, mousey));
185 else if (button == 3) new SimpleMessage("DoubleClick3", Boolean.TRUE, root.whoIs(mousex, mousey));
188 /** sends a KeyPressed message; subclasses should not add the C- or A- prefixes, nor should they capitalize alphabet characters */
189 protected final void KeyPressed(String key) {
190 if (key == null) return;
192 if (key.equals("alt")) alt = true;
193 else if (alt) key = "A-" + key;
195 if (key.endsWith("control")) control = true;
196 else if (control) key = "C-" + key;
198 if (key.endsWith("shift")) shift = true;
199 else if (shift) key = key.toUpperCase();
201 final String fkey = key;
202 MessageQueue.add(new KMessage(key));
205 // This is implemented as a private static class instead of an anonymous class to work around a GCJ bug
206 private class KMessage implements Message {
208 public KMessage(String k) { key = k; }
209 public void perform() {
210 if (key.equals("C-v") || key.equals("A-v")) Platform.clipboardReadEnabled = true;
211 outer: for(int i=0; i<keywatchers.size(); i++) {
212 Box b = (Box)keywatchers.elementAt(i);
213 for(Box cur = b; cur != null; cur = cur.getParent())
214 if (cur.invisible) continue outer;
215 b.put("KeyPressed", null, key);
217 Platform.clipboardReadEnabled = false;
221 /** sends a KeyReleased message; subclasses should not add the C- or A- prefixes, nor should they capitalize alphabet characters */
222 protected final void KeyReleased(final String key) {
223 if (key == null) return;
224 if (key.equals("alt")) alt = false;
225 else if (key.equals("control")) control = false;
226 else if (key.equals("shift")) shift = false;
227 MessageQueue.add(new Message() { public void perform() {
228 outer: for(int i=0; i<keywatchers.size(); i++) {
229 Box b = (Box)keywatchers.elementAt(i);
230 for(Box cur = b; cur != null; cur = cur.getParent())
231 if (cur.invisible) continue outer;
232 b.put("KeyReleased", null, key);
238 * Notify XWT that the mouse has moved. If the mouse leaves the
239 * surface, but the host windowing system does not provide its new
240 * position (for example, a Java MouseListener.mouseExited()
241 * message), the subclass should use (-1,-1).
243 protected final void Move(final int newmousex, final int newmousey) {
244 MessageQueue.add(lastMoveMessage = new Message() { public void perform() {
245 synchronized(Surface.this) {
247 // if move messages are arriving faster than we can process them, we just start ignoring them
248 if (lastMoveMessage != this) return;
250 int oldmousex = mousex;
251 int oldmousey = mousey;
255 String oldcursor = cursor;
258 // Root gets motion events outside itself (if trapped, of course)
259 if (root.is_trapped("Move") && !root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
260 root.put("Move", null, Boolean.TRUE);
262 root.Move(oldmousex, oldmousey, mousex, mousey);
263 if (!cursor.equals(oldcursor)) syncCursor();
268 protected final void SizeChange(int width, int height) {
270 this.height = height;
272 long lastResizeTime = System.currentTimeMillis();
273 lastResizeTimeTop = (int)(lastResizeTime >> 32);
274 lastResizeTimeBottom = (int)(lastResizeTime & 0xffffffff);
278 protected final void PosChange(int x, int y) {
279 root.set(Box.abs, 0, x);
280 root.set(Box.abs, 1, y);
281 new SimpleMessage("PosChange", Boolean.TRUE, null);
284 protected final void Close() { new SimpleMessage("Close", Boolean.TRUE, null); }
285 protected final void Minimized(boolean b) { minimized = b; new SimpleMessage("Minimized", b ? Boolean.TRUE : Boolean.FALSE, null); }
286 protected final void Maximized(boolean b) { maximized = b; new SimpleMessage("Maximized", b ? Boolean.TRUE : Boolean.FALSE, null); }
287 protected final void Focused(boolean b) { new SimpleMessage("Focused", b ? Boolean.TRUE : Boolean.FALSE, null); }
288 public static void Refresh() { MessageQueue.refresh(); }
290 // the following value is split into two int's to work around GCJ bug java/6393
292 /** used in conjunction with Platform.supressDirtyOnResize() */
293 private int lastResizeTimeTop = 0;
294 private int lastResizeTimeBottom = 0;
296 /** This is how subclasses signal a 'shallow dirty', indicating that although the backbuffer is valid, the screen is not */
297 public final void Dirty(int x, int y, int w, int h) {
298 long lastResizeTime = (((long)lastResizeTimeTop) << 32) | (long)lastResizeTimeBottom;
299 if (Platform.supressDirtyOnResize() && System.currentTimeMillis() - lastResizeTime < 100 && (w >= width - 1 || h >= height - 1)) return;
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 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;
330 // Other Methods ///////////////////////////////////////////////////////////////////////////////
332 /** If <tt>b == true</tt>, maximize the surface; otherwise, un-maximize it. */
333 public final void setMaximized(boolean b) {
334 if (b == maximized) return;
339 /** If <tt>b == true</tt>, minimize the surface; otherwise, un-minimize it. */
340 public final void setMinimized(boolean b) {
341 if (b == minimized) return;
346 /** wrapper for setSize() which makes sure to dirty the place where the scar used to be */
347 void _setSize(int width, int height) {
349 root.size(1) - vscar - scarPicture.getHeight(),
350 scarPicture.getWidth(), scarPicture.getHeight());
351 setSize(width, height);
353 this.height = height;
356 /** Indicates that the Surface is no longer needed */
357 public final void dispose() {
358 if (root == null) return;
359 if (Log.on) Log.log(this, "disposing " + this);
360 allSurfaces.removeElement(this);
363 // quit when all windows are closed
364 if (allSurfaces.size() == 0) {
366 if (refreshableSurfaceWasCreated) Log.log(this, "exiting because last remaining surface was disposed");
367 else Log.log(this, "exiting because no surface was ever created");
373 /** Indicates that the backbuffer region x,y,w,h is no longer correct and must be regenerated */
374 public void dirty(int x, int y, int w, int h) {
375 backbufferDirtyRegions.dirty(x, y, w, h);
379 public Surface(Box root) {
381 if (root.surface != null && root.surface.root == root) root.surface.dispose();
383 root.setSurface(this);
385 // make sure the root is properly sized
386 while (root.needs_prerender || abort) {
391 // this is a bit dangerous since we're passing ourselves to another method before subclasses' ctors have run...
392 backbuffer = Platform.createDoubleBuffer(Platform.getScreenWidth(), Platform.getScreenHeight(), this);
394 root.mark_for_prerender();
399 /** runs the prerender() and render() pipelines in the root Box to regenerate the backbuffer, then blits it to the screen */
400 public synchronized void render() {
402 // if the window size changed as a result of a user action, we have to update the root box's size
403 if (root.size(0) != width || root.size(1) != height) {
405 // since the scar will be moving, dirty the place it used to be
407 root.size(1) - vscar - scarPicture.getHeight(),
408 scarPicture.getWidth(), scarPicture.getHeight());
410 // sort of ugly; we can't use set() here because it will cause an infinite mutual recursion
411 root._size_0 = (short)width;
412 root._size_1 = (short)height;
414 root.mark_for_prerender();
415 root.put("SizeChange", null, Boolean.TRUE);
418 while (root.needs_prerender || abort) {
422 // update mouseinside and trigger Enter/Leave as a result of box size/position changes
423 String oldcursor = cursor;
425 root.Move(mousex, mousey, mousex, mousey);
426 if (!cursor.equals(oldcursor)) syncCursor();
429 if (centerSurfaceOnRender) {
430 centerSurfaceOnRender = false;
431 int x = (Platform.getScreenWidth() - width) / 2;
432 int y = (Platform.getScreenHeight() - height) / 2;
434 root.set(Box.abs, 0, x);
435 root.set(Box.abs, 1, y);
438 sizePosChangesSinceLastRender = 0;
439 int[][] dirt = backbufferDirtyRegions.flush();
440 for(int i = 0; dirt != null && i < dirt.length; i++) {
441 if (dirt[i] == null) continue;
448 if (x+w > width) w = width - x;
449 if (y+h > height) h = height - y;
450 if (w <= 0 || h <= 0) continue;
452 root.render(x, y, w, h, backbuffer);
454 // if any area under the scar was repainted, rescar that area
455 if (x < hscar + scarPicture.getWidth() &&
456 y + h > height - scarPicture.getHeight() - vscar) {
457 int _x1 = Math.max(x, hscar);
458 int _x2 = Math.min(x + w, hscar + scarPicture.getWidth());
459 int _y1 = Math.max(y, height - scarPicture.getHeight() - vscar);
460 int _y2 = Math.min(y + h, height - vscar);
462 backbuffer.drawPicture(scarPicture, _x1, _y1, _x2, _y2,
464 _y1 - (height - scarPicture.getHeight() - vscar),
466 _y2 - (height - scarPicture.getHeight() - vscar)
472 // x,y,w,h is only partially reconstructed, so we must be careful not to re-blit it
473 blitDirtyScreenRegions(x, y, w, h);
474 screenDirtyRegions.dirty(x, y, w, h);
476 // put back all the dirty regions we haven't yet processed (including the current one)
477 for(int j=i; j<dirt.length; j++)
479 backbufferDirtyRegions.dirty(dirt[j][0], dirt[j][1], dirt[j][2], dirt[j][3]);
486 // now that we've reconstructed this region in the backbuffer, queue it to be reblitted
487 screenDirtyRegions.dirty(x, y, w, h);
490 // blit out all the areas we've just reconstructed
491 blitDirtyScreenRegions();
494 /** blits from the backbuffer to the screen for all regions of the screen which have become dirty */
495 public synchronized void blitDirtyScreenRegions() { blitDirtyScreenRegions(-1, -1, 0, 0); }
497 /** same as blitDirtyScreenRegions(), except that it will skip any regions within a,b,c,d */
498 private synchronized void blitDirtyScreenRegions(int a, int b, int c, int d) {
500 int[][] dirt = screenDirtyRegions.flush();
501 if (Main.showRenders && dirt != null && dirt.length > 0 && a == -1)
502 blit(backbuffer, 0, 0, 0, 0, width, height);
504 for(int i = 0; dirt != null && i < dirt.length; i++) {
505 if (dirt[i] == null) continue;
512 if (x+w > root.size(0)) w = root.size(0) - x;
513 if (y+h > root.size(1)) h = root.size(1) - y;
514 if (w <= 0 || h <= 0) continue;
516 // if any part of this region falls within the "bad region", just skip it
517 boolean hhit = (x >= a && x <= a + c) || (x+w >= a && x+w <= a + c);
518 boolean vhit = (y >= b && y <= b + d) || (y+h >= b && y+h <= b + d);
520 screenDirtyRegions.dirty(x, y, w, h);
524 blit(backbuffer, x, y, x, y, w + x, h + y);
526 if (Main.showRenders) {
527 if (showRenderBuf == null) {
528 showRenderBuf = Platform.createDoubleBuffer(10, 10, this);
529 showRenderBuf.fillRect(0, 0, 10, 10, 0x00FF0000);
530 showRenderBuf2 = Platform.createDoubleBuffer(100, 100, this);
531 for(int y1 = 0; y1<100; y1++)
532 for(int x1 = 0; x1<100; x1++)
533 if ((x1 + y1) % 5 == 0)
534 showRenderBuf2.fillRect(x1, y1, x1 + 1, y1 + 1, 0x00FF0000);
536 for(int x1 = x; x1<x + w; x1 += 100)
537 for(int y1 = y; y1< y + h; y1 += 100) {
538 blit(showRenderBuf2, 0, 0, x1, y1, Math.min(x1 + 100, x + w), Math.min(y1 + 100, y + h));
540 for(int j=x; j<x + w; j += 10) {
541 blit(showRenderBuf, 0, 0, j, y, Math.min(j+ 10, x + w), y + 1);
542 blit(showRenderBuf, 0, 0, j, y + h, Math.min(j + 10, x + w), y + h + 1);
544 for(int j=y; j<y + h; j += 10) {
545 blit(showRenderBuf, 0, 0, x, j, x + 1, Math.min(j + 10, y + h));
546 blit(showRenderBuf, 0, 0, x + w, j, x + w + 1, Math.min(j + 10, y + h));
553 // FEATURE: reinstate recycler
554 private class SimpleMessage implements Message {
556 private Box boxContainingMouse;
557 private Object value;
560 SimpleMessage(String name, Object value, Box boxContainingMouse) {
561 this.boxContainingMouse = boxContainingMouse;
564 MessageQueue.add(this);
567 public void perform() {
568 Surface.this.boxContainingMouse = this.boxContainingMouse;
569 root.put(name, root, value);
572 public String toString() {
573 return "SimpleMessage [name=" + name + ", value=" + value + "]";
578 // Scar-Related Stuff ////////////////////////////////////////////////////////////////////
580 /** The scar's horizontal offset */
583 /** The scar's vertical offset */
586 /** the scar image drawn on the bottom right hand corner */
587 static Picture scarPicture = null;