1 11 package org.eclipse.ui.internal.browser; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.Point; 15 import org.eclipse.swt.graphics.Rectangle; 16 import org.eclipse.swt.widgets.Combo; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Layout; 20 23 public class ToolbarLayout extends Layout { 24 private static final int SPACING = 5; 25 private static final int EXTRA_BUSY_SPACING = 2; 26 private static final int MARGIN = 2; 27 28 31 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { 32 if (hHint != SWT.DEFAULT) 33 return new Point(wHint, hHint); 34 35 Control[] children = composite.getChildren(); 36 int h = 0; 37 int size = children.length; 38 for (int i = 0; i < size; i++) { 39 h = Math.max(h, children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT).y); 40 } 41 42 return new Point(wHint, h + MARGIN * 2); 43 } 44 45 48 protected void layout(Composite composite, boolean flushCache) { 49 Control[] children = composite.getChildren(); 50 Rectangle r = composite.getClientArea(); 51 52 int size = children.length; 53 Point[] sizes = new Point[size]; 54 for (int i = 0; i < size; i++) { 55 sizes[i] = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT); 56 } 57 58 int h = r.height - MARGIN * 2; 59 60 int busy = size - 1; 62 children[busy].setBounds(r.x + r.width - MARGIN - sizes[busy].x, 63 r.y + MARGIN + (h-sizes[busy].y) / 2, 64 sizes[busy].x, sizes[busy].y); 65 66 int combo = -1; 68 int tw = r.width - MARGIN * 2 - (size - 1) * SPACING 69 - sizes[size-1].x - EXTRA_BUSY_SPACING; 70 for (int i = 0; i < size - 1; i++) { 71 if (children[i] instanceof Combo) 72 combo = i; 73 else 74 tw -= sizes[i].x; 75 } 76 if (combo >= 0) 77 sizes[combo].x = tw; 78 79 int x = MARGIN; 82 for (int i = 0; i < size - 1; i++) { 83 children[i].setBounds(r.x + x, r.y + MARGIN + (h-sizes[i].y) / 2, 84 sizes[i].x, sizes[i].y); 85 x += SPACING + sizes[i].x; 86 } 87 } 88 } | Popular Tags |