changeset 71:77c7d3235114

Separated the grid layout widget's implementation into a base and a derived class, to allow other uses of layout.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 05 Jul 2008 15:36:39 +0100
parents 7fc0a8295c83
children 159775502bb4
files codeDoc/jobs.txt mde/gui/widget/layout.d
diffstat 2 files changed, 98 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/codeDoc/jobs.txt	Fri Jul 04 19:04:16 2008 +0100
+++ b/codeDoc/jobs.txt	Sat Jul 05 15:36:39 2008 +0100
@@ -3,7 +3,7 @@
 
 
 In progress:
-GUI Content.
+in mdeOld branch: GUI Content.
 
 
 
--- a/mde/gui/widget/layout.d	Fri Jul 04 19:04:16 2008 +0100
+++ b/mde/gui/widget/layout.d	Sat Jul 05 15:36:39 2008 +0100
@@ -27,21 +27,18 @@
     }
 }
 
-/** Encapsulates a grid of Widgets.
-*
-* Currently there is no support for changing number of cells, sub-widgets or sub-widget properties
-* (namely isW/HSizable and minimal size) after this() has run.
-*
-* Since a grid with either dimension zero is not useful, there must be at least one sub-widget.
-*
-* The grid has no border but has spacing between widgets. */
-/* TODO:
-        separate positioning & sub-widget handling from creation/saving
-        other types of layouts (e.g. lists from content)
-*/
-class GridLayoutWidget : Widget
+/*************************************************************************************************
+ * Encapsulates a grid of Widgets.
+ *
+ * Currently there is no support for changing number of cells, sub-widgets or sub-widget properties
+ * (namely isW/HSizable and minimal size) after this() has run.
+ *
+ * Since a grid with either dimension zero is not useful, there must be at least one sub-widget.
+ *
+ * The grid has no border but has spacing between widgets.
+ *************************************************************************************************/
+class GridLayoutWidget : GridWidget
 {
-    //BEGIN Creation & saving
     /** Constructor for a grid layout widget.
      *
      * Widget uses the initialisation data:
@@ -52,7 +49,6 @@
         // Get grid size and check data
         // Check sufficient data for rows, cols, and at least one widget:
         if (data.length < 4) throw new WidgetDataException;
-        super (wind, data);
         
         rows = data[1];
         cols = data[2];
@@ -66,27 +62,93 @@
         // Get all sub-widgets
         subWidgets.length = rows*cols;
         foreach (i, ref subWidget; subWidgets) {
-            subWidget = window.makeWidget (data[i+3]);
+            subWidget = wind.makeWidget (data[i+3]);
         }
+        super (wind, data);
+    }
+    /** Return construction data to recreate this GridLayoutWidget. */
+    int[] getCreationData () {
+        int[] ret;
+        ret.length = 3 + subWidgets.length;
+        
+        ret [0..3] = [widgetType, rows, cols];  // first data
+        
+        foreach (i,widget; subWidgets)          // sub widgets
+            ret[i+3] = window.addCreationData (widget);
+        
+        return ret;
+    }
+}
+
+
+/*************************************************************************************************
+ * Trial layout of sub-widgets of one type only.
+ *************************************************************************************************/
+class TrialLayout : GridWidget
+{
+    this (IWindow wind, int[] data) {
+        assert (false, "Not ready");
+        if (data.length != 6) throw new WidgetDataException;
+        super (wind, data);
+        
+        rows = data[1];
+        cols = data[2];
+        
+        // Get all sub-widgets
+        subWidgets.length = rows*cols;
+        foreach (i, ref subWidget; subWidgets) {
+            //subWidget = new ContentWidget (data[3..6]);
+        }
+    }
+    
+    
+}
+
+
+/*************************************************************************************************
+ * Backend for grid-based (includes column/row) layout widgets.
+ *
+ * A deriving class must at least do some work in it's constructor (see Ddoc for this() below)
+ * and provide an implementation of getCreationData() (unless Widget's version is sufficient).
+ *
+ * Since a grid with either dimension zero is not useful, there must be at least one sub-widget.
+ *
+ * The grid has no border but has spacing between widgets.
+ *************************************************************************************************/
+class GridWidget : Widget
+{
+    //BEGIN Creation & saving
+    /** Partial constructor for a grid layout widget.
+     *
+     * Deriving classes should check data length, and set rows, cols, and the subWidgets array,
+     * before calling this super constructor. (If it's necessary to call super(...) first,
+     * the call to genCachedConstructionData can be moved to the derived this() methods.) */
+    protected this (IWindow wind, int[] data) {
+        super (wind, data);
+        
+        // Needn't be set before genCachedConstructionData is called:
+        col.setColWidth = &setColWidth;
+        row.setColWidth = &setRowHeight;
         
         // Calculate cached construction data
         genCachedConstructionData;
     }
     
-    /* This does two things:
+    /** This implementation of adjust() does two things:
      * 1. Pass adjust data on to sub-widgets
      * 2. Set the size, from the adjust data if possible
-     */
+     *
+     * Can be overridden (probably along with getMutableData()) if a different implementation is
+     * wanted. adjustCache() may still be useful. */
     int[] adjust (int[] data) {
         // Give all sub-widgets their data:
         foreach (widget; subWidgets)
             data = widget.adjust (data);
         
-        /* We basically short-cut setSize by loading previous col/row sizes and doing the final
+        /** We basically short-cut setSize by loading previous col/row sizes and doing the final
          * calculations.
          * Note: if setSize gets called afterwards, it should have same dimensions and so not do
          * anything. */
-        
         int lenUsed = 0;
         if (data.length < rows + cols) {    // data error; use defaults
             col.dupMin;
@@ -97,7 +159,14 @@
             row.setCheck (cast(wdim[])data[cols..lenUsed]);
         }
         
-        
+        adjustCache();
+        return data[lenUsed..$];
+    }
+    /** Generates cached mutable data.
+     *
+     * Should be called by adjust() after setting col and row widths (currently via dupMin or
+     * setCheck). */
+    void adjustCache () {
         // Generate cached mutable data
         // Calculate column and row locations:
         w = col.genPositions;
@@ -109,21 +178,9 @@
             widget.setWidth  (col.width[i % cols], -1);
             widget.setHeight (row.width[i / cols], -1);
         }
-        
-        return data[lenUsed..$];
     }
-    
-    int[] getCreationData () {
-        int[] ret;
-        ret.length = 3 + subWidgets.length;
-        
-        ret [0..3] = [widgetType, rows, cols];  // first data
-        
-        foreach (i,widget; subWidgets)          // sub widgets
-            ret[i+3] = window.addCreationData (widget);
-        
-        return ret;
-    }
+    /** Returns sub-widget mutable data along with column widths and row heights, as used by
+     *  adjust(). */
     int[] getMutableData () {
         int[] ret;
         foreach (widget; subWidgets)
@@ -211,11 +268,13 @@
 private:
     //BEGIN Cache calculation functions
     /* Calculations which need to be run whenever a new sub-widget structure is set
-     * (i.e. to produce cached data calculated from construction data). */
+     * (i.e. to produce cached data calculated from construction data).
+     * Also need to be re-run if the renderer changes.
+     *
+     * rows, cols and subWidgets must be set before calling. */
     void genCachedConstructionData () {
+        // Will only change if renderer changes:
         col.spacing = row.spacing = window.renderer.layoutSpacing;
-        col.setColWidth = &setColWidth;
-        row.setColWidth = &setRowHeight;
         
         // Calculate the minimal column and row sizes:
         // set length, making sure the arrays are initialised to zero: