/* Copyright 2003 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <glibmm/vectorutils.h>

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


namespace Gtk
{

void ActionGroup::add(const Glib::RefPtr<Action>& action)
{
  gtk_action_group_add_action_with_accel(gobj(), Glib::unwrap(action),
    0 /* accelerator=0 means use the stock accelerator if this is a stock item */ );
}

void ActionGroup::add(const Glib::RefPtr<Action>& action, const AccelKey& accel_key)
{
  // We need to half-duplicate the gtk_action_group_add_action_with_accel() implementation, because we want to
  // use AccelKey, not just the accelerator string format that is _one_ of the ways to create an AccelKey. murrayc.

  //The AccelKey might have been constructed from key+mod or from an accelerator string,
  //but it the end that just produces a key+mod.
  guint key = accel_key.get_key();
  auto mod = accel_key.get_mod();
  if(key)
  {
    // The AccelKey constructor can take an accel path, so I suppose we should not ignore it,
    // but I can't imagine how it would be useful with the UIManager. murrayc.
    auto accel_path = accel_key.get_path();
    if(accel_path.empty())
    {
      //Copied from the gtk_action_group_add_action_with_accel() implementation:

      gchar* action_name = nullptr;
      g_object_get(G_OBJECT(Glib::unwrap(action)), "name", &action_name, static_cast<char*>(0));
      if(action_name)
      {
        accel_path =  "<Actions>/" + get_name() + '/' + action_name;
        g_free(action_name);
      }
    }

    AccelMap::add_entry(accel_path, key, mod);

    action->set_accel_path(accel_path);
  }

  gtk_action_group_add_action(gobj(), action->gobj());
}



void ActionGroup::add(const Glib::RefPtr<Action>& action, const Action::SlotActivate& slot)
{
  //This is meant to be similar to the gtk_action_group_add_actions() convenience method that is used from C.
  //This also does the work of gtk_action_group_add_toggle_actions() and gtk_action_group_add_radio_actions_full(),
  //because the extra stuff there is already done when the Action (or a derived type, such as ToggleAction) is create()ed.

  action->signal_activate().connect(slot);

  //We probably don't need to use translate_string(), because the label and tooltip will be regular translated strings in our C++ application code.
  //But C needs it because gtk_action_group_add_actions() takes a static array of GtkActionEntrys  whose fields
  //can not be translated with gettext macros.
  //But we should soon know if menus and toolbars are not translated in non-english locales. murrayc.

  add(action);
}

void ActionGroup::add(const Glib::RefPtr<Action>& action, const AccelKey& accel_key, const Action::SlotActivate& slot)
{
  //This is meant to be similar to the gtk_action_group_add_actions() convenience method that is used from C.
  //This also does the work of gtk_action_group_add_toggle_actions() and gtk_action_group_add_radio_actions_full(),
  //because the extra stuff there is already done when the Action (or a derived type, such as ToggleAction) is create()ed.

  action->signal_activate().connect(slot);
  add(action, accel_key);

  /*
  //Create the accel path (a kind of unique key):
  Glib::ustring accel_path = "<Actions>/" + get_name() + "/" + action->get_name();

  //Register the accel path:
  Gtk::AccelMap::add_entry(accel_path, accel_key.get_key(), accel_key.get_mod());

  //USe the accel path:
  action->set_accel_path(accel_path);
  */
}


} // namespace Gtk

