/* 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 <gtk/gtk.h>
#include <gdkmm/cairoutils.h>

namespace
{
extern "C"
{
static void SignalProxy_Draw_gtk_callback(GtkDrawingArea* /* drawing_area */,
  cairo_t* cr, int width, int height, void* user_data)
{
  auto the_slot = static_cast<Gtk::DrawingArea::SlotDraw*>(user_data);
  auto cr2 = Gdk::Cairo::wrap(cr, false /* has_reference */);

  try
  {
    (*the_slot)(cr2, width, height);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
}

static void SignalProxy_Draw_gtk_callback_destroy(void* data)
{
  delete static_cast<Gtk::DrawingArea::SlotDraw*>(data);
}
} // extern "C"
} // anonymous namespace

namespace Gtk
{
void DrawingArea::set_draw_func(const SlotDraw& slot)
{
  // 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 SignalProxy_Draw_gtk_callback_destroy() is called.
  auto slot_copy = new SlotDraw(slot);

  gtk_drawing_area_set_draw_func(gobj(),
    &SignalProxy_Draw_gtk_callback, slot_copy,
    &SignalProxy_Draw_gtk_callback_destroy);
}

} //namespace Gtk
