/* 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 <gtkmm/widget.h>
#include <gtkmm/orientable.h>
#include <gtkmm/flowboxchild.h>
#include <giomm/liststore.h>
#include <glibmm/exceptionhandler.h>
#include <vector>
#include <utility> // std::forward

_DEFS(gtkmm,gtk)
_PINCLUDE(gtkmm/private/widget_p.h)

namespace Gtk
{
#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C"
{
GTKMM_API
GtkWidget* gtkmm_FlowBox_create_widget_callback(void* item, void* data);

GTKMM_API
void gtkmm_FlowBox_create_widget_callback_destroy(void* data);

} // extern "C"

// Like GtkFlowBoxCreateWidgetFunc and GDestroyNotify, but with C++ linkage.
using FlowBoxCreateWidgetFuncType = GtkWidget* (*)(void* item, void* data);
using FlowBoxCreateWidgetDestroyFuncType = void (*)(void* data);

// A FlowBoxCreateWidgetDestroyFuncType destroys the data in FlowBoxCreateWidgetData.
// gtkmm_FlowBox_create_widget_callback_destroy() also destroys the FlowBoxCreateWidgetData.
struct FlowBoxCreateWidgetData
{
  FlowBoxCreateWidgetFuncType func;
  FlowBoxCreateWidgetDestroyFuncType destroy_func;
  void* data; // A SlotCreateWidget<T_item>
};
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

/** A container that allows reflowing its children.
 *
 * A %FlowBox positions child widgets in sequence according to its
 * orientation.
 *
 * For instance, with the horizontal orientation, the widgets will be
 * arranged from left to right, starting a new row under the previous
 * row when necessary. Reducing the width in this case will require more
 * rows, so a larger height will be requested.
 *
 * Likewise, with the vertical orientation, the widgets will be arranged
 * from top to bottom, starting a new column to the right when necessary.
 * Reducing the height will require more columns, so a larger width will
 * be requested.
 *
 * The children of a FlowBox can be dynamically sorted and filtered.
 *
 * Although a %FlowBox must have only FlowBoxChild children,
 * you can add any kind of widget to it via insert(), and
 * a FlowBoxChild widget will automatically be inserted between
 * the box and the widget.
 *
 * Also see ListBox.
 *
 * @ingroup Widgets
 * @ingroup Containers
 *
 * @newin{3,12}
 */
class GTKMM_API FlowBox
  : public Widget,
    public Orientable
{
  _CLASS_GTKOBJECT(FlowBox, GtkFlowBox, GTK_FLOW_BOX, Gtk::Widget, GtkWidget, , , GTKMM_API)
  _IMPLEMENTS_INTERFACE(Orientable)
public:

  /** Creates a FlowBox.
   */
  _CTOR_DEFAULT

  /** For instance: void foreach_child(FlowBox* box, FlowBoxChild* child);
   *
   * A function used by FlowBox::selected_foreach().
   * It will be called on every selected child of the @a box.
   *
   * @param box FlowBox containing the selected children
   * @param child Each selected FlowBoxChild
   *
   * @newin{3,12}
   */
  typedef sigc::slot<void(FlowBox*, FlowBoxChild*)> SlotSelectedForeach;

  /** For instance: bool on_filter(FlowBoxChild* child);
   *
   * Will be called for each child after a call to FlowBox::set_filter_func(),
   * and it will continue to be called each time a child changes (via
   * FlowBoxChild::changed()) or when FlowBox::invalidate_filter()
   * is called.
   *
   * @param child A FlowBoxChild that may be filtered
   * @returns <tt>true</tt> if the child should be visible, <tt>false</tt> otherwise
   *
   * @newin{3,12}
   */
  typedef sigc::slot<bool(FlowBoxChild*)> SlotFilter;

  /** For instance: int on_sort(FlowBoxChild* child1, FlowBoxChild* child2)
   *
   * Will be called for each child after a call to FlowBox::set_sort_func(),
   * and will continue to be called each time a child changes (via
   * FlowBoxChild::changed()) and when FlowBox::invalidate_sort()
   * is called.
   *
   * @param child1 The first child.
   * @param child2 The second child.
   * @returns < 0 if @a child1 should be before @a child2, 0 if
   * they are equal, and > 0 otherwise
   *
   * @newin{3,12}
   */
  typedef sigc::slot<int(FlowBoxChild*, FlowBoxChild*)> SlotSort;

  _WRAP_METHOD(void set_homogeneous(bool homogeneous = true), gtk_flow_box_set_homogeneous)
  _WRAP_METHOD(bool get_homogeneous() const, gtk_flow_box_get_homogeneous)

  _WRAP_METHOD(void set_row_spacing(guint spacing), gtk_flow_box_set_row_spacing)
  _WRAP_METHOD(guint get_row_spacing() const, gtk_flow_box_get_row_spacing)

  _WRAP_METHOD(void set_column_spacing(guint spacing), gtk_flow_box_set_column_spacing)
  _WRAP_METHOD(guint get_column_spacing() const, gtk_flow_box_get_column_spacing)

  _WRAP_METHOD(void set_min_children_per_line(guint n_children), gtk_flow_box_set_min_children_per_line)
  _WRAP_METHOD(guint get_min_children_per_line() const, gtk_flow_box_get_min_children_per_line)

  _WRAP_METHOD(void set_max_children_per_line(guint n_children), gtk_flow_box_set_max_children_per_line)
  _WRAP_METHOD(guint get_max_children_per_line() const, gtk_flow_box_get_max_children_per_line)

  _WRAP_METHOD(void set_activate_on_single_click(bool single = true), gtk_flow_box_set_activate_on_single_click)
  _WRAP_METHOD(bool get_activate_on_single_click() const, gtk_flow_box_get_activate_on_single_click)

  _WRAP_METHOD(void prepend(Widget& child), gtk_flow_box_prepend)
  _WRAP_METHOD(void append(Widget& child), gtk_flow_box_append)
  _WRAP_METHOD(void insert(Widget& widget, int position), gtk_flow_box_insert)
  _WRAP_METHOD(void remove(Widget& widget), gtk_flow_box_remove)
  _WRAP_METHOD(void remove_all(), gtk_flow_box_remove_all)

  _WRAP_METHOD(FlowBoxChild* get_child_at_index(int idx), gtk_flow_box_get_child_at_index)
  _WRAP_METHOD(const FlowBoxChild* get_child_at_index(int idx) const, gtk_flow_box_get_child_at_index, constversion)

  _WRAP_METHOD(FlowBoxChild* get_child_at_pos(int x, int y), gtk_flow_box_get_child_at_pos)
  _WRAP_METHOD(const FlowBoxChild* get_child_at_pos(int x, int y) const, gtk_flow_box_get_child_at_pos, constversion)

  /** Calls a function for each selected child.
   *
   * Note that the selection cannot be modified from within
   * this function.
   *
   * @param slot The function to call for each selected child
   *
   * @newin{3,12}
   */
  void selected_foreach(const SlotSelectedForeach& slot);
  _IGNORE(gtk_flow_box_selected_foreach)

  // transfer container
#m4 _CONVERSION(`GList*',`std::vector< Gtk::FlowBoxChild*>',`Glib::ListHandler< Gtk::FlowBoxChild* >::list_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
  _WRAP_METHOD(std::vector< Gtk::FlowBoxChild*> get_selected_children(), gtk_flow_box_get_selected_children)
#m4 _CONVERSION(`GList*',`std::vector< const Gtk::FlowBoxChild*>',`Glib::ListHandler< const Gtk::FlowBoxChild* >::list_to_vector($3, Glib::OWNERSHIP_SHALLOW)')
  _WRAP_METHOD(std::vector< const Gtk::FlowBoxChild*> get_selected_children() const, gtk_flow_box_get_selected_children)

  _WRAP_METHOD(void select_child(FlowBoxChild& child), gtk_flow_box_select_child)
  _WRAP_METHOD(void unselect_child(FlowBoxChild& child), gtk_flow_box_unselect_child)

  _WRAP_METHOD(void select_all(), gtk_flow_box_select_all)
  _WRAP_METHOD(void unselect_all(), gtk_flow_box_unselect_all)

  _WRAP_METHOD(void set_selection_mode(SelectionMode mode = SelectionMode::NONE), gtk_flow_box_set_selection_mode)
  _WRAP_METHOD(SelectionMode get_selection_mode() const, gtk_flow_box_get_selection_mode)

  _WRAP_METHOD(void set_hadjustment(const Glib::RefPtr<Adjustment>& adjustment), gtk_flow_box_set_hadjustment)
  _WRAP_METHOD(void set_vadjustment(const Glib::RefPtr<Adjustment>& adjustment), gtk_flow_box_set_vadjustment)

  /** Sets a filter function.
   *
   * By setting a filter function on the FlowBox one can decide dynamically
   * which of the children to show. For instance, to implement a search
   * function that only shows the children matching the search terms.
   *
   * The @a slot will be called for each child after the call, and
   * it will continue to be called each time a child changes (via
   * FlowBoxChild::changed()) or when FlowBox::invalidate_filter()
   * is called.
   *
   * @param slot Callback that lets you filter which children to show
   *
   * @newin{3,12}
   */
  void set_filter_func(const SlotFilter& slot);
  _IGNORE(gtk_flow_box_set_filter_func)

  /** Removes the filter function, if any.
   *
   * @newin{3,12}
   */
  void unset_filter_func();

  _WRAP_METHOD(void invalidate_filter(), gtk_flow_box_invalidate_filter)

  /** Sets a sort function.
   *
   * By setting a sort function on the FlowBox, one can dynamically
   * reorder the children of the box, based on the contents of
   * the children.
   *
   * The @a slot will be called for each child after the call,
   * and will continue to be called each time a child changes (via
   * FlowBoxChild::changed()) and when FlowBox::invalidate_sort()
   * is called.
   *
   * @param slot The sort function
   *
   * @newin{3,12}
   */
  void set_sort_func(const SlotSort& slot);
  _IGNORE(gtk_flow_box_set_sort_func)

  /** Removes the sort function, if any.
   *
   * @newin{3,12}
   */
  void unset_sort_func();

  _WRAP_METHOD(void invalidate_sort(), gtk_flow_box_invalidate_sort)

  /** For instance:
   * Gtk::Widget* on_create_widget(const Glib::RefPtr<T_item>& item);
   *
   * Called for flow boxes that are bound to a Gio::ListModel with bind_model()
   * or bind_list_store() for each item that gets added to the model.
   *
   * @newin{3,22}
   *
   * @tparam T_item Base class of the items in the Gio::ListModel. All items must
   *                be of type T_item or a type derived from T_item.
   *                T_item must be Glib::Object or a type derived from Glib::Object.
   * @param item The item from the model for which to create a widget.
   * @return A Gtk::Widget that represents @a item.
   */
  template <typename T_item>
  using SlotCreateWidget = sigc::slot<Gtk::Widget*(const Glib::RefPtr<T_item>&)>;

  /** Binds a Gio::ListModel.
   *
   * If this FlowBox was already bound to a model, that previous binding is
   * destroyed.
   *
   * The contents of the FlowBox are cleared and then filled with widgets that
   * represent items from @a model. The FlowBox is updated whenever @a model changes.
   * If @a model is an empty Glib::RefPtr, the FlowBox is left empty.
   *
   * It is undefined to add or remove widgets directly (for example, with
   * insert() or Gtk::Container::add()) while the FlowBox is bound to a model.
   *
   * Note that using a model is incompatible with the filtering and sorting
   * functionality in FlowBox. When using a model, filtering and sorting
   * should be implemented by the model.
   *
   * @newin{3,22}
   *
   * @param model The Gio::ListModel to be bound.
   * @param slot_create_widget A slot that creates widgets for items.
   *
   * @see bind_list_store()
   */
  void bind_model(const Glib::RefPtr<Gio::ListModel>& model,
    const SlotCreateWidget<Glib::Object>& slot_create_widget);
  _IGNORE(gtk_flow_box_bind_model)

  /** Binds a Gio::ListStore<>.
   *
   * If this FlowBox was already bound to a Gio::ListModel, that previous binding is
   * destroyed. (Gio::ListStore is a Gio::ListModel.)
   *
   * The contents of the FlowBox are cleared and then filled with widgets that
   * represent items from @a store. The FlowBox is updated whenever @a store changes.
   * If @a store is an empty Glib::RefPtr, the FlowBox is left empty.
   *
   * It is undefined to add or remove widgets directly (for example, with
   * insert() or Gtk::Container::add()) while the FlowBox is bound to a model.
   *
   * Note that using a model is incompatible with the filtering and sorting
   * functionality in FlowBox. When using a model, filtering and sorting
   * should be implemented by the model.
   *
   * @newin{3,22}
   *
   * @tparam T_item Base class of the items in the Gio::ListStore. All items must
   *                be of type T_item or a type derived from T_item.
   *                T_item must be Glib::Object or a type derived from Glib::Object.
   * @tparam T_slot SlotCreateWidget<T_item> or a type that can be converted
   *                to SlotCreateWidget<T_item>.
   * @param store The Gio::ListStore<> to be bound.
   * @param slot_create_widget A slot that creates widgets for items.
   */
  template <typename T_item, typename T_slot>
  void bind_list_store(const Glib::RefPtr<Gio::ListStore<T_item>>& store,
    T_slot&& slot_create_widget);

  _WRAP_PROPERTY("selection-mode", SelectionMode)
  _WRAP_PROPERTY("activate-on-single-click", bool)
  _WRAP_PROPERTY("accept-unpaired-release", bool)
  _WRAP_PROPERTY("homogeneous", bool)
  _WRAP_PROPERTY("min-children-per-line", guint)
  _WRAP_PROPERTY("max-children-per-line", guint)
  _WRAP_PROPERTY("row-spacing", guint)
  _WRAP_PROPERTY("column-spacing", guint)

  // no_default_handler because GtkFlowBoxClass is private.
  _WRAP_SIGNAL(void child_activated(FlowBoxChild* child), "child-activated", no_default_handler)
  _WRAP_SIGNAL(void selected_children_changed(), "selected-children-changed", no_default_handler)
  _IGNORE_SIGNAL(activate-cursor-child, toggle-cursor-child, move-cursor, select-all, unselect-all) // Action signals

private:
  template <typename T_item>
  static GtkWidget* proxy_bind_list_store_create_widget_callback(void* item, void* data);
};

#ifndef DOXYGEN_SHOULD_SKIP_THIS

template <typename T_item, typename T_slot>
void FlowBox::bind_list_store(const Glib::RefPtr<Gio::ListStore<T_item>>& store,
  T_slot&& slot_create_widget)
{
  // Create a copy of the slot.
  // It will be deleted by Glib::destroy_notify_delete<SlotCreateWidget>.
  auto slot_copy = new SlotCreateWidget<T_item>(std::forward<T_slot>(slot_create_widget));

  auto create_widget_data =
    new FlowBoxCreateWidgetData{&proxy_bind_list_store_create_widget_callback<T_item>,
    &Glib::destroy_notify_delete<SlotCreateWidget<T_item>>, slot_copy};
  gtk_flow_box_bind_model(gobj(),
    store ? store->Gio::ListModel::gobj() : nullptr,
    &gtkmm_FlowBox_create_widget_callback, create_widget_data,
    &gtkmm_FlowBox_create_widget_callback_destroy);
}

template <typename T_item>
GtkWidget* FlowBox::proxy_bind_list_store_create_widget_callback(void* item, void* data)
{
  auto& slot = *static_cast<SlotCreateWidget<T_item>*>(data);
  auto cobject = static_cast<typename T_item::BaseObjectType*>(item);

  try
  {
    // cast_dynamic is necessary if T_item is a user-defined type, such as
    // class MyObject : public Glib::Object
    // take_copy is true here, because wrap() returns a Glib::RefPtr<>.
    // cobject will be unreferenced when the RefPtr is deleted.
    Gtk::Widget* widget = slot(std::dynamic_pointer_cast<T_item>(Glib::wrap(cobject, true)));
    if (widget)
      return widget->gobj();
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  return nullptr;
}

#endif // DOXYGEN_SHOULD_SKIP_THIS

} // namespace Gtk
