// -*- c++ -*-
/* $Id: menuitem.ccg,v 1.5 2006/05/11 11:40:24 murrayc Exp $ */

/* 
 *
 * 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 Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gtk/gtkmenuitem.h>
#include <gtk/gtkaccelmap.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.

  #ifdef GLIBMM_PROPERTIES_ENABLED
  pLabel->property_xalign() = 0.0;
  #else
  pLabel->set_property("xalign", 0.0f);
  #endif //GLIBMM_PROPERTIES_ENABLED

  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());

      Item::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);
}


} // namespace Gtk

