/* Copyright 1998-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <glibmm/vectorutils.h>

#include <gtk/gtk.h>


namespace Gtk
{

ListStore::ListStore(const TreeModelColumnRecord& columns)
:
  _CONSTRUCT()
{
  gtk_list_store_set_column_types(gobj(), columns.size(), const_cast<GType*>(columns.types()));
}

void ListStore::set_column_types(const TreeModelColumnRecord& columns)
{
   gtk_list_store_set_column_types(gobj(), columns.size(), const_cast<GType*>(columns.types()));
}


TreeModel::iterator ListStore::erase(const iterator& iter)
{
  g_assert(iter.get_gobject_if_not_end() != 0);

  iterator next(iter);
  ++next;

  GtkTreeIter tmp = *iter.gobj();
  gtk_list_store_remove(gobj(), &tmp);

  return next;
}

TreeModel::iterator ListStore::insert(const iterator& iter)
{
  iterator new_pos(this);

  // get_gobject_if_not_end() returns 0 if iter is an end iterator, which
  // is in turn interpreted by gtk_list_store_insert_before() as a request to
  // insert at the end of the list.

  gtk_list_store_insert_before(
      gobj(), new_pos.gobj(),
      const_cast<GtkTreeIter*>(iter.get_gobject_if_not_end()));

  // GtkTreeIter::stamp should always have a value if it's valid.
  // For end iterators, we need to remember the iter's parent, and
  // this is what setup_end_iterator() does.

  if(new_pos.gobj()->stamp == 0)
    new_pos.setup_end_iterator(iter);

  return new_pos;
}

TreeModel::iterator ListStore::insert_after(const iterator& iter)
{
  iterator new_pos(this);

  // get_gobject_if_not_end() returns 0 if iter is an end iterator, which
  // is in turn interpreted by gtk_list_store_insert_after() as a request to
  // insert at the beginning of the list.

  gtk_list_store_insert_after(
      gobj(), new_pos.gobj(),
      const_cast<GtkTreeIter*>(iter.get_gobject_if_not_end()));

  // GtkTreeIter::stamp should always have a value if it's valid.
  // For end iterators, we need to remember the iter's parent, and
  // this is what setup_end_iterator() does.

  if(new_pos.gobj()->stamp == 0)
    new_pos.setup_end_iterator(iter);

  return new_pos;
}

TreeModel::iterator ListStore::prepend()
{
  iterator new_pos(this);
  gtk_list_store_prepend(gobj(), new_pos.gobj());
  return new_pos;
}

TreeModel::iterator ListStore::append()
{
  iterator new_pos(this);
  gtk_list_store_append(gobj(), new_pos.gobj());
  return new_pos;
}

void ListStore::move(const iterator& source, const iterator& destination)
{
  gtk_list_store_move_before(gobj(),
      const_cast<GtkTreeIter*>(source.get_gobject_if_not_end()),
      const_cast<GtkTreeIter*>(destination.get_gobject_if_not_end()));
}

void ListStore::reorder(const std::vector<int>& new_order)
{
  gtk_list_store_reorder(gobj(), const_cast<int*>(Glib::ArrayHandler<int>::vector_to_array(new_order).data()));
}

void ListStore::set_value_impl(const iterator& row, int column, const Glib::ValueBase& value)
{
  gtk_list_store_set_value(
      gobj(), const_cast<GtkTreeIter*>(row.gobj()),
      column, const_cast<GValue*>(value.gobj()));
}

} // namespace Gtk

