2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2017 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.GraphicsChars
;
33 import jexer
.event
.TMouseEvent
;
36 * THScroller implements a simple horizontal scroll bar.
38 public class THScroller
extends TWidget
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Value that corresponds to being on the left edge of the scroll bar.
47 private int leftValue
= 0;
50 * Value that corresponds to being on the right edge of the scroll bar.
52 private int rightValue
= 100;
55 * Current value of the scroll.
57 private int value
= 0;
60 * The increment for clicking on an arrow.
62 private int smallChange
= 1;
65 * The increment for clicking in the bar between the box and an arrow.
67 private int bigChange
= 20;
70 * When true, the user is dragging the scroll box.
72 private boolean inScroll
= false;
74 // ------------------------------------------------------------------------
75 // Constructors -----------------------------------------------------------
76 // ------------------------------------------------------------------------
81 * @param parent parent widget
82 * @param x column relative to parent
83 * @param y row relative to parent
84 * @param width height of scroll bar
86 public THScroller(final TWidget parent
, final int x
, final int y
,
89 // Set parent and window
90 super(parent
, x
, y
, width
, 1);
93 // ------------------------------------------------------------------------
94 // Event handlers ---------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * Handle mouse button releases.
100 * @param mouse mouse button release event
103 public void onMouseUp(final TMouseEvent mouse
) {
110 if (rightValue
== leftValue
) {
114 if ((mouse
.getX() == 0)
115 && (mouse
.getY() == 0)
117 // Clicked on the left arrow
122 if ((mouse
.getY() == 0)
123 && (mouse
.getX() == getWidth() - 1)
125 // Clicked on the right arrow
130 if ((mouse
.getY() == 0)
131 && (mouse
.getX() > 0)
132 && (mouse
.getX() < boxPosition())
134 // Clicked between the left arrow and the box
136 if (value
< leftValue
) {
142 if ((mouse
.getY() == 0)
143 && (mouse
.getX() > boxPosition())
144 && (mouse
.getX() < getWidth() - 1)
146 // Clicked between the box and the right arrow
148 if (value
> rightValue
) {
156 * Handle mouse movement events.
158 * @param mouse mouse motion event
161 public void onMouseMotion(final TMouseEvent mouse
) {
163 if (rightValue
== leftValue
) {
168 if ((mouse
.isMouse1())
170 && (mouse
.getX() > 0)
171 && (mouse
.getX() < getWidth() - 1)
173 // Recompute value based on new box position
174 value
= (rightValue
- leftValue
)
175 * (mouse
.getX()) / (getWidth() - 3) + leftValue
;
176 if (value
> rightValue
) {
179 if (value
< leftValue
) {
188 * Handle mouse button press events.
190 * @param mouse mouse button press event
193 public void onMouseDown(final TMouseEvent mouse
) {
194 if (rightValue
== leftValue
) {
199 if ((mouse
.getY() == 0)
200 && (mouse
.getX() == boxPosition())
208 // ------------------------------------------------------------------------
209 // TWidget ----------------------------------------------------------------
210 // ------------------------------------------------------------------------
213 * Draw a horizontal scroll bar.
217 CellAttributes arrowColor
= getTheme().getColor("tscroller.arrows");
218 CellAttributes barColor
= getTheme().getColor("tscroller.bar");
219 getScreen().putCharXY(0, 0, GraphicsChars
.CP437
[0x11], arrowColor
);
220 getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars
.CP437
[0x10],
224 if (rightValue
> leftValue
) {
225 getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars
.CP437
[0xB1],
227 getScreen().putCharXY(boxPosition(), 0, GraphicsChars
.BOX
,
230 getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars
.HATCH
,
236 // ------------------------------------------------------------------------
237 // THScroller -------------------------------------------------------------
238 // ------------------------------------------------------------------------
241 * Get the value that corresponds to being on the left edge of the scroll
244 * @return the scroll value
246 public int getLeftValue() {
251 * Set the value that corresponds to being on the left edge of the
254 * @param leftValue the new scroll value
256 public void setLeftValue(final int leftValue
) {
257 this.leftValue
= leftValue
;
261 * Get the value that corresponds to being on the right edge of the
264 * @return the scroll value
266 public int getRightValue() {
271 * Set the value that corresponds to being on the right edge of the
274 * @param rightValue the new scroll value
276 public void setRightValue(final int rightValue
) {
277 this.rightValue
= rightValue
;
281 * Get current value of the scroll.
283 * @return the scroll value
285 public int getValue() {
290 * Set current value of the scroll.
292 * @param value the new scroll value
294 public void setValue(final int value
) {
299 * Get the increment for clicking on an arrow.
301 * @return the increment value
303 public int getSmallChange() {
308 * Set the increment for clicking on an arrow.
310 * @param smallChange the new increment value
312 public void setSmallChange(final int smallChange
) {
313 this.smallChange
= smallChange
;
317 * Set the increment for clicking in the bar between the box and an
320 * @return the increment value
322 public int getBigChange() {
327 * Set the increment for clicking in the bar between the box and an
330 * @param bigChange the new increment value
332 public void setBigChange(final int bigChange
) {
333 this.bigChange
= bigChange
;
337 * Compute the position of the scroll box (a.k.a. grip, thumb).
339 * @return Y position of the box, between 1 and width - 2
341 private int boxPosition() {
342 return (getWidth() - 3) * (value
- leftValue
) / (rightValue
- leftValue
) + 1;
346 * Perform a small step change left.
348 public void decrement() {
349 if (leftValue
== rightValue
) {
352 value
-= smallChange
;
353 if (value
< leftValue
) {
359 * Perform a small step change right.
361 public void increment() {
362 if (leftValue
== rightValue
) {
365 value
+= smallChange
;
366 if (value
> rightValue
) {
372 * Perform a big step change left.
374 public void bigDecrement() {
375 if (leftValue
== rightValue
) {
379 if (value
< leftValue
) {
385 * Perform a big step change right.
387 public void bigIncrement() {
388 if (rightValue
== leftValue
) {
392 if (value
> rightValue
) {
398 * Go to the left edge of the scroller.
400 public void toLeft() {
405 * Go to the right edge of the scroller.
407 public void toRight() {