view mde/gui/content/Content.d @ 65:891211f034f2

Changes to widgets: widgets may now get strings as creation data. Strings for TextWidgets can be set in files (in a temporary mannor).
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 29 Jun 2008 15:40:37 +0100
parents 7cab2af4ba21
children 159775502bb4
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().
 */
// Don't include dimension/drawing stuff because it's renderer specific and content should not be!
// NOTE: an interface or a class?
alias IContent Content;	// use either name until it's settled...
/** ditto */
interface IContent
{
    /** 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 : Content
{
    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 : Content
{
    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_;
}