// -*- c++ -*-
/* $Id: treesortable.ccg,v 1.7 2006/05/11 11:40:24 murrayc Exp $ */

/* 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 Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

static int SignalProxy_Compare_gtk_callback(GtkTreeModel* model, GtkTreeIter* lhs, GtkTreeIter* rhs, void* data)
{
  Gtk::TreeSortable::SlotCompare* the_slot = static_cast<Gtk::TreeSortable::SlotCompare*>(data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    // use Slot::operator()
    return (*the_slot)(Gtk::TreeIter(model, lhs), Gtk::TreeIter(model, rhs));
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return 0;
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

static void SignalProxy_Compare_gtk_callback_destroy(void* data)
{
  delete static_cast<Gtk::TreeSortable::SlotCompare*>(data);
}

namespace Gtk
{

void TreeSortable::set_sort_func(int sort_column_id, const SlotCompare& slot)
{
  SlotCompare* slot_copy = new SlotCompare(slot);

  gtk_tree_sortable_set_sort_func(
      gobj(), sort_column_id,
      &SignalProxy_Compare_gtk_callback, slot_copy,
      &SignalProxy_Compare_gtk_callback_destroy);
}

void TreeSortable::set_sort_func(const Gtk::TreeModelColumnBase& sort_column, const SlotCompare& slot)
{
  set_sort_func(sort_column.index(), slot);
}

void TreeSortable::set_default_sort_func(const SlotCompare& slot)
{
  SlotCompare* slot_copy = new SlotCompare(slot);

  gtk_tree_sortable_set_default_sort_func(
      gobj(),
      &SignalProxy_Compare_gtk_callback, slot_copy,
      &SignalProxy_Compare_gtk_callback_destroy);
}

void TreeSortable::unset_default_sort_func()
{
  gtk_tree_sortable_set_default_sort_func(
      gobj(), 0, 0, 0); /* See GTK+ docs about the 0s. */
}

_DEPRECATE_IFDEF_START
void TreeSortable::set_sort_column_id(const TreeModelColumnBase& sort_column, SortType order)
{
  set_sort_column(sort_column, order);
}

void TreeSortable::set_sort_column_id(int sort_column_id, SortType order)
{
  set_sort_column(sort_column_id, order);
}  
_DEPRECATE_IFDEF_END

} // namespace Gtk

