Commit | Line | Data |
---|---|---|
0bcb5c7f | 1 | package be.nikiroo.jexer; |
74360832 NR |
2 | |
3 | import java.util.List; | |
4 | ||
5 | import jexer.TScrollableWidget; | |
6 | import jexer.TWidget; | |
7 | import jexer.event.TResizeEvent; | |
8 | import jexer.event.TResizeEvent.Type; | |
9 | ||
10 | public class TSizeConstraint { | |
11 | private TWidget widget; | |
12 | private Integer x1; | |
13 | private Integer y1; | |
14 | private Integer x2; | |
15 | private Integer y2; | |
16 | ||
17 | // TODO: include in the window classes I use? | |
18 | ||
19 | public TSizeConstraint(TWidget widget, Integer x1, Integer y1, Integer x2, | |
20 | Integer y2) { | |
21 | this.widget = widget; | |
22 | this.x1 = x1; | |
23 | this.y1 = y1; | |
24 | this.x2 = x2; | |
25 | this.y2 = y2; | |
26 | } | |
27 | ||
28 | public TWidget getWidget() { | |
29 | return widget; | |
30 | } | |
31 | ||
32 | public Integer getX1() { | |
33 | if (x1 != null && x1 < 0) | |
34 | return widget.getParent().getWidth() + x1; | |
35 | return x1; | |
36 | } | |
37 | ||
38 | public Integer getY1() { | |
39 | if (y1 != null && y1 < 0) | |
40 | return widget.getParent().getHeight() + y1; | |
41 | return y1; | |
42 | } | |
43 | ||
44 | public Integer getX2() { | |
45 | if (x2 != null && x2 <= 0) | |
46 | return widget.getParent().getWidth() - 2 + x2; | |
47 | return x2; | |
48 | } | |
49 | ||
50 | public Integer getY2() { | |
51 | if (y2 != null && y2 <= 0) | |
52 | return widget.getParent().getHeight() - 2 + y2; | |
53 | return y2; | |
54 | } | |
55 | ||
56 | // coordinates < 0 = from the other side, x2 or y2 = 0 = max size | |
0bcb5c7f | 57 | static public void setSize(List<TSizeConstraint> sizeConstraints, TWidget child, |
ce424b19 NR |
58 | Integer x1, Integer y1, Integer x2, Integer y2) { |
59 | sizeConstraints.add(new TSizeConstraint(child, x1, y1, x2, y2)); | |
74360832 NR |
60 | } |
61 | ||
0bcb5c7f | 62 | static public void resize(List<TSizeConstraint> sizeConstraints) { |
74360832 NR |
63 | for (TSizeConstraint sizeConstraint : sizeConstraints) { |
64 | TWidget widget = sizeConstraint.getWidget(); | |
65 | Integer x1 = sizeConstraint.getX1(); | |
66 | Integer y1 = sizeConstraint.getY1(); | |
67 | Integer x2 = sizeConstraint.getX2(); | |
68 | Integer y2 = sizeConstraint.getY2(); | |
69 | ||
70 | if (x1 != null) | |
71 | widget.setX(x1); | |
72 | if (y1 != null) | |
73 | widget.setY(y1); | |
74 | ||
75 | if (x2 != null) | |
76 | widget.setWidth(x2 - widget.getX()); | |
77 | if (y2 != null) | |
78 | widget.setHeight(y2 - widget.getY()); | |
79 | ||
80 | // Resize the text field | |
81 | // TODO: why setW/setH/reflow not enough for the scrollbars? | |
82 | widget.onResize(new TResizeEvent(Type.WIDGET, widget.getWidth(), | |
83 | widget.getHeight())); | |
84 | ||
85 | if (widget instanceof TScrollableWidget) { | |
86 | ((TScrollableWidget) widget).reflowData(); | |
87 | } | |
88 | } | |
89 | } | |
90 | } |