/* Copyright 2010 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 <gtk/gtk.h>

using Policy = Gtk::Scrollable::Policy;

namespace Gtk
{

// This vfunc callback is custom implemented because we want the output
// argument of the C++ vfunc to be Gtk::Border& (not GtkBorder* or GtkBorder&),
// and when a GtkBorder is wrapped in a Gtk::Border, the Gtk::Border takes
// ownership of the GtkBorder or a copy of it. The wrapped copy must be assigned
// to the original GtkBorder after the call to the C++ vfunc.
gboolean Scrollable_Class::get_border_vfunc_callback(GtkScrollable* self, GtkBorder* border)
{
  const auto obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    const auto obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        Border border_copy(border, true);
        auto result = obj->get_border_vfunc(border_copy);
        if (border)
          *border = *border_copy.gobj();
        return result;
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }

  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
)  );

  // Call the original underlying C function:
  if(base && base->get_border)
    return (*base->get_border)(self, border);

  using RType = gboolean;
  return RType();
}

void Scrollable::unset_hadjustment()
{
  gtk_scrollable_set_hadjustment(gobj(), nullptr);
}

void Scrollable::unset_vadjustment()
{
  gtk_scrollable_set_vadjustment(gobj(), nullptr);
}

} //namespace Gtk
