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 final class THScroller
extends TWidget
{
41 * Value that corresponds to being on the left edge of the scroll bar.
43 private int leftValue
= 0;
46 * Get the value that corresponds to being on the left edge of the scroll
49 * @return the scroll value
51 public int getLeftValue() {
56 * Set the value that corresponds to being on the left edge of the
59 * @param leftValue the new scroll value
61 public void setLeftValue(final int leftValue
) {
62 this.leftValue
= leftValue
;
66 * Value that corresponds to being on the right edge of the scroll bar.
68 private int rightValue
= 100;
71 * Get the value that corresponds to being on the right edge of the
74 * @return the scroll value
76 public int getRightValue() {
81 * Set the value that corresponds to being on the right edge of the
84 * @param rightValue the new scroll value
86 public void setRightValue(final int rightValue
) {
87 this.rightValue
= rightValue
;
91 * Current value of the scroll.
93 private int value
= 0;
96 * Get current value of the scroll.
98 * @return the scroll value
100 public int getValue() {
105 * Set current value of the scroll.
107 * @param value the new scroll value
109 public void setValue(final int value
) {
114 * The increment for clicking on an arrow.
116 private int smallChange
= 1;
119 * Set the increment for clicking on an arrow.
121 * @param smallChange the new increment value
123 public void setSmallChange(final int smallChange
) {
124 this.smallChange
= smallChange
;
128 * The increment for clicking in the bar between the box and an arrow.
130 private int bigChange
= 20;
133 * Set the increment for clicking in the bar between the box and an
136 * @param bigChange the new increment value
138 public void setBigChange(final int bigChange
) {
139 this.bigChange
= bigChange
;
143 * When true, the user is dragging the scroll box.
145 private boolean inScroll
= false;
148 * Public constructor.
150 * @param parent parent widget
151 * @param x column relative to parent
152 * @param y row relative to parent
153 * @param width height of scroll bar
155 public THScroller(final TWidget parent
, final int x
, final int y
,
158 // Set parent and window
159 super(parent
, x
, y
, width
, 1);
163 * Compute the position of the scroll box (a.k.a. grip, thumb).
165 * @return Y position of the box, between 1 and width - 2
167 private int boxPosition() {
168 return (getWidth() - 3) * (value
- leftValue
) / (rightValue
- leftValue
) + 1;
172 * Draw a horizontal scroll bar.
176 CellAttributes arrowColor
= getTheme().getColor("tscroller.arrows");
177 CellAttributes barColor
= getTheme().getColor("tscroller.bar");
178 getScreen().putCharXY(0, 0, GraphicsChars
.CP437
[0x11], arrowColor
);
179 getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars
.CP437
[0x10],
183 if (rightValue
> leftValue
) {
184 getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars
.CP437
[0xB1],
186 getScreen().putCharXY(boxPosition(), 0, GraphicsChars
.BOX
,
189 getScreen().hLineXY(1, 0, getWidth() - 2, GraphicsChars
.HATCH
,
196 * Perform a small step change left.
198 public void decrement() {
199 if (leftValue
== rightValue
) {
202 value
-= smallChange
;
203 if (value
< leftValue
) {
209 * Perform a small step change right.
211 public void increment() {
212 if (leftValue
== rightValue
) {
215 value
+= smallChange
;
216 if (value
> rightValue
) {
222 * Handle mouse button releases.
224 * @param mouse mouse button release event
227 public void onMouseUp(final TMouseEvent mouse
) {
234 if (rightValue
== leftValue
) {
238 if ((mouse
.getX() == 0)
239 && (mouse
.getY() == 0)
241 // Clicked on the left arrow
246 if ((mouse
.getY() == 0)
247 && (mouse
.getX() == getWidth() - 1)
249 // Clicked on the right arrow
254 if ((mouse
.getY() == 0)
255 && (mouse
.getX() > 0)
256 && (mouse
.getX() < boxPosition())
258 // Clicked between the left arrow and the box
260 if (value
< leftValue
) {
266 if ((mouse
.getY() == 0)
267 && (mouse
.getX() > boxPosition())
268 && (mouse
.getX() < getWidth() - 1)
270 // Clicked between the box and the right arrow
272 if (value
> rightValue
) {
280 * Handle mouse movement events.
282 * @param mouse mouse motion event
285 public void onMouseMotion(final TMouseEvent mouse
) {
287 if (rightValue
== leftValue
) {
292 if ((mouse
.isMouse1())
294 && (mouse
.getX() > 0)
295 && (mouse
.getX() < getWidth() - 1)
297 // Recompute value based on new box position
298 value
= (rightValue
- leftValue
)
299 * (mouse
.getX()) / (getWidth() - 3) + leftValue
;
306 * Handle mouse button press events.
308 * @param mouse mouse button press event
311 public void onMouseDown(final TMouseEvent mouse
) {
312 if (rightValue
== leftValue
) {
317 if ((mouse
.getY() == 0)
318 && (mouse
.getX() == boxPosition())