Merge branch 'subtree'
[fanfix.git] / src / jexer / TVScroller.java
index 32e173ab6b5ab080c32b375b99d52d9f9465e4e9..444e058542c8d566be5395f2f51ee1e88ff57a28 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -35,13 +35,199 @@ import jexer.event.TMouseEvent;
 /**
  * TVScroller implements a simple vertical scroll bar.
  */
-public final class TVScroller extends TWidget {
+public class TVScroller extends TWidget {
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Value that corresponds to being on the top edge of the scroll bar.
      */
     private int topValue = 0;
 
+    /**
+     * Value that corresponds to being on the bottom edge of the scroll bar.
+     */
+    private int bottomValue = 100;
+
+    /**
+     * Current value of the scroll.
+     */
+    private int value = 0;
+
+    /**
+     * The increment for clicking on an arrow.
+     */
+    private int smallChange = 1;
+
+    /**
+     * The increment for clicking in the bar between the box and an arrow.
+     */
+    private int bigChange = 20;
+
+    /**
+     * When true, the user is dragging the scroll box.
+     */
+    private boolean inScroll = false;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Public constructor.
+     *
+     * @param parent parent widget
+     * @param x column relative to parent
+     * @param y row relative to parent
+     * @param height height of scroll bar
+     */
+    public TVScroller(final TWidget parent, final int x, final int y,
+        final int height) {
+
+        // Set parent and window
+        super(parent, x, y, 1, height);
+    }
+
+    // ------------------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Handle mouse button releases.
+     *
+     * @param mouse mouse button release event
+     */
+    @Override
+    public void onMouseUp(final TMouseEvent mouse) {
+        if (bottomValue == topValue) {
+            return;
+        }
+
+        if (inScroll) {
+            inScroll = false;
+            return;
+        }
+
+        if ((mouse.getX() == 0)
+            && (mouse.getY() == 0)
+        ) {
+            // Clicked on the top arrow
+            decrement();
+            return;
+        }
+
+        if ((mouse.getX() == 0)
+            && (mouse.getY() == getHeight() - 1)
+        ) {
+            // Clicked on the bottom arrow
+            increment();
+            return;
+        }
+
+        if ((mouse.getX() == 0)
+            && (mouse.getY() > 0)
+            && (mouse.getY() < boxPosition())
+        ) {
+            // Clicked between the top arrow and the box
+            value -= bigChange;
+            if (value < topValue) {
+                value = topValue;
+            }
+            return;
+        }
+
+        if ((mouse.getX() == 0)
+            && (mouse.getY() > boxPosition())
+            && (mouse.getY() < getHeight() - 1)
+        ) {
+            // Clicked between the box and the bottom arrow
+            value += bigChange;
+            if (value > bottomValue) {
+                value = bottomValue;
+            }
+            return;
+        }
+    }
+
+    /**
+     * Handle mouse movement events.
+     *
+     * @param mouse mouse motion event
+     */
+    @Override
+    public void onMouseMotion(final TMouseEvent mouse) {
+        if (bottomValue == topValue) {
+            return;
+        }
+
+        if ((mouse.isMouse1())
+            && (inScroll)
+            && (mouse.getY() > 0)
+            && (mouse.getY() < getHeight() - 1)
+        ) {
+            // Recompute value based on new box position
+            value = (bottomValue - topValue)
+                * (mouse.getY()) / (getHeight() - 3) + topValue;
+            if (value > bottomValue) {
+                value = bottomValue;
+            }
+            if (value < topValue) {
+                value = topValue;
+            }
+            return;
+        }
+
+        inScroll = false;
+    }
+
+    /**
+     * Handle mouse press events.
+     *
+     * @param mouse mouse button press event
+     */
+    @Override
+    public void onMouseDown(final TMouseEvent mouse) {
+        if (bottomValue == topValue) {
+            return;
+        }
+
+        if ((mouse.getX() == 0)
+            && (mouse.getY() == boxPosition())
+        ) {
+            inScroll = true;
+            return;
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // TWidget ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Draw a vertical scroll bar.
+     */
+    @Override
+    public void draw() {
+        CellAttributes arrowColor = getTheme().getColor("tscroller.arrows");
+        CellAttributes barColor = getTheme().getColor("tscroller.bar");
+        putCharXY(0, 0, GraphicsChars.CP437[0x1E], arrowColor);
+        putCharXY(0, getHeight() - 1, GraphicsChars.CP437[0x1F], arrowColor);
+
+        // Place the box
+        if (bottomValue > topValue) {
+            vLineXY(0, 1, getHeight() - 2, GraphicsChars.CP437[0xB1], barColor);
+            putCharXY(0, boxPosition(), GraphicsChars.BOX, arrowColor);
+        } else {
+            vLineXY(0, 1, getHeight() - 2, GraphicsChars.HATCH, barColor);
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // TVScroller -------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Get the value that corresponds to being on the top edge of the scroll
      * bar.
@@ -62,11 +248,6 @@ public final class TVScroller extends TWidget {
         this.topValue = topValue;
     }
 
-    /**
-     * Value that corresponds to being on the bottom edge of the scroll bar.
-     */
-    private int bottomValue = 100;
-
     /**
      * Get the value that corresponds to being on the bottom edge of the
      * scroll bar.
@@ -87,11 +268,6 @@ public final class TVScroller extends TWidget {
         this.bottomValue = bottomValue;
     }
 
-    /**
-     * Current value of the scroll.
-     */
-    private int value = 0;
-
     /**
      * Get current value of the scroll.
      *
@@ -111,9 +287,13 @@ public final class TVScroller extends TWidget {
     }
 
     /**
-     * The increment for clicking on an arrow.
+     * Get the increment for clicking on an arrow.
+     *
+     * @return the increment value
      */
-    private int smallChange = 1;
+    public int getSmallChange() {
+        return smallChange;
+    }
 
     /**
      * Set the increment for clicking on an arrow.
@@ -125,9 +305,14 @@ public final class TVScroller extends TWidget {
     }
 
     /**
-     * The increment for clicking in the bar between the box and an arrow.
+     * Set the increment for clicking in the bar between the box and an
+     * arrow.
+     *
+     * @return the increment value
      */
-    private int bigChange = 20;
+    public int getBigChange() {
+        return bigChange;
+    }
 
     /**
      * Set the increment for clicking in the bar between the box and an
@@ -139,26 +324,6 @@ public final class TVScroller extends TWidget {
         this.bigChange = bigChange;
     }
 
-    /**
-     * When true, the user is dragging the scroll box.
-     */
-    private boolean inScroll = false;
-
-    /**
-     * Public constructor.
-     *
-     * @param parent parent widget
-     * @param x column relative to parent
-     * @param y row relative to parent
-     * @param height height of scroll bar
-     */
-    public TVScroller(final TWidget parent, final int x, final int y,
-        final int height) {
-
-        // Set parent and window
-        super(parent, x, y, 1, height);
-    }
-
     /**
      * Compute the position of the scroll box (a.k.a. grip, thumb).
      *
@@ -168,30 +333,6 @@ public final class TVScroller extends TWidget {
         return (getHeight() - 3) * (value - topValue) / (bottomValue - topValue) + 1;
     }
 
-    /**
-     * Draw a vertical scroll bar.
-     */
-    @Override
-    public void draw() {
-        CellAttributes arrowColor = getTheme().getColor("tscroller.arrows");
-        CellAttributes barColor = getTheme().getColor("tscroller.bar");
-        getScreen().putCharXY(0, 0, GraphicsChars.CP437[0x1E], arrowColor);
-        getScreen().putCharXY(0, getHeight() - 1, GraphicsChars.CP437[0x1F],
-            arrowColor);
-
-        // Place the box
-        if (bottomValue > topValue) {
-            getScreen().vLineXY(0, 1, getHeight() - 2,
-                GraphicsChars.CP437[0xB1], barColor);
-            getScreen().putCharXY(0, boxPosition(), GraphicsChars.BOX,
-                arrowColor);
-        } else {
-            getScreen().vLineXY(0, 1, getHeight() - 2, GraphicsChars.HATCH,
-                barColor);
-        }
-
-    }
-
     /**
      * Perform a small step change up.
      */
@@ -258,105 +399,4 @@ public final class TVScroller extends TWidget {
         value = bottomValue;
     }
 
-    /**
-     * Handle mouse button releases.
-     *
-     * @param mouse mouse button release event
-     */
-    @Override
-    public void onMouseUp(final TMouseEvent mouse) {
-        if (bottomValue == topValue) {
-            return;
-        }
-
-        if (inScroll) {
-            inScroll = false;
-            return;
-        }
-
-        if ((mouse.getX() == 0)
-            && (mouse.getY() == 0)
-        ) {
-            // Clicked on the top arrow
-            decrement();
-            return;
-        }
-
-        if ((mouse.getX() == 0)
-            && (mouse.getY() == getHeight() - 1)
-        ) {
-            // Clicked on the bottom arrow
-            increment();
-            return;
-        }
-
-        if ((mouse.getX() == 0)
-            && (mouse.getY() > 0)
-            && (mouse.getY() < boxPosition())
-        ) {
-            // Clicked between the top arrow and the box
-            value -= bigChange;
-            if (value < topValue) {
-                value = topValue;
-            }
-            return;
-        }
-
-        if ((mouse.getX() == 0)
-            && (mouse.getY() > boxPosition())
-            && (mouse.getY() < getHeight() - 1)
-        ) {
-            // Clicked between the box and the bottom arrow
-            value += bigChange;
-            if (value > bottomValue) {
-                value = bottomValue;
-            }
-            return;
-        }
-    }
-
-    /**
-     * Handle mouse movement events.
-     *
-     * @param mouse mouse motion event
-     */
-    @Override
-    public void onMouseMotion(final TMouseEvent mouse) {
-        if (bottomValue == topValue) {
-            return;
-        }
-
-        if ((mouse.isMouse1())
-            && (inScroll)
-            && (mouse.getY() > 0)
-            && (mouse.getY() < getHeight() - 1)
-        ) {
-            // Recompute value based on new box position
-            value = (bottomValue - topValue)
-                * (mouse.getY()) / (getHeight() - 3) + topValue;
-            return;
-        }
-
-        inScroll = false;
-    }
-
-    /**
-     * Handle mouse press events.
-     *
-     * @param mouse mouse button press event
-     */
-    @Override
-    public void onMouseDown(final TMouseEvent mouse) {
-        if (bottomValue == topValue) {
-            return;
-        }
-
-        if ((mouse.getX() == 0)
-            && (mouse.getY() == boxPosition())
-        ) {
-            inScroll = true;
-            return;
-        }
-    }
-
 }