/*  Inti: Integrated Foundation Classes
 *  Copyright (C) 2002 The Inti Development Team.
 *
 *  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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library 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.
 */
 
//! @file inti/gtk/treestore.h
//! @brief A GtkTreeStore C++ wrapper interface.
//! 
//! Provides TreeStore, a tree-like model object for use with a TreeView widget.

#ifndef INTI_GTK_TREE_STORE_H
#define INTI_GTK_TREE_STORE_H

#ifndef INTI_G_OBJECT_H
#include <inti/glib/object.h>
#endif

#ifndef INTI_G_VALUE_H
#include <inti/glib/value.h>
#endif

#ifndef INTI_GTK_TREE_MODEL_H
#include <inti/gtk/treemodel.h>
#endif

#ifndef INTI_GTK_TREE_SORTABLE_H
#include <inti/gtk/treesortable.h>
#endif

#ifndef __GTK_TREE_STORE_H__
#include <gtk/gtktreestore.h>
#endif

namespace Inti {

namespace Gtk {

class TreeIter;
class TreeStoreClass;

//! @class TreeStore treestore.h inti/gtk/treestore.h
//! @brief A GtkTreeStore C++ wrapper class.
//!
//! The TreeStore object is a tree-like model for use with a TreeView widget. It implements the
//! TreeModel interface, and consequentialy, can use all of the methods available there.
//! It also implements the TreeSortable interface so it can be sorted by the view. Finally,
//! it also implements the tree drag and drop interfaces. 

class TreeStore : public G::Object, public TreeModel, public TreeSortable
{
	friend class G::Object;
	friend class TreeStoreClass;

	TreeStore(const TreeStore&);
	TreeStore& operator=(const TreeStore&);

protected:
//! @name Constructors
//! @{

	explicit TreeStore(GtkTreeStore *tree_store, bool reference = true);
	//!< Construct a new TreeStore from an existing GtkTreeStore.
	//!< @param tree_store A pointer to a GtkTreeStore.
	//!< @param reference Set false if the initial reference count is floating, set true if it's not.
	//!<
	//!< <BR>The <EM>tree_store</EM> can be a newly created GtkTreeStore or an existing
	//!< GtkTreeStore (see G::Object::Object).
	
//! @}

public:
//! @name Constructors
//! @{

	TreeStore(int n_columns, ...);
	//!< Construct a tree store of n_columns columns of each of the types passed in the variable argument list.
	//!< @param n_columns The number of columns in the tree store.
	//!< @param ... All the GType types for the columns, from first to last.
	//!<
	//!< <BR>As an example, TreeStore(3, G_TYPE_INT, G_TYPE_STRING, GDK_TYPE_PIXBUF);
	//!< will create a new tree store with three columns, of type int, string and GdkPixbuf respectively.

	TreeStore(const std::vector<GType>& types);
	//!< Construct a tree store with a column for each type passed in the vector of GType.
	//!< @param types A vector of GType containing the column types, from first to last.

	virtual ~TreeStore();
	//!< Destructor.
	
//! @}
//! @name Accessors
//! @{

	GtkTreeStore* gtk_tree_store() const { return (GtkTreeStore*)instance; }
	//!< Get a pointer to the GtkTreeStore structure.

	GtkTreeStoreClass* gtk_tree_store_class() const;
	//!< Get a pointer to the GtkTreeStoreClass structure.

	operator GtkTreeStore* () const;
	//!< Conversion operator; safely converts a TreeStore to a GtkTreeStore pointer.

	bool is_ancestor(const TreeIter& iter, const TreeIter& descendant) const;
	//!< Returns true if <EM>iter</EM> is an ancestor of <EM>descendant</EM>, that is,
	//!< <EM>iter</EM> is the parent (or grandparent or great-grandparent) of descendant.
	//!< @param iter A valid TreeIter.
	//!< @param descendant A valid TreeIter. 
	//!< @return <EM>true</EM> if iter is an ancestor of descendant.
	 
	int iter_depth(const TreeIter& iter) const;
	//!< Returns the depth of <EM>iter</EM>. 
	//!< @param iter A valid TreeIter.
	//!< @return The depth of <EM>iter</EM>.
	//!<
	//!< <BR>The depth will be 0 for anything on the root level, 1 for anything down a level, etc.

	bool iter_is_valid(const TreeIter& iter) const;
	//!< Checks if the given <EM>iter</EM> is a valid iter for the tree store.
	//!< @param iter A TreeIter.
	//!< @return <EM>true</EM> if the iter is still valid, <EM>false</EM> if it's not.
	//!<
	//!< <B>WARNING:</B> This function is slow. Only use it for debugging and/or testing purposes.

//! @}
//! @name Methods
//! @{

	TreeIter prepend(TreeIter *parent = 0);
	//!< Prepends a new row to the tree store. 
	//!< @param parent A valid TreeIter, or null.
	//!< @return An unset TreeIter to set to the prepended row. 
	//!<	
	//!< <BR>If parent is non-null, then it will prepend the new row before the first child
	//!< of <EM>parent</EM>, otherwise it will prepend a row to the top level. The returned
	//!< TreeIter will point to this new row. The row will be empty after this function is
	//!< called. To fill in values, you need to call set_value() or set_value_type().

	TreeIter append(TreeIter *parent = 0);
	//!< Appends a new row to the tree store. 
	//!< @param parent A valid TreeIter, or null.
	//!< @return An unset TreeIter to set to the appended row. 
	//!<	
	//!< <BR>If parent is non-null, then it will append the new row after the last child
	//!< of <EM>parent</EM>, otherwise it will append a row to the top level. The returned
	//!< TreeIter will point to this new row. The row will be empty after this function is
	//!< called. To fill in values, you need to call set_value() or set_value_type().

	TreeIter insert(int position, TreeIter *parent = 0);
	//!< Creates a new row at <EM>position</EM>.
	//!< @param position The position to insert the new row.
	//!< @param parent A valid TreeIter, or null.
	//!< @return An TreeIter set to the new row. 
	//!<	
	//!< <BR>If parent is non-null, then the row will be made a child of parent. Otherwise,
	//!< the row will be created at the toplevel. If position is larger than the number of
	//!< rows at that level, then the new row will be inserted to the end of the list. An
	//!< empty TreeIter pointing to the new row is returned. To fill in values, you need to
	//!< call set_value() or set_value_type().
	
	TreeIter insert_before(TreeIter& sibling, TreeIter *parent = 0);
	//!< Inserts a new row before <EM>sibling</EM>. 
	//!< @param sibling A valid TreeIter. 
	//!< @param parent A valid TreeIter, or null. 
	//!< @return An TreeIter set to the new row. 
	//!<	
	//!< <BR><EM>parent</EM> is optional but if set it must be the parent of <EM>sibling</EM>. 
	//!< An empty TreeIter pointing to the new row is returned. To fill in values, you need
	//!< to call set_value() or set_value_type().

	TreeIter insert_after(TreeIter& sibling, TreeIter *parent = 0);
	//!< Inserts a new row after <EM>sibling</EM>. 
	//!< @param sibling A valid TreeIter. 
	//!< @param parent A valid TreeIter, or null. 
	//!< @return An TreeIter set to the new row. 
	//!<
	//!< <BR><EM>parent</EM> is optional but if set it must be the parent of <EM>sibling</EM>. 
	//!< An empty TreeIter pointing to the new row is returned. To fill in values, you need
	//!< to call set_value() or set_value_type().

	bool remove(TreeIter& iter);
	//!< Removes iter from the tree store. 
	//!< @param iter A valid TreeIter. 
	//!< @return <EM>true</EM> if <EM>iter</EM> is valid, <EM>false</EM> if it's not.
	//!<
	//!< <BR>After being removed, iter is set to the next valid row at that level, or invalidated
	//!< if it previously pointed to the last one.

	void clear();
	//!< Removes all rows from the tree store.
 
	void reorder(const TreeIter& parent, int *new_order);
	//!< Reorders the children of <EM>parent</EM> in the tree store to follow the order
	//!< indicated by <EM>new_order</EM>.
	//!< @param parent A TreeIter.
	//!< @param new_order An integer array indicating the new order for the list.
	//!<
	//!< <BR><B>Note:</B> This method only works with unsorted stores.

	void swap(const TreeIter& a, const TreeIter& b);
	//!< Swaps <EM>a</EM> and <EM>b</EM> in the same level of the tree store.
	//!< @param a A TreeIter.
	//!< @param b Another TreeIter.
	//!<
	//!< <BR><B>Note:</B> This method only works with unsorted stores.

	void move_after(const TreeIter& iter, const TreeIter *position);
	//!< Moves <EM>iter</EM> in the tree store to the position after position
	//!< (iter and position should be in the same level).
	//!< @param iter A TreeIter.
	//!< @param position A TreeIter, or null.
	//!<
	//!< <BR><B>Note:</B> This function only works with unsorted stores. If position
	//!< is null, iter will be moved to the start of the level.

	void move_before(const TreeIter& iter, const TreeIter *position);
	//!< Moves <EM>iter</EM> in the tree store to the position before position
	//!< (iter and position should be in the same level).
	//!< @param iter A TreeIter.
	//!< @param position A TreeIter, or null.
	//!<
	//!< <BR><B>Note:</B> This function only works with unsorted stores. If position
	//!< is null, iter will be moved to the end of the level.

	void set_value(const TreeIter& iter, int column, const G::Value& value);
	//!< Sets the data in the cell specified by <EM>iter</EM> and <EM>column</EM>.
	//!< @param iter A valid TreeIter for the row being modified.
	//!< @param column The column number to modify.
	//!< @param value The new value for the cell.
	//!< 	 
	//!< <BR>The type of value must be convertible to the type of the column.

	void set_value(const TreeIter& iter, int column, const char *str);
	//!< Sets a string value in the cell specified by iter and column.
	//!< @param iter A valid TreeIter for the row being modified.
	//!< @param column The column number to modify.
	//!< @param str The new string for the cell.
	
//! @}
//! @name Templates
//! @{
	
	template<typename DataType>
	void set_value(const TreeIter& iter, int column, const DataType& data)
	{
		G::Value value(gtk_tree_store()->column_headers[column]);
		value.set(data);
		gtk_tree_store_set_value(gtk_tree_store(), iter, column, value.g_value());
	}
	//!< Sets the data in the cell specified by iter and column.
	//!< @param iter A valid TreeIter for the row being modified.
	//!< @param column The column number to modify.
	//!< @param data The data to set for the cell.
	//!<
	//!< <BR>This method is used to set values corresponding to the standard data types used
	//!< by G::Value, such as bool, int, double, String and unsigned int. There is a good
	//!< example of setting values in the inti-demo program <demos/inti-demo/liststore.cc>.

	template<typename DataType>
	void set_enum(const TreeIter& iter, int column, const DataType& data)
	{
		G::Value value(gtk_tree_store()->column_headers[column]);
		value.set((int)data);
		gtk_tree_store_set_value(gtk_tree_store(), iter, column, value.g_value());
	}
	//!< Sets the enum data in the cell specified by iter and column.
	//!< @param iter A valid TreeIter for the row being modified.
	//!< @param column The column number to modify.
	//!< @param data The enum value to set.
	//!<
	//!< <BR>This method is used to set enumeration values. The DataType is the type of the
	//!< enumeration being set. There is a good example of setting values in the inti-demo
	//!< program <demos/inti-demo/liststore.cc>.

	template<typename DataType>
	void set_object(const TreeIter& iter, int column, const DataType& data)
	{
		G::Value value(gtk_tree_store()->column_headers[column]);
		value.set((G::Object*)data);
		gtk_tree_store_set_value(gtk_tree_store(), iter, column, value.g_value());
	}
	//!< Sets the object pointer <EM>data</EM> in the cell specified by iter and column.
	//!< @param iter A valid TreeIter for the row being modified.
	//!< @param column The column number to modify.
	//!< @param data A pointer to an object derived from G::Object, passed by reference.
	//!<
	//!< <BR>There is a good example of setting values in the inti-demo program
	//!< <demos/inti-demo/liststore.cc>.

	template<typename DataType>
	void set_pointer(const TreeIter& iter, int column, const DataType& data)
	{
		G::Value value(gtk_tree_store()->column_headers[column]);
		value.set((void*)data);
		gtk_tree_store_set_value(gtk_tree_store(), iter, column, value.g_value());
	}
	//!< Sets the pointer <EM>data</EM> in the cell specified by iter and column.
	//!< @param iter A valid TreeIter for the row being modified.
	//!< @param column The column number to modify.
	//!< @param data The pointer to set in the cell.
	//!<
	//!< <BR>The <EM>data</EM> argument can be a pointer to any object. The pointer is managed
	//!< internally as a generic (void*) pointer. Unlike set_object() which passes the G::Object
	//!< pointer internally as a GObject pointer, set_pointer() passes the pointer as is, without
	//!< interpretation. There is a good example of setting values in the inti-demo program
	//!< <demos/inti-demo/liststore.cc>.
	
//! @}
};

} // namespace Gtk

} // namespace Inti

#endif // INTI_GTK_TREE_STORE_H

