2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import jexer
.bits
.CellAttributes
;
34 import jexer
.bits
.GraphicsChars
;
35 import jexer
.event
.TMouseEvent
;
38 * TVScroller implements a simple vertical scroll bar.
40 public final class TVScroller
extends TWidget
{
43 * Value that corresponds to being on the top edge of the scroll bar.
45 private int topValue
= 0;
48 * Get the value that corresponds to being on the top edge of the scroll
51 * @return the scroll value
53 public int getTopValue() {
58 * Set the value that corresponds to being on the top edge of the scroll
61 * @param topValue the new scroll value
63 public void setTopValue(final int topValue
) {
64 this.topValue
= topValue
;
68 * Value that corresponds to being on the bottom edge of the scroll bar.
70 private int bottomValue
= 100;
73 * Get the value that corresponds to being on the bottom edge of the
76 * @return the scroll value
78 public int getBottomValue() {
83 * Set the value that corresponds to being on the bottom edge of the
86 * @param bottomValue the new scroll value
88 public void setBottomValue(final int bottomValue
) {
89 this.bottomValue
= bottomValue
;
93 * Current value of the scroll.
95 private int value
= 0;
98 * Get current value of the scroll.
100 * @return the scroll value
102 public int getValue() {
107 * Set current value of the scroll.
109 * @param value the new scroll value
111 public void setValue(final int value
) {
116 * The increment for clicking on an arrow.
118 private int smallChange
= 1;
121 * Set the increment for clicking on an arrow.
123 * @param smallChange the new increment value
125 public void setSmallChange(final int smallChange
) {
126 this.smallChange
= smallChange
;
130 * The increment for clicking in the bar between the box and an arrow.
132 private int bigChange
= 20;
135 * Set the increment for clicking in the bar between the box and an
138 * @param bigChange the new increment value
140 public void setBigChange(final int bigChange
) {
141 this.bigChange
= bigChange
;
145 * When true, the user is dragging the scroll box.
147 private boolean inScroll
= false;
150 * Public constructor.
152 * @param parent parent widget
153 * @param x column relative to parent
154 * @param y row relative to parent
155 * @param height height of scroll bar
157 public TVScroller(final TWidget parent
, final int x
, final int y
,
160 // Set parent and window
161 super(parent
, x
, y
, 1, height
);
165 * Compute the position of the scroll box (a.k.a. grip, thumb).
167 * @return Y position of the box, between 1 and height - 2
169 private int boxPosition() {
170 return (getHeight() - 3) * (value
- topValue
) / (bottomValue
- topValue
) + 1;
174 * Draw a vertical scroll bar.
178 CellAttributes arrowColor
= getTheme().getColor("tscroller.arrows");
179 CellAttributes barColor
= getTheme().getColor("tscroller.bar");
180 getScreen().putCharXY(0, 0, GraphicsChars
.CP437
[0x1E], arrowColor
);
181 getScreen().putCharXY(0, getHeight() - 1, GraphicsChars
.CP437
[0x1F],
185 if (bottomValue
> topValue
) {
186 getScreen().vLineXY(0, 1, getHeight() - 2,
187 GraphicsChars
.CP437
[0xB1], barColor
);
188 getScreen().putCharXY(0, boxPosition(), GraphicsChars
.BOX
,
191 getScreen().vLineXY(0, 1, getHeight() - 2, GraphicsChars
.HATCH
,
198 * Perform a small step change up.
200 public void decrement() {
201 if (bottomValue
== topValue
) {
204 value
-= smallChange
;
205 if (value
< topValue
) {
211 * Perform a small step change down.
213 public void increment() {
214 if (bottomValue
== topValue
) {
217 value
+= smallChange
;
218 if (value
> bottomValue
) {
224 * Perform a big step change up.
226 public void bigDecrement() {
227 if (bottomValue
== topValue
) {
231 if (value
< topValue
) {
237 * Perform a big step change down.
239 public void bigIncrement() {
240 if (bottomValue
== topValue
) {
244 if (value
> bottomValue
) {
250 * Go to the top edge of the scroller.
252 public void toTop() {
257 * Go to the bottom edge of the scroller.
259 public void toBottom() {
264 * Handle mouse button releases.
266 * @param mouse mouse button release event
269 public void onMouseUp(final TMouseEvent mouse
) {
270 if (bottomValue
== topValue
) {
279 if ((mouse
.getX() == 0)
280 && (mouse
.getY() == 0)
282 // Clicked on the top arrow
287 if ((mouse
.getX() == 0)
288 && (mouse
.getY() == getHeight() - 1)
290 // Clicked on the bottom arrow
295 if ((mouse
.getX() == 0)
296 && (mouse
.getY() > 0)
297 && (mouse
.getY() < boxPosition())
299 // Clicked between the top arrow and the box
301 if (value
< topValue
) {
307 if ((mouse
.getX() == 0)
308 && (mouse
.getY() > boxPosition())
309 && (mouse
.getY() < getHeight() - 1)
311 // Clicked between the box and the bottom arrow
313 if (value
> bottomValue
) {
321 * Handle mouse movement events.
323 * @param mouse mouse motion event
326 public void onMouseMotion(final TMouseEvent mouse
) {
327 if (bottomValue
== topValue
) {
331 if ((mouse
.isMouse1()) &&
333 (mouse
.getY() > 0) &&
334 (mouse
.getY() < getHeight() - 1)
336 // Recompute value based on new box position
337 value
= (bottomValue
- topValue
) * (mouse
.getY()) / (getHeight() - 3) + topValue
;
345 * Handle mouse press events.
347 * @param mouse mouse button press event
350 public void onMouseDown(final TMouseEvent mouse
) {
351 if (bottomValue
== topValue
) {
355 if ((mouse
.getX() == 0)
356 && (mouse
.getY() == boxPosition())