Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / layout / StretchLayoutManager.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.layout;
30
31 import java.awt.Rectangle;
32 import java.util.HashMap;
33
34 import jexer.TWidget;
35 import jexer.event.TResizeEvent;
36
37 /**
38 * StretchLayoutManager repositions child widgets based on their coordinates
39 * when added and the current widget size.
40 */
41 public class StretchLayoutManager implements LayoutManager {
42
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
46
47 /**
48 * Current width.
49 */
50 private int width = 0;
51
52 /**
53 * Current height.
54 */
55 private int height = 0;
56
57 /**
58 * Original width.
59 */
60 private int originalWidth = 0;
61
62 /**
63 * Original height.
64 */
65 private int originalHeight = 0;
66
67 /**
68 * Map of widget to original dimensions.
69 */
70 private HashMap<TWidget, Rectangle> children = new HashMap<TWidget, Rectangle>();
71
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
75
76 /**
77 * Public constructor.
78 *
79 * @param width the width of the parent widget
80 * @param height the height of the parent widget
81 */
82 public StretchLayoutManager(final int width, final int height) {
83 originalWidth = width;
84 originalHeight = height;
85 this.width = width;
86 this.height = height;
87 }
88
89 // ------------------------------------------------------------------------
90 // LayoutManager ----------------------------------------------------------
91 // ------------------------------------------------------------------------
92
93 /**
94 * Process the parent widget's resize event, and resize/reposition child
95 * widgets.
96 *
97 * @param resize resize event
98 */
99 public void onResize(final TResizeEvent resize) {
100 if (resize.getType() == TResizeEvent.Type.WIDGET) {
101 width = resize.getWidth();
102 height = resize.getHeight();
103 layoutChildren();
104 }
105 }
106
107 /**
108 * Add a child widget to manage.
109 *
110 * @param child the widget to manage
111 */
112 public void add(final TWidget child) {
113 Rectangle rect = new Rectangle(child.getX(), child.getY(),
114 child.getWidth(), child.getHeight());
115 children.put(child, rect);
116 layoutChildren();
117 }
118
119 /**
120 * Remove a child widget from those managed by this LayoutManager.
121 *
122 * @param child the widget to remove
123 */
124 public void remove(final TWidget child) {
125 children.remove(child);
126 layoutChildren();
127 }
128
129 /**
130 * Reset a child widget's original/preferred size.
131 *
132 * @param child the widget to manage
133 */
134 public void resetSize(final TWidget child) {
135 // For this layout, adding is the same as replacing.
136 add(child);
137 }
138
139 // ------------------------------------------------------------------------
140 // StretchLayoutManager ---------------------------------------------------
141 // ------------------------------------------------------------------------
142
143 /**
144 * Resize/reposition child widgets based on difference between current
145 * dimensions and the original dimensions.
146 */
147 private void layoutChildren() {
148 double widthRatio = (double) width / originalWidth;
149 if (!Double.isFinite(widthRatio)) {
150 widthRatio = 1;
151 }
152 double heightRatio = (double) height / originalHeight;
153 if (!Double.isFinite(heightRatio)) {
154 heightRatio = 1;
155 }
156 for (TWidget child: children.keySet()) {
157 Rectangle rect = children.get(child);
158 child.setDimensions((int) (rect.getX() * widthRatio),
159 (int) (rect.getY() * heightRatio),
160 (int) (rect.getWidth() * widthRatio),
161 (int) (rect.getHeight() * heightRatio));
162 }
163 }
164
165 }