view mde/gui/widget/TextWidget.d @ 156:36df0ffe34d2

Fix to reload translations.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 18 Apr 2009 21:51:03 +0200
parents 783969f4665c
children 2476790223b8
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/>. */

/** Simple text widgets. */
module mde.gui.widget.TextWidget;

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

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

/** Base text widget. */
class ATextWidget : AChildWidget
{
    /** Set the adapter first:
     * adapter = mgr.renderer.getAdapter (...); */
    protected this (IWidgetManager mgr, IParentWidget parent, widgetID id) {
        super (mgr, parent, id);
    }
    
    /** Recalculates dims if the renderer changed. */
    override bool setup (uint,uint flags) {
	if (flags & 1) {
	    adapter.getDimensions (mw, mh);
	    if (mw != w || mh != h) {
		w = mw;
		h = mh;
		return true;
	    }
	}
	return false;
    }
    
    override void draw () {
        super.draw();
        adapter.draw (x,y);
    }
    
protected:
    IRenderer.TextAdapter adapter;
}


/** Basic text widget.
 *
 * Displays data.strings[0] directly (no translation). Using
 * DisplayContentWidget with a content is recommended to allow translation.
 * (Use EventContent if the content is not used for anything else; EventContent
 * is the simplest Content available.) */
class TextLabelWidget : ATextWidget
{
    /** Constructor for a widget containing [fixed] content.
     *
     * Widget uses the initialisation data:
     * [widgetID, contentID, colour]
     * where contentID is an ID for the string ID of the contained content
     * and colour is an 8-bit-per-channel RGB colour of the form 0xRRGGBB. */
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data) {
        WDCheck (data, 2, 1);
        super (mgr, parent, id);
        adapter = mgr.renderer.getAdapter (data.ints[1]);
	adapter.text = data.strings[0];
    }
}

/** Displays text from a content.
 *
 * Displays the value, name, description or possibly another field of a
 * content, depending on data.ints[2]. */
class DisplayContentWidget : ATextWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) {
	content_ = cast(Content) c;
	WDCMinCheck (data, 1, 0, content_);
        super (mgr, parent, id);
        
        if (data.ints.length >= 3)
            cIndex = data.ints[2];	// otherwise display '0', the value
        if (cIndex == 0)
            content_.addCallback (&updateVal);
        
        adapter = mgr.renderer.getAdapter ();
    }
    
    override bool setup (uint n, uint flags) {
	if (!(flags & 3)) return false;	// string or renderer (and possibly font) changed
	adapter.text = content_.toString(cIndex);
        return super.setup (n, 3);	// force redimensioning
    }
    
    override IContent content () {
        return content_;
    }
    
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);
    }
    Content content_;
    int cIndex = 0;
}

/// Text-box for editing a content's value. Generic − any AStringContent.
class AStringContentWidget : ATextWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(AStringContent) c;
        if (content_ is null) throw new ContentException (this);
        super (mgr, parent, id);
        
        content_.addCallback (&update);
        adapter = mgr.renderer.getAdapter ();
	adapter.text = content_.toString(0);
    }
    
    override IContent content () {
        return content_;
    }
    
    override bool isWSizable () {
        return true;
    }
    override bool isHSizable () {
        return false;
    }
    
    /** On click, request keyboard input. */
    override int clickEvent (wdabs cx, wdabs, ubyte, bool) {
	//adapter.index = content_.editIndex;
        content_.editIndex = adapter.setIndex (cx - x);
	mgr.requestRedraw;
	return 1;	// get keyboard input via keyEvent
    }
    
    override void keyEvent (ushort s, char[] i) {
	adapter.text = content_.keyStroke (s, i);
	adapter.index = content_.editIndex;
        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 void keyFocusLost () {
	adapter.text = content_.endEdit;	// update other users of content_ relying on callbacks
	adapter.index;
	mgr.requestRedraw;
    }
    
protected:
    void update (Content) {	// callback
        adapter.text = content_.toString(0);
        wdim omw = mw, omh = mh;
        adapter.getDimensions (mw, mh);
        if (omw != mw)
            parent.minWChange (this, mw);
        if (omh != mh)
            parent.minHChange (this, mh);
    }
    AStringContent content_;
}