/* 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 <gtkmm/adjustment.h>

#include <gtk/gtk.h>
#include <math.h>
#include <cstring> //For strlen()
#include <memory>

using std::strlen;

namespace
{
extern "C"
{
static char* SignalProxy_Scale_format_value_callback(GtkScale* /* scale */,
  double value, gpointer user_data)
{
  auto the_slot = static_cast<Gtk::Scale::SlotFormatValue*>(user_data);

  try
  {
    return g_strdup(((*the_slot)(value)).c_str());
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return g_strdup("?");
}

static void SignalProxy_Scale_format_value_callback_destroy(void* data)
{
  delete static_cast<Gtk::Scale::SlotFormatValue*>(data);
}
} // extern "C"
} // anonymous namespace

namespace Gtk
{

Scale::Scale(Orientation orientation)
:
  _CONSTRUCT_SPECIFIC(Range, Scale, "orientation", (GtkOrientation)(orientation))
{
}

Scale::Scale(const Glib::RefPtr<Adjustment>& adjustment, Orientation orientation)
:
  _CONSTRUCT_SPECIFIC(Range, Scale, "adjustment", Glib::unwrap(adjustment), "orientation", (GtkOrientation)(orientation))
{
}

void Scale::set_format_value_func(const SlotFormatValue& 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_Scale_format_value_callback_destroy() is called.
  auto slot_copy = new SlotFormatValue(slot);

  gtk_scale_set_format_value_func(gobj(),
    &SignalProxy_Scale_format_value_callback, slot_copy,
    &SignalProxy_Scale_format_value_callback_destroy);
}

void Scale::unset_format_value_func()
{
  gtk_scale_set_format_value_func(gobj(), nullptr, nullptr, nullptr);
}

} // namespace Gtk
