view mde/gui/widget/miscContent.d @ 176:d5d5fe04ca6c

Fixes to CollapsibleWidget. Disabled AChildWidget.invariant.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 12 Sep 2009 09:14:43 +0200
parents 1cbde9807293
children af40e9679436
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/>. */

/******************************************************************************
 * Some non-parent widgets for displaying or editing content.
 * 
 * (There are other non-parent content widgets in TextWidget).
 *****************************************************************************/
module mde.gui.widget.miscContent;

import mde.gui.widget.AChildWidget;
import mde.content.AStringContent;
import mde.gui.exception;


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

/// Editable boolean widget
class BoolContentWidget : AButtonWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(IBoolContent) c;
        if (content_ is null) throw new ContentException (this);
        super (mgr, parent, id);
        wdimPair s = mgr.renderer.getToggleSize;
        w = mw = s.x;
        h = mh = s.y;
        content_.addCallback (&contentRedraw);
    }
    
    override IContent content () {
        return content_;
    }
    override void setContent (IContent c) {
	auto cont = cast(IBoolContent) c;
	if (!cont) {
	    logger.warn ("BoolContentWidget: invalid content set: {}; ignoring", c);
	    return;
	}
	content_ = cont;
	mgr.requestRedraw;
    }
    
    override bool dropContent (IContent content) {
	if (content_.set (content))
	    return true;
	return parent.dropContent (content);
    }
    
    override void draw () {
        mgr.renderer.drawToggle (x,y, content_(), pushed);
    }
    
    override void activated () {
        content_ = !content_();
    }
    
protected:
    IBoolContent content_;
}

/// A button connected to an EventContent
class ButtonContentWidget : AButtonWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(Content) c;
        if (content_ is null) throw new ContentException (this);
        adapter = mgr.renderer.getAdapter ();
        super (mgr, parent, id);
    }
    
    override bool setup (uint n, uint flags) {
	if (!(flags & 3)) return false;	// string or renderer (and possibly font) changed
	adapter.text = content_.toString(1);
	adapter.getDimensions (mw, mh);
	w = mw;
	h = mh;
	return true;
    }
    
    override IContent content () {
        return content_;
    }
    override void setContent (IContent c) {
	auto cont = cast(Content) c;
	if (!cont) {
	    logger.warn ("ButtonContentWidget: invalid content set: {}; ignoring", c);
	    return;
	}
	content_ = cont;
	adapter.text = content_.toString(1);
	wdim omw = mw, omh = mh;
	adapter.getDimensions (mw, mh);
	if (omw != mw)
	    parent.minWChange (this, mw);
	if (omh != mh)
	    parent.minHChange (this, mh);
	mgr.requestRedraw;
    }
    
    override int clickEvent (wdabs cx, wdabs cy, ubyte b, bool state) {
	// Also display if dragging
	return super.clickEvent (cx,cy, b, state) | 4;
    }
    
    override bool dragRelease (wdabs cx, wdabs cy, IChildWidget widg) {
	super.dragRelease (cx, cy, widg);
	if (widg !is this) {	// don't copy content to self
	    widg.dropContent (content_);
	}
	return true;
    }
    
    override void draw () {
	super.draw();
	adapter.draw (x,y);
    }
    
    override void activated () {
	content_.endEvent;
    }
    
protected:
    IRenderer.TextAdapter adapter;
    Content content_;
    int index;
}

/// Display a double in a progress bar/slider. Non-editable.
class SliderContentWidget : AChildWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(DoubleContent) c;
        if (content_ is null) throw new ContentException (this);
        super (mgr, parent, id);
        wdimPair s = mgr.renderer.getSliderSize;
        w = mw = s.x;
        h = mh = s.y;
        content_.addCallback (&contentRedraw);
    }
    
    override IContent content () {
        return content_;
    }
    override void setContent (IContent c) {
	auto cont = cast(DoubleContent) c;
	if (!cont) {
	    logger.warn ("SliderContentWidget: invalid content set: {}; ignoring", c);
	    return;
	}
	content_ = cont;
	mgr.requestRedraw;
    }
    
    override bool isWSizable () {
        return true;
    }
    
    override void draw () {
        mgr.renderer.drawSlider (x,y, w, content_());
    }
    
protected:
    DoubleContent content_;
}