/* Copyright (C) 2007 The cluttermm 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <clutter/clutter.h>

namespace Clutter
{

#ifdef GLIBMM_EXCEPTIONS_ENABLED
guint Script::load_from_data(const Glib::ustring& data)
#else
guint Script::load_from_data(const Glib::ustring& data, std::auto_ptr<Glib::Error>& error)
#endif
{
  GError* gerror = 0;
  guint result = clutter_script_load_from_data(gobj(), data.c_str(), data.bytes(), &gerror);
#ifdef GLIBMM_EXCEPTIONS_ENABLED
  if(gerror)
    ::Glib::Error::throw_exception(gerror);
#else
  if(gerror)
    error = ::Glib::Error::throw_exception(gerror);
#endif // GLIBMM_EXCEPTIONS_ENABLED

  return result;
}

void Script::add_search_paths(const std::vector<Glib::ustring>& paths)
{
  const int pathCount = paths.size();
  const char* searchPaths[pathCount];

  for(uint i = 0; i < paths.size(); ++i)
  {
      searchPaths[i] = paths[i].data();
  }

  clutter_script_add_search_paths(gobj(), searchPaths, paths.size());
}

std::vector<Glib::RefPtr<Glib::Object> > Script::list_objects()
{
  GList* objectList = clutter_script_list_objects(gobj());

  const guint objectCount = g_list_length(objectList);
  std::vector<Glib::RefPtr<Glib::Object> > objectVec;
  objectVec.reserve(objectCount);

  for(GList* objectNode = objectList; objectNode->next; objectNode = objectList->next)
  {
    objectVec.push_back(Glib::wrap(static_cast<GObject*>(objectNode->data), true));
  }

  return objectVec;
}

std::vector<Glib::RefPtr<const Glib::Object> > Script::list_objects() const
{
  GList* objectList = clutter_script_list_objects(const_cast<ClutterScript*>(gobj()));

  const guint objectCount = g_list_length(objectList);
  std::vector<Glib::RefPtr<const Glib::Object> > objectVec;
  objectVec.reserve(objectCount);

  for(GList* objectNode = objectList; objectNode->next; objectNode = objectList->next)
  {
    objectVec.push_back(Glib::wrap(static_cast<GObject*>(objectNode->data), true));
  }

  return objectVec;
}

#ifdef GLIBMM_VFUNCS_ENABLED
// Custom coded so that we can custom-code the vfunc in the ClutterScript class.
// This is marginally better than modifying gtkmmproc to allow this.
GType Script_Class::get_type_from_name_vfunc_callback(ClutterScript* self, const char* type_name)
{
  CppObjectType *const obj = dynamic_cast<CppObjectType*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*) self));

  if(obj)
  {
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    try
    {
    #endif //GLIBMM_EXCEPTIONS_ENABLED
      return obj->get_type_from_name_vfunc(Glib::convert_const_gchar_ptr_to_ustring(type_name));
    #ifdef GLIBMM_EXCEPTIONS_ENABLED
    }
    catch(...)
    {
      Glib::exception_handlers_invoke();
    }
    #endif //GLIBMM_EXCEPTIONS_ENABLED
  }
  else
  {
    BaseClassType *const base = static_cast<BaseClassType*>(
        g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class.
    );

    if(base && base->get_type_from_name)
      return (*base->get_type_from_name)(self, type_name);
  }

  return 0;
}
#endif // GLIBMM_VFUNCS_ENABLED

GType Script::get_type_from_name_vfunc(const Glib::ustring& type_name)
{
  // See if there is a gtkmm version of the gclass:
  Glib::ustring classname_prefixed ("gtkmm__"); // gtkmm uses a prefix
  classname_prefixed += type_name;

  GType gtype = g_type_from_name(classname_prefixed.c_str());

  if(gtype == G_TYPE_INVALID) // if it's not a registered typename
  {
    // There's no gtkmm derived type, so just use the normal one.
    gtype = g_type_from_name(type_name.c_str());
  }

  return gtype;
}

} //namespace Clutter

