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 * 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 putCharXY(0, 0, GraphicsChars
.CP437
[0x11], arrowColor
);
220 putCharXY(getWidth() - 1, 0, GraphicsChars
.CP437
[0x10], arrowColor
);
223 if (rightValue
> leftValue
) {
224 hLineXY(1, 0, getWidth() - 2, GraphicsChars
.CP437
[0xB1], barColor
);
225 putCharXY(boxPosition(), 0, GraphicsChars
.BOX
, arrowColor
);
227 hLineXY(1, 0, getWidth() - 2, GraphicsChars
.HATCH
, barColor
);
232 // ------------------------------------------------------------------------
233 // THScroller -------------------------------------------------------------
234 // ------------------------------------------------------------------------
237 * Get the value that corresponds to being on the left edge of the scroll
240 * @return the scroll value
242 public int getLeftValue() {
247 * Set the value that corresponds to being on the left edge of the
250 * @param leftValue the new scroll value
252 public void setLeftValue(final int leftValue
) {
253 this.leftValue
= leftValue
;
257 * Get the value that corresponds to being on the right edge of the
260 * @return the scroll value
262 public int getRightValue() {
267 * Set the value that corresponds to being on the right edge of the
270 * @param rightValue the new scroll value
272 public void setRightValue(final int rightValue
) {
273 this.rightValue
= rightValue
;
277 * Get current value of the scroll.
279 * @return the scroll value
281 public int getValue() {
286 * Set current value of the scroll.
288 * @param value the new scroll value
290 public void setValue(final int value
) {
295 * Get the increment for clicking on an arrow.
297 * @return the increment value
299 public int getSmallChange() {
304 * Set the increment for clicking on an arrow.
306 * @param smallChange the new increment value
308 public void setSmallChange(final int smallChange
) {
309 this.smallChange
= smallChange
;
313 * Set the increment for clicking in the bar between the box and an
316 * @return the increment value
318 public int getBigChange() {
323 * Set the increment for clicking in the bar between the box and an
326 * @param bigChange the new increment value
328 public void setBigChange(final int bigChange
) {
329 this.bigChange
= bigChange
;
333 * Compute the position of the scroll box (a.k.a. grip, thumb).
335 * @return Y position of the box, between 1 and width - 2
337 private int boxPosition() {
338 return (getWidth() - 3) * (value
- leftValue
) / (rightValue
- leftValue
) + 1;
342 * Perform a small step change left.
344 public void decrement() {
345 if (leftValue
== rightValue
) {
348 value
-= smallChange
;
349 if (value
< leftValue
) {
355 * Perform a small step change right.
357 public void increment() {
358 if (leftValue
== rightValue
) {
361 value
+= smallChange
;
362 if (value
> rightValue
) {
368 * Perform a big step change left.
370 public void bigDecrement() {
371 if (leftValue
== rightValue
) {
375 if (value
< leftValue
) {
381 * Perform a big step change right.
383 public void bigIncrement() {
384 if (rightValue
== leftValue
) {
388 if (value
> rightValue
) {
394 * Go to the left edge of the scroller.
396 public void toLeft() {
401 * Go to the right edge of the scroller.
403 public void toRight() {