view 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 source

/* LICENSE BLOCK
Part of mde: a Modular D game-oriented Engine
Copyright © 2007-2008 Diggory Hardy

This program is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/******************************************************************************
 * A pop-up widget and a switch (tab) widget (both parent widgets using
 * content).
 *****************************************************************************/
module mde.gui.widget.ParentContent;

import mde.gui.widget.AParentWidget;
import mde.gui.widget.layout;
import mde.content.AStringContent;
import mde.gui.exception;

debug {
    import tango.util.log.Log : Log, Logger;
    private Logger logger;
    static this () {
	logger = Log.getLogger ("mde.gui.widget.ParentContent");
    }
}

/******************************************************************************
 * Widget which pops up a ContentListWidget created with its content.
 * 
 * Is a button displaying a content string, just like DisplayContentWidget.
 * 
 * Popped up widget is a ContentListWidget created from the same creation data.
 *****************************************************************************/
class PopupMenuWidget : APopupParentWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) {
	content = cast(Content)c;
	WDCMinCheck (data, 3,1, content);
        super (mgr, parent, id);
        
        //popup = mgr.makeWidget (this, data.strings[0], content);
        popup = new ContentListWidget (mgr, this, id, data, c);
        subWidgets = [popup];
	
        cIndex = data.ints[2];
        if (cIndex == 0)
            content.addCallback (&updateVal);
        adapter = mgr.renderer.getAdapter;
	adapter.text = content.toString (cIndex);
	adapter.getDimensions (mw, mh);
	w = mw;
	h = mh;
    }
    
    override void recursionCheck (widgetID wID, IContent c) {
        if (wID is id && c is content)
            throw new WidgetRecursionException (wID);
        parent.recursionCheck (wID, c);
    }

    override int clickEvent (wdabs, wdabs, ubyte b, bool state) {
	if (b == 1 && state == true) {
	    if (!pushed) {
                parentIPPW.addChildIPPW (this);
                parentIPPW.menuActive = true;
                if (popup.width != w && popup.minWidth <= w)
                    popup.setWidth (w, -1);		// neatness
                mgr.positionPopup (this, popup);
                pushed = true;
            } else if (!parentIPPW.parentMenuActive) {	// if not a submenu
                parentIPPW.removeChildIPPW (this);
            }
	}
	return 0;
    }
    
    override void removedIPPW () {
        super.removedIPPW;
	pushed = false;
    }
    
    override void underMouse (bool state) {
        if (state && !pushed && parentIPPW.menuActive) {
            parentIPPW.addChildIPPW (this);
            menuActive = true;
            if (parentIPPW.parentMenuActive)
                mgr.positionPopup (this, popup, 1);
            else {
                if (popup.width != w && popup.minWidth <= w)
                    popup.setWidth (w, -1);		// neatness
                mgr.positionPopup (this, popup);
            }
            pushed = true;
        }
    }
    
    override void draw () {
	mgr.renderer.drawButton (x,y, w,h, pushed);
	adapter.draw (x,y);
    }
    
protected:
    void updateVal (Content) {	// callback
        adapter.text = content.toString(cIndex);
        wdim omw = mw, omh = mh;
        adapter.getDimensions (mw, mh);
        if (omw != mw)
            parent.minWChange (this, mw);
        if (omh != mh)
            parent.minHChange (this, mh);
    }
    bool pushed = false;
    IRenderer.TextAdapter adapter;
    Content content;
    int cIndex;
}

/** A "tab" widget: it doesn't display the tabs, but shows one of a number of
 * widgets dependant on an EnumContent.
 *
 * Sizability is set once. Min-size is updated when switching. */
class SwitchWidget : AParentWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) {
        super (mgr, parent, id);
        content = cast(EnumContent) c;
        if (content is null || (subWidgets.length = content.list.length) == 0)
            throw new ContentException (this);
        WDCheck (data, 1, subWidgets.length);
        
        foreach (i,sc; content.list)
            subWidgets[i] = mgr.makeWidget (this, data.strings[i], sc);
        currentW = subWidgets[content()];
        
        content.addCallback (&switchWidget);
    }
    
    override bool setup (uint n, uint flags) {
        bool r = super.setup (n, flags);
        if (r) {
            mw = currentW.minWidth;
            mh = currentW.minHeight;
            w = currentW.width;
            h = currentW.height;
            static if (SIZABILITY & SIZABILITY_ENUM.START_TRUE)
                    isWS = isHS = true;
            foreach (i,sc; content.list) {
                static if (SIZABILITY == SIZABILITY_ENUM.ANY_SUBWIDGETS) {
                    isWS |= subWidgets[i].isWSizable;
                    isHS |= subWidgets[i].isHSizable;
                } else static if (SIZABILITY == SIZABILITY_ENUM.ALL_SUBWIDGETS) {
                    isWS &= subWidgets[i].isWSizable;
                    isHS &= subWidgets[i].isHSizable;
                }
            }
        }
        return r;
    }
    
    override void minWChange (IChildWidget widget, wdim nmw) {
        if (widget !is currentW) return;
        mw = nmw;
        parent.minWChange (this, nmw);
    }
    override void minHChange (IChildWidget widget, wdim nmh) {
        if (widget !is currentW) return;
        mh = nmh;
        parent.minHChange (this, nmh);
    }
    
    override bool isWSizable () {
        return isWS;
    }
    override bool isHSizable () {
        return isHS;
    }
    
    override void setWidth (wdim nw, int dir) {
	w = (nw >= mw ? nw : mw);
        currentW.setWidth (w, dir);
    }
    override void setHeight (wdim nh, int dir) {
        h = (nh >= mh ? nh : mh);
        currentW.setHeight (h, dir);
    }
    
    override void setPosition (wdim nx, wdim ny) {
        x = nx;
        y = ny;
        currentW.setPosition (nx,ny);
    }
    
    override IChildWidget getWidget (wdim cx, wdim cy) {
        return currentW.getWidget (cx, cy);
    }
    
    override void draw () {
        currentW.draw;
    }
    
protected:
    // callback on content
    void switchWidget (Content) {
        currentW = subWidgets[content()];
        mw = currentW.minWidth;
        mh = currentW.minHeight;
        parent.minWChange (this, mw);
        parent.minHChange (this, mh);
        // Parent may change size. If it doesn't, we must set child's size.
        // We can't tell if it did, so do it (call will be fast if size isn't
        // changed anyway).
        currentW.setWidth (w, -1);
        currentW.setHeight (h, -1);
        currentW.setPosition (x,y);
    }
    
    IChildWidget currentW;
    EnumContent content;
    
    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;
}