Merge branch 'subtree'
[fanfix.git] / src / jexer / THScroller.java
index eb7256bcab8e8f181185e9e50c03453db9f9b6db..a07bcd7a52b7636949e57948892d395cba41bcf9 100644 (file)
@@ -1,29 +1,27 @@
 /*
  * Jexer - Java Text User Interface
  *
- * License: LGPLv3 or later
+ * The MIT License (MIT)
  *
- * This module is licensed under the GNU Lesser General Public License
- * Version 3.  Please see the file "COPYING" in this directory for more
- * information about the GNU Lesser General Public License Version 3.
+ * Copyright (C) 2019 Kevin Lamonte
  *
- *     Copyright (C) 2015  Kevin Lamonte
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 3 of
- * the License, or (at your option) any later version.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, see
- * http://www.gnu.org/licenses/, or write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
  * @author Kevin Lamonte [kevin.lamonte@gmail.com]
  * @version 1
@@ -37,115 +35,46 @@ import jexer.event.TMouseEvent;
 /**
  * THScroller implements a simple horizontal scroll bar.
  */
-public final class THScroller extends TWidget {
+public class THScroller extends TWidget {
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Value that corresponds to being on the left edge of the scroll bar.
      */
     private int leftValue = 0;
 
-    /**
-     * Get the value that corresponds to being on the left edge of the scroll
-     * bar.
-     *
-     * @return the scroll value
-     */
-    public int getLeftValue() {
-        return leftValue;
-    }
-
-    /**
-     * Set the value that corresponds to being on the left edge of the
-     * scroll bar.
-     *
-     * @param leftValue the new scroll value
-     */
-    public void setLeftValue(final int leftValue) {
-        this.leftValue = leftValue;
-    }
-
     /**
      * Value that corresponds to being on the right edge of the scroll bar.
      */
     private int rightValue = 100;
 
-    /**
-     * Get the value that corresponds to being on the right edge of the
-     * scroll bar.
-     *
-     * @return the scroll value
-     */
-    public int getRightValue() {
-        return rightValue;
-    }
-
-    /**
-     * Set the value that corresponds to being on the right edge of the
-     * scroll bar.
-     *
-     * @param rightValue the new scroll value
-     */
-    public void setRightValue(final int rightValue) {
-        this.rightValue = rightValue;
-    }
-
     /**
      * Current value of the scroll.
      */
     private int value = 0;
 
-    /**
-     * Get current value of the scroll.
-     *
-     * @return the scroll value
-     */
-    public int getValue() {
-        return value;
-    }
-
-    /**
-     * Set current value of the scroll.
-     *
-     * @param value the new scroll value
-     */
-    public void setValue(final int value) {
-        this.value = value;
-    }
-
     /**
      * The increment for clicking on an arrow.
      */
     private int smallChange = 1;
 
-    /**
-     * Set the increment for clicking on an arrow.
-     *
-     * @param smallChange the new increment value
-     */
-    public void setSmallChange(final int smallChange) {
-        this.smallChange = smallChange;
-    }
-
     /**
      * The increment for clicking in the bar between the box and an arrow.
      */
     private int bigChange = 20;
 
-    /**
-     * Set the increment for clicking in the bar between the box and an
-     * arrow.
-     *
-     * @param bigChange the new increment value
-     */
-    public void setBigChange(final int bigChange) {
-        this.bigChange = bigChange;
-    }
-
     /**
      * When true, the user is dragging the scroll box.
      */
     private boolean inScroll = false;
 
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Public constructor.
      *
@@ -161,64 +90,9 @@ public final class THScroller extends TWidget {
         super(parent, x, y, width, 1);
     }
 
-    /**
-     * Compute the position of the scroll box (a.k.a. grip, thumb).
-     *
-     * @return Y position of the box, between 1 and width - 2
-     */
-    private int boxPosition() {
-        return (getWidth() - 3) * (value - leftValue) / (rightValue - leftValue) + 1;
-    }
-
-    /**
-     * Draw a horizontal scroll bar.
-     */
-    @Override
-    public void draw() {
-        CellAttributes arrowColor = getTheme().getColor("tscroller.arrows");
-        CellAttributes barColor = getTheme().getColor("tscroller.bar");
-        getScreen().putCharXY(0, 0, GraphicsChars.CP437[0x11], arrowColor);
-        getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0x10],
-            arrowColor);
-
-        // Place the box
-        if (rightValue > leftValue) {
-            getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars.CP437[0xB1],
-                barColor);
-            getScreen().putCharXY(boxPosition(), 0, GraphicsChars.BOX,
-                arrowColor);
-        } else {
-            getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars.HATCH,
-                barColor);
-        }
-
-    }
-
-    /**
-     * Perform a small step change left.
-     */
-    public void decrement() {
-        if (leftValue == rightValue) {
-            return;
-        }
-        value -= smallChange;
-        if (value < leftValue) {
-            value = leftValue;
-        }
-    }
-
-    /**
-     * Perform a small step change right.
-     */
-    public void increment() {
-        if (leftValue == rightValue) {
-            return;
-        }
-        value += smallChange;
-        if (value > rightValue) {
-            value = rightValue;
-        }
-    }
+    // ------------------------------------------------------------------------
+    // Event handlers ---------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Handle mouse button releases.
@@ -297,7 +171,14 @@ public final class THScroller extends TWidget {
             && (mouse.getX() < getWidth() - 1)
         ) {
             // Recompute value based on new box position
-            value = (rightValue - leftValue) * (mouse.getX()) / (getWidth() - 3) + leftValue;
+            value = (rightValue - leftValue)
+                * (mouse.getX()) / (getWidth() - 3) + leftValue;
+            if (value > rightValue) {
+                value = rightValue;
+            }
+            if (value < leftValue) {
+                value = leftValue;
+            }
             return;
         }
         inScroll = false;
@@ -324,4 +205,203 @@ public final class THScroller extends TWidget {
 
     }
 
+    // ------------------------------------------------------------------------
+    // TWidget ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Draw a horizontal scroll bar.
+     */
+    @Override
+    public void draw() {
+        CellAttributes arrowColor = getTheme().getColor("tscroller.arrows");
+        CellAttributes barColor = getTheme().getColor("tscroller.bar");
+        putCharXY(0, 0, GraphicsChars.CP437[0x11], arrowColor);
+        putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0x10], arrowColor);
+
+        // Place the box
+        if (rightValue > leftValue) {
+            hLineXY(1, 0, getWidth() - 2, GraphicsChars.CP437[0xB1], barColor);
+            putCharXY(boxPosition(), 0, GraphicsChars.BOX, arrowColor);
+        } else {
+            hLineXY(1, 0, getWidth() - 2, GraphicsChars.HATCH, barColor);
+        }
+
+    }
+
+    // ------------------------------------------------------------------------
+    // THScroller -------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Get the value that corresponds to being on the left edge of the scroll
+     * bar.
+     *
+     * @return the scroll value
+     */
+    public int getLeftValue() {
+        return leftValue;
+    }
+
+    /**
+     * Set the value that corresponds to being on the left edge of the
+     * scroll bar.
+     *
+     * @param leftValue the new scroll value
+     */
+    public void setLeftValue(final int leftValue) {
+        this.leftValue = leftValue;
+    }
+
+    /**
+     * Get the value that corresponds to being on the right edge of the
+     * scroll bar.
+     *
+     * @return the scroll value
+     */
+    public int getRightValue() {
+        return rightValue;
+    }
+
+    /**
+     * Set the value that corresponds to being on the right edge of the
+     * scroll bar.
+     *
+     * @param rightValue the new scroll value
+     */
+    public void setRightValue(final int rightValue) {
+        this.rightValue = rightValue;
+    }
+
+    /**
+     * Get current value of the scroll.
+     *
+     * @return the scroll value
+     */
+    public int getValue() {
+        return value;
+    }
+
+    /**
+     * Set current value of the scroll.
+     *
+     * @param value the new scroll value
+     */
+    public void setValue(final int value) {
+        this.value = value;
+    }
+
+    /**
+     * Get the increment for clicking on an arrow.
+     *
+     * @return the increment value
+     */
+    public int getSmallChange() {
+        return smallChange;
+    }
+
+    /**
+     * Set the increment for clicking on an arrow.
+     *
+     * @param smallChange the new increment value
+     */
+    public void setSmallChange(final int smallChange) {
+        this.smallChange = smallChange;
+    }
+
+    /**
+     * Set the increment for clicking in the bar between the box and an
+     * arrow.
+     *
+     * @return the increment value
+     */
+    public int getBigChange() {
+        return bigChange;
+    }
+
+    /**
+     * Set the increment for clicking in the bar between the box and an
+     * arrow.
+     *
+     * @param bigChange the new increment value
+     */
+    public void setBigChange(final int bigChange) {
+        this.bigChange = bigChange;
+    }
+
+    /**
+     * Compute the position of the scroll box (a.k.a. grip, thumb).
+     *
+     * @return Y position of the box, between 1 and width - 2
+     */
+    private int boxPosition() {
+        return (getWidth() - 3) * (value - leftValue) / (rightValue - leftValue) + 1;
+    }
+
+    /**
+     * Perform a small step change left.
+     */
+    public void decrement() {
+        if (leftValue == rightValue) {
+            return;
+        }
+        value -= smallChange;
+        if (value < leftValue) {
+            value = leftValue;
+        }
+    }
+
+    /**
+     * Perform a small step change right.
+     */
+    public void increment() {
+        if (leftValue == rightValue) {
+            return;
+        }
+        value += smallChange;
+        if (value > rightValue) {
+            value = rightValue;
+        }
+    }
+
+    /**
+     * Perform a big step change left.
+     */
+    public void bigDecrement() {
+        if (leftValue == rightValue) {
+            return;
+        }
+        value -= bigChange;
+        if (value < leftValue) {
+            value = leftValue;
+        }
+    }
+
+    /**
+     * Perform a big step change right.
+     */
+    public void bigIncrement() {
+        if (rightValue == leftValue) {
+            return;
+        }
+        value += bigChange;
+        if (value > rightValue) {
+            value = rightValue;
+        }
+    }
+
+    /**
+     * Go to the left edge of the scroller.
+     */
+    public void toLeft() {
+        value = leftValue;
+    }
+
+    /**
+     * Go to the right edge of the scroller.
+     */
+    public void toRight() {
+        value = rightValue;
+    }
+
 }