view mde/gui/widget/Ifaces.d @ 34:6b4116e6355c

Work on the Gui: some of the framework for drag & drop. Also made Window an IWidget. Implemented getWidget(x,y) to find the widget under this location for IWidgets (but not Gui). Made Window an IWidget and made it work a little more similarly to widgets. Implemented callbacks on the Gui for mouse events (enabling drag & drop, etc.). committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 02 May 2008 16:03:52 +0100
parents 316b0230a849
children 57d000574d75
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/>. */

/** Window and widget interfaces. */
module mde.gui.widget.Ifaces;

public import mde.gui.renderer.IRenderer;
import mde.gui.IGui;

/** Interface for Window, allowing widgets to call some of Window's methods.
*
* Contains the methods in Window available for widgets to call on their root. */
interface IWindow : IWidget
{
    /** Widget ID type. Each ID is unique under this window.
    *
    * Type is int since this is the widget data type. */
    alias int widgetID;
    
    /** Get a widget by ID.
    *
    * Returns the widget with the given ID from the Window's widget list. If the widget hasn't yet
    * been created, creates it using the Window's widget creation data.
    *
    * Widgets should never be used more than once (must have a unique parent and position!), and
    * this function is only intended for widgets to get child-widgets, hence a warning is logged
    * if a widget is asked for more than once. */
    //NOTE: possibly revise: parent isn't actually used any more
    IWidget makeWidget (widgetID i, IWidget parent);
    
    /** Get the managing Gui. */
    IGui gui ();
    
    /+ Currently draw-on-event isn't used.
    /** Called by a sub-widget when a redraw is necessary (since drawing may sometimes be done on
    * event. */
    void requestRedraw ();+/
    
    /** Get the window's renderer.
    *
    * Normally specific to the GUI, but widgets have no direct contact with the GUI and this
    * provides the possibility of per-window renderers (if desired). */
    IRenderer renderer ();
}

/** Interface for widgets.
*
* Note that Window also implements this interface so that widgets can interact with their parent in
* a uniform way.
*
* A widget is a region of a GUI window which handles rendering and user-interaction for itself
* and is able to communicate with it's window and parent/child widgets as necessary.
*
* A widget's constructor should have this prototype:
* ----------------------------------
* this (IWindow window, IWidget parent, int[] data);
* ----------------------------------
* Where window is the root window (the window to which the widget belongs), parent is the parent
* widget, and data is an array of initialisation data. The method should throw a
* WidgetDataException (created without parameters) if the data has wrong length or is otherwise
* invalid. */
interface IWidget
{
    /** Calculate the minimum size the widget could be shrunk to, taking into account
     * child-widgets. */
    void getMinimumSize (out int w, out int h);
    
    /** Get the current size of the widget.
     *
     * On the first call (during loading), this may be a value saved as part of the config or
     * something else (e.g. revert to getMinimumSize). */
    void getCurrentSize (out int w, out int h);
    
    /** Set the current position (i.e. called on init and move). */
    void setPosition (int x, int y);
    
    /** Recursively scan the widget tree to find the widget under (x,y).
     *
     * If called on a widget, that widget should assume the location is over itself, and so should
     * either return itself or the result of calling getWidget on the appropriate child widget.
     *
     * In the case of Window this may not be the case; it should check and return null if not under
     * (x,y).
     *
     * Note: use global coordinates (x,y) not coordinates relative to the widget. */
    IWidget getWidget (int x, int y);
    
    /** Receive a mouse click event.
     *
     * See mde.input.input.Input.MouseClickCallback for parameters. However, cx and cy are adjusted
     * to the Widget's local coordinates.
     *
     * Widget may assume coordinates are on the widget (caller must check). */
    void clickEvent (ushort cx, ushort cy, ubyte b, bool state);
    
    /** Draw, using the stored values of x and y.
     *
     * Maybe later enforce clipping of all sub-widget drawing, particularly for cases where only
     * part of the widget is visible: scroll bars or a hidden window. */
    void draw ();
}