/*
 * Copyright (c) 2003-2006, Bram Stein
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer. 
 *  2. Redistributions in binary form must reproduce the above copyright 
 *     notice, this list of conditions and the following disclaimer in the 
 *     documentation and/or other materials provided with the distribution. 
 *  3. The name of the author may not be used to endorse or promote products 
 *     derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CONTAINER_H
#define CONTAINER_H

#include "./Component.h"

namespace ui
{
	/**
	 * A Component that can hold other
	 * components.
	 * A container can contain other Components,
	 * and has public methods for manipulating them.
	 */
	class Container : public Component
	{
	public:
		/**
		 * Constructor.
		 */ 
		Container();

		/**
		 * Copy constructor.
		 */
		Container(const Container& rhs);

		/**
		 * Assignment operator.
		 */
		Container& operator=(const Container& rhs);

		void swap(Container& rhs) throw();

		/**
		 * Destructor.
		 */
		virtual ~Container();

		/**
		 * Returns true if this Component is a Container or not.
		 * @note
		 *	Returned true doesnt mean the Container has children,
		 *	it might as well be an empty Container.
		 * @return
		 *	true if this Component is a Container, false otherwise.
		 */
		bool isContainer() const;

		/**
		 * Returns wether this Container is a top level Container.
		 */
		virtual bool isRootContainer() const;

		/**
		 * Returns true if this container has children.
		 */
		bool hasChildren() const;

		/**
		 * Enables or disables this Component.
		 * Disabling the Component makes this Component
		 * ignore any user input (that is, mouse and key events
		 * generated by a user).
		 */
		void setEnabled(bool enable); 

		/**
		 * Returns the preferred size for this Container.
		 */
		virtual const util::Dimension& getPreferredSize() const;

		/**
		 * Remove the specified Component.
		 */
		virtual void remove(Component* component);

		/**
		 * Remove all containers children.
		 */
		void removeAll();

		/**
		 * Returns the Component at n.
		 * N means the depth level of the Container.
		 * @param
		 *	n depth/index level
		 * @returns
		 *	the Component at n, or NULL if none is found.
		 */
		//Component* getChild(int n) const;

		/**
		 * Returns the number of components
		 * in the container.
		 * @return
		 *	number of components in the container.
		 */
		int getNumberOfChildren() const;

		/**
		 * Add a Component to this Container with given constraint.
		 * Can be overridden to create custom
		 * containers.
		 * @param
		 *	comp Component to add to this Container.
		 * @param
		 *	constraints Constraints used for laying out this Container. These are hints for the LayoutManager.
		 */
		 virtual void add(Component* comp, int constraints = -1);

		 /**
		  * Sets the LayoutManager for this Container.
		  * @param
		  * newLayout the new LayoutManager.
		  * @note Note that the previous LayoutManager is freed from
		  * memory.
		  */
		 void setLayout(layout::LayoutManager* newLayout);

		 /**
		 * Validates this Component.
		 */
		 void validate() const;

		/**
		 * Update, for time based motion. After updating, it calls
		 * all the update methods on it's children.
		 * @param
		 *	deltaTime time between two sequential frames. This can be 
		 *	used for calculating frame rate independant motion.
		 */
		 virtual void updateComponent(float deltaTime);
	protected:
		/**
		 * Add a Component to this Container.
		 * This method should not be overridden,
		 * as it is used by add(Component *comp).
		 * @param
		 * component Component to add to this Container.
		 * @param
		 * constraints Constraints used for laying out this Container. These are hints for the LayoutManager.
		 */
		void addImpl(Component* component, int constraints);

		/**
		 * Removes a Component from this Container.
		 * This is the implementation of the normal
		 * remove function (which can be overridden).
		 */
		void removeImpl(Component *component);

		/**
		 * Paint this Component and it's
		 * children.
		 * @param
		 * g A Graphics reference.
		 */
		virtual void paintComponent(Graphics& g) const;

		/**
		 * Paints the Children of this Container.
		 */
		void paintChildren(const Container* parent, Graphics& g) const;

		/**
		 * Draws all the child components of this element.
		 * Method can be overridden to enhance or modify
		 * the drawing routine.
		 */
		virtual void paintChildren(Graphics& g) const;		
	private:
		/**
		 * Validate this Component
		 * and it children recursively.
		 */
		void validateTree() const;

		/**
		 * Holds a pointer to the layoutManager.
		 * Can be NULL(0).
		 * @see LayoutManager.
		 */
		layout::LayoutManager*	layout;
	};
}
#endif