2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 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 class TVScroller
extends TWidget
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Value that corresponds to being on the top edge of the scroll bar.
47 private int topValue
= 0;
50 * Value that corresponds to being on the bottom edge of the scroll bar.
52 private int bottomValue
= 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 height height of scroll bar
86 public TVScroller(final TWidget parent
, final int x
, final int y
,
89 // Set parent and window
90 super(parent
, x
, y
, 1, height
);
93 // ------------------------------------------------------------------------
94 // Event handlers ---------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * Handle mouse button releases.
100 * @param mouse mouse button release event
103 public void onMouseUp(final TMouseEvent mouse
) {
104 if (bottomValue
== topValue
) {
113 if ((mouse
.getX() == 0)
114 && (mouse
.getY() == 0)
116 // Clicked on the top arrow
121 if ((mouse
.getX() == 0)
122 && (mouse
.getY() == getHeight() - 1)
124 // Clicked on the bottom arrow
129 if ((mouse
.getX() == 0)
130 && (mouse
.getY() > 0)
131 && (mouse
.getY() < boxPosition())
133 // Clicked between the top arrow and the box
135 if (value
< topValue
) {
141 if ((mouse
.getX() == 0)
142 && (mouse
.getY() > boxPosition())
143 && (mouse
.getY() < getHeight() - 1)
145 // Clicked between the box and the bottom arrow
147 if (value
> bottomValue
) {
155 * Handle mouse movement events.
157 * @param mouse mouse motion event
160 public void onMouseMotion(final TMouseEvent mouse
) {
161 if (bottomValue
== topValue
) {
165 if ((mouse
.isMouse1())
167 && (mouse
.getY() > 0)
168 && (mouse
.getY() < getHeight() - 1)
170 // Recompute value based on new box position
171 value
= (bottomValue
- topValue
)
172 * (mouse
.getY()) / (getHeight() - 3) + topValue
;
173 if (value
> bottomValue
) {
176 if (value
< topValue
) {
186 * Handle mouse press events.
188 * @param mouse mouse button press event
191 public void onMouseDown(final TMouseEvent mouse
) {
192 if (bottomValue
== topValue
) {
196 if ((mouse
.getX() == 0)
197 && (mouse
.getY() == boxPosition())
204 // ------------------------------------------------------------------------
205 // TWidget ----------------------------------------------------------------
206 // ------------------------------------------------------------------------
209 * Draw a vertical scroll bar.
213 CellAttributes arrowColor
= getTheme().getColor("tscroller.arrows");
214 CellAttributes barColor
= getTheme().getColor("tscroller.bar");
215 putCharXY(0, 0, GraphicsChars
.CP437
[0x1E], arrowColor
);
216 putCharXY(0, getHeight() - 1, GraphicsChars
.CP437
[0x1F], arrowColor
);
219 if (bottomValue
> topValue
) {
220 vLineXY(0, 1, getHeight() - 2, GraphicsChars
.CP437
[0xB1], barColor
);
221 putCharXY(0, boxPosition(), GraphicsChars
.BOX
, arrowColor
);
223 vLineXY(0, 1, getHeight() - 2, GraphicsChars
.HATCH
, barColor
);
227 // ------------------------------------------------------------------------
228 // TVScroller -------------------------------------------------------------
229 // ------------------------------------------------------------------------
232 * Get the value that corresponds to being on the top edge of the scroll
235 * @return the scroll value
237 public int getTopValue() {
242 * Set the value that corresponds to being on the top edge of the scroll
245 * @param topValue the new scroll value
247 public void setTopValue(final int topValue
) {
248 this.topValue
= topValue
;
252 * Get the value that corresponds to being on the bottom edge of the
255 * @return the scroll value
257 public int getBottomValue() {
262 * Set the value that corresponds to being on the bottom edge of the
265 * @param bottomValue the new scroll value
267 public void setBottomValue(final int bottomValue
) {
268 this.bottomValue
= bottomValue
;
272 * Get current value of the scroll.
274 * @return the scroll value
276 public int getValue() {
281 * Set current value of the scroll.
283 * @param value the new scroll value
285 public void setValue(final int value
) {
290 * Get the increment for clicking on an arrow.
292 * @return the increment value
294 public int getSmallChange() {
299 * Set the increment for clicking on an arrow.
301 * @param smallChange the new increment value
303 public void setSmallChange(final int smallChange
) {
304 this.smallChange
= smallChange
;
308 * Set the increment for clicking in the bar between the box and an
311 * @return the increment value
313 public int getBigChange() {
318 * Set the increment for clicking in the bar between the box and an
321 * @param bigChange the new increment value
323 public void setBigChange(final int bigChange
) {
324 this.bigChange
= bigChange
;
328 * Compute the position of the scroll box (a.k.a. grip, thumb).
330 * @return Y position of the box, between 1 and height - 2
332 private int boxPosition() {
333 return (getHeight() - 3) * (value
- topValue
) / (bottomValue
- topValue
) + 1;
337 * Perform a small step change up.
339 public void decrement() {
340 if (bottomValue
== topValue
) {
343 value
-= smallChange
;
344 if (value
< topValue
) {
350 * Perform a small step change down.
352 public void increment() {
353 if (bottomValue
== topValue
) {
356 value
+= smallChange
;
357 if (value
> bottomValue
) {
363 * Perform a big step change up.
365 public void bigDecrement() {
366 if (bottomValue
== topValue
) {
370 if (value
< topValue
) {
376 * Perform a big step change down.
378 public void bigIncrement() {
379 if (bottomValue
== topValue
) {
383 if (value
> bottomValue
) {
389 * Go to the top edge of the scroller.
391 public void toTop() {
396 * Go to the bottom edge of the scroller.
398 public void toBottom() {