double-click support
[fanfix.git] / src / jexer / TWidget.java
index d292ec1490c938f304e142a565551d8eb6060e74..8833f8ff7d0668fb232222ef6fa032a232976be8 100644 (file)
@@ -302,6 +302,25 @@ public abstract class TWidget implements Comparable<TWidget> {
      * @return if true, this widget has a visible cursor
      */
     public final boolean isCursorVisible() {
+        // If cursor is out of my bounds, it is not visible.
+        if ((cursorX >= width)
+            || (cursorX < 0)
+            || (cursorY >= height)
+            || (cursorY < 0)
+        ) {
+            return false;
+        }
+
+        // If cursor is out of my window's bounds, it is not visible.
+        if ((getCursorAbsoluteX() >= window.getAbsoluteX()
+                + window.getWidth() - 1)
+            || (getCursorAbsoluteX() < 0)
+            || (getCursorAbsoluteY() >= window.getAbsoluteY()
+                + window.getHeight() - 1)
+            || (getCursorAbsoluteY() < 0)
+        ) {
+            return false;
+        }
         return cursorVisible;
     }
 
@@ -603,7 +622,12 @@ public abstract class TWidget implements Comparable<TWidget> {
         this.parent = parent;
         this.window = parent.window;
         children = new ArrayList<TWidget>();
-        parent.addChild(this);
+
+        // Do not add TStatusBars, they are drawn by TApplication
+        if (this instanceof TStatusBar) {
+        } else {
+            parent.addChild(this);
+        }
     }
 
     /**
@@ -623,7 +647,12 @@ public abstract class TWidget implements Comparable<TWidget> {
         this.parent = parent;
         this.window = parent.window;
         children = new ArrayList<TWidget>();
-        parent.addChild(this);
+
+        // Do not add TStatusBars, they are drawn by TApplication
+        if (this instanceof TStatusBar) {
+        } else {
+            parent.addChild(this);
+        }
 
         this.x = x;
         this.y = y;
@@ -951,6 +980,29 @@ public abstract class TWidget implements Comparable<TWidget> {
         }
     }
 
+    /**
+     * Method that subclasses can override to handle mouse button
+     * double-clicks.
+     *
+     * @param mouse mouse button event
+     */
+    public void onMouseDoubleClick(final TMouseEvent mouse) {
+        // Default: do nothing, pass to children instead
+        for (int i = children.size() - 1 ; i >= 0 ; i--) {
+            TWidget widget = children.get(i);
+            if (widget.mouseWouldHit(mouse)) {
+                // Dispatch to this child, also activate it
+                activate(widget);
+
+                // Set x and y relative to the child's coordinates
+                mouse.setX(mouse.getAbsoluteX() - widget.getAbsoluteX());
+                mouse.setY(mouse.getAbsoluteY() - widget.getAbsoluteY());
+                widget.handleEvent(mouse);
+                return;
+            }
+        }
+    }
+
     /**
      * Method that subclasses can override to handle window/screen resize
      * events.
@@ -1043,6 +1095,10 @@ public abstract class TWidget implements Comparable<TWidget> {
                 onMouseMotion(mouse);
                 break;
 
+            case MOUSE_DOUBLE_CLICK:
+                onMouseDoubleClick(mouse);
+                break;
+
             default:
                 throw new IllegalArgumentException("Invalid mouse event type: "
                     + mouse.getType());