#34 call onResize() when window size is changed
[fanfix.git] / src / jexer / TApplication.java
index 6dd503f547a141d1a05f0511652c14e038a25f42..96dad6cc0b0cece1541d43dccab333b813f233d0 100644 (file)
@@ -1125,8 +1125,14 @@ public class TApplication implements Runnable {
         }
 
         secondaryEventReceiver.handleEvent(event);
-        if (doubleClick != null) {
-            secondaryEventReceiver.handleEvent(doubleClick);
+        // Note that it is possible for secondaryEventReceiver to be null
+        // now, because its handleEvent() might have finished out on the
+        // secondary thread.  So put any extra processing inside a null
+        // check.
+        if (secondaryEventReceiver != null) {
+            if (doubleClick != null) {
+                secondaryEventReceiver.handleEvent(doubleClick);
+            }
         }
     }
 
@@ -1199,7 +1205,8 @@ public class TApplication implements Runnable {
                     keepTimers.add(timer);
                 }
             }
-            timers = keepTimers;
+            timers.clear();
+            timers.addAll(keepTimers);
         }
 
         // Call onIdle's
@@ -1384,8 +1391,16 @@ public class TApplication implements Runnable {
                 System.currentTimeMillis(), Thread.currentThread(), x, y);
         }
         CellAttributes attr = getScreen().getAttrXY(x, y);
-        attr.setForeColor(attr.getForeColor().invert());
-        attr.setBackColor(attr.getBackColor().invert());
+        if (attr.getForeColorRGB() < 0) {
+            attr.setForeColor(attr.getForeColor().invert());
+        } else {
+            attr.setForeColorRGB(attr.getForeColorRGB() ^ 0x00ffffff);
+        }
+        if (attr.getBackColorRGB() < 0) {
+            attr.setBackColor(attr.getBackColor().invert());
+        } else {
+            attr.setBackColorRGB(attr.getBackColorRGB() ^ 0x00ffffff);
+        }
         getScreen().putAttrXY(x, y, attr, false);
     }
 
@@ -1902,11 +1917,12 @@ public class TApplication implements Runnable {
     }
 
     /**
-     * Add a window to my window list and make it active.
+     * Add a window to my window list and make it active.  Note package
+     * private access.
      *
      * @param window new window to add
      */
-    public final void addWindowToApplication(final TWindow window) {
+    final void addWindowToApplication(final TWindow window) {
 
         // Do not add menu windows to the window list.
         if (window instanceof TMenu) {
@@ -1919,6 +1935,11 @@ public class TApplication implements Runnable {
         }
 
         synchronized (windows) {
+            if (windows.contains(window)) {
+                throw new IllegalArgumentException("Window " + window +
+                    " is already in window list");
+            }
+
             // Whatever window might be moving/dragging, stop it now.
             for (TWindow w: windows) {
                 if (w.inMovements()) {
@@ -2042,6 +2063,9 @@ public class TApplication implements Runnable {
                 }
 
                 TWindow w = sorted.get(i);
+                int oldWidth = w.getWidth();
+                int oldHeight = w.getHeight();
+
                 w.setX(logicalX * newWidth);
                 w.setWidth(newWidth);
                 if (i >= ((a - 1) * b)) {
@@ -2051,6 +2075,12 @@ public class TApplication implements Runnable {
                     w.setY((logicalY * newHeight1) + 1);
                     w.setHeight(newHeight1);
                 }
+                if ((w.getWidth() != oldWidth)
+                    || (w.getHeight() != oldHeight)
+                ) {
+                    w.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET,
+                            w.getWidth(), w.getHeight()));
+                }
             }
         }
     }
@@ -2840,6 +2870,22 @@ public class TApplication implements Runnable {
         return new TInputBox(this, title, caption, text);
     }
 
+    /**
+     * Convenience function to spawn an input box.
+     *
+     * @param title window title, will be centered along the top border
+     * @param caption message to display.  Use embedded newlines to get a
+     * multi-line box.
+     * @param text initial text to seed the field with
+     * @param type one of the Type constants.  Default is Type.OK.
+     * @return the new input box
+     */
+    public final TInputBox inputBox(final String title, final String caption,
+        final String text, final TInputBox.Type type) {
+
+        return new TInputBox(this, title, caption, text, type);
+    }
+
     /**
      * Convenience function to open a terminal window.
      *
@@ -2947,6 +2993,7 @@ public class TApplication implements Runnable {
      * @param title window title, will be centered along the top border
      * @param width width of window
      * @param height height of window
+     * @return the new window
      */
     public final TWindow addWindow(final String title, final int width,
         final int height) {
@@ -2963,6 +3010,7 @@ public class TApplication implements Runnable {
      * @param width width of window
      * @param height height of window
      * @param flags bitmask of RESIZABLE, CENTERED, or MODAL
+     * @return the new window
      */
     public final TWindow addWindow(final String title,
         final int width, final int height, final int flags) {
@@ -2979,6 +3027,7 @@ public class TApplication implements Runnable {
      * @param y row relative to parent
      * @param width width of window
      * @param height height of window
+     * @return the new window
      */
     public final TWindow addWindow(final String title,
         final int x, final int y, final int width, final int height) {
@@ -2996,6 +3045,7 @@ public class TApplication implements Runnable {
      * @param width width of window
      * @param height height of window
      * @param flags mask of RESIZABLE, CENTERED, or MODAL
+     * @return the new window
      */
     public final TWindow addWindow(final String title,
         final int x, final int y, final int width, final int height,
@@ -3010,6 +3060,7 @@ public class TApplication implements Runnable {
      * active.
      *
      * @param file the file to open
+     * @return the new editor window
      * @throws IOException if a java.io operation throws
      */
     public final TEditorWindow addEditor(final File file) throws IOException {