Merge branch 'subtree'
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / tui / TSimpleScrollableWindow.java
CommitLineData
b6d17298
NR
1package be.nikiroo.fanfix.reader.tui;
2
3import jexer.TApplication;
4import jexer.THScroller;
5import jexer.TPanel;
6import jexer.TScrollableWindow;
7import jexer.TVScroller;
8import jexer.TWidget;
a8659744 9import jexer.event.TMouseEvent;
c6815053 10import jexer.event.TResizeEvent;
b6d17298
NR
11
12public 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
c6815053 32 mainPane = new TPanel(this, 0, 0, 1, 1) {
b6d17298
NR
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
c6815053
NR
46 mainPane.setWidth(getWidth());
47 mainPane.setHeight(getHeight());
b6d17298
NR
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 }
a8659744 74 setRightValue(realWidth);
b6d17298
NR
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 }
a8659744 90 setBottomValue(realHeight);
b6d17298
NR
91 }
92
93 reflowData();
94 }
95
c6815053
NR
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
b6d17298
NR
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
a8659744
NR
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
b6d17298
NR
143 if (prevHorizontal != getHorizontalValue()
144 || prevVertical != getVerticalValue()) {
145 prevHorizontal = getHorizontalValue();
146 prevVertical = getVerticalValue();
147 reflowData();
148 }
b6d17298
NR
149 }
150}