|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.internal.presentations; |
| 12 |
|
| 13 |
import org.eclipse.jface.util.Geometry; |
| 14 |
import org.eclipse.swt.SWT; |
| 15 |
import org.eclipse.swt.custom.CTabFolder; |
| 16 |
import org.eclipse.swt.custom.CTabItem; |
| 17 |
import org.eclipse.swt.graphics.Point; |
| 18 |
import org.eclipse.swt.graphics.Rectangle; |
| 19 |
import org.eclipse.swt.widgets.Control; |
| 20 |
|
| 21 |
/** |
| 22 |
* Arranges the contents of a CTabFolder |
| 23 |
* |
| 24 |
* @since 3.0 |
| 25 |
*/ |
| 26 |
public class TabFolderLayout { |
| 27 |
private CTabFolder tabFolder; |
| 28 |
private Control[] topControls; |
| 29 |
//private Control upperRightProxy; |
| 30 |
private LayoutCache cache; |
| 31 |
private Rectangle centerArea = new Rectangle(0,0,0,0); |
| 32 |
private int trimStart; |
| 33 |
|
| 34 |
public TabFolderLayout(CTabFolder folder) { |
| 35 |
this.tabFolder = folder; |
| 36 |
|
| 37 |
// Composite cmp = new Composite(folder, SWT.DEFAULT); |
| 38 |
// upperRightProxy = cmp; |
| 39 |
// upperRightProxy.setVisible(false); |
| 40 |
// |
| 41 |
// cmp.setLayout(new Layout() { |
| 42 |
// |
| 43 |
// protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { |
| 44 |
// return new Point(10,10); |
| 45 |
// } |
| 46 |
// |
| 47 |
// protected void layout(Composite composite, boolean flushCache) { |
| 48 |
// |
| 49 |
// } |
| 50 |
// }); |
| 51 |
|
| 52 |
//tabFolder.setTopRight(upperRightProxy); |
| 53 |
setTopRight(new Control[0]); |
| 54 |
} |
| 55 |
|
| 56 |
public void dispose() { |
| 57 |
//tabFolder.setTopRight(null); |
| 58 |
//upperRightProxy.dispose(); |
| 59 |
} |
| 60 |
|
| 61 |
public Rectangle getClientBounds() { |
| 62 |
return centerArea; |
| 63 |
} |
| 64 |
|
| 65 |
public int getTrimStart() { |
| 66 |
return trimStart; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Sets the list of controls to be placed at the top-right of the CTabFolder |
| 71 |
* |
| 72 |
* @param topRight a list of Control |
| 73 |
*/ |
| 74 |
public void setTopRight(Control[] upperRight) { |
| 75 |
topControls = upperRight; |
| 76 |
|
| 77 |
cache = new LayoutCache(upperRight); |
| 78 |
layout(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Arranges all the controls in this layout |
| 83 |
*/ |
| 84 |
public void layout() { |
| 85 |
cache.flush(); |
| 86 |
|
| 87 |
if (topControls.length == 0) { |
| 88 |
tabFolder.setTopRight(null); |
| 89 |
} |
| 90 |
|
| 91 |
// DragUtil.getDisplayBounds(tabFolder); |
| 92 |
// |
| 93 |
// displayBounds.height = tabFolder.getTabHeight(); |
| 94 |
// displayBounds.x = displayBounds.x + displayBounds.width - 30; |
| 95 |
// displayBounds.width = 30; |
| 96 |
|
| 97 |
int trimWidth = computeTrimWidth(); |
| 98 |
int available = getAvailableSpace(tabFolder); |
| 99 |
|
| 100 |
Rectangle clientBounds = calculatePageBounds(tabFolder); |
| 101 |
|
| 102 |
Rectangle bounds = tabFolder.getBounds(); |
| 103 |
trimStart = bounds.x + bounds.width; |
| 104 |
|
| 105 |
// Check if we have room for all our topRight controls on the top border |
| 106 |
if (available >= trimWidth) { |
| 107 |
//upperRightProxy.setSize(0, 0); |
| 108 |
//tabFolder.setTopRight(upperRightProxy); |
| 109 |
|
| 110 |
Rectangle displayBounds = //DragUtil.getDisplayBounds(upperRightProxy); |
| 111 |
Geometry.toDisplay(tabFolder, tabFolder.getMinimizeBounds()); |
| 112 |
|
| 113 |
//displayBounds.height = tabFolder.getTabHeight(); |
| 114 |
displayBounds.width = 0; |
| 115 |
displayBounds.y --; |
| 116 |
displayBounds.height += 2; |
| 117 |
Rectangle targetBounds = Geometry.toControl(tabFolder.getParent(), displayBounds); |
| 118 |
|
| 119 |
targetBounds.x = targetBounds.x + targetBounds.width - trimWidth; |
| 120 |
|
| 121 |
Rectangle currentRectangle = new Rectangle(targetBounds.x, targetBounds.y, 0, targetBounds.height); |
| 122 |
|
| 123 |
trimStart = targetBounds.x; |
| 124 |
|
| 125 |
for (int idx = 0; idx < topControls.length; idx++) { |
| 126 |
Point nextSize = cache.computeSize(idx, SWT.DEFAULT, SWT.DEFAULT); |
| 127 |
|
| 128 |
currentRectangle.width = nextSize.x; |
| 129 |
//currentRectangle.height = nextSize.y; |
| 130 |
|
| 131 |
//topControls[idx].moveAbove(null); |
| 132 |
topControls[idx].setBounds(currentRectangle); |
| 133 |
|
| 134 |
currentRectangle.x += currentRectangle.width; |
| 135 |
} |
| 136 |
|
| 137 |
centerArea = Geometry.copy(clientBounds); |
| 138 |
|
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
//tabFolder.setTopRight(null); |
| 143 |
|
| 144 |
// Else we need to place the controls below the title |
| 145 |
int y = clientBounds.y; |
| 146 |
int x = clientBounds.x + clientBounds.width; |
| 147 |
|
| 148 |
int rowHeight = 0; |
| 149 |
for (int idx = 0; idx < topControls.length; idx++) { |
| 150 |
Point nextSize = cache.computeSize(idx, SWT.DEFAULT, SWT.DEFAULT); |
| 151 |
|
| 152 |
if (nextSize.x < clientBounds.width) { |
| 153 |
topControls[idx].setBounds(x - nextSize.x, y, nextSize.x, nextSize.y); |
| 154 |
rowHeight = Math.max(rowHeight, nextSize.y); |
| 155 |
x -= nextSize.x; |
| 156 |
} else { |
| 157 |
y += rowHeight; |
| 158 |
x = clientBounds.x; |
| 159 |
rowHeight = 0; |
| 160 |
|
| 161 |
nextSize = cache.computeSize(idx, clientBounds.width, SWT.DEFAULT); |
| 162 |
topControls[idx].setBounds(x, y, nextSize.x, nextSize.y); |
| 163 |
|
| 164 |
y += nextSize.y; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
y += rowHeight; |
| 169 |
|
| 170 |
centerArea = new Rectangle(clientBounds.x, y, clientBounds.width, |
| 171 |
clientBounds.height + clientBounds.y - y); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Computes the maximium size available for the trim controls without causing |
| 176 |
* tabs to disappear. |
| 177 |
* |
| 178 |
* @param folder |
| 179 |
* @return the amount of empty space to the right of the tabs |
| 180 |
*/ |
| 181 |
protected static int getAvailableSpace(CTabFolder folder) { |
| 182 |
int available = folder.getBounds().width; |
| 183 |
|
| 184 |
available -= 2 * folder.getBorderWidth(); |
| 185 |
available -= folder.getChevronBounds().width; |
| 186 |
available -= folder.getMaximizeBounds().width; |
| 187 |
available -= folder.getMinimizeBounds().width; |
| 188 |
|
| 189 |
// Add a safety margin to avoid stomping on the curve |
| 190 |
available -= 10; |
| 191 |
|
| 192 |
CTabItem[] tabs = folder.getItems(); |
| 193 |
for (int idx = 0; idx < tabs.length; idx++) { |
| 194 |
CTabItem item = tabs[idx]; |
| 195 |
|
| 196 |
if (!item.isShowing()) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
available -= item.getBounds().width; |
| 201 |
} |
| 202 |
|
| 203 |
return Math.max(0, available); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the total preferred width of the top controls |
| 208 |
* |
| 209 |
* @return the total preferred width of the top controls |
| 210 |
*/ |
| 211 |
protected int computeTrimWidth() { |
| 212 |
int width = 0; |
| 213 |
for (int idx = 0; idx < topControls.length; idx++) { |
| 214 |
width += cache.computeSize(idx, SWT.DEFAULT, SWT.DEFAULT).x; |
| 215 |
} |
| 216 |
|
| 217 |
return width; |
| 218 |
} |
| 219 |
|
| 220 |
private static Rectangle calculatePageBounds(CTabFolder folder) { |
| 221 |
if (folder == null) |
| 222 |
return new Rectangle(0, 0, 0, 0); |
| 223 |
Rectangle bounds = folder.getBounds(); |
| 224 |
Rectangle offset = folder.getClientArea(); |
| 225 |
bounds.x += offset.x; |
| 226 |
bounds.y += offset.y; |
| 227 |
bounds.width = offset.width; |
| 228 |
bounds.height = offset.height; |
| 229 |
return bounds; |
| 230 |
} |
| 231 |
|
| 232 |
} |