001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.PaintEvent;
014: import org.eclipse.swt.events.PaintListener;
015: import org.eclipse.swt.graphics.Color;
016: import org.eclipse.swt.graphics.GC;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.graphics.RGB;
019: import org.eclipse.swt.graphics.Rectangle;
020: import org.eclipse.swt.widgets.Canvas;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Control;
023: import org.eclipse.swt.widgets.CoolBar;
024: import org.eclipse.swt.widgets.Layout;
025: import org.eclipse.swt.widgets.ToolBar;
026: import org.eclipse.ui.themes.ColorUtil;
027:
028: /**
029: * Draws a styled frame around its contained controls.
030: * This class is intended to be used to wrap various
031: * trim elements that appear in the workbench.
032: *
033: * Currently this class expects a <b>single</b> child
034: * control.
035: *
036: * @since 3.3
037: *
038: */
039: public class TrimFrame {
040: private static int blend = 40;
041:
042: Canvas canvas = null;
043:
044: public TrimFrame(Composite parent) {
045: createControl(parent);
046: }
047:
048: private void createControl(Composite parent) {
049: dispose();
050: canvas = new Canvas(parent, SWT.NONE);
051: canvas.setBackground(parent.getDisplay().getSystemColor(
052: SWT.COLOR_WIDGET_BACKGROUND));
053:
054: // paint the border
055: canvas.addPaintListener(new PaintListener() {
056: private void drawLine(GC gc, int x1, int y1, int x2,
057: int y2, boolean flipXY) {
058: if (flipXY) {
059: int tmp = x1;
060: x1 = y1;
061: y1 = tmp;
062: tmp = x2;
063: x2 = y2;
064: y2 = tmp;
065: }
066:
067: gc.drawLine(x1, y1, x2, y2);
068: }
069:
070: public void paintControl(PaintEvent e) {
071: Canvas canvas = (Canvas) e.widget;
072: Control child = canvas.getChildren()[0];
073:
074: // Are we horizontally or vertically aligned
075: boolean flipXY = false;
076: if (child instanceof ToolBar
077: && (((ToolBar) child).getStyle() & SWT.VERTICAL) != 0)
078: flipXY = true;
079: else if (child instanceof CoolBar
080: && (((CoolBar) child).getStyle() & SWT.VERTICAL) != 0)
081: flipXY = true;
082:
083: Rectangle bb = canvas.getBounds();
084: int maxX = bb.width - 1;
085: int maxY = bb.height - 1;
086:
087: if (flipXY) {
088: int tmp = maxX;
089: maxX = maxY;
090: maxY = tmp;
091: }
092:
093: Color white = e.gc.getDevice().getSystemColor(
094: SWT.COLOR_WHITE);
095: Color shadow = e.gc.getDevice().getSystemColor(
096: SWT.COLOR_WIDGET_NORMAL_SHADOW);
097: RGB outerRGB = ColorUtil.blend(white.getRGB(), shadow
098: .getRGB(), blend);
099: Color outerColor = new Color(e.gc.getDevice(), outerRGB);
100:
101: // Draw the 'outer' bits
102: e.gc.setForeground(outerColor);
103:
104: // Top Line and curve
105: drawLine(e.gc, 1, 0, maxX - 5, 0, flipXY);
106: drawLine(e.gc, maxX - 4, 1, maxX - 3, 1, flipXY);
107: drawLine(e.gc, maxX - 2, 2, maxX - 2, 2, flipXY);
108: drawLine(e.gc, maxX - 1, 3, maxX - 1, 4, flipXY);
109:
110: // Bottom line and curve
111: drawLine(e.gc, 1, maxY, maxX - 5, maxY, flipXY);
112: drawLine(e.gc, maxX - 4, maxY - 1, maxX - 3, maxY - 1,
113: flipXY);
114: drawLine(e.gc, maxX - 2, maxY - 2, maxX - 2, maxY - 2,
115: flipXY);
116: drawLine(e.gc, maxX - 1, maxY - 3, maxX - 1, maxY - 4,
117: flipXY);
118:
119: // Left & Right edges
120: drawLine(e.gc, 0, 1, 0, maxY - 1, flipXY);
121: drawLine(e.gc, maxX, 5, maxX, maxY - 5, flipXY);
122:
123: // Dispose the color since we created it...
124: outerColor.dispose();
125:
126: // Draw the 'inner' curve
127: e.gc.setForeground(white);
128:
129: drawLine(e.gc, 1, 1, maxX - 5, 1, flipXY);
130: drawLine(e.gc, maxX - 4, 2, maxX - 3, 2, flipXY);
131: drawLine(e.gc, maxX - 3, 3, maxX - 2, 3, flipXY);
132: drawLine(e.gc, maxX - 2, 4, maxX - 2, 4, flipXY);
133:
134: drawLine(e.gc, 1, maxY - 1, maxX - 5, maxY - 1, flipXY);
135: drawLine(e.gc, maxX - 4, maxY - 2, maxX - 3, maxY - 2,
136: flipXY);
137: drawLine(e.gc, maxX - 3, maxY - 3, maxX - 2, maxY - 3,
138: flipXY);
139: drawLine(e.gc, maxX - 2, maxY - 4, maxX - 2, maxY - 4,
140: flipXY);
141:
142: // Left and Right sides
143: drawLine(e.gc, 1, 1, 1, maxY - 1, flipXY);
144: drawLine(e.gc, maxX - 1, 5, maxX - 1, maxY - 5, flipXY);
145: }
146: });
147:
148: // provide a layout that provides enough extra space to
149: // draw the border and to place the child conrol in the
150: // correct location
151: canvas.setLayout(new Layout() {
152:
153: protected Point computeSize(Composite composite, int wHint,
154: int hHint, boolean changed) {
155: Control[] children = composite.getChildren();
156:
157: if (children.length == 0)
158: return new Point(0, 0);
159:
160: Point innerSize = children[0].computeSize(hHint, wHint,
161: changed);
162: innerSize.x += 4;
163: innerSize.y += 4;
164:
165: Control child = children[0];
166: if (child instanceof CoolBar
167: && (((CoolBar) child).getStyle() & SWT.VERTICAL) != 0)
168: innerSize.y += 3;
169: else
170: innerSize.x += 3;
171:
172: return innerSize;
173: }
174:
175: protected void layout(Composite composite,
176: boolean flushCache) {
177: Control[] children = composite.getChildren();
178: if (children.length == 0)
179: return;
180:
181: children[0].setLocation(2, 2);
182: }
183: });
184: }
185:
186: /**
187: * Dispose the frame
188: */
189: private void dispose() {
190: if (canvas != null && !canvas.isDisposed())
191: canvas.dispose();
192: }
193:
194: /**
195: * @return The border canvas
196: */
197: public Composite getComposite() {
198: return canvas;
199: }
200: }
|