view mde/gui/content/Content.d @ 90:b525ff28774b

Widgets generated dynamically from a list can now be standard widgets selected from data files. Started on allowing alignment to be shared between instances of a layout widget in a dynamic list (to allow column alignment of list's rows).
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 01 Oct 2008 23:37:51 +0100
parents 159775502bb4
children 4d5d53e4f881
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/>. */

/** The content system − type agnostic part.
 */
module mde.gui.content.Content;

import Int = tango.text.convert.Integer;

/** Content − universal part.
 *
 * Services like copy/paste could work on universal content. However, they would need to run a
 * conversion to the appropriate type (or try next-oldest item on clipboard?).
 *
 * Currently Content instances can only have their data set on creation.
 * Each Content class should provide a method to get it's content, e.g. the method text().
 *
 * Extensions to content:
 * 
 * These extensions require that a content can notify any dependants of changes.
 * 
 * Use as a state switch (one option from an enumeration). E.g. a button/selection box could set a
 * state, and a tabbed box could show a tab based on this. Or could represent an option.
 */
// Don't include dimension/drawing stuff because it's renderer specific and content should not be!
// NOTE: an interface or a class?
interface IContent
{
    /+ NOTE: None of this is really used yet, but was (mostly) intended for clipboard copying.
    /** Return a copy of self. */
    IContent dup ();
    
    /** Attempt to convert the content to a specific type (may simply return this if appropriate).
     *
     * Annoyingly we can't use cast because overloading by return type isn't supported. */
    // FIXME: throw or return null on error or unsupported conversion?
    ContentText	toText ();
    ContentInt	toInt ();	/// ditto
    +/
    
    /** Every Content should be convertible to a string, which, if possible, should be a sensible
     * conversion of its content. */
    char[] toString ();
}

/** Get a content from the list (what list?). */
ContentText getContentText (char[] id) {
    return new ContentText (id);	// forget the list for now
}

/** ditto */
ContentInt getContentInt (char[] id) {
    return new ContentInt (42);	// forget the list for now
}

/** Text content. */
/* May end up extending a universal content type.
 *  Services like copy/paste could work on universal content.
 *
 * NOTE: Needs to be a reference type really.
 *  Could alternately be:
 *      alias ContentTextStruct* ContentText
 *  where ContentTextStruct is a struct. */
class ContentText : IContent
{
    this () {}
    this (char[] text) {
        text_ = text;
    }
    
    ContentText dup () {
        return new ContentText (text_);
    }
    
    ContentText toText () {
        return this;
    }
    ContentInt  toInt () {
        // FIXME: convert
        return null;
    }
    
    alias toString text;
    
    /// Get the text.
    char[] toString () {
        return text_;
    }
    
protected:
    //NOTE: need to allow cache-invalidating when text changes!
    char[] text_;
}

/** Integer content. */
class ContentInt : IContent
{
    this () {}
    this (int integer) {
        int_ = integer;
    }
    
    ContentInt dup () {
        return new ContentInt (int_);
    }
    
    ContentText toText () {
        return new ContentText(toString);
    }
    ContentInt  toInt () {
        return this;
    }
    
    /// Get the integer.
    int integer () {
        return int_;
    }
    
    char[] toString () {
        return Int.toString (int_);
    }
    
protected:
    int int_;
}