diff mde/gui/widget/ParentContent.d @ 144:66c58e5b0062

Added a BoolContent-based collapsible widget.
author Diggory Hardy <diggory.hardy@gmail.com>
date Tue, 10 Feb 2009 12:57:09 +0000
parents 2ac3e0012788
children 783969f4665c
line wrap: on
line diff
--- a/mde/gui/widget/ParentContent.d	Mon Feb 09 23:27:41 2009 +0000
+++ b/mde/gui/widget/ParentContent.d	Tue Feb 10 12:57:09 2009 +0000
@@ -227,3 +227,102 @@
     
     bool isWS, isHS;	// no infrastructure for changing sizability, so need to fix it.
 }
+
+/** A collapsible widget: shows/hides a child dependant on a BoolContent.
+ *
+ * Sizability is set once. Min-size is changed (but cannot force size to 0).
+ * 
+ * Uses its content as a switch, which means content cannot be passed through.
+ * A builtin button would improve this. */
+class CollapsibleWidget : AParentWidget
+{
+    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) {
+        super (mgr, parent, id);
+        content = cast(BoolContent) c;
+        WDCCheck (data, 1, 1, content);
+        
+        subWidgets = [mgr.makeWidget (this, data.strings[0])];
+        
+        content.addCallback (&collapse);
+    }
+    
+    override bool setup (uint n, uint flags) {
+        bool r = super.setup (n, flags);
+        if (r) {
+            collapsed = content();
+            if (!collapsed) {
+            	mw = subWidgets[0].minWidth;
+            	mh = subWidgets[0].minHeight;
+            	w = subWidgets[0].width;
+            	h = subWidgets[0].height;
+            }
+        }
+        return r;
+    }
+    
+    override void minWChange (IChildWidget widget, wdim nmw) {
+        debug assert (widget is subWidgets[0]);
+        mw = nmw;
+        parent.minWChange (this, nmw);
+    }
+    override void minHChange (IChildWidget widget, wdim nmh) {
+        debug assert (widget is subWidgets[0]);
+        mh = nmh;
+        parent.minHChange (this, nmh);
+    }
+    
+    // Doesn't change:
+    override bool isWSizable () {
+        return subWidgets[0].isWSizable;
+    }
+    override bool isHSizable () {
+        return subWidgets[0].isHSizable;
+    }
+    
+    override void setWidth (wdim nw, int dir) {
+	w = (nw >= mw ? nw : mw);
+        if (!collapsed) subWidgets[0].setWidth (w, dir);
+    }
+    override void setHeight (wdim nh, int dir) {
+        h = (nh >= mh ? nh : mh);
+        if (!collapsed) subWidgets[0].setHeight (h, dir);
+    }
+    
+    override void setPosition (wdim nx, wdim ny) {
+        x = nx;
+        y = ny;
+        if (!collapsed) subWidgets[0].setPosition (nx,ny);
+    }
+    
+    override IChildWidget getWidget (wdim cx, wdim cy) {
+        if (!collapsed)
+            return subWidgets[0].getWidget (cx, cy);
+        else return this;
+    }
+    
+    override void draw () {
+        if (!collapsed) subWidgets[0].draw;
+    }
+    
+protected:
+    // callback on content
+    void collapse (Content) {
+        collapsed = content();
+        if (collapsed) {
+            mw = mh = 0;
+        } else {
+            mw = subWidgets[0].minWidth;
+            mh = subWidgets[0].minHeight;
+        }
+        parent.minWChange (this, mw);
+        parent.minHChange (this, mh);
+        if (collapsed) return;
+    	// set incase parent didn't:
+        subWidgets[0].setWidth (w, -1);
+        subWidgets[0].setHeight (h, -1);
+        subWidgets[0].setPosition (x,y);
+    }
+    
+    bool collapsed = false;
+    BoolContent content;
+}