/* Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <gtkmm/widget.h>

namespace
{
extern "C"
{
static gboolean SlotShortcut_callback(GtkWidget* widget, GVariant* args, gpointer user_data)
{
  auto the_slot = static_cast<Gtk::CallbackAction::SlotShortcut*>(user_data);

  try
  {
    return (*the_slot)(*Glib::wrap(widget, false), Glib::wrap(args, true));
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return false;
}

static void SlotShortcut_callback_destroy(void* data)
{
  delete static_cast<Gtk::CallbackAction::SlotShortcut*>(data);
}
} // extern "C"
} // anonymous namespace

namespace Gtk
{

CallbackAction::CallbackAction(const SlotShortcut& slot)
:
  // Mark this class as non-derived to allow C++ vfuncs to be skipped.
  Glib::ObjectBase(nullptr),
  // Create a copy of the slot object. A pointer to this will be passed
  // through the callback's user_data parameter. It will be deleted
  // when SlotShortcut_callback_destroy() is called.
  ShortcutAction(gtk_callback_action_new(&SlotShortcut_callback, new SlotShortcut(slot),
    &SlotShortcut_callback_destroy))
{
}

} // namespace Gtk
