diff mde/gui/widget/layout.d @ 77:3dfd934100f7

Continuing widget data changes started in last commit: all previous widgets work again now (but lacking saving).
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 29 Jul 2008 17:11:22 +0100
parents 159775502bb4
children 79a1809421aa
line wrap: on
line diff
--- a/mde/gui/widget/layout.d	Mon Jul 28 18:49:18 2008 +0100
+++ b/mde/gui/widget/layout.d	Tue Jul 29 17:11:22 2008 +0100
@@ -48,27 +48,38 @@
      * [widgetID, r, c, w11, w12, ..., w1c, ..., wr1, ..., wrc]
      * where r and c are the number of rows and columns, and wij is the ID (from parent Window's
      * list) for the widget in row i and column j. The number of parameters must be r*c + 3. */
-    this (IWindow wind, int[] data) {
+    this (IWidgetManager mgr, WidgetData data) {
         // Get grid size and check data
-        // Check sufficient data for rows, cols, and at least one widget:
-        if (data.length < 4) throw new WidgetDataException;
+        // Check sufficient data for rows, cols, and possibly row/col widths.
+        if (data.ints.length < 3) throw new WidgetDataException;
         
-        rows = data[1];
-        cols = data[2];
-        if (data.length != 3 + rows * cols) throw new WidgetDataException;
-        /* data.length >= 4 so besides checking the length is correct, this tells us:
-         *      rows * cols >= 4 - 3 = 1            a free check!
-         * The only thing not checked is whether both rows and cols are negative, which would
-         * cause an exception when dynamic arrays are allocated by genCachedConstructionData, which
-         * is an acceptible method of failure (and is unlikely anyway). */
+        rows = data.ints[1];
+        cols = data.ints[2];
+        // Check: at least one sub-widget, ints length == 3 or also contains row & col widths,
+        // strings' length is correct:
+        if (rows < 1 || cols < 1 ||
+            (data.ints.length != 3 && data.ints.length != 3 + rows + cols) ||
+            data.strings.length != rows * cols)
+            throw new WidgetDataException;
         
         // Get all sub-widgets
         subWidgets.length = rows*cols;
         foreach (i, ref subWidget; subWidgets) {
-            subWidget = wind.makeWidget (data[i+3]);
+            subWidget = mgr.makeWidget (data.strings[i]);
         }
-        super (wind, data);
+        
+        super (mgr, data);
+        
+        if (data.ints.length == 3 + rows + cols) {
+            col.setCheck (cast(wdim[]) data.ints[3..cols+3]);
+            row.setCheck (cast(wdim[]) data.ints[cols+3..$]);
+        } else {
+            col.dupMin;
+            row.dupMin;
+        }
+        adjustCache;
     }
+    /+FIXME
     /** Return construction data to recreate this GridLayoutWidget. */
     int[] getCreationData () {
         int[] ret;
@@ -80,7 +91,7 @@
             ret[i+3] = window.addCreationData (widget);
         
         return ret;
-    }
+    }+/
 }
 
 
@@ -89,10 +100,10 @@
  *************************************************************************************************/
 class TrialContentLayoutWidget : GridWidget
 {
-    this (IWindow wind, int[] data) {
+    this (IWidgetManager mgr, WidgetData data) {
         debug scope (failure)
                 logger.warn ("TrialContentLayoutWidget: failure");
-        if (data.length != 3) throw new WidgetDataException;
+        WDCheck (data, 2);
         
         OptionList optsList = OptionList.trial();
         rows = optsList.list.length;
@@ -100,15 +111,23 @@
         
         // Get all sub-widgets
         subWidgets.length = rows*cols;
+        WidgetData COWData;
+        COWData.ints = [0, data.ints[1]];
         foreach (i, c; optsList.list) {
-            subWidgets[i] = new ContentOptionWidget (wind, data[1..3], c);
+            subWidgets[i] = new ContentOptionWidget (mgr, COWData, c);
         }
-        super (wind, data);
+        super (mgr, data);
+        
+        // Set col/row widths to minimals.
+        col.dupMin;
+        row.dupMin;
+        adjustCache;
     }
     
+    /+FIXME
     int[] getCreationData () {
         return [widgetType];
-    }
+    }+/
     
 private:
     OptionList optsList;
@@ -130,11 +149,14 @@
     //BEGIN Creation & saving
     /** Partial constructor for a grid layout widget.
      *
-     * Deriving classes should check data length, and set rows, cols, and the subWidgets array,
+     * Deriving classes should check data lengths, 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);
+     * the call to genCachedConstructionData can be moved to the derived this() methods.)
+     * 
+     * Derived constructors should call either dupMin or setCheck on col and row, and then call
+     * adjustCache, after calling this. */
+    protected this (IWidgetManager mgr, WidgetData data) {
+        super (mgr, data);
         
         // Needn't be set before genCachedConstructionData is called:
         col.setColWidth = &setColWidth;
@@ -144,34 +166,6 @@
         genCachedConstructionData;
     }
     
-    /** 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
-         * 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;
-            row.dupMin;
-        } else {                            // sufficient data
-            lenUsed = rows+cols;
-            col.setCheck (cast(wdim[])data[0..cols]);
-            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
@@ -189,6 +183,7 @@
             widget.setHeight (row.width[i / cols], -1);
         }
     }
+    /+ FIXME - saving
     /** Returns sub-widget mutable data along with column widths and row heights, as used by
      *  adjust(). */
     int[] getMutableData () {
@@ -197,7 +192,7 @@
             ret ~= widget.getMutableData;
         
         return ret ~ cast(int[])col.width ~ cast(int[])row.width;
-    }
+    }+/
     //END Creation & saving
     
     //BEGIN Size & position
@@ -234,7 +229,7 @@
     
     
     // Find the relevant widget.
-    IWidget getWidget (wdim cx, wdim cy) {
+    IChildWidget getWidget (wdim cx, wdim cy) {
         debug scope (failure)
                 logger.warn ("getWidget: failure");
         // Find row/column:
@@ -263,8 +258,8 @@
             dragX = cx;
             dragY = cy;
             
-            window.gui.addClickCallback (&endCallback);
-            window.gui.addMotionCallback (&resizeCallback);
+            mgr.addClickCallback (&endCallback);
+            mgr.addMotionCallback (&resizeCallback);
         }
     }
     
@@ -284,7 +279,7 @@
      * 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.spacing = row.spacing = mgr.renderer.layoutSpacing;
         
         // Calculate the minimal column and row sizes:
         // set length, making sure the arrays are initialised to zero:
@@ -302,7 +297,7 @@
         
         
         // Calculate the overall minimal size, starting with the spacing:
-        mh = window.renderer.layoutSpacing;	// use mh temporarily
+        mh = mgr.renderer.layoutSpacing;	// use mh temporarily
         mw = mh * cast(wdim)(cols - 1);
         mh *= cast(wdim)(rows - 1);
         
@@ -370,11 +365,11 @@
         foreach (i,widget; subWidgets)
             widget.setPosition (x + col.pos[i % cols],
                                 y + row.pos[i / cols]);
-        window.requestRedraw;
+        mgr.requestRedraw;
     }
     bool endCallback (wdabs cx, wdabs cy, ubyte b, bool state) {
         if (b == 1 && state == false) {
-            window.gui.removeCallbacks (cast(void*) this);
+            mgr.removeCallbacks (cast(void*) this);
             return true;	// we've handled the up-click
         }
         return false;		// we haven't handled it
@@ -390,7 +385,7 @@
     
     /* All widgets in the grid, by row. Order:  [ 0 1 ]
      *                                          [ 2 3 ] */
-    IWidget[] subWidgets;
+    IChildWidget[] subWidgets;
     
     /* Widths, positions, etc., either of columns or of rows
      *
@@ -491,7 +486,7 @@
         */
         wdim adjustCellSizes (wdim diff, myDiff start, int incr)
         in {
-            // Could occur if adjust isn't called first, but this would be a code error:
+            // Could occur if constructor doesn't call dupMin/setCheck (code error):
             assert (width !is null, "adjustCellSizes: width is null");
             // Most likely if passed negative when sizing is disabled:
             assert (start >= 0 && start < minWidth.length, "adjustCellSizes: invalid start");