/* Copyright (C) 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>

namespace
{
static void CssProvider_signal_parsing_error_callback(GtkCssProvider* self, GtkCssSection* p0,const GError* p1,void* data)
{
  using namespace Gtk;
  typedef sigc::slot< void,const Glib::RefPtr<const CssSection>&,const Glib::Error& > SlotType;

  CssProvider* obj = dynamic_cast<CssProvider*>(Glib::ObjectBase::_get_current_wrapper((GObject*) self));
  // Do not try to call a signal on a disassociated wrapper.
  if(obj)
  {
    try
    {
      Glib::Error::throw_exception(g_error_copy(p1));
    }
    catch (const Glib::Error& ex)
    {
      // ex can be (and usually is) an instance of a subclass ofGlib::Error.
      try
      {
        if(sigc::slot_base *const slot = Glib::SignalProxyNormal::data_to_slot(data))
          (*static_cast<SlotType*>(slot))(Glib::wrap(p0, true), ex);
      }
      catch (...)
      {
         Glib::exception_handlers_invoke();
      }
    }
  }
}

} // anonymous namespace

namespace Gtk
{

bool CssProvider::load_from_data(const std::string& data)
{
  GError* gerror = 0;
  bool retvalue = gtk_css_provider_load_from_data(gobj(), data.c_str(), -1, &(gerror));
  if(gerror)
    Glib::Error::throw_exception(gerror);

  return retvalue;
}

//TODO: When we can break ABI, remove no_default_handler in _WRAP_SIGNAL and
// uncomment CssProvider_Class::parsing_error_callback().
#if 0
void CssProvider_Class::parsing_error_callback(GtkCssProvider* self, GtkCssSection* p0, const GError* p1)
{
  Glib::ObjectBase *const 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_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try
      {
        Glib::Error::throw_exception(g_error_copy(p1));
      }
      catch (const Glib::Error& ex)
      {
        // ex can be (and usually is) an instance of a subclass ofGlib::Error.
        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.
          obj->on_parsing_error(Glib::wrap(p0, true), ex);
          return;
        }
        catch(...)
        {
          Glib::exception_handlers_invoke();
        }
      }
    }
  }

  BaseClassType *const base = static_cast<BaseClassType*>(
        g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
    );

  // Call the original underlying C function:
  if(base && base->parsing_error)
    (*base->parsing_error)(self, p0, p1);
}
#endif
} //namespace Gtk
