/* 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>

static void SignalProxy_behaviours_foreach_callback(ClutterBehaviour* behaviour, ClutterActor *actor, gpointer user_data)
{
  Clutter::Behaviour::SlotForEach* the_slot = static_cast<Clutter::Behaviour::SlotForEach*>(user_data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    Glib::RefPtr<Clutter::Behaviour> cpp_behaviour = Glib::wrap(behaviour, true /* take reference */);
    Glib::RefPtr<Clutter::Actor> cpp_actor = Glib::wrap(actor, true /* take reference */);
    (*the_slot)(cpp_behaviour, cpp_actor);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}
namespace Clutter
{

void Behaviour::actors_foreach(const SlotForEach& slot)
{
  clutter_behaviour_actors_foreach(
      gobj(),
      &SignalProxy_behaviours_foreach_callback, const_cast<SlotForEach*>(&slot));
}

std::vector<Glib::RefPtr<Actor> > Behaviour::get_actors()
{
  GSList* actorList = clutter_behaviour_get_actors(gobj());

  const guint actorCount = g_slist_length(actorList);
  std::vector<Glib::RefPtr<Actor> > actorVec;
  actorVec.reserve(actorCount);

  for(GSList* actorNode = actorList; actorNode->next; actorNode = actorList->next)
  {
    actorVec.push_back(Glib::wrap(static_cast<ClutterActor*>(actorNode->data), true));
  }

  return actorVec;
}

std::vector<Glib::RefPtr<const Actor> > Behaviour::get_actors() const
{
  GSList* actorList = clutter_behaviour_get_actors(const_cast<ClutterBehaviour*>(gobj()));

  const guint actorCount = g_slist_length(actorList);
  std::vector<Glib::RefPtr<const Actor> > actorVec;
  actorVec.reserve(actorCount);

  for(GSList* actorNode = actorList; actorNode->next; actorNode = actorList->next)
  {
    actorVec.push_back(Glib::wrap(static_cast<ClutterActor*>(actorNode->data), true));
  }

  return actorVec;
}

} //namespace Clutter

