misc cleanup
authorKevin Lamonte <kevin.lamonte@gmail.com>
Thu, 19 Mar 2015 11:06:03 +0000 (07:06 -0400)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Thu, 19 Mar 2015 11:06:03 +0000 (07:06 -0400)
15 files changed:
README.md
src/jexer/TApplication.java
src/jexer/TCheckbox.java
src/jexer/TField.java
src/jexer/THScroller.java
src/jexer/TLabel.java
src/jexer/TProgressBar.java
src/jexer/TRadioButton.java
src/jexer/TRadioGroup.java
src/jexer/TText.java
src/jexer/TVScroller.java
src/jexer/TWidget.java
src/jexer/TWindow.java
src/jexer/backend/ECMA48Backend.java
src/jexer/io/ECMA48Terminal.java

index a58e024c2930fff1aab09040e51298a601f926a4..c2946c69e8b8a6fe1f0756ff49bff73c35782259 100644 (file)
--- a/README.md
+++ b/README.md
@@ -82,9 +82,9 @@ Many tasks remain before calling this version 1.0:
 
 0.0.2:
 
+- Fix clipping errors
 - AWT:
   - Blinking cursor
-- Clean up TWidget constuctors (everyone is doing setX() / setY() / set...)
 - ECMA48Backend running on socket
 - TTreeView
 - TDirectoryList
@@ -99,10 +99,9 @@ Many tasks remain before calling this version 1.0:
 0.0.4:
 
 - Bugs
-  - Bare ESC isn't being returned immediately
   - TTimer is jittery with I/O
   - TSubMenu keyboard mnemonic not working
-  - kbDel and use by TMenu (MID_CLEAR)
+  - Making TMenu keyboard accelerators active/inactive
   - TDirectoryList cannot be navigated only with keyboard
   - TTreeView cannot be navigated only with keyboard
   - RangeViolation after dragging scrollbar up/down
index 4aaab1d05f5ee2b1e1ff6791321b45cbc1b7d5e6..9d845be18affeecbd94a91ad2939a9ad8da4fa2c 100644 (file)
@@ -69,6 +69,11 @@ public class TApplication {
      */
     private static final boolean debugThreads = false;
 
+    /**
+     * If true, emit events being processed to System.err.
+     */
+    private static final boolean debugEvents = false;
+
     /**
      * WidgetEventHandler is the main event consumer loop.  There are at most
      * two such threads in existence: the primary for normal case and a
@@ -644,10 +649,10 @@ public class TApplication {
      */
     private void metaHandleEvent(final TInputEvent event) {
 
-        /*
-        System.err.printf(String.format("metaHandleEvents event: %s\n",
-                event)); System.err.flush();
-         */
+        if (debugEvents) {
+            System.err.printf(String.format("metaHandleEvents event: %s\n",
+                    event)); System.err.flush();
+        }
 
         if (quit) {
             // Do no more processing if the application is already trying
@@ -711,7 +716,9 @@ public class TApplication {
      */
     private void primaryHandleEvent(final TInputEvent event) {
 
-        // System.err.printf("Handle event: %s\n", event);
+        if (debugEvents) {
+            System.err.printf("Handle event: %s\n", event);
+        }
 
         // Special application-wide events -----------------------------------
 
@@ -804,7 +811,10 @@ public class TApplication {
                     mouse.setX(mouse.getX() - window.getX());
                     mouse.setY(mouse.getY() - window.getY());
                 }
-                // System.err("TApplication dispatch event: %s\n", event);
+                if (debugEvents) {
+                    System.err.printf("TApplication dispatch event: %s\n",
+                        event);
+                }
                 window.handleEvent(event);
                 break;
             }
index 939f3fed0272ccd9905ffd8f9c3bf24db53764ee..b33d1f59252705042adf945af8a325bc6785c618 100644 (file)
@@ -64,13 +64,9 @@ public final class TCheckbox extends TWidget {
         final String label, final boolean checked) {
 
         // Set parent and window
-        super(parent);
+        super(parent, x, y, label.length() + 4, 1);
 
-        setX(x);
-        setY(y);
-        setHeight(1);
         this.label = label;
-        setWidth(label.length() + 4);
         this.checked = checked;
 
         setHasCursor(true);
index 406c57567bc2871e6c13e1bed93c275ef4b75ce8..083efdda3853ad6898e9b31c010ae01c703ce503 100644 (file)
@@ -139,13 +139,9 @@ public class TField extends TWidget {
         final TAction enterAction, final TAction updateAction) {
 
         // Set parent and window
-        super(parent);
-        setX(x);
-        setY(y);
-        setHeight(1);
-        setWidth(width);
-        setHasCursor(true);
+        super(parent, x, y, width, 1);
 
+        setHasCursor(true);
         this.fixed = fixed;
         this.text = text;
         this.enterAction = enterAction;
index a3599a9c25e09071b0217b1f86cb11f1aef40604..3539ff4e874d579092be6c83cd05bc3dfa7f40cb 100644 (file)
@@ -158,12 +158,7 @@ public final class THScroller extends TWidget {
         final int width) {
 
         // Set parent and window
-        super(parent);
-
-        setX(x);
-        setY(y);
-        setHeight(1);
-        setWidth(width);
+        super(parent, x, y, width, 1);
     }
 
     /**
index d56be67f7d3815a42dc25c6f43c7e9a416db9cb8..e57ad9b1de1dbe9e94844ca3e2a6ff85dd5de618 100644 (file)
@@ -92,13 +92,9 @@ public final class TLabel extends TWidget {
         final int y, final String colorKey) {
 
         // Set parent and window
-        super(parent, false);
+        super(parent, false, x, y, text.length(), 1);
 
         this.text = text;
-        setX(x);
-        setY(y);
-        setHeight(1);
-        setWidth(text.length());
         this.colorKey = colorKey;
     }
 
index 1cd950c9babed03fdbcfa1c013b00ec8215f2aaf..51a8853251efb0949eee46297a14e7d8d809035a 100644 (file)
@@ -120,12 +120,8 @@ public final class TProgressBar extends TWidget {
         final int width, final int value) {
 
         // Set parent and window
-        super(parent, false);
+        super(parent, false, x, y, width, 1);
 
-        setX(x);
-        setY(y);
-        setHeight(1);
-        setWidth(width);
         this.value = value;
     }
 
index 23663bb2c84d6ec0894d36bd033a3b47037711e8..685f1f484d752072d11eca248f997426a0070ce1 100644 (file)
@@ -101,13 +101,9 @@ public final class TRadioButton extends TWidget {
         final String label, final int id) {
 
         // Set parent and window
-        super(parent);
+        super(parent, x, y, label.length() + 4, 1);
 
-        setX(x);
-        setY(y);
-        setHeight(1);
         this.label = label;
-        setWidth(label.length() + 4);
         this.id = id;
 
         setHasCursor(true);
index c1b652352d1b4b36049be132fed9fa229d96382e..e2e24a33ddf232a226636214891bbf35e0b0329d 100644 (file)
@@ -84,13 +84,9 @@ public final class TRadioGroup extends TWidget {
         final String label) {
 
         // Set parent and window
-        super(parent);
+        super(parent, x, y, label.length() + 4, 2);
 
-        setX(x);
-        setY(y);
-        setHeight(2);
         this.label = label;
-        setWidth(label.length() + 4);
     }
 
     /**
index 1f1359fb01cd2e40afec8165616e0d1e1df32ba2..6d2fe27faaf6e3ae9652761acd52e9c7ea8d1830 100644 (file)
@@ -243,12 +243,8 @@ public final class TText extends TWidget {
         final int y, final int width, final int height, final String colorKey) {
 
         // Set parent and window
-        super(parent);
+        super(parent, x, y, width, height);
 
-        setX(x);
-        setY(y);
-        setWidth(width);
-        setHeight(height);
         this.text = text;
         this.colorKey = colorKey;
 
index d9d5ec852bb1495c7ce4fed35a9498f7d4ab1775..51fcd8519f6bd47eba9064b5b2f4582316f2776b 100644 (file)
@@ -158,12 +158,7 @@ public final class TVScroller extends TWidget {
         final int height) {
 
         // Set parent and window
-        super(parent);
-
-        setX(x);
-        setY(y);
-        setHeight(height);
-        setWidth(1);
+        super(parent, x, y, 1, height);
     }
 
     /**
index 2a92d09d25c429a6afe916f80f055a54156e5a2e..d6864db746316659ef5d3ed4fed3cc76b0934d6a 100644 (file)
@@ -547,6 +547,21 @@ public abstract class TWidget implements Comparable<TWidget> {
         this(parent, true);
     }
 
+    /**
+     * Protected constructor.
+     *
+     * @param parent parent widget
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param width width of widget
+     * @param height height of widget
+     */
+    protected TWidget(final TWidget parent, final int x, final int y,
+        final int width, final int height) {
+
+        this(parent, true, x, y, width, height);
+    }
+
     /**
      * Protected constructor used by subclasses that are disabled by default.
      *
@@ -561,6 +576,31 @@ public abstract class TWidget implements Comparable<TWidget> {
         parent.addChild(this);
     }
 
+    /**
+     * Protected constructor used by subclasses that are disabled by default.
+     *
+     * @param parent parent widget
+     * @param enabled if true assume enabled
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param width width of widget
+     * @param height height of widget
+     */
+    protected TWidget(final TWidget parent, final boolean enabled,
+        final int x, final int y, final int width, final int height) {
+
+        this.enabled = enabled;
+        this.parent = parent;
+        this.window = parent.window;
+        children = new LinkedList<TWidget>();
+        parent.addChild(this);
+
+        this.x = x;
+        this.y = y;
+        this.width = width;
+        this.height = height;
+    }
+
     /**
      * Add a child widget to my list of children.  We set its tabOrder to 0
      * and increment the tabOrder of all other children.
@@ -723,7 +763,7 @@ public abstract class TWidget implements Comparable<TWidget> {
         if ((children.size() == 0)
             // TODO
             // || (cast(TTreeView)this)
-            // || (cast(TText)this)
+            || (this instanceof TText)
         ) {
 
             // Defaults:
index 2fc4aa5d578288da8cf9daf18cecee088eacdccf..65eaa4835d342e00d1cf7065b44c5eea1b20bcce 100644 (file)
@@ -751,22 +751,30 @@ public class TWindow extends TWidget {
                 }
             }
             if (keypress.equals(kbShiftLeft)) {
-                if (getWidth() > minimumWindowWidth) {
+                if ((getWidth() > minimumWindowWidth)
+                    || (minimumWindowWidth <= 0)
+                ) {
                     setWidth(getWidth() - 1);
                 }
             }
             if (keypress.equals(kbShiftRight)) {
-                if (getWidth() < maximumWindowWidth) {
+                if ((getWidth() < maximumWindowWidth)
+                    || (maximumWindowWidth <= 0)
+                ) {
                     setWidth(getWidth() + 1);
                 }
             }
             if (keypress.equals(kbShiftUp)) {
-                if (getHeight() > minimumWindowHeight) {
+                if ((getHeight() > minimumWindowHeight)
+                    || (minimumWindowHeight <= 0)
+                ) {
                     setHeight(getHeight() - 1);
                 }
             }
             if (keypress.equals(kbShiftDown)) {
-                if (getHeight() < maximumWindowHeight) {
+                if ((getHeight() < maximumWindowHeight)
+                    || (maximumWindowHeight <= 0)
+                ) {
                     setHeight(getHeight() + 1);
                 }
             }
index 99c0200325354308dfb2f78c72f73313990a3c6a..da9c9c35d8f13794cd820311eedcdd1ffc0fc99a 100644 (file)
@@ -103,20 +103,13 @@ public final class ECMA48Backend extends Backend {
             synchronized (terminal) {
                 try {
                     terminal.wait(timeout);
-                    if (terminal.hasEvents()) {
-                        // System.err.println("getEvents()");
-                        terminal.getEvents(queue);
-                    } else {
-                        // If I got here, then I timed out.  Call
-                        // terminal.getIdleEvents() to pick up stragglers
-                        // like bare resize.
-                        // System.err.println("getIdleEvents()");
-                        terminal.getIdleEvents(queue);
-                    }
                 } catch (InterruptedException e) {
                     // Spurious interrupt, pretend it was like a timeout.
                     // System.err.println("[interrupt] getEvents()");
-                    terminal.getIdleEvents(queue);
+                }
+                if (terminal.hasEvents()) {
+                    // System.err.println("getEvents()");
+                    terminal.getEvents(queue);
                 }
             }
         } else {
index f6bc0b3a076c03f358c4c811f0c9a3c8450e46c6..d466646d5d129b312a8d93aa85c61812502738ec 100644 (file)
@@ -127,6 +127,12 @@ public final class ECMA48Terminal implements Runnable {
      */
     private long escapeTime;
 
+    /**
+     * The time we last checked the window size.  We try not to spawn stty
+     * more than once per second.
+     */
+    private long windowSizeTime;
+
     /**
      * true if mouse1 was down.  Used to report mouse1 on the release event.
      */
@@ -647,30 +653,34 @@ public final class ECMA48Terminal implements Runnable {
      *
      * @param queue list to append new events to
      */
-    public void getIdleEvents(final List<TInputEvent> queue) {
+    private void getIdleEvents(final List<TInputEvent> queue) {
+        Date now = new Date();
 
         // Check for new window size
-        sessionInfo.queryWindowSize();
-        int newWidth = sessionInfo.getWindowWidth();
-        int newHeight = sessionInfo.getWindowHeight();
-        if ((newWidth != windowResize.getWidth())
-            || (newHeight != windowResize.getHeight())
-        ) {
-            TResizeEvent event = new TResizeEvent(TResizeEvent.Type.SCREEN,
-                newWidth, newHeight);
-            windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN,
-                newWidth, newHeight);
-            synchronized (eventQueue) {
-                eventQueue.add(event);
+        long windowSizeDelay = now.getTime() - windowSizeTime;
+        if (windowSizeDelay > 1000) {
+            sessionInfo.queryWindowSize();
+            int newWidth = sessionInfo.getWindowWidth();
+            int newHeight = sessionInfo.getWindowHeight();
+            if ((newWidth != windowResize.getWidth())
+                || (newHeight != windowResize.getHeight())
+            ) {
+                TResizeEvent event = new TResizeEvent(TResizeEvent.Type.SCREEN,
+                    newWidth, newHeight);
+                windowResize = new TResizeEvent(TResizeEvent.Type.SCREEN,
+                    newWidth, newHeight);
+                queue.add(event);
             }
+            windowSizeTime = now.getTime();
         }
 
-        synchronized (eventQueue) {
-            if (eventQueue.size() > 0) {
-                synchronized (queue) {
-                    queue.addAll(eventQueue);
-                }
-                eventQueue.clear();
+        // ESCDELAY type timeout
+        if (state == ParseState.ESCAPE) {
+            long escDelay = now.getTime() - escapeTime;
+            if (escDelay > 100) {
+                // After 0.1 seconds, assume a true escape character
+                queue.add(controlChar((char)0x1B, false));
+                reset();
             }
         }
     }
@@ -796,7 +806,7 @@ public final class ECMA48Terminal implements Runnable {
             // Parameter separator
             if (ch == ';') {
                 paramI++;
-                params.set(paramI, "");
+                params.add("");
                 return;
             }
 
@@ -904,7 +914,7 @@ public final class ECMA48Terminal implements Runnable {
             // Parameter separator
             if (ch == ';') {
                 paramI++;
-                params.set(paramI, "");
+                params.add(paramI, "");
                 return;
             }
 
@@ -1449,6 +1459,14 @@ public final class ECMA48Terminal implements Runnable {
                         }
                     }
                 } else {
+                    getIdleEvents(events);
+                    if (events.size() > 0) {
+                        synchronized (eventQueue) {
+                            eventQueue.addAll(events);
+                        }
+                        events.clear();
+                    }
+
                     // Wait 10 millis for more data
                     Thread.sleep(10);
                 }