| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2019 Kevin Lamonte |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 27 | * @version 1 |
| 28 | */ |
| 29 | package jexer; |
| 30 | |
| 31 | import jexer.event.TResizeEvent; |
| 32 | |
| 33 | /** |
| 34 | * TScrollableWidget is a convenience superclass for widgets that have |
| 35 | * scrollbars. |
| 36 | */ |
| 37 | public class TScrollableWidget extends TWidget implements Scrollable { |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Variables -------------------------------------------------------------- |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
| 43 | /** |
| 44 | * The horizontal scrollbar. |
| 45 | */ |
| 46 | protected THScroller hScroller = null; |
| 47 | |
| 48 | /** |
| 49 | * The vertical scrollbar. |
| 50 | */ |
| 51 | protected TVScroller vScroller = null; |
| 52 | |
| 53 | // ------------------------------------------------------------------------ |
| 54 | // Constructors ----------------------------------------------------------- |
| 55 | // ------------------------------------------------------------------------ |
| 56 | |
| 57 | /** |
| 58 | * Protected constructor. |
| 59 | * |
| 60 | * @param parent parent widget |
| 61 | */ |
| 62 | protected TScrollableWidget(final TWidget parent) { |
| 63 | super(parent); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Protected constructor. |
| 68 | * |
| 69 | * @param parent parent widget |
| 70 | * @param x column relative to parent |
| 71 | * @param y row relative to parent |
| 72 | * @param width width of widget |
| 73 | * @param height height of widget |
| 74 | */ |
| 75 | protected TScrollableWidget(final TWidget parent, final int x, final int y, |
| 76 | final int width, final int height) { |
| 77 | |
| 78 | super(parent, x, y, width, height); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Protected constructor used by subclasses that are disabled by default. |
| 83 | * |
| 84 | * @param parent parent widget |
| 85 | * @param enabled if true assume enabled |
| 86 | */ |
| 87 | protected TScrollableWidget(final TWidget parent, final boolean enabled) { |
| 88 | |
| 89 | super(parent, enabled); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Protected constructor used by subclasses that are disabled by default. |
| 94 | * |
| 95 | * @param parent parent widget |
| 96 | * @param enabled if true assume enabled |
| 97 | * @param x column relative to parent |
| 98 | * @param y row relative to parent |
| 99 | * @param width width of widget |
| 100 | * @param height height of widget |
| 101 | */ |
| 102 | protected TScrollableWidget(final TWidget parent, final boolean enabled, |
| 103 | final int x, final int y, final int width, final int height) { |
| 104 | |
| 105 | super(parent, enabled, x, y, width, height); |
| 106 | } |
| 107 | |
| 108 | // ------------------------------------------------------------------------ |
| 109 | // TWidget ---------------------------------------------------------------- |
| 110 | // ------------------------------------------------------------------------ |
| 111 | |
| 112 | /** |
| 113 | * Handle window/screen resize events. |
| 114 | * |
| 115 | * @param event resize event |
| 116 | */ |
| 117 | @Override |
| 118 | public void onResize(final TResizeEvent event) { |
| 119 | if (event.getType() == TResizeEvent.Type.WIDGET) { |
| 120 | setWidth(event.getWidth()); |
| 121 | setHeight(event.getHeight()); |
| 122 | |
| 123 | reflowData(); |
| 124 | placeScrollbars(); |
| 125 | return; |
| 126 | } else { |
| 127 | super.onResize(event); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // ------------------------------------------------------------------------ |
| 132 | // TScrollableWidget ------------------------------------------------------ |
| 133 | // ------------------------------------------------------------------------ |
| 134 | |
| 135 | /** |
| 136 | * Place the scrollbars on the edge of this widget, and adjust bigChange |
| 137 | * to match the new size. This is called by onResize(). |
| 138 | */ |
| 139 | protected void placeScrollbars() { |
| 140 | if (hScroller != null) { |
| 141 | hScroller.setY(getHeight() - 1); |
| 142 | hScroller.setWidth(getWidth() - 1); |
| 143 | hScroller.setBigChange(getWidth() - 1); |
| 144 | } |
| 145 | if (vScroller != null) { |
| 146 | vScroller.setX(getWidth() - 1); |
| 147 | vScroller.setHeight(getHeight() - 1); |
| 148 | vScroller.setBigChange(getHeight() - 1); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Recompute whatever data is displayed by this widget. |
| 154 | */ |
| 155 | public void reflowData() { |
| 156 | // Default: nothing to do |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the horizontal scrollbar, or null if this Viewport does not |
| 161 | * support horizontal scrolling. |
| 162 | * |
| 163 | * @return the horizontal scrollbar |
| 164 | */ |
| 165 | public THScroller getHorizontalScroller() { |
| 166 | return hScroller; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the vertical scrollbar, or null if this Viewport does not support |
| 171 | * vertical scrolling. |
| 172 | * |
| 173 | * @return the vertical scrollbar |
| 174 | */ |
| 175 | public TVScroller getVerticalScroller() { |
| 176 | return vScroller; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get the value that corresponds to being on the top edge of the |
| 181 | * vertical scroll bar. |
| 182 | * |
| 183 | * @return the scroll value |
| 184 | */ |
| 185 | public int getTopValue() { |
| 186 | if (vScroller == null) { |
| 187 | return 0; |
| 188 | } else { |
| 189 | return vScroller.getTopValue(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Set the value that corresponds to being on the top edge of the |
| 195 | * vertical scroll bar. |
| 196 | * |
| 197 | * @param topValue the new scroll value |
| 198 | */ |
| 199 | public void setTopValue(final int topValue) { |
| 200 | if (vScroller == null) { |
| 201 | return; |
| 202 | } else { |
| 203 | vScroller.setTopValue(topValue); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get the value that corresponds to being on the bottom edge of the |
| 209 | * vertical scroll bar. |
| 210 | * |
| 211 | * @return the scroll value |
| 212 | */ |
| 213 | public int getBottomValue() { |
| 214 | if (vScroller == null) { |
| 215 | return 0; |
| 216 | } else { |
| 217 | return vScroller.getBottomValue(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Set the value that corresponds to being on the bottom edge of the |
| 223 | * vertical scroll bar. |
| 224 | * |
| 225 | * @param bottomValue the new scroll value |
| 226 | */ |
| 227 | public void setBottomValue(final int bottomValue) { |
| 228 | if (vScroller == null) { |
| 229 | return; |
| 230 | } else { |
| 231 | vScroller.setBottomValue(bottomValue); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Get current value of the vertical scroll. |
| 237 | * |
| 238 | * @return the scroll value |
| 239 | */ |
| 240 | public int getVerticalValue() { |
| 241 | if (vScroller == null) { |
| 242 | return 0; |
| 243 | } else { |
| 244 | return vScroller.getValue(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Set current value of the vertical scroll. |
| 250 | * |
| 251 | * @param value the new scroll value |
| 252 | */ |
| 253 | public void setVerticalValue(final int value) { |
| 254 | if (vScroller == null) { |
| 255 | return; |
| 256 | } else { |
| 257 | vScroller.setValue(value); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get the increment for clicking on an arrow on the vertical scrollbar. |
| 263 | * |
| 264 | * @return the increment value |
| 265 | */ |
| 266 | public int getVerticalSmallChange() { |
| 267 | if (vScroller == null) { |
| 268 | return 0; |
| 269 | } else { |
| 270 | return vScroller.getSmallChange(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Set the increment for clicking on an arrow on the vertical scrollbar. |
| 276 | * |
| 277 | * @param smallChange the new increment value |
| 278 | */ |
| 279 | public void setVerticalSmallChange(final int smallChange) { |
| 280 | if (vScroller == null) { |
| 281 | return; |
| 282 | } else { |
| 283 | vScroller.setSmallChange(smallChange); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get the increment for clicking in the bar between the box and an |
| 289 | * arrow on the vertical scrollbar. |
| 290 | * |
| 291 | * @return the increment value |
| 292 | */ |
| 293 | public int getVerticalBigChange() { |
| 294 | if (vScroller == null) { |
| 295 | return 0; |
| 296 | } else { |
| 297 | return vScroller.getBigChange(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Set the increment for clicking in the bar between the box and an |
| 303 | * arrow on the vertical scrollbar. |
| 304 | * |
| 305 | * @param bigChange the new increment value |
| 306 | */ |
| 307 | public void setVerticalBigChange(final int bigChange) { |
| 308 | if (vScroller == null) { |
| 309 | return; |
| 310 | } else { |
| 311 | vScroller.setBigChange(bigChange); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Perform a small step change up. |
| 317 | */ |
| 318 | public void verticalDecrement() { |
| 319 | if (vScroller == null) { |
| 320 | return; |
| 321 | } else { |
| 322 | vScroller.decrement(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Perform a small step change down. |
| 328 | */ |
| 329 | public void verticalIncrement() { |
| 330 | if (vScroller == null) { |
| 331 | return; |
| 332 | } else { |
| 333 | vScroller.increment(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Perform a big step change up. |
| 339 | */ |
| 340 | public void bigVerticalDecrement() { |
| 341 | if (vScroller == null) { |
| 342 | return; |
| 343 | } else { |
| 344 | vScroller.bigDecrement(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Perform a big step change down. |
| 350 | */ |
| 351 | public void bigVerticalIncrement() { |
| 352 | if (vScroller == null) { |
| 353 | return; |
| 354 | } else { |
| 355 | vScroller.bigIncrement(); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Go to the top edge of the vertical scroller. |
| 361 | */ |
| 362 | public void toTop() { |
| 363 | if (vScroller == null) { |
| 364 | return; |
| 365 | } else { |
| 366 | vScroller.toTop(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Go to the bottom edge of the vertical scroller. |
| 372 | */ |
| 373 | public void toBottom() { |
| 374 | if (vScroller == null) { |
| 375 | return; |
| 376 | } else { |
| 377 | vScroller.toBottom(); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Get the value that corresponds to being on the left edge of the |
| 383 | * horizontal scroll bar. |
| 384 | * |
| 385 | * @return the scroll value |
| 386 | */ |
| 387 | public int getLeftValue() { |
| 388 | if (hScroller == null) { |
| 389 | return 0; |
| 390 | } else { |
| 391 | return hScroller.getLeftValue(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Set the value that corresponds to being on the left edge of the |
| 397 | * horizontal scroll bar. |
| 398 | * |
| 399 | * @param leftValue the new scroll value |
| 400 | */ |
| 401 | public void setLeftValue(final int leftValue) { |
| 402 | if (hScroller == null) { |
| 403 | return; |
| 404 | } else { |
| 405 | hScroller.setLeftValue(leftValue); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Get the value that corresponds to being on the right edge of the |
| 411 | * horizontal scroll bar. |
| 412 | * |
| 413 | * @return the scroll value |
| 414 | */ |
| 415 | public int getRightValue() { |
| 416 | if (hScroller == null) { |
| 417 | return 0; |
| 418 | } else { |
| 419 | return hScroller.getRightValue(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Set the value that corresponds to being on the right edge of the |
| 425 | * horizontal scroll bar. |
| 426 | * |
| 427 | * @param rightValue the new scroll value |
| 428 | */ |
| 429 | public void setRightValue(final int rightValue) { |
| 430 | if (hScroller == null) { |
| 431 | return; |
| 432 | } else { |
| 433 | hScroller.setRightValue(rightValue); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Get current value of the horizontal scroll. |
| 439 | * |
| 440 | * @return the scroll value |
| 441 | */ |
| 442 | public int getHorizontalValue() { |
| 443 | if (hScroller == null) { |
| 444 | return 0; |
| 445 | } else { |
| 446 | return hScroller.getValue(); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Set current value of the horizontal scroll. |
| 452 | * |
| 453 | * @param value the new scroll value |
| 454 | */ |
| 455 | public void setHorizontalValue(final int value) { |
| 456 | if (hScroller == null) { |
| 457 | return; |
| 458 | } else { |
| 459 | hScroller.setValue(value); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Get the increment for clicking on an arrow on the horizontal |
| 465 | * scrollbar. |
| 466 | * |
| 467 | * @return the increment value |
| 468 | */ |
| 469 | public int getHorizontalSmallChange() { |
| 470 | if (hScroller == null) { |
| 471 | return 0; |
| 472 | } else { |
| 473 | return hScroller.getSmallChange(); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Set the increment for clicking on an arrow on the horizontal |
| 479 | * scrollbar. |
| 480 | * |
| 481 | * @param smallChange the new increment value |
| 482 | */ |
| 483 | public void setHorizontalSmallChange(final int smallChange) { |
| 484 | if (hScroller == null) { |
| 485 | return; |
| 486 | } else { |
| 487 | hScroller.setSmallChange(smallChange); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Get the increment for clicking in the bar between the box and an |
| 493 | * arrow on the horizontal scrollbar. |
| 494 | * |
| 495 | * @return the increment value |
| 496 | */ |
| 497 | public int getHorizontalBigChange() { |
| 498 | if (hScroller == null) { |
| 499 | return 0; |
| 500 | } else { |
| 501 | return hScroller.getBigChange(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Set the increment for clicking in the bar between the box and an |
| 507 | * arrow on the horizontal scrollbar. |
| 508 | * |
| 509 | * @param bigChange the new increment value |
| 510 | */ |
| 511 | public void setHorizontalBigChange(final int bigChange) { |
| 512 | if (hScroller == null) { |
| 513 | return; |
| 514 | } else { |
| 515 | hScroller.setBigChange(bigChange); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Perform a small step change left. |
| 521 | */ |
| 522 | public void horizontalDecrement() { |
| 523 | if (hScroller == null) { |
| 524 | return; |
| 525 | } else { |
| 526 | hScroller.decrement(); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Perform a small step change right. |
| 532 | */ |
| 533 | public void horizontalIncrement() { |
| 534 | if (hScroller == null) { |
| 535 | return; |
| 536 | } else { |
| 537 | hScroller.increment(); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Perform a big step change left. |
| 543 | */ |
| 544 | public void bigHorizontalDecrement() { |
| 545 | if (hScroller == null) { |
| 546 | return; |
| 547 | } else { |
| 548 | hScroller.bigDecrement(); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Perform a big step change right. |
| 554 | */ |
| 555 | public void bigHorizontalIncrement() { |
| 556 | if (hScroller == null) { |
| 557 | return; |
| 558 | } else { |
| 559 | hScroller.bigIncrement(); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Go to the left edge of the horizontal scroller. |
| 565 | */ |
| 566 | public void toLeft() { |
| 567 | if (hScroller == null) { |
| 568 | return; |
| 569 | } else { |
| 570 | hScroller.toLeft(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Go to the right edge of the horizontal scroller. |
| 576 | */ |
| 577 | public void toRight() { |
| 578 | if (hScroller == null) { |
| 579 | return; |
| 580 | } else { |
| 581 | hScroller.toRight(); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Go to the top-left edge of the horizontal and vertical scrollers. |
| 587 | */ |
| 588 | public void toHome() { |
| 589 | if (hScroller != null) { |
| 590 | hScroller.toLeft(); |
| 591 | } |
| 592 | if (vScroller != null) { |
| 593 | vScroller.toTop(); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Go to the bottom-right edge of the horizontal and vertical scrollers. |
| 599 | */ |
| 600 | public void toEnd() { |
| 601 | if (hScroller != null) { |
| 602 | hScroller.toRight(); |
| 603 | } |
| 604 | if (vScroller != null) { |
| 605 | vScroller.toBottom(); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | } |