// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   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, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#ifndef WIDGETCONTAINER_H
#define WIDGETCONTAINER_H

#include "widgetmultiparent.h"

/// WidgetContainer is a WidgetMultiParent with a simple layout scheme.
/// Widgets are placed in rows and columns. Each row and column is
/// stretched to the size of the largest thing inside of it (based on
/// minimum size), and all other Widgets are stretched to fit that. The
/// row or column designated as being "stretchable" will be stretched
/// to fill space when the WidgetContainer itself is larger than would
/// be needed to contain all its children.
LUA_WRAP class WidgetContainer : public WidgetMultiParent {
	public:

		// Unique stuff.
	
		/// Set the number of rows.
		LUA_WRAP void setNumRows(int numRows);

		/// Get the number of rows.
		LUA_WRAP int getNumRows(void);
		
		/// Set the number of columns.
		LUA_WRAP void setNumCols(int numCols);

		/// Get the number of columns.
		LUA_WRAP int getNumCols(void);
		
		/// Set which row is stretchable. (-1 for none, which is
		/// default.)
		LUA_WRAP void setStretchableRow(int rowNum);

		/// Get which row is stretchable.
		LUA_WRAP int getStretchableRow(void);
		
		/// Set which column is stretchable. (-1 for none.)
		LUA_WRAP void setStretchableCol(int colNum);

		/// Get which column is stretchable.
		LUA_WRAP int getStretchableCol(void);
		
		/// Set spacing in pixels between rows and columns.
		LUA_WRAP void setCellSpacing(float spacing);

		/// Get spacing.
		LUA_WRAP float getCellSpacing(void);
		
		/// Assign a new Widget to a specific slot. Not for reassigning
		/// a Widget that's already a child of anything, including
		/// this.
		LUA_WRAP void setSlot(Widget *w, int x, int y);
		
		/// Get the widget in a given slot. Returns NULL if it's empty.
		LUA_WRAP Widget *getSlot(int x, int y);
		
		/// Get the index of a given slot. Not guaranteed to be a valid
		/// index in the children std::vector.
		LUA_WRAP int getSlotIndex(int x, int y);
		
		/// Get the highest minHeight in a row.
		LUA_WRAP float getBiggestThingInRow(int row);
		
		/// Get the highest minWidth in a column.
		LUA_WRAP float getBiggestThingInCol(int col);
		
		// Inherited from Widget
		bool isSizeDirty(void);
		bool needsMouseEvents(void);
		bool handleEvent(const WidgetEvent *event);
		float getMinHeight(void);
		float getMinWidth(void);
		void setWidth(float newWidth);
		void setHeight(float newHeight);
		void draw();

		// Inherited from WidgetMultiParent
		void addChild(Widget *child);
		void removeChild(Widget *child);
		void disconnectSpecificChild(Widget *child);
		
		// Constructors/destructor
		WidgetContainer(void);
		WidgetContainer(TiXmlElement *xml);
		virtual ~WidgetContainer(void);
		
	protected:

		/// Pixel spacing between rows and columns.
		float cellSpacing;
	
		/// Fixup widths to be consistent.
		void recalcWidths(void);
		
		/// Fixup heights to be consistent.
		void recalcHeights(void);
	
		/// Which column is stretchable at the moment.
		int stretchableColumn;
		
		/// Width of the stretchable column.
		float stretchableColumnWidth;
		
		/// Which row is stretchable at the moment.
		int stretchableRow;
		
		/// Height of the stretchable row.
		float stretchableRowHeight;

		/// Sizes of the rows. The length of this std::vector also
		/// determines how many rows it has.
		std::vector<float> rowSizes;
		
		/// Sizes of the columns. The length of this std::vector also
		/// determines how many columns it has.
		std::vector<float> colSizes;
	

};

#endif

