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

static double SignalProxy_Alpha_callback(ClutterAlpha *alpha, gpointer user_data)
{
  double value = 0;
  Clutter::Alpha::SlotAlphaFunc* the_slot = static_cast<Clutter::Alpha::SlotAlphaFunc*>(user_data);

#ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
#endif
    Glib::RefPtr<Clutter::Alpha> cpp_alpha = Glib::wrap(alpha, true /* take reference */);
    value = (*the_slot)(cpp_alpha);
#ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
#endif
  return value;
}

static void SignalProxy_Alpha_callback_destroy(void* data)
{
  delete static_cast<Clutter::Alpha::SlotAlphaFunc*>(data);
}

} // anonymous namespace

namespace Clutter
{

Alpha::Alpha(const Glib::RefPtr<Timeline>& timeline, const SlotAlphaFunc& slot)
:
  _CONSTRUCT()
{
  _INITIALLY_UNOWNED_SINK
  set_timeline(timeline);
  set_func(slot);
}

void Alpha::set_func(const SlotAlphaFunc& slot)
{
  //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
  //It will be deleted when SignalProxy_Alpha_callback_destroy() is called.
  SlotAlphaFunc *const slot_copy = new SlotAlphaFunc(slot);

  clutter_alpha_set_func(
      gobj(),
      &SignalProxy_Alpha_callback, slot_copy,
      &SignalProxy_Alpha_callback_destroy);
}

} // namespace Clutter
