| 1 | package be.nikiroo.jexer; |
| 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 |
| 57 | // x2 or y2 = 0 = max size |
| 58 | // coordinate NULL = do not work on that side at all |
| 59 | static public void setSize(List<TSizeConstraint> sizeConstraints, TWidget child, |
| 60 | Integer x1, Integer y1, Integer x2, Integer y2) { |
| 61 | sizeConstraints.add(new TSizeConstraint(child, x1, y1, x2, y2)); |
| 62 | } |
| 63 | |
| 64 | static public void resize(List<TSizeConstraint> sizeConstraints) { |
| 65 | for (TSizeConstraint sizeConstraint : sizeConstraints) { |
| 66 | TWidget widget = sizeConstraint.getWidget(); |
| 67 | Integer x1 = sizeConstraint.getX1(); |
| 68 | Integer y1 = sizeConstraint.getY1(); |
| 69 | Integer x2 = sizeConstraint.getX2(); |
| 70 | Integer y2 = sizeConstraint.getY2(); |
| 71 | |
| 72 | if (x1 != null) |
| 73 | widget.setX(x1); |
| 74 | if (y1 != null) |
| 75 | widget.setY(y1); |
| 76 | |
| 77 | if (x2 != null) |
| 78 | widget.setWidth(x2 - widget.getX()); |
| 79 | if (y2 != null) |
| 80 | widget.setHeight(y2 - widget.getY()); |
| 81 | |
| 82 | // Resize the text field |
| 83 | // TODO: why setW/setH/reflow not enough for the scrollbars? |
| 84 | widget.onResize(new TResizeEvent(Type.WIDGET, widget.getWidth(), |
| 85 | widget.getHeight())); |
| 86 | |
| 87 | if (widget instanceof TScrollableWidget) { |
| 88 | ((TScrollableWidget) widget).reflowData(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |