view org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/layout/FillLayout.d @ 120:536e43f63c81

Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661 ===D2=== * added [Try]Immutable/Const/Shared templates to work with differenses in D1/D2 instead of version statements used these templates to work with strict type storage rules of dmd-2.053 * com.ibm.icu now also compilable with D2, but not tested yet * small fixes Snippet288 - shared data is in TLS ===Phobos=== * fixed critical bugs in Phobos implemention completely incorrect segfault prone fromStringz (Linux's port ruthless killer) terrible, incorrect StringBuffer realization (StyledText killer) * fixed small bugs as well Snippet72 - misprint in the snippet * implemented missed functionality for Phobos ByteArrayOutputStream implemented (image loading available) formatting correctly works for all DWT's cases As a result, folowing snippets now works with Phobos (Snippet### - what is fixed): Snippet24, 42, 111, 115, 130, 235, 276 - bad string formatting Snippet48, 282 - crash on image loading Snippet163, 189, 211, 213, 217, 218, 222 - crash on copy/cut in StyledText Snippet244 - hang-up ===Tango=== * few changes for the latest Tango trunc-r5661 * few small performance improvments ===General=== * implMissing-s for only one version changed to implMissingInTango/InPhobos * incorrect calls to Format in toString-s fixed * fixed loading \uXXXX characters in ResourceBundle * added good UTF-8 support for StyledText, TextLayout (Win32) and friends UTF functions revised and tested. It is now in java.nonstandard.*Utf modules StyledText and TextLayout (Win32) modules revised for UTF-8 support * removed small diferences in most identical files in *.swt.* folders *.swt.internal.image, *.swt.events and *.swt.custom are identical in Win32/Linux32 now 179 of 576 (~31%) files in *.swt.* folders are fully identical * Win32: snippets now have right subsystem, pretty icons and native system style controls * small fixes in snippets Snippet44 - it's not Snippet44 Snippet212 - functions work with different images and offsets arrays Win32: Snippet282 - crash on close if the button has an image Snippet293 - setGrayed is commented and others Win32: As a result, folowing snippets now works Snippet68 - color doesn't change Snippet163, 189, 211, 213, 217, 218, 222 - UTF-8 issues (see above) Snippet193 - no tabel headers
author Denis Shelomovskij <verylonglogin.reg@gmail.com>
date Sat, 09 Jul 2011 15:50:20 +0300
parents c01d033c633a
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/
module org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.layout.FillData;

import java.lang.all;

/**
 * <code>FillLayout</code> is the simplest layout class. It lays out
 * controls in a single row or column, forcing them to be the same size.
 * <p>
 * Initially, the controls will all be as tall as the tallest control,
 * and as wide as the widest. <code>FillLayout</code> does not wrap,
 * but you can specify margins and spacing. You might use it to
 * lay out buttons in a task bar or tool bar, or to stack checkboxes
 * in a <code>Group</code>. <code>FillLayout</code> can also be used
 * when a <code>Composite</code> only has one child. For example,
 * if a <code>Shell</code> has a single <code>Group</code> child,
 * <code>FillLayout</code> will cause the <code>Group</code> to
 * completely fill the <code>Shell</code> (if margins are 0).
 * </p>
 * <p>
 * Example code: first a <code>FillLayout</code> is created and
 * its type field is set, and then the layout is set into the
 * <code>Composite</code>. Note that in a <code>FillLayout</code>,
 * children are always the same size, and they fill all available space.
 * <pre>
 *      FillLayout fillLayout = new FillLayout();
 *      fillLayout.type = SWT.VERTICAL;
 *      shell.setLayout(fillLayout);
 * </pre>
 * </p>
 *
 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a>
 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 */
public final class FillLayout : Layout {
    /**
     * type specifies how controls will be positioned
     * within the layout.
     *
     * The default value is HORIZONTAL.
     *
     * Possible values are: <ul>
     *    <li>HORIZONTAL: Position the controls horizontally from left to right</li>
     *    <li>VERTICAL: Position the controls vertically from top to bottom</li>
     * </ul>
     */
    public int type = SWT.HORIZONTAL;

    /**
     * marginWidth specifies the number of pixels of horizontal margin
     * that will be placed along the left and right edges of the layout.
     *
     * The default value is 0.
     *
     * @since 3.0
     */
    public int marginWidth = 0;

    /**
     * marginHeight specifies the number of pixels of vertical margin
     * that will be placed along the top and bottom edges of the layout.
     *
     * The default value is 0.
     *
     * @since 3.0
     */
    public int marginHeight = 0;

    /**
     * spacing specifies the number of pixels between the edge of one cell
     * and the edge of its neighbouring cell.
     *
     * The default value is 0.
     *
     * @since 3.0
     */
    public int spacing = 0;

/**
 * Constructs a new instance of this class.
 */
public this () {
}

/**
 * Constructs a new instance of this class given the type.
 *
 * @param type the type of fill layout
 *
 * @since 2.0
 */
public this (int type) {
    this.type = type;
}

override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) {
    Control [] children = composite.getChildren ();
    int count = children.length;
    int maxWidth = 0, maxHeight = 0;
    for (int i=0; i<count; i++) {
        Control child = children [i];
        int w = wHint, h = hHint;
        if (count > 0) {
            if (type is SWT.HORIZONTAL && wHint !is SWT.DEFAULT) {
                w = Math.max (0, (wHint - (count - 1) * spacing) / count);
            }
            if (type is SWT.VERTICAL && hHint !is SWT.DEFAULT) {
                h = Math.max (0, (hHint - (count - 1) * spacing) / count);
            }
        }
        Point size = computeChildSize (child, w, h, flushCache);
        maxWidth = Math.max (maxWidth, size.x);
        maxHeight = Math.max (maxHeight, size.y);
    }
    int width = 0, height = 0;
    if (type is SWT.HORIZONTAL) {
        width = count * maxWidth;
        if (count !is 0) width += (count - 1) * spacing;
        height = maxHeight;
    } else {
        width = maxWidth;
        height = count * maxHeight;
        if (count !is 0) height += (count - 1) * spacing;
    }
    width += marginWidth * 2;
    height += marginHeight * 2;
    if (wHint !is SWT.DEFAULT) width = wHint;
    if (hHint !is SWT.DEFAULT) height = hHint;
    return new Point (width, height);
}

Point computeChildSize (Control control, int wHint, int hHint, bool flushCache) {
    FillData data = cast(FillData)control.getLayoutData ();
    if (data is null) {
        data = new FillData ();
        control.setLayoutData (data);
    }
    Point size = null;
    if (wHint is SWT.DEFAULT && hHint is SWT.DEFAULT) {
        size = data.computeSize (control, wHint, hHint, flushCache);
    } else {
        // TEMPORARY CODE
        int trimX, trimY;
        if ( auto sa = cast(Scrollable)control ) {
            Rectangle rect = sa.computeTrim (0, 0, 0, 0);
            trimX = rect.width;
            trimY = rect.height;
        } else {
            trimX = trimY = control.getBorderWidth () * 2;
        }
        int w = wHint is SWT.DEFAULT ? wHint : Math.max (0, wHint - trimX);
        int h = hHint is SWT.DEFAULT ? hHint : Math.max (0, hHint - trimY);
        size = data.computeSize (control, w, h, flushCache);
    }
    return size;
}

override protected bool flushCache (Control control) {
    Object data = control.getLayoutData();
    if (data !is null) (cast(FillData)data).flushCache();
    return true;
}

String getName () {
    String string = this.classinfo.name;
    int index = string.lastIndexOf( '.');
    if (index is -1 ) return string;
    return string[ index + 1 .. string.length ];
}

override protected void layout (Composite composite, bool flushCache) {
    Rectangle rect = composite.getClientArea ();
    Control [] children = composite.getChildren ();
    int count = children.length;
    if (count is 0) return;
    int width = rect.width - marginWidth * 2;
    int height = rect.height - marginHeight * 2;
    if (type is SWT.HORIZONTAL) {
        width -= (count - 1) * spacing;
        int x = rect.x + marginWidth, extra = width % count;
        int y = rect.y + marginHeight, cellWidth = width / count;
        for (int i=0; i<count; i++) {
            Control child = children [i];
            int childWidth = cellWidth;
            if (i is 0) {
                childWidth += extra / 2;
            } else {
                if (i is count - 1) childWidth += (extra + 1) / 2;
            }
            child.setBounds (x, y, childWidth, height);
            x += childWidth + spacing;
        }
    } else {
        height -= (count - 1) * spacing;
        int x = rect.x + marginWidth, cellHeight = height / count;
        int y = rect.y + marginHeight, extra = height % count;
        for (int i=0; i<count; i++) {
            Control child = children [i];
            int childHeight = cellHeight;
            if (i is 0) {
                childHeight += extra / 2;
            } else {
                if (i is count - 1) childHeight += (extra + 1) / 2;
            }
            child.setBounds (x, y, width, childHeight);
            y += childHeight + spacing;
        }
    }
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the layout
 */
override public String toString () {
    String string = getName () ~ " {";
    string ~= "type="~((type is SWT.VERTICAL) ? "SWT.VERTICAL" : "SWT.HORIZONTAL")~" ";
    if (marginWidth !is 0) string ~= "marginWidth="~String_valueOf(marginWidth)~" ";
    if (marginHeight !is 0) string ~= "marginHeight="~String_valueOf(marginHeight)~" ";
    if (spacing !is 0) string ~= "spacing="~String_valueOf(spacing)~" ";
    string = string.trim();
    string ~= "}";
    return string;
}
}