Merge branch 'subtree'
[nikiroo-utils.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.TMouseEvent;
10 import jexer.event.TResizeEvent;
11
12 public class TSimpleScrollableWindow extends TScrollableWindow {
13 protected TPanel mainPane;
14 private int prevHorizontal = -1;
15 private int prevVertical = -1;
16
17 public TSimpleScrollableWindow(TApplication application, String title,
18 int width, int height) {
19 this(application, title, width, height, 0, 0, 0);
20 }
21
22 public TSimpleScrollableWindow(TApplication application, String title,
23 int width, int height, int flags) {
24 this(application, title, width, height, flags, 0, 0);
25 }
26
27 // 0 = none (so, no scrollbar)
28 public TSimpleScrollableWindow(TApplication application, String title,
29 int width, int height, int flags, int realWidth, int realHeight) {
30 super(application, title, width, height, flags);
31
32 mainPane = new TPanel(this, 0, 0, 1, 1) {
33 @Override
34 public void draw() {
35 for (TWidget children : mainPane.getChildren()) {
36 int y = children.getY() + children.getHeight();
37 int x = children.getX() + children.getWidth();
38 boolean visible = (y > getVerticalValue())
39 && (x > getHorizontalValue());
40 children.setVisible(visible);
41 }
42 super.draw();
43 }
44 };
45
46 mainPane.setWidth(getWidth());
47 mainPane.setHeight(getHeight());
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 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 setBottomValue(realHeight);
91 }
92
93 reflowData();
94 }
95
96 @Override
97 public void onResize(TResizeEvent event) {
98 super.onResize(event);
99 mainPane.setWidth(getWidth());
100 mainPane.setHeight(getHeight());
101 mainPane.onResize(event);
102 }
103
104 @Override
105 public void reflowData() {
106 super.reflowData();
107 reflowData(getHorizontalValue(), getVerticalValue());
108 }
109
110 protected void reflowData(int totalX, int totalY) {
111 super.reflowData();
112 mainPane.setX(-totalX);
113 mainPane.setY(-totalY);
114 }
115
116 @Override
117 public void onMouseUp(TMouseEvent mouse) {
118 super.onMouseUp(mouse);
119
120 // TODO: why? this should already be done by the scrollers
121 // it could also mean we do it twice if, somehow, it sometime works...
122 int mrx = mouse.getX();
123 int mry = mouse.getY();
124
125 int mx = mouse.getAbsoluteX();
126 int my = mouse.getAbsoluteY();
127
128 if (vScroller != null) {
129 mouse.setX(mx - vScroller.getAbsoluteX());
130 mouse.setY(my - vScroller.getAbsoluteY());
131 vScroller.onMouseUp(mouse);
132 }
133 if (hScroller != null) {
134 mouse.setX(mx - hScroller.getAbsoluteX());
135 mouse.setY(my - hScroller.getAbsoluteY());
136 hScroller.onMouseUp(mouse);
137 }
138
139 mouse.setX(mrx);
140 mouse.setY(mry);
141 //
142
143 if (prevHorizontal != getHorizontalValue()
144 || prevVertical != getVerticalValue()) {
145 prevHorizontal = getHorizontalValue();
146 prevVertical = getVerticalValue();
147 reflowData();
148 }
149 }
150 }