resizing fixes
[fanfix.git] / src / jexer / layout / StretchLayoutManager.java
CommitLineData
6cdd4553
KL
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 */
29package jexer.layout;
30
d8dc8aea
KL
31import java.awt.Rectangle;
32import java.util.HashMap;
33
6cdd4553
KL
34import jexer.TWidget;
35import jexer.event.TResizeEvent;
36
37/**
38 * StretchLayoutManager repositions child widgets based on their coordinates
39 * when added and the current widget size.
40 */
41public class StretchLayoutManager implements LayoutManager {
42
43 // ------------------------------------------------------------------------
44 // Variables --------------------------------------------------------------
45 // ------------------------------------------------------------------------
46
d8dc8aea
KL
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
6cdd4553
KL
72 // ------------------------------------------------------------------------
73 // Constructors -----------------------------------------------------------
74 // ------------------------------------------------------------------------
75
d8dc8aea
KL
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 }
6cdd4553
KL
86
87 // ------------------------------------------------------------------------
88 // LayoutManager ----------------------------------------------------------
89 // ------------------------------------------------------------------------
90
91 /**
92 * Process the parent widget's resize event, and resize/reposition child
93 * widgets.
94 *
95 * @param resize resize event
96 */
97 public void onResize(final TResizeEvent resize) {
d8dc8aea
KL
98 if (resize.getType() == TResizeEvent.Type.WIDGET) {
99 width = resize.getWidth();
100 height = resize.getHeight();
101 layoutChildren();
102 }
6cdd4553
KL
103 }
104
105 /**
106 * Add a child widget to manage.
107 *
108 * @param child the widget to manage
109 */
110 public void add(final TWidget child) {
d8dc8aea
KL
111 Rectangle rect = new Rectangle(child.getX(), child.getY(),
112 child.getWidth(), child.getHeight());
113 children.put(child, rect);
6cdd4553
KL
114 }
115
116 /**
117 * Remove a child widget from those managed by this LayoutManager.
118 *
119 * @param child the widget to remove
120 */
121 public void remove(final TWidget child) {
d8dc8aea
KL
122 children.remove(child);
123 }
124
125 // ------------------------------------------------------------------------
126 // StretchLayoutManager ---------------------------------------------------
127 // ------------------------------------------------------------------------
128
129 /**
130 * Resize/reposition child widgets based on difference between current
131 * dimensions and the original dimensions.
132 */
133 public void layoutChildren() {
134 double widthRatio = (double) width / originalWidth;
135 if (!Double.isFinite(widthRatio)) {
136 widthRatio = 1;
137 }
138 double heightRatio = (double) height / originalHeight;
139 if (!Double.isFinite(heightRatio)) {
140 heightRatio = 1;
141 }
142 for (TWidget child: children.keySet()) {
143 Rectangle rect = children.get(child);
144 child.setDimensions((int) (rect.getX() * widthRatio),
145 (int) (rect.getY() * heightRatio),
146 (int) (rect.getWidth() * widthRatio),
147 (int) (rect.getHeight() * heightRatio));
148 }
6cdd4553
KL
149 }
150
151}