001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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.layout;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.graphics.Point;
014: import org.eclipse.swt.layout.GridData;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.swt.widgets.Control;
017: import org.eclipse.swt.widgets.Shell;
018:
019: /**
020: * Contains helper methods for CellLayout. Many of these methods are workarounds for
021: * known SWT bugs. They should be updated once the original bugs are fixed in SWT.
022: *
023: * @since 3.0
024: */
025: class CellLayoutUtil {
026:
027: private static Point zero = new Point(0, 0);
028:
029: private static Point minimumShellSize;
030:
031: private static CellData defaultData = new CellData();
032:
033: /**
034: * Returns the minimum size for the given composite. That is,
035: * this returns the smallest values that will have any effect
036: * when passed into the composite's setSize method. Passing any
037: * smaller value is equivalent to passing the minimum size.
038: * <p>
039: * This method is intended for use by layouts. The layout can
040: * use this information when determining its preferred size.
041: * Returning a preferred size smaller than the composite's
042: * minimum size is pointless since the composite could never
043: * be set to that size. The layout may choose a different preferred
044: * size in this situation.
045: * </p><p>
046: * Note that this method is only concerned with restrictions imposed
047: * by the composite; not it's layout. If the only restriction on the
048: * composite's size is imposed by the layout, then this method returns (0,0).
049: * </p><p>
050: * Currently SWT does not expose this information through
051: * API, so this method is developed using trial-and-error. Whenever
052: * a composite is discovered that will not accept sizes below
053: * a certain threshold on some platform, this method should be
054: * updated to reflect that fact.
055: * </p><p>
056: * At this time, the only known composite that has a minimum size
057: * are Shells.
058: * </p>
059: *
060: * @param toCompute the composite whose minimum size is being computed
061: * @return a size, in pixels, which is the smallest value that can be
062: * passed into the composite's setSize(...) method.
063: */
064: static Point computeMinimumSize(Composite toCompute) {
065: if (toCompute instanceof Shell) {
066: if (minimumShellSize == null) {
067: Shell testShell = new Shell((Shell) toCompute,
068: SWT.DIALOG_TRIM | SWT.RESIZE);
069: testShell.setSize(0, 0);
070: minimumShellSize = testShell.getSize();
071: testShell.dispose();
072: }
073:
074: return minimumShellSize;
075: }
076:
077: // If any other composites are discovered to have minimum sizes,
078: // add heuristics for them here.
079:
080: // Otherwise, the composite can be reduced to size (0,0)
081:
082: return zero;
083: }
084:
085: /**
086: * Returns the CellData associated with the given control. If the control
087: * does not have any layout data associated with it, a default object is returned.
088: * If the control has a GridData object associated with it, an equivalent
089: * CellData object will be returned.
090: *
091: * @param control
092: * @return
093: */
094: static CellData getData(Control control) {
095: Object layoutData = control.getLayoutData();
096: CellData data = null;
097:
098: if (layoutData instanceof CellData) {
099: data = (CellData) layoutData;
100: } else if (layoutData instanceof GridData) {
101: data = new CellData((GridData) layoutData);
102: }
103:
104: if (data == null) {
105: data = defaultData;
106: }
107:
108: return data;
109: }
110: }
|