/* Copyright (C) 1998-2002 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, see <https://www.gnu.org/licenses/>.
 */

#include <gdk/gdk.h>
namespace
{
extern "C"
{
static GdkTexture* SlotGetTexture_gdk_callback(GdkCursor* /* cursor */,
  int cursor_size, double scale, int* width, int* height,
  int* hotspot_x, int* hotspot_y, void* data)
{
  auto the_slot = static_cast<Gdk::Cursor::SlotGetTexture*>(data);

  try
  {
    return Glib::unwrap_copy((*the_slot)(cursor_size, scale, *width, *height, *hotspot_x, *hotspot_y));
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return nullptr;
}

static void SlotGetTexture_gdk_callback_destroy(void* data)
{
  delete static_cast<Gdk::Cursor::SlotGetTexture*>(data);
}
} // extern "C"
} // anonymous namespace

namespace Gdk
{
Glib::RefPtr<Cursor> Cursor::create_from_slot(const SlotGetTexture& slot,
  const Glib::RefPtr<Cursor>& fallback)
{
  // Create a copy of the slot object. A pointer to this will be passed
  // through the callback's data parameter. It will be deleted
  // when SlotGetTexture_gdk_callback_destroy() is called.
  auto slot_copy = new SlotGetTexture(slot);

  return Glib::wrap(gdk_cursor_new_from_callback(
    &SlotGetTexture_gdk_callback, slot_copy,
    &SlotGetTexture_gdk_callback_destroy, Glib::unwrap(fallback)));
}
} //namespace Gdk
