|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2023 Christoph Läubrich and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + ********************************************************************************/ |
| 13 | + |
| 14 | +package org.eclipse.swt.layout; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +import org.eclipse.swt.*; |
| 19 | +import org.eclipse.swt.graphics.*; |
| 20 | +import org.eclipse.swt.widgets.*; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * @since 3.123 |
| 25 | + * |
| 26 | + */ |
| 27 | +public class GridBagConstraints { |
| 28 | + |
| 29 | + /** |
| 30 | + * |
| 31 | + */ |
| 32 | + private static final String DATA_KEY = GridBag.class.getName(); |
| 33 | + |
| 34 | + /** |
| 35 | + * |
| 36 | + */ |
| 37 | + private static final GridBagConstraints DEFAULT = new GridBagConstraints(); |
| 38 | + |
| 39 | + public static final int RELATIVE = -1; |
| 40 | + |
| 41 | + public static final int REMAINDER = -1; |
| 42 | + |
| 43 | + /** |
| 44 | + * Specify the grid coordinate where the component will be placed, starting with 0 and defaulting to |
| 45 | + * {@link #RELATIVE}, it is recommended to always set a value to have more predictable layouts that are independent of |
| 46 | + * the order components are added to the parent composite. |
| 47 | + */ |
| 48 | + public final Point grid = new Point(RELATIVE, RELATIVE); |
| 49 | + |
| 50 | + /** |
| 51 | + * Specify how much columns/rows the component spans (default to 1), specify {@link #REMAINDER} to take all remaining |
| 52 | + * space of the row/column, it is recommended to always specify a value to get more predictable layouts |
| 53 | + */ |
| 54 | + public final Point span = new Point(1, 1); |
| 55 | + |
| 56 | + /** |
| 57 | + * The weighting factor for the column this component is placed with, at least one control needs to define a positive |
| 58 | + * weight factor for each col/row, usually one want to take values in the range 0...100 to ease recognition by a human |
| 59 | + * being in percentages. |
| 60 | + */ |
| 61 | + public final Point weight = new Point(0, 0); |
| 62 | + |
| 63 | + /** |
| 64 | + * Height hint used to determine the preferred size of the component |
| 65 | + */ |
| 66 | + public int hHint = SWT.DEFAULT; |
| 67 | + |
| 68 | + /** |
| 69 | + * Width hint used to determine the preferred size of the component |
| 70 | + */ |
| 71 | + public int wHint = SWT.DEFAULT; |
| 72 | + |
| 73 | + /** |
| 74 | + * Controls how/if the control should fill: |
| 75 | + * <ul> |
| 76 | + * <li>{@link SWT#NONE} - the component does not fill (default)</li> |
| 77 | + * <li>{@link SWT#HORIZONTAL} - the component is filled horizontal</li> |
| 78 | + * <li>{@link SWT#VERTICAL} - the component fills vertical</li> |
| 79 | + * <li>{@link SWT#FILL} - the component is filled in both directions</li> |
| 80 | + * </ul> |
| 81 | + */ |
| 82 | + public int fill = SWT.NONE; |
| 83 | + |
| 84 | + static GridBag getGridBag(Composite composite, boolean changed) { |
| 85 | + if(!changed) { |
| 86 | + Object data = composite.getData(DATA_KEY); |
| 87 | + if(data instanceof GridBag) { |
| 88 | + return (GridBag) data; |
| 89 | + } |
| 90 | + } |
| 91 | + Control[] children = composite.getChildren(); |
| 92 | + GridBagConstraints[] contrains = new GridBagConstraints[children.length]; |
| 93 | + GridBag grid = new GridBag(); |
| 94 | + for(int i = 0; i < children.length; i++ ) { |
| 95 | + Control control = children[i]; |
| 96 | + GridBagConstraints c = contrains[i] = getConstraints(control); |
| 97 | + int x = c.grid.x; |
| 98 | + int y = c.grid.y; |
| 99 | + if(x == GridBagConstraints.RELATIVE) { |
| 100 | + //TODO |
| 101 | + } |
| 102 | + if(y == GridBagConstraints.RELATIVE) { |
| 103 | + //TODO |
| 104 | + } |
| 105 | + //TODO calculate the span if it is RELATIVE or REMAINDER! |
| 106 | + //For this to work, we maybe need to move this to the second loop! |
| 107 | + //probably better handle this in the paint loop?! |
| 108 | + int spanx = c.span.x; |
| 109 | + int spany = c.span.y; |
| 110 | + int xd = x + spanx; |
| 111 | + if(xd > grid.dimension.x) { |
| 112 | + grid.dimension.x = xd; |
| 113 | + } |
| 114 | + int yd = y + spany; |
| 115 | + if(yd > grid.dimension.y) { |
| 116 | + grid.dimension.y = yd; |
| 117 | + } |
| 118 | + grid.positionMap.put(control, new Point(x, y)); |
| 119 | + grid.constraintMap.put(control, c); |
| 120 | + } |
| 121 | + grid.controls = new Control[grid.dimension.x][grid.dimension.y]; |
| 122 | + grid.cols = new int[grid.dimension.x]; |
| 123 | + grid.rows = new int[grid.dimension.y]; |
| 124 | + grid.rowWeights = new double[grid.rows.length]; |
| 125 | + grid.colWeights = new double[grid.cols.length]; |
| 126 | + for(int i = 0; i < children.length; i++ ) { |
| 127 | + Control control = children[i]; |
| 128 | + GridBagConstraints c = contrains[i]; |
| 129 | + Point size = control.computeSize(c.wHint, c.hHint, changed); |
| 130 | + grid.sizeMap.put(control, size); |
| 131 | + Point pos = grid.positionMap.get(control); |
| 132 | + int x = pos.x; |
| 133 | + int y = pos.y; |
| 134 | + int w = size.x / c.span.x; |
| 135 | + int h = size.y / c.span.y; |
| 136 | + grid.controls[x][y] = control; |
| 137 | + for(int gi = x; gi < x + c.span.x; gi++ ) { |
| 138 | + if(w > grid.cols[gi]) { |
| 139 | + grid.cols[gi] = w; |
| 140 | + } |
| 141 | + } |
| 142 | + for(int gi = y; gi < y + c.span.y; gi++ ) { |
| 143 | + if(h > grid.rows[gi]) { |
| 144 | + grid.rows[gi] = h; |
| 145 | + } |
| 146 | + } |
| 147 | + double rw = (double) c.weight.x / c.span.x; |
| 148 | + double cw = (double) c.weight.y / c.span.y; |
| 149 | + for(int gi = x; gi < x + c.span.x; gi++ ) { |
| 150 | + if(rw > grid.colWeights[gi]) { |
| 151 | + grid.colWeights[gi] = rw; |
| 152 | + } |
| 153 | + } |
| 154 | + for(int gi = y; gi < y + c.span.y; gi++ ) { |
| 155 | + if(cw > grid.rowWeights[gi]) { |
| 156 | + grid.rowWeights[gi] = cw; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + composite.setData(DATA_KEY, grid); |
| 161 | + return grid; |
| 162 | + } |
| 163 | + |
| 164 | + static GridBagConstraints getConstraints(Control control) { |
| 165 | + Object layoutData = control.getLayoutData(); |
| 166 | + if(layoutData instanceof GridBagConstraints) { |
| 167 | + return (GridBagConstraints) layoutData; |
| 168 | + } |
| 169 | + return DEFAULT; |
| 170 | + } |
| 171 | + |
| 172 | + static final class GridBag { |
| 173 | + |
| 174 | + public int[] cols; |
| 175 | + |
| 176 | + int[] rows; |
| 177 | + |
| 178 | + double[] rowWeights; |
| 179 | + |
| 180 | + double[] colWeights; |
| 181 | + |
| 182 | + Point dimension = new Point(0, 0); |
| 183 | + |
| 184 | + Map<Control, Point> positionMap = new HashMap<>(); |
| 185 | + |
| 186 | + Map<Control, GridBagConstraints> constraintMap = new HashMap<>(); |
| 187 | + |
| 188 | + Map<Control, Point> sizeMap = new HashMap<>(); |
| 189 | + |
| 190 | + Control[][] controls; |
| 191 | + |
| 192 | + Point getSize(Control control) { |
| 193 | + return sizeMap.get(control); |
| 194 | + } |
| 195 | + |
| 196 | +// int getColumnWidth(int column) { |
| 197 | +// return cols[column]; |
| 198 | +// } |
| 199 | +// |
| 200 | +// int getRowHeight(int row) { |
| 201 | +// return rows[row]; |
| 202 | +// } |
| 203 | + |
| 204 | + public int[] getRowHeights(int height) { |
| 205 | + if(rows.length == 0) { |
| 206 | + return new int[0]; |
| 207 | + } |
| 208 | + int[] is = new int[rows.length]; |
| 209 | + double weights = 0; |
| 210 | + for(int i = 0; i < is.length; i++ ) { |
| 211 | + weights += rowWeights[i]; |
| 212 | + } |
| 213 | + double dx = height / weights; |
| 214 | + int sum = 0; |
| 215 | + for(int i = 0; i < is.length; i++ ) { |
| 216 | + sum += is[i] += Math.rint(dx * rowWeights[i]); |
| 217 | + } |
| 218 | + //any rounding error go to the last column |
| 219 | + is[is.length - 1] += height - sum; |
| 220 | + return is; |
| 221 | + } |
| 222 | + |
| 223 | + public int[] getColumnWidths(int width) { |
| 224 | + if(cols.length == 0) { |
| 225 | + return new int[0]; |
| 226 | + } |
| 227 | + int[] is = new int[cols.length]; |
| 228 | + double weights = 0; |
| 229 | + for(int i = 0; i < is.length; i++ ) { |
| 230 | + weights += colWeights[i]; |
| 231 | + } |
| 232 | + double dx = width / weights; |
| 233 | + int sum = 0; |
| 234 | + for(int i = 0; i < is.length; i++ ) { |
| 235 | + sum += is[i] += Math.rint(dx * colWeights[i]); |
| 236 | + } |
| 237 | + //any rounding error go to the last column |
| 238 | + is[is.length - 1] += width - sum; |
| 239 | + return is; |
| 240 | + } |
| 241 | + |
| 242 | + } |
| 243 | +} |
0 commit comments