01: /**********************************************************************
02: * Copyright (c) 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - Initial API and implementation
10: **********************************************************************/package org.eclipse.ui.internal.browser;
11:
12: import org.eclipse.swt.SWT;
13: import org.eclipse.swt.graphics.Point;
14: import org.eclipse.swt.graphics.Rectangle;
15: import org.eclipse.swt.widgets.Combo;
16: import org.eclipse.swt.widgets.Composite;
17: import org.eclipse.swt.widgets.Control;
18: import org.eclipse.swt.widgets.Layout;
19:
20: /**
21: * Custom layout for the browser's toolbar.
22: */
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: /* (non-Javadoc)
29: * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite, int, int, boolean)
30: */
31: protected Point computeSize(Composite composite, int wHint,
32: int hHint, boolean flushCache) {
33: if (hHint != SWT.DEFAULT)
34: return new Point(wHint, hHint);
35:
36: Control[] children = composite.getChildren();
37: int h = 0;
38: int size = children.length;
39: for (int i = 0; i < size; i++) {
40: h = Math.max(h, children[i].computeSize(SWT.DEFAULT,
41: SWT.DEFAULT).y);
42: }
43:
44: return new Point(wHint, h + MARGIN * 2);
45: }
46:
47: /* (non-Javadoc)
48: * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, boolean)
49: */
50: protected void layout(Composite composite, boolean flushCache) {
51: Control[] children = composite.getChildren();
52: Rectangle r = composite.getClientArea();
53:
54: int size = children.length;
55: Point[] sizes = new Point[size];
56: for (int i = 0; i < size; i++) {
57: sizes[i] = children[i]
58: .computeSize(SWT.DEFAULT, SWT.DEFAULT);
59: }
60:
61: int h = r.height - MARGIN * 2;
62:
63: // put busy icon at right with a little extra spacing
64: int busy = size - 1;
65: children[busy].setBounds(
66: r.x + r.width - MARGIN - sizes[busy].x, r.y + MARGIN
67: + (h - sizes[busy].y) / 2, sizes[busy].x,
68: sizes[busy].y);
69:
70: // find combo
71: int combo = -1;
72: int tw = r.width - MARGIN * 2 - (size - 1) * SPACING
73: - sizes[size - 1].x - EXTRA_BUSY_SPACING;
74: for (int i = 0; i < size - 1; i++) {
75: if (children[i] instanceof Combo)
76: combo = i;
77: else
78: tw -= sizes[i].x;
79: }
80: if (combo >= 0)
81: sizes[combo].x = tw;
82:
83: // space out other children with their standard size, give combo all
84: // remaining space (if it exists)
85: int x = MARGIN;
86: for (int i = 0; i < size - 1; i++) {
87: children[i].setBounds(r.x + x, r.y + MARGIN
88: + (h - sizes[i].y) / 2, sizes[i].x, sizes[i].y);
89: x += SPACING + sizes[i].x;
90: }
91: }
92: }
|