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