/* Copyright (C) 2014 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, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <gtkmm/adjustment.h>

namespace
{
extern "C"
{
static void FuncProxy_Foreach_gtk_callback(GtkFlowBox* box, GtkFlowBoxChild* child, void* data)
{
  auto the_slot = static_cast<Gtk::FlowBox::SlotSelectedForeach*>(data);

  try
  {
    (*the_slot)(Glib::wrap(box), Glib::wrap(child));
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
}

static gboolean SignalProxy_Filter_gtk_callback(GtkFlowBoxChild* child, void* data)
{
  auto the_slot = static_cast<Gtk::FlowBox::SlotFilter*>(data);

  try
  {
    return (*the_slot)(Glib::wrap(child));
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
    return FALSE;
  }
}

static void SignalProxy_Filter_gtk_callback_destroy(void* data)
{
  delete static_cast<Gtk::FlowBox::SlotFilter*>(data);
}

static int SignalProxy_Sort_gtk_callback(GtkFlowBoxChild* child1, GtkFlowBoxChild* child2, void* data)
{
  auto the_slot = static_cast<Gtk::FlowBox::SlotSort*>(data);

  try
  {
    return (*the_slot)(Glib::wrap(child1), Glib::wrap(child2));
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
    return 0;
  }
}

static void SignalProxy_Sort_gtk_callback_destroy(void* data)
{
  delete static_cast<Gtk::FlowBox::SlotSort*>(data);
}

static GtkWidget* proxy_bind_model_create_widget_callback(void* item, void* data)
{
  auto& slot = *static_cast<Gtk::FlowBox::SlotCreateWidget<Glib::Object>*>(data);
  auto cobject = static_cast<GObject*>(item);

  try
  {
    // take_copy is true here, because wrap() returns a Glib::RefPtr<Glib::Object>.
    // cobject will be unreferenced when the RefPtr is deleted.
    Gtk::Widget* widget = slot(Glib::wrap(cobject, true));
    if (widget)
      return widget->gobj();
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  return nullptr;
}

static void proxy_bind_model_create_widget_callback_destroy(void* data)
{
  delete static_cast<Gtk::FlowBox::SlotCreateWidget<Glib::Object>*>(data);
}
} // extern "C"
} // anonymous namespace

namespace Gtk
{
extern "C"
{
// A function with external linkage and C linkage does not get a mangled name.
// Even though gtkmm_FlowBox_create_widget_callback() is declared in a named
// namespace, the linker does not see the namespace name.
// Therefore the function name shall have a prefix, hopefully unique.
GtkWidget* gtkmm_FlowBox_create_widget_callback(void* item, void* data)
{
  auto create_widget_data = reinterpret_cast<FlowBoxCreateWidgetData*>(data);
  return (*create_widget_data->func)(item, create_widget_data->data);
}

void gtkmm_FlowBox_create_widget_callback_destroy(void* data)
{
  auto create_widget_data = reinterpret_cast<FlowBoxCreateWidgetData*>(data);
  // Delete the slot.
  (*create_widget_data->destroy_func)(create_widget_data->data);
  // Delete the FlowBoxCreateWidgetData.
  delete create_widget_data;
}
} // extern "C"

void FlowBox::selected_foreach(const SlotSelectedForeach& slot)
{
  SlotSelectedForeach slot_local_copy(slot);

  gtk_flow_box_selected_foreach(gobj(),
    &FuncProxy_Foreach_gtk_callback, &slot_local_copy);
}

void FlowBox::set_filter_func(const SlotFilter& slot)
{
  // Create a copy of the slot object. A pointer to this will be passed
  // through the callback's data parameter. It will be deleted
  // when SignalProxy_Filter_gtk_callback_destroy() is called.
  auto slot_copy = new SlotFilter(slot);

  gtk_flow_box_set_filter_func(gobj(),
    &SignalProxy_Filter_gtk_callback, slot_copy,
    &SignalProxy_Filter_gtk_callback_destroy);
}

void FlowBox::unset_filter_func()
{
  gtk_flow_box_set_filter_func(gobj(), nullptr, nullptr, nullptr);
}

void FlowBox::set_sort_func(const SlotSort& slot)
{
  // Create a copy of the slot object. A pointer to this will be passed
  // through the callback's data parameter. It will be deleted
  // when SignalProxy_Sort_gtk_callback_destroy() is called.
  auto slot_copy = new SlotSort(slot);

  gtk_flow_box_set_sort_func(gobj(),
    &SignalProxy_Sort_gtk_callback, slot_copy,
    &SignalProxy_Sort_gtk_callback_destroy);
}

void FlowBox::unset_sort_func()
{
  gtk_flow_box_set_sort_func(gobj(), nullptr, nullptr, nullptr);
}

void FlowBox::bind_model(const Glib::RefPtr<Gio::ListModel>& model,
  const SlotCreateWidget<Glib::Object>& slot_create_widget)
{
  // Create a copy of the slot.
  // It will be deleted by proxy_bind_model_create_widget_callback_destroy().
  auto slot_copy = new SlotCreateWidget<Glib::Object>(slot_create_widget);

  gtk_flow_box_bind_model(gobj(), Glib::unwrap(model), &proxy_bind_model_create_widget_callback,
    slot_copy, &proxy_bind_model_create_widget_callback_destroy);
}

} //namespace Gtk

