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 * TVScroller implements a simple vertical scroll bar.
38 public final class TVScroller
extends TWidget
{
41 * Value that corresponds to being on the top edge of the scroll bar.
43 private int topValue
= 0;
46 * Get the value that corresponds to being on the top edge of the scroll
49 * @return the scroll value
51 public int getTopValue() {
56 * Set the value that corresponds to being on the top edge of the scroll
59 * @param topValue the new scroll value
61 public void setTopValue(final int topValue
) {
62 this.topValue
= topValue
;
66 * Value that corresponds to being on the bottom edge of the scroll bar.
68 private int bottomValue
= 100;
71 * Get the value that corresponds to being on the bottom edge of the
74 * @return the scroll value
76 public int getBottomValue() {
81 * Set the value that corresponds to being on the bottom edge of the
84 * @param bottomValue the new scroll value
86 public void setBottomValue(final int bottomValue
) {
87 this.bottomValue
= bottomValue
;
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 * Get the increment for clicking on an arrow.
121 * @return the increment value
123 public int getSmallChange() {
128 * Set the increment for clicking on an arrow.
130 * @param smallChange the new increment value
132 public void setSmallChange(final int smallChange
) {
133 this.smallChange
= smallChange
;
137 * The increment for clicking in the bar between the box and an arrow.
139 private int bigChange
= 20;
142 * Set the increment for clicking in the bar between the box and an
145 * @return the increment value
147 public int getBigChange() {
152 * Set the increment for clicking in the bar between the box and an
155 * @param bigChange the new increment value
157 public void setBigChange(final int bigChange
) {
158 this.bigChange
= bigChange
;
162 * When true, the user is dragging the scroll box.
164 private boolean inScroll
= false;
167 * Public constructor.
169 * @param parent parent widget
170 * @param x column relative to parent
171 * @param y row relative to parent
172 * @param height height of scroll bar
174 public TVScroller(final TWidget parent
, final int x
, final int y
,
177 // Set parent and window
178 super(parent
, x
, y
, 1, height
);
182 * Compute the position of the scroll box (a.k.a. grip, thumb).
184 * @return Y position of the box, between 1 and height - 2
186 private int boxPosition() {
187 return (getHeight() - 3) * (value
- topValue
) / (bottomValue
- topValue
) + 1;
191 * Draw a vertical scroll bar.
195 CellAttributes arrowColor
= getTheme().getColor("tscroller.arrows");
196 CellAttributes barColor
= getTheme().getColor("tscroller.bar");
197 getScreen().putCharXY(0, 0, GraphicsChars
.CP437
[0x1E], arrowColor
);
198 getScreen().putCharXY(0, getHeight() - 1, GraphicsChars
.CP437
[0x1F],
202 if (bottomValue
> topValue
) {
203 getScreen().vLineXY(0, 1, getHeight() - 2,
204 GraphicsChars
.CP437
[0xB1], barColor
);
205 getScreen().putCharXY(0, boxPosition(), GraphicsChars
.BOX
,
208 getScreen().vLineXY(0, 1, getHeight() - 2, GraphicsChars
.HATCH
,
215 * Perform a small step change up.
217 public void decrement() {
218 if (bottomValue
== topValue
) {
221 value
-= smallChange
;
222 if (value
< topValue
) {
228 * Perform a small step change down.
230 public void increment() {
231 if (bottomValue
== topValue
) {
234 value
+= smallChange
;
235 if (value
> bottomValue
) {
241 * Perform a big step change up.
243 public void bigDecrement() {
244 if (bottomValue
== topValue
) {
248 if (value
< topValue
) {
254 * Perform a big step change down.
256 public void bigIncrement() {
257 if (bottomValue
== topValue
) {
261 if (value
> bottomValue
) {
267 * Go to the top edge of the scroller.
269 public void toTop() {
274 * Go to the bottom edge of the scroller.
276 public void toBottom() {
281 * Handle mouse button releases.
283 * @param mouse mouse button release event
286 public void onMouseUp(final TMouseEvent mouse
) {
287 if (bottomValue
== topValue
) {
296 if ((mouse
.getX() == 0)
297 && (mouse
.getY() == 0)
299 // Clicked on the top arrow
304 if ((mouse
.getX() == 0)
305 && (mouse
.getY() == getHeight() - 1)
307 // Clicked on the bottom arrow
312 if ((mouse
.getX() == 0)
313 && (mouse
.getY() > 0)
314 && (mouse
.getY() < boxPosition())
316 // Clicked between the top arrow and the box
318 if (value
< topValue
) {
324 if ((mouse
.getX() == 0)
325 && (mouse
.getY() > boxPosition())
326 && (mouse
.getY() < getHeight() - 1)
328 // Clicked between the box and the bottom arrow
330 if (value
> bottomValue
) {
338 * Handle mouse movement events.
340 * @param mouse mouse motion event
343 public void onMouseMotion(final TMouseEvent mouse
) {
344 if (bottomValue
== topValue
) {
348 if ((mouse
.isMouse1())
350 && (mouse
.getY() > 0)
351 && (mouse
.getY() < getHeight() - 1)
353 // Recompute value based on new box position
354 value
= (bottomValue
- topValue
)
355 * (mouse
.getY()) / (getHeight() - 3) + topValue
;
363 * Handle mouse press events.
365 * @param mouse mouse button press event
368 public void onMouseDown(final TMouseEvent mouse
) {
369 if (bottomValue
== topValue
) {
373 if ((mouse
.getX() == 0)
374 && (mouse
.getY() == boxPosition())