view mde/gui/widget/PopupMenu.d @ 141:6f69a9c111eb

Fix for using BoolContentWidget in a menu. Made popups' widths match their parents under certain conditions. Removed dummy testing menu items.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 08 Feb 2009 15:49:45 +0000
parents 3468e9bfded1
children
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/>. */

/******************************************************************************
 * Pop-up menus based on content structures.
 *****************************************************************************/
module mde.gui.widget.PopupMenu;

import mde.gui.widget.AParentWidget;

import mde.gui.widget.layout;
import mde.content.Content;
import mde.gui.exception;

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

/******************************************************************************
 * Widget which pops up a menu based on a 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;
}