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

#include <gtk/gtk.h>
#include <gtkmm/label.h>
#include <gtkmm/misc.h>
#include <gtkmm/menu.h>
#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <gtkmm/menubar.h>
#include <gtkmm/accelmap.h>

// gtk_menu_item_(de)select are just a wrapper for gtk_item_(de)select
// and defined in Gtk_Item, so they do not be redefined here

namespace Gtk
{


MenuItem::MenuItem(Widget& widget)
:
  _CONSTRUCT()
{
  add(widget);
}

MenuItem::MenuItem(const Glib::ustring& label, bool mnemonic)
:
  _CONSTRUCT()
{
  add_accel_label(label, mnemonic); /* Left-aligned label */
}

void MenuItem::add_accel_label(const Glib::ustring& label, bool mnemonic)
{
  AccelLabel* pLabel = manage(new AccelLabel(label, mnemonic));

  //Labels are centered by default, but in menus they should be left-aligned.
  pLabel->set_halign(ALIGN_START);

  add(*pLabel);

  //Note that we have to unset the accel_widget in  MenuList::remove() to avoid
  //a memory leak due to the extra reference:
  pLabel->set_accel_widget(*this);
  pLabel->show();
}

void MenuItem::set_accel_key(const AccelKey& accel_key)
{
  accel_key_ = accel_key;
}

bool MenuItem::has_submenu() const
{
  return get_submenu() != 0;
}

void MenuItem::accelerate(Window& window)
{
  if(!accel_key_.is_null())
  {
    if(accel_key_.get_path().empty())
    {
      add_accelerator("activate", window.get_accel_group(),
                      accel_key_.get_key(), accel_key_.get_mod(), ACCEL_VISIBLE);
    }
    else
    {
      AccelMap::add_entry(accel_key_.get_path(),
                          accel_key_.get_key(),
                          accel_key_.get_mod());

      Bin::set_accel_path(accel_key_.get_path(), window.get_accel_group());
    }
  }

  if(has_submenu())
    get_submenu()->accelerate(window);
}

void MenuItem::unset_accel_path()
{
  gtk_menu_item_set_accel_path(gobj(), 0);
}

void MenuItem::unset_submenu()
{
  gtk_menu_item_set_submenu(gobj(), 0);
}

} // namespace Gtk
