/* Copyright (C) 2019 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 <memory>

namespace Gtk
{

PadController::PadController(const Glib::RefPtr<Gio::ActionGroup>& action_group, const Glib::RefPtr<Gdk::Device>& pad)
:
  // gtk_pad_controller_new() sets the propagation-phase property.
  _CONSTRUCT("propagation-phase", GTK_PHASE_CAPTURE,
    "action-group", Glib::unwrap(action_group), "pad", Glib::unwrap(pad))
{
}

void PadController::set_action_entries(const std::vector<PadActionEntry>& entries)
{
  if (entries.empty())
    return;

  auto c_entries = std::make_unique<GtkPadActionEntry[]>(entries.size());

  // Copy the GtkPadActionEntry instances to an array without duplicating the
  // strings. Those strings are owned by entries. They are not deleted
  // when the array in c_entries is deleted.
  for (std::size_t i = 0; i < entries.size(); ++i)
    c_entries[i] = *entries[i].gobj();

  gtk_pad_controller_set_action_entries(gobj(), c_entries.get(), entries.size());
}

} // namespace Gtk
