# HG changeset patch # User Frank Benoit # Date 1200043296 -3600 # Node ID a67796650ad4fb0ffe028514db42ca3e2507079e # Parent 8e9ea24111fd4b7ef1ba0e4cc7caf5633be9af7a FillData and FillLayout diff -r 8e9ea24111fd -r a67796650ad4 dwt/layout/FillData.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/layout/FillData.d Fri Jan 11 10:21:36 2008 +0100 @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2005 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 + *******************************************************************************/ +module dwt.layout.FillData; + +import dwt.SWT; +import dwt.graphics.Point; +import dwt.widgets.Control; + +class FillData { + + int defaultWidth = -1, defaultHeight = -1; + int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1; + +Point computeSize (Control control, int wHint, int hHint, bool flushCache_) { + if (flushCache_) flushCache(); + if (wHint is SWT.DEFAULT && hHint is SWT.DEFAULT) { + if (defaultWidth is -1 || defaultHeight is -1) { + Point size = control.computeSize (wHint, hHint, flushCache_); + defaultWidth = size.x; + defaultHeight = size.y; + } + return new Point(defaultWidth, defaultHeight); + } + if (currentWidth is -1 || currentHeight is -1 || wHint !is currentWhint || hHint !is currentHhint) { + Point size = control.computeSize (wHint, hHint, flushCache_); + currentWhint = wHint; + currentHhint = hHint; + currentWidth = size.x; + currentHeight = size.y; + } + return new Point(currentWidth, currentHeight); +} +void flushCache () { + defaultWidth = defaultHeight = -1; + currentWidth = currentHeight = -1; +} +} diff -r 8e9ea24111fd -r a67796650ad4 dwt/layout/FillLayout.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/layout/FillLayout.d Fri Jan 11 10:21:36 2008 +0100 @@ -0,0 +1,243 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 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 + *******************************************************************************/ +module dwt.layout.FillLayout; + +import dwt.SWT; +import dwt.graphics.Point; +import dwt.graphics.Rectangle; +import dwt.widgets.Control; +import dwt.widgets.Layout; +import dwt.widgets.Composite; +import dwt.widgets.Scrollable; +import dwt.layout.FillData; + +import tango.text.Util; +import tango.util.Convert; + +/** + * FillLayout is the simplest layout class. It lays out + * controls in a single row or column, forcing them to be the same size. + *

+ * Initially, the controls will all be as tall as the tallest control, + * and as wide as the widest. FillLayout 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 Group. FillLayout can also be used + * when a Composite only has one child. For example, + * if a Shell has a single Group child, + * FillLayout will cause the Group to + * completely fill the Shell (if margins are 0). + *

+ *

+ * Example code: first a FillLayout is created and + * its type field is set, and then the layout is set into the + * Composite. Note that in a FillLayout, + * children are always the same size, and they fill all available space. + *

+ * 		FillLayout fillLayout = new FillLayout();
+ * 		fillLayout.type = SWT.VERTICAL;
+ * 		shell.setLayout(fillLayout);
+ * 
+ *

+ */ +public final class FillLayout : Layout { + /** + * type specifies how controls will be positioned + * within the layout. + * + * The default value is HORIZONTAL. + * + * Possible values are: + */ + 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; +} + +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 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; +} + +protected bool flushCache (Control control) { + Object data = control.getLayoutData(); + if (data !is null) (cast(FillData)data).flushCache(); + return true; +} + +char[] getName () { + char[] string = this.classinfo.name; + int index = locatePrior( string, '.'); + if (index is string.length ) return string; + return string[ index + 1 .. string.length ]; +} + +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