|
1 |
| -/******************************************************************************* |
2 |
| - * Copyright (c) 2000, 2025 IBM Corporation and others. |
3 |
| - * |
4 |
| - * This program and the accompanying materials |
5 |
| - * are made available under the terms of the Eclipse Public License 2.0 |
6 |
| - * which accompanies this distribution, and is available at |
7 |
| - * https://www.eclipse.org/legal/epl-2.0/ |
8 |
| - * |
9 |
| - * SPDX-License-Identifier: EPL-2.0 |
10 |
| - * |
11 |
| - * Contributors: |
12 |
| - * IBM Corporation - initial API and implementation |
13 |
| - *******************************************************************************/ |
14 |
| -package org.eclipse.swt.widgets.toolbar; |
15 |
| - |
16 |
| -import java.util.*; |
17 |
| - |
18 |
| -import org.eclipse.swt.graphics.*; |
19 |
| - |
20 |
| -public class ToolBarLayout { |
21 |
| - |
22 |
| - /** |
23 |
| - * A record of an item and layouting relevant data. |
24 |
| - */ |
25 |
| - public static record ItemRecord(int index, Rectangle bounds, boolean isSeprator) { |
26 |
| - } |
27 |
| - |
28 |
| - /** |
29 |
| - * Layout class for placeing items into a row. NOTE: While this class is called |
30 |
| - * {@link Row}, it is also used for column rendering. Some of the field names |
31 |
| - * make sense for a row but not for a column. |
32 |
| - */ |
33 |
| - public static class Row { |
34 |
| - private static final int NONE = -1; |
35 |
| - |
36 |
| - final List<ItemRecord> items = new ArrayList<>(); |
37 |
| - /** The amount of pixels the row has space for items. */ |
38 |
| - final Point availableSpace; |
39 |
| - |
40 |
| - final boolean horizontal; |
41 |
| - |
42 |
| - /** The start position of the first item. */ |
43 |
| - Point usedSpace; |
44 |
| - |
45 |
| - /** The position of the row. */ |
46 |
| - int position; |
47 |
| - |
48 |
| - /** Indicates if this row contains a separator. */ |
49 |
| - boolean containsSeparator; |
50 |
| - |
51 |
| - /** Indicates if this row has a row separator. */ |
52 |
| - boolean hasRowSeparator; |
53 |
| - |
54 |
| - public Row(Point availableSpace, int offset, boolean horizontal) { |
55 |
| - this.availableSpace = availableSpace; |
56 |
| - this.usedSpace = horizontal ? new Point(offset, 0) : new Point(0, offset); |
57 |
| - this.horizontal = horizontal; |
58 |
| - } |
59 |
| - |
60 |
| - /** |
61 |
| - * Checks if the row has enough space for the given item. |
62 |
| - */ |
63 |
| - public boolean hasSpaceFor(ItemRecord itemRecord) { |
64 |
| - if (horizontal) { |
65 |
| - if (availableSpace.x == NONE) { |
66 |
| - return true; |
67 |
| - } |
68 |
| - int freeSpace = availableSpace.x - usedSpace.x; |
69 |
| - return freeSpace >= itemRecord.bounds.width; |
70 |
| - } else { |
71 |
| - return true; |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - /** |
76 |
| - * Adds the given item to the row layout. |
77 |
| - * |
78 |
| - * @param itemRecord |
79 |
| - */ |
80 |
| - public void add(ItemRecord itemRecord) { |
81 |
| - items.add(itemRecord); |
82 |
| - containsSeparator |= itemRecord.isSeprator; |
83 |
| - if (horizontal) { |
84 |
| - itemRecord.bounds.x = usedSpace.x; |
85 |
| - usedSpace.x += itemRecord.bounds().width + 1; |
86 |
| - usedSpace.y = Math.max(usedSpace.y, itemRecord.bounds.height); |
87 |
| - } else { |
88 |
| - itemRecord.bounds.y = usedSpace.y; |
89 |
| - usedSpace.y += itemRecord.bounds().height; |
90 |
| - usedSpace.x = Math.max(usedSpace.x, itemRecord.bounds.width); |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - /** |
95 |
| - * Increases the height of all items to the height of the item with the biggest |
96 |
| - * height. |
97 |
| - */ |
98 |
| - void normalizeHeight() { |
99 |
| - int maxPossibleSize = Math.min(usedSpace.y, availableSpace.y); |
100 |
| - for (ItemRecord itemRecord : items) { |
101 |
| - itemRecord.bounds.height = maxPossibleSize; |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - /** |
106 |
| - * Increases the width of all items to the height of the item with the biggest |
107 |
| - * width. |
108 |
| - */ |
109 |
| - void normalizeWidth() { |
110 |
| - int maxPossibleSize = Math.min(usedSpace.x, availableSpace.x); |
111 |
| - for (ItemRecord itemRecord : items) { |
112 |
| - itemRecord.bounds.width = maxPossibleSize; |
113 |
| - } |
114 |
| - } |
115 |
| - |
116 |
| - /** |
117 |
| - * Sets the y position of all items to the given value. |
118 |
| - */ |
119 |
| - void setY(int y) { |
120 |
| - position = y; |
121 |
| - for (ItemRecord itemRecord : items) { |
122 |
| - itemRecord.bounds.y = y; |
123 |
| - } |
124 |
| - } |
125 |
| - |
126 |
| - /** |
127 |
| - * Sets the y position of all items to the given value. |
128 |
| - */ |
129 |
| - void setX(int x) { |
130 |
| - for (ItemRecord itemRecord : items) { |
131 |
| - itemRecord.bounds.x = x; |
132 |
| - } |
133 |
| - } |
134 |
| - |
135 |
| - /** |
136 |
| - * Mirrors the layout vertically. |
137 |
| - */ |
138 |
| - void mirrow() { |
139 |
| - for (ItemRecord itemRecord : items) { |
140 |
| - itemRecord.bounds.x = availableSpace.x - itemRecord.bounds.x - itemRecord.bounds.width; |
141 |
| - } |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - private final List<Row> rows; |
146 |
| - private final Point size; |
147 |
| - |
148 |
| - public ToolBarLayout(List<Row> rows, Point size) { |
149 |
| - this.rows = rows; |
150 |
| - this.size = size; |
151 |
| - } |
152 |
| - |
153 |
| - public List<Row> rows() { |
154 |
| - return rows; |
155 |
| - } |
156 |
| - |
157 |
| - public Point size() { |
158 |
| - return size; |
159 |
| - } |
160 |
| - |
161 |
| -} |
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2000, 2025 IBM Corporation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.swt.widgets.toolbar; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +import org.eclipse.swt.graphics.*; |
| 19 | + |
| 20 | +public class ToolBarLayout { |
| 21 | + |
| 22 | + /** |
| 23 | + * A record of an item and layouting relevant data. |
| 24 | + */ |
| 25 | + public static record ItemRecord(int index, Rectangle bounds, boolean isSeprator) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Layout class for placeing items into a row. NOTE: While this class is called |
| 30 | + * {@link Row}, it is also used for column rendering. Some of the field names |
| 31 | + * make sense for a row but not for a column. |
| 32 | + */ |
| 33 | + public static class Row { |
| 34 | + private static final int NONE = -1; |
| 35 | + |
| 36 | + final List<ItemRecord> items = new ArrayList<>(); |
| 37 | + /** The amount of pixels the row has space for items. */ |
| 38 | + final Point availableSpace; |
| 39 | + |
| 40 | + final boolean horizontal; |
| 41 | + |
| 42 | + /** The start position of the first item. */ |
| 43 | + Point usedSpace; |
| 44 | + |
| 45 | + /** The position of the row. */ |
| 46 | + int position; |
| 47 | + |
| 48 | + /** Indicates if this row contains a separator. */ |
| 49 | + boolean containsSeparator; |
| 50 | + |
| 51 | + /** Indicates if this row has a row separator. */ |
| 52 | + boolean hasRowSeparator; |
| 53 | + |
| 54 | + public Row(Point availableSpace, int offset, boolean horizontal) { |
| 55 | + this.availableSpace = availableSpace; |
| 56 | + this.usedSpace = horizontal ? new Point(offset, 0) : new Point(0, offset); |
| 57 | + this.horizontal = horizontal; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Checks if the row has enough space for the given item. |
| 62 | + */ |
| 63 | + public boolean hasSpaceFor(ItemRecord itemRecord) { |
| 64 | + if (horizontal) { |
| 65 | + if (availableSpace.x == NONE) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + int freeSpace = availableSpace.x - usedSpace.x; |
| 69 | + return freeSpace >= itemRecord.bounds.width; |
| 70 | + } else { |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Adds the given item to the row layout. |
| 77 | + * |
| 78 | + * @param itemRecord |
| 79 | + */ |
| 80 | + public void add(ItemRecord itemRecord) { |
| 81 | + items.add(itemRecord); |
| 82 | + containsSeparator |= itemRecord.isSeprator; |
| 83 | + if (horizontal) { |
| 84 | + itemRecord.bounds.x = usedSpace.x; |
| 85 | + usedSpace.x += itemRecord.bounds().width + 1; |
| 86 | + usedSpace.y = Math.max(usedSpace.y, itemRecord.bounds.height); |
| 87 | + } else { |
| 88 | + itemRecord.bounds.y = usedSpace.y; |
| 89 | + usedSpace.y += itemRecord.bounds().height; |
| 90 | + usedSpace.x = Math.max(usedSpace.x, itemRecord.bounds.width); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Increases the height of all items to the height of the item with the biggest |
| 96 | + * height. |
| 97 | + */ |
| 98 | + void normalizeHeight() { |
| 99 | + int maxPossibleSize = Math.min(usedSpace.y, availableSpace.y); |
| 100 | + for (ItemRecord itemRecord : items) { |
| 101 | + itemRecord.bounds.height = maxPossibleSize; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Increases the width of all items to the height of the item with the biggest |
| 107 | + * width. |
| 108 | + */ |
| 109 | + void normalizeWidth() { |
| 110 | + int maxPossibleSize = Math.min(usedSpace.x, availableSpace.x); |
| 111 | + for (ItemRecord itemRecord : items) { |
| 112 | + itemRecord.bounds.width = maxPossibleSize; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets the y position of all items to the given value. |
| 118 | + */ |
| 119 | + void setY(int y) { |
| 120 | + position = y; |
| 121 | + for (ItemRecord itemRecord : items) { |
| 122 | + itemRecord.bounds.y = y; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets the y position of all items to the given value. |
| 128 | + */ |
| 129 | + void setX(int x) { |
| 130 | + for (ItemRecord itemRecord : items) { |
| 131 | + itemRecord.bounds.x = x; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Mirrors the layout vertically. |
| 137 | + */ |
| 138 | + void mirrow() { |
| 139 | + for (ItemRecord itemRecord : items) { |
| 140 | + itemRecord.bounds.x = availableSpace.x - itemRecord.bounds.x - itemRecord.bounds.width; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private final List<Row> rows; |
| 146 | + private final Point size; |
| 147 | + |
| 148 | + public ToolBarLayout(List<Row> rows, Point size) { |
| 149 | + this.rows = rows; |
| 150 | + this.size = size; |
| 151 | + } |
| 152 | + |
| 153 | + public List<Row> rows() { |
| 154 | + return rows; |
| 155 | + } |
| 156 | + |
| 157 | + public Point size() { |
| 158 | + return size; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments