diff dwt/layout/GridLayout.d @ 40:fbe68c33eeee

Sync layout with dwt-linux
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 14:41:31 +0200
parents a9ab4c738ed8
children d8635bb48c7c
line wrap: on
line diff
--- a/dwt/layout/GridLayout.d	Tue Oct 07 14:41:16 2008 +0200
+++ b/dwt/layout/GridLayout.d	Tue Oct 07 14:41:31 2008 +0200
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * 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
@@ -7,27 +7,35 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
  *******************************************************************************/
 module dwt.layout.GridLayout;
 
-import dwt.dwthelper.utils;
-
+import dwt.layout.GridData;
 import dwt.DWT;
 import dwt.graphics.Point;
 import dwt.graphics.Rectangle;
+import dwt.widgets.Control;
 import dwt.widgets.Composite;
-import dwt.widgets.Control;
 import dwt.widgets.Layout;
 import dwt.widgets.Scrollable;
 
+import dwt.dwthelper.System;
+
+import tango.util.Convert;
+import Math = tango.math.Math;
+import dwt.dwthelper.utils;
+
+
 /**
- * Instances of this class lay out the control children of a 
- * <code>Composite</code> in a grid. 
+ * Instances of this class lay out the control children of a
+ * <code>Composite</code> in a grid.
  * <p>
- * <code>GridLayout</code> has a number of configuration fields, and the 
- * controls it lays out can have an associated layout data object, called 
- * <code>GridData</code>. The power of <code>GridLayout</code> lies in the 
- * ability to configure <code>GridData</code> for each control in the layout. 
+ * <code>GridLayout</code> has a number of configuration fields, and the
+ * controls it lays out can have an associated layout data object, called
+ * <code>GridData</code>. The power of <code>GridLayout</code> lies in the
+ * ability to configure <code>GridData</code> for each control in the layout.
  * </p>
  * <p>
  * The following code creates a shell managed by a <code>GridLayout</code>
@@ -39,16 +47,19 @@
  *      gridLayout.numColumns = 3;
  *      shell.setLayout(gridLayout);
  * </pre>
- * The <code>numColumns</code> field is the most important field in a 
- * <code>GridLayout</code>. Widgets are laid out in columns from left 
- * to right, and a new row is created when <code>numColumns</code> + 1 
+ * The <code>numColumns</code> field is the most important field in a
+ * <code>GridLayout</code>. Widgets are laid out in columns from left
+ * to right, and a new row is created when <code>numColumns</code> + 1
  * controls are added to the <code>Composite<code>.
  * </p>
- * 
+ *
  * @see GridData
+ * @see <a href="http://www.eclipse.org/swt/snippets/#gridlayout">GridLayout snippets</a>
+ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: LayoutExample</a>
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  */
 public final class GridLayout : Layout {
- 
+
     /**
      * numColumns specifies the number of cell columns in the layout.
      * If numColumns has a value less than 1, the layout will not
@@ -65,7 +76,7 @@
      * The default value is false.
      */
     public bool makeColumnsEqualWidth = false;
-    
+
     /**
      * marginWidth specifies the number of pixels of horizontal margin
      * that will be placed along the left and right edges of the layout.
@@ -73,7 +84,7 @@
      * The default value is 5.
      */
     public int marginWidth = 5;
-    
+
     /**
      * marginHeight specifies the number of pixels of vertical margin
      * that will be placed along the top and bottom edges of the layout.
@@ -87,7 +98,7 @@
      * that will be placed along the left edge of the layout.
      *
      * The default value is 0.
-     * 
+     *
      * @since 3.1
      */
     public int marginLeft = 0;
@@ -97,7 +108,7 @@
      * that will be placed along the top edge of the layout.
      *
      * The default value is 0.
-     * 
+     *
      * @since 3.1
      */
     public int marginTop = 0;
@@ -107,7 +118,7 @@
      * that will be placed along the right edge of the layout.
      *
      * The default value is 0.
-     * 
+     *
      * @since 3.1
      */
     public int marginRight = 0;
@@ -117,7 +128,7 @@
      * that will be placed along the bottom edge of the layout.
      *
      * The default value is 0.
-     * 
+     *
      * @since 3.1
      */
     public int marginBottom = 0;
@@ -138,7 +149,7 @@
      * The default value is 5.
      */
     public int verticalSpacing = 5;
- 
+
 /**
  * Constructs a new instance of this class.
  */
@@ -153,7 +164,7 @@
  *
  * @param numColumns the number of columns in the grid
  * @param makeColumnsEqualWidth whether or not the columns will have equal width
- * 
+ *
  * @since 2.0
  */
 public this (int numColumns, bool makeColumnsEqualWidth) {
@@ -161,14 +172,14 @@
     this.makeColumnsEqualWidth = makeColumnsEqualWidth;
 }
 
-protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) {
-    Point size = layout (composite, false, 0, 0, wHint, hHint, flushCache);
+override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache_) {
+    Point size = layout (composite, false, 0, 0, wHint, hHint, flushCache_);
     if (wHint !is DWT.DEFAULT) size.x = wHint;
     if (hHint !is DWT.DEFAULT) size.y = hHint;
     return size;
 }
 
-protected bool flushCache (Control control) {
+override protected bool flushCache (Control control) {
     Object data = control.getLayoutData ();
     if (data !is null) (cast(GridData) data).flushCache ();
     return true;
@@ -191,12 +202,12 @@
     return null;
 }
 
-protected void layout (Composite composite, bool flushCache) {
+override protected void layout (Composite composite, bool flushCache_) {
     Rectangle rect = composite.getClientArea ();
-    layout (composite, true, rect.x, rect.y, rect.width, rect.height, flushCache);
+    layout (composite, true, rect.x, rect.y, rect.width, rect.height, flushCache_);
 }
 
-Point layout (Composite composite, bool move, int x, int y, int width, int height, bool flushCache) {
+Point layout (Composite composite, bool move, int x, int y, int width, int height, bool flushCache_) {
     if (numColumns < 1) {
         return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
     }
@@ -207,7 +218,7 @@
         GridData data = cast(GridData) control.getLayoutData ();
         if (data is null || !data.exclude) {
             children [count++] = children [i];
-        } 
+        }
     }
     if (count is 0) {
         return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
@@ -216,14 +227,14 @@
         Control child = children [i];
         GridData data = cast(GridData) child.getLayoutData ();
         if (data is null) child.setLayoutData (data = new GridData ());
-        if (flushCache) data.flushCache ();
-        data.computeSize (child, data.widthHint, data.heightHint, flushCache);
+        if (flushCache_) data.flushCache ();
+        data.computeSize (child, data.widthHint, data.heightHint, flushCache_);
         if (data.grabExcessHorizontalSpace && data.minimumWidth > 0) {
             if (data.cacheWidth < data.minimumWidth) {
                 int trim = 0;
                 //TEMPORARY CODE
-                if ( null !is cast(Scrollable)child ) {
-                    Rectangle rect = (cast(Scrollable) child).computeTrim (0, 0, 0, 0);
+                if ( auto sa = cast(Scrollable)child ) {
+                    Rectangle rect = sa.computeTrim (0, 0, 0, 0);
                     trim = rect.width;
                 } else {
                     trim = child.getBorderWidth () * 2;
@@ -239,8 +250,8 @@
 
     /* Build the grid */
     int row = 0, column = 0, rowCount = 0, columnCount = numColumns;
-    Control [][] grid = new Control [4] [columnCount];
-    for (int i=0; i<count; i++) {   
+    Control [][] grid = new Control [][]( 4, columnCount );
+    for (int i=0; i<count; i++) {
         Control child = children [i];
         GridData data = cast(GridData) child.getLayoutData ();
         int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
@@ -248,8 +259,8 @@
         while (true) {
             int lastRow = row + vSpan;
             if (lastRow >= grid.length) {
-                Control [][] newGrid = new Control [lastRow + 4] [columnCount];
-                System.arraycopy (grid, 0, newGrid, 0, grid.length);
+                Control [][] newGrid = new Control[][]( lastRow + 4, columnCount );
+                SimpleType!(Control[]).arraycopy (grid, 0, newGrid, 0, grid.length);
                 grid = newGrid;
             }
             if (grid [row] is null) {
@@ -406,7 +417,7 @@
                     }
                 }
                 if (last > -1) widths [last] += remainder;
-                
+
                 for (int j=0; j<columnCount; j++) {
                     for (int i=0; i<rowCount; i++) {
                         GridData data = getData (grid, i, j, rowCount, columnCount, false);
@@ -432,7 +443,7 @@
                                                     widths [last2=j-k] += delta2;
                                                 }
                                             }
-                                            if (last2 > -1) widths [last2] += remainder2;   
+                                            if (last2 > -1) widths [last2] += remainder2;
                                         }
                                     }
                                 }
@@ -471,8 +482,8 @@
                         currentWidth += (hSpan - 1) * horizontalSpacing - data.horizontalIndent;
                         if ((currentWidth !is data.cacheWidth && data.horizontalAlignment is DWT.FILL) || (data.cacheWidth > currentWidth)) {
                             int trim = 0;
-                            if ( null !is cast(Scrollable)child ) {
-                                Rectangle rect = (cast(Scrollable) child).computeTrim (0, 0, 0, 0);
+                            if ( auto sa = cast(Scrollable)child ) {
+                                Rectangle rect = sa.computeTrim (0, 0, 0, 0);
                                 trim = rect.width;
                             } else {
                                 trim = child.getBorderWidth () * 2;
@@ -544,7 +555,7 @@
                                     heights [last=i-k] += delta;
                                 }
                             }
-                            if (last > -1) heights [last] += remainder; 
+                            if (last > -1) heights [last] += remainder;
                         }
                     }
                     if (!data.grabExcessVerticalSpace || data.minimumHeight !is 0) {
@@ -561,7 +572,7 @@
                                         minHeights [last=i-k] += delta;
                                     }
                                 }
-                                if (last > -1) minHeights [last] += remainder;  
+                                if (last > -1) minHeights [last] += remainder;
                             }
                         }
                     }
@@ -591,7 +602,7 @@
                 }
             }
             if (last > -1) heights [last] += remainder;
-            
+
             for (int i=0; i<rowCount; i++) {
                 for (int j=0; j<columnCount; j++) {
                     GridData data = getData (grid, i, j, rowCount, columnCount, false);
@@ -669,6 +680,7 @@
                         case DWT.FILL:
                             childWidth = cellWidth - data.horizontalIndent;
                             break;
+                        default:
                     }
                     cellHeight += verticalSpacing * (vSpan - 1);
                     int childY = gridY + data.verticalIndent;
@@ -686,6 +698,7 @@
                         case DWT.FILL:
                             childHeight = cellHeight - data.verticalIndent;
                             break;
+                        default:
                     }
                     Control child = grid [i][j];
                     if (child !is null) {
@@ -717,10 +730,10 @@
 }
 
 String getName () {
-    String string = getClass ().getName ();
-    int index = string.lastIndexOf ('.');
-    if (index is -1) return string;
-    return string.substring (index + 1, string.length ());
+    String string = this.classinfo.name;
+    int index = string.lastIndexOf('.');
+    if (index is -1 ) return string;
+    return string[ index + 1 .. string.length ];
 }
 
 /**
@@ -729,20 +742,20 @@
  *
  * @return a string representation of the layout
  */
-public String toString () {
-    String string = getName ()+" {";
-    if (numColumns !is 1) string += "numColumns="+numColumns+" ";
-    if (makeColumnsEqualWidth) string += "makeColumnsEqualWidth="+makeColumnsEqualWidth+" ";
-    if (marginWidth !is 0) string += "marginWidth="+marginWidth+" ";
-    if (marginHeight !is 0) string += "marginHeight="+marginHeight+" ";
-    if (marginLeft !is 0) string += "marginLeft="+marginLeft+" ";
-    if (marginRight !is 0) string += "marginRight="+marginRight+" ";
-    if (marginTop !is 0) string += "marginTop="+marginTop+" ";
-    if (marginBottom !is 0) string += "marginBottom="+marginBottom+" ";
-    if (horizontalSpacing !is 0) string += "horizontalSpacing="+horizontalSpacing+" ";
-    if (verticalSpacing !is 0) string += "verticalSpacing="+verticalSpacing+" ";
+override public String toString () {
+    String string = getName ()~" {";
+    if (numColumns !is 1) string ~= "numColumns="~to!(String)(numColumns)~" ";
+    if (makeColumnsEqualWidth) string ~= "makeColumnsEqualWidth="~to!(String)(makeColumnsEqualWidth)~" ";
+    if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" ";
+    if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" ";
+    if (marginLeft !is 0) string ~= "marginLeft="~to!(String)(marginLeft)~" ";
+    if (marginRight !is 0) string ~= "marginRight="~to!(String)(marginRight)~" ";
+    if (marginTop !is 0) string ~= "marginTop="~to!(String)(marginTop)~" ";
+    if (marginBottom !is 0) string ~= "marginBottom="~to!(String)(marginBottom)~" ";
+    if (horizontalSpacing !is 0) string ~= "horizontalSpacing="~to!(String)(horizontalSpacing)~" ";
+    if (verticalSpacing !is 0) string ~= "verticalSpacing="~to!(String)(verticalSpacing)~" ";
     string = string.trim();
-    string += "}";
+    string ~= "}";
     return string;
 }
 }