Changeset 71:77c7d3235114

Show
Ignore:
Timestamp:
07/05/08 10:36:39 (6 months ago)
Author:
Diggory Hardy <diggory.hardy@gmail.com>
branch:
default
Message:

Separated the grid layout widget's implementation into a base and a derived class, to allow other uses of layout.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • codeDoc/jobs.txt

    r64 r71  
    44 
    55In progress: 
    6 GUI Content. 
     6in mdeOld branch: GUI Content. 
    77 
    88 
  • mde/gui/widget/layout.d

    r66 r71  
    2828} 
    2929 
    30 /** Encapsulates a grid of Widgets. 
    31 
    32 * Currently there is no support for changing number of cells, sub-widgets or sub-widget properties 
    33 * (namely isW/HSizable and minimal size) after this() has run. 
    34 
    35 * Since a grid with either dimension zero is not useful, there must be at least one sub-widget. 
    36 
    37 * The grid has no border but has spacing between widgets. */ 
    38 /* TODO: 
    39         separate positioning & sub-widget handling from creation/saving 
    40         other types of layouts (e.g. lists from content) 
    41 */ 
    42 class GridLayoutWidget : Widget 
     30/************************************************************************************************* 
     31 * Encapsulates a grid of Widgets. 
     32 * 
     33 * Currently there is no support for changing number of cells, sub-widgets or sub-widget properties 
     34 * (namely isW/HSizable and minimal size) after this() has run. 
     35 * 
     36 * Since a grid with either dimension zero is not useful, there must be at least one sub-widget. 
     37 * 
     38 * The grid has no border but has spacing between widgets. 
     39 *************************************************************************************************/ 
     40class GridLayoutWidget : GridWidget 
    4341{ 
    44     //BEGIN Creation & saving 
    4542    /** Constructor for a grid layout widget. 
    4643     * 
     
    5350        // Check sufficient data for rows, cols, and at least one widget: 
    5451        if (data.length < 4) throw new WidgetDataException; 
    55         super (wind, data); 
    5652         
    5753        rows = data[1]; 
     
    6763        subWidgets.length = rows*cols; 
    6864        foreach (i, ref subWidget; subWidgets) { 
    69             subWidget = window.makeWidget (data[i+3]); 
    70         } 
     65            subWidget = wind.makeWidget (data[i+3]); 
     66        } 
     67        super (wind, data); 
     68    } 
     69    /** Return construction data to recreate this GridLayoutWidget. */ 
     70    int[] getCreationData () { 
     71        int[] ret; 
     72        ret.length = 3 + subWidgets.length; 
     73         
     74        ret [0..3] = [widgetType, rows, cols];  // first data 
     75         
     76        foreach (i,widget; subWidgets)          // sub widgets 
     77            ret[i+3] = window.addCreationData (widget); 
     78         
     79        return ret; 
     80    } 
     81
     82 
     83 
     84/************************************************************************************************* 
     85 * Trial layout of sub-widgets of one type only. 
     86 *************************************************************************************************/ 
     87class TrialLayout : GridWidget 
     88
     89    this (IWindow wind, int[] data) { 
     90        assert (false, "Not ready"); 
     91        if (data.length != 6) throw new WidgetDataException; 
     92        super (wind, data); 
     93         
     94        rows = data[1]; 
     95        cols = data[2]; 
     96         
     97        // Get all sub-widgets 
     98        subWidgets.length = rows*cols; 
     99        foreach (i, ref subWidget; subWidgets) { 
     100            //subWidget = new ContentWidget (data[3..6]); 
     101        } 
     102    } 
     103     
     104     
     105
     106 
     107 
     108/************************************************************************************************* 
     109 * Backend for grid-based (includes column/row) layout widgets. 
     110 * 
     111 * A deriving class must at least do some work in it's constructor (see Ddoc for this() below) 
     112 * and provide an implementation of getCreationData() (unless Widget's version is sufficient). 
     113 * 
     114 * Since a grid with either dimension zero is not useful, there must be at least one sub-widget. 
     115 * 
     116 * The grid has no border but has spacing between widgets. 
     117 *************************************************************************************************/ 
     118class GridWidget : Widget 
     119
     120    //BEGIN Creation & saving 
     121    /** Partial constructor for a grid layout widget. 
     122     * 
     123     * Deriving classes should check data length, and set rows, cols, and the subWidgets array, 
     124     * before calling this super constructor. (If it's necessary to call super(...) first, 
     125     * the call to genCachedConstructionData can be moved to the derived this() methods.) */ 
     126    protected this (IWindow wind, int[] data) { 
     127        super (wind, data); 
     128         
     129        // Needn't be set before genCachedConstructionData is called: 
     130        col.setColWidth = &setColWidth; 
     131        row.setColWidth = &setRowHeight; 
    71132         
    72133        // Calculate cached construction data 
     
    74135    } 
    75136     
    76     /* This does two things: 
     137    /** This implementation of adjust() does two things: 
    77138     * 1. Pass adjust data on to sub-widgets 
    78139     * 2. Set the size, from the adjust data if possible 
    79      */ 
     140     * 
     141     * Can be overridden (probably along with getMutableData()) if a different implementation is 
     142     * wanted. adjustCache() may still be useful. */ 
    80143    int[] adjust (int[] data) { 
    81144        // Give all sub-widgets their data: 
     
    83146            data = widget.adjust (data); 
    84147         
    85         /* We basically short-cut setSize by loading previous col/row sizes and doing the final 
     148        /** We basically short-cut setSize by loading previous col/row sizes and doing the final 
    86149         * calculations. 
    87150         * Note: if setSize gets called afterwards, it should have same dimensions and so not do 
    88151         * anything. */ 
    89          
    90152        int lenUsed = 0; 
    91153        if (data.length < rows + cols) {    // data error; use defaults 
     
    98160        } 
    99161         
    100          
     162        adjustCache(); 
     163        return data[lenUsed..$]; 
     164    } 
     165    /** Generates cached mutable data. 
     166     * 
     167     * Should be called by adjust() after setting col and row widths (currently via dupMin or 
     168     * setCheck). */ 
     169    void adjustCache () { 
    101170        // Generate cached mutable data 
    102171        // Calculate column and row locations: 
     
    110179            widget.setHeight (row.width[i / cols], -1); 
    111180        } 
    112          
    113         return data[lenUsed..$]; 
    114     } 
    115      
    116     int[] getCreationData () { 
    117         int[] ret; 
    118         ret.length = 3 + subWidgets.length; 
    119          
    120         ret [0..3] = [widgetType, rows, cols];  // first data 
    121          
    122         foreach (i,widget; subWidgets)          // sub widgets 
    123             ret[i+3] = window.addCreationData (widget); 
    124          
    125         return ret; 
    126     } 
     181    } 
     182    /** Returns sub-widget mutable data along with column widths and row heights, as used by 
     183     *  adjust(). */ 
    127184    int[] getMutableData () { 
    128185        int[] ret; 
     
    212269    //BEGIN Cache calculation functions 
    213270    /* Calculations which need to be run whenever a new sub-widget structure is set 
    214      * (i.e. to produce cached data calculated from construction data). */ 
     271     * (i.e. to produce cached data calculated from construction data). 
     272     * Also need to be re-run if the renderer changes. 
     273     * 
     274     * rows, cols and subWidgets must be set before calling. */ 
    215275    void genCachedConstructionData () { 
     276        // Will only change if renderer changes: 
    216277        col.spacing = row.spacing = window.renderer.layoutSpacing; 
    217         col.setColWidth = &setColWidth; 
    218         row.setColWidth = &setRowHeight; 
    219278         
    220279        // Calculate the minimal column and row sizes: