/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_gtk_menu.cc

#include "cardwords_gtk_menu.hh"

CardWords_Gtk_MenuWithItems::
CardWords_Gtk_MenuWithItems(const gchar ** pointer_to_list_of_strings,
                            gint           no_of_strings)
{
  int index;
  assert (no_of_strings >= 0);
  for (index = 0; index < no_of_strings; ++index) {
    menu_items.push_back(new Gtk_MenuItem(pointer_to_list_of_strings[index]));
    append(*menu_items.back());
    menu_items.back()->show();
  }
}
CardWords_Gtk_MenuWithItems::~CardWords_Gtk_MenuWithItems()
{
  while (menu_items.empty() == false) {
    menu_items.back()->hide();
    remove(menu_items.back());
    delete menu_items.back();
    menu_items.pop_back();
  }
}

Gtk_MenuItem *
CardWords_Gtk_MenuWithItems::get_menu_item(const gchar * label) const
{
  list<Gtk_MenuItem *>::const_iterator iter;
  for (iter = menu_items.begin(); iter != menu_items.end(); ++iter) {
    if (((Gtk_Label *)(*iter)->get_child())->get_text() == label) {
      return *iter;
    }
  }
  return 0;
}

CardWords_Gtk_MenuBar::CardWords_Gtk_MenuBar(void)
{
}
CardWords_Gtk_MenuBar::~CardWords_Gtk_MenuBar()
{
  while (menus.empty() == false) {
    delete menus.back();
    menus.pop_back();
  }
  while (menu_names.empty() == false) {
    menu_names.back()->hide();
    remove(menu_names.back());
    delete menu_names.back();
    menu_names.pop_back();
  }
}

void
CardWords_Gtk_MenuBar::
add_menu(const gchar *  menu_name,
         const gchar ** pointer_to_list_of_menu_item_names,
         gint           no_of_menu_item_names)
{
  assert (no_of_menu_item_names >= 0);

  menu_names.push_back(new Gtk_MenuItem(menu_name));
  append(*menu_names.back());
  menu_names.back()->show();

  menus.
    push_back(new
              CardWords_Gtk_MenuWithItems(pointer_to_list_of_menu_item_names,
                                          no_of_menu_item_names));
  menu_names.back()->set_submenu(*menus.back());
}
  

Gtk_MenuItem *
CardWords_Gtk_MenuBar::
get_menu_item(const gchar * menu_name, const gchar * menu_item_name) const
{
  assert(menu_names.size() == menus.size());
  list<Gtk_MenuItem *>::const_iterator iter;
  list<CardWords_Gtk_MenuWithItems *>::const_iterator follow;
  for ((iter = menu_names.begin()),(follow = menus.begin());
       iter != menu_names.end();
       (++iter),(++follow)) {
    if (((Gtk_Label *)(*iter)->get_child())->get_text() == menu_name) {
      return (*follow)->get_menu_item(menu_item_name);
    }
  }
  return 0;
}

