Merge commit 'edcd53bbbba9f94e21f43fd03d3a2febcc2b1564'
[fanfix.git] / src / be / nikiroo / fanfix / reader / tui / TSimpleScrollableWindow.java
1 package be.nikiroo.fanfix.reader.tui;
2
3 import jexer.TApplication;
4 import jexer.THScroller;
5 import jexer.TPanel;
6 import jexer.TScrollableWindow;
7 import jexer.TVScroller;
8 import jexer.TWidget;
9
10 public class TSimpleScrollableWindow extends TScrollableWindow {
11 protected TPanel mainPane;
12 private int prevHorizontal = -1;
13 private int prevVertical = -1;
14
15 public TSimpleScrollableWindow(TApplication application, String title,
16 int width, int height) {
17 this(application, title, width, height, 0, 0, 0);
18 }
19
20 public TSimpleScrollableWindow(TApplication application, String title,
21 int width, int height, int flags) {
22 this(application, title, width, height, flags, 0, 0);
23 }
24
25 // 0 = none (so, no scrollbar)
26 public TSimpleScrollableWindow(TApplication application, String title,
27 int width, int height, int flags, int realWidth, int realHeight) {
28 super(application, title, width, height, flags);
29
30 mainPane = new TPanel(this, 0, 0, width, 80) {
31 @Override
32 public void draw() {
33 for (TWidget children : mainPane.getChildren()) {
34 int y = children.getY() + children.getHeight();
35 int x = children.getX() + children.getWidth();
36 boolean visible = (y > getVerticalValue())
37 && (x > getHorizontalValue());
38 children.setVisible(visible);
39 }
40 super.draw();
41 }
42 };
43
44 // // TODO: test
45 // for (int i = 0; i < 80; i++) {
46 // mainPane.addLabel("ligne " + i, i, i);
47 // }
48
49 setRealWidth(realWidth);
50 setRealHeight(realHeight);
51 placeScrollbars();
52 }
53
54 /**
55 * The main pane on which you can add other widgets for this scrollable
56 * window.
57 *
58 * @return the main pane
59 */
60 public TPanel getMainPane() {
61 return mainPane;
62 }
63
64 public void setRealWidth(int realWidth) {
65 if (realWidth <= 0) {
66 if (hScroller != null) {
67 hScroller.remove();
68 }
69 } else {
70 if (hScroller == null) {
71 // size/position will be fixed by placeScrollbars()
72 hScroller = new THScroller(this, 0, 0, 10);
73 }
74 hScroller.setRightValue(realWidth);
75 }
76
77 reflowData();
78 }
79
80 public void setRealHeight(int realHeight) {
81 if (realHeight <= 0) {
82 if (vScroller != null) {
83 vScroller.remove();
84 }
85 } else {
86 if (vScroller == null) {
87 // size/position will be fixed by placeScrollbars()
88 vScroller = new TVScroller(this, 0, 0, 10);
89 }
90 vScroller.setBottomValue(realHeight);
91 }
92
93 reflowData();
94 }
95
96 @Override
97 public void reflowData() {
98 super.reflowData();
99 reflowData(getHorizontalValue(), getVerticalValue());
100 }
101
102 protected void reflowData(int totalX, int totalY) {
103 super.reflowData();
104 mainPane.setX(-totalX);
105 mainPane.setY(-totalY);
106 }
107
108 @Override
109 public void draw() {
110 if (prevHorizontal != getHorizontalValue()
111 || prevVertical != getVerticalValue()) {
112 prevHorizontal = getHorizontalValue();
113 prevVertical = getVerticalValue();
114 reflowData();
115 }
116
117 super.draw();
118 }
119 }