/* 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"
#include <gtk--/label.h>

CardWords_Gtk_MenuWithItems::
CardWords_Gtk_MenuWithItems(const list<string> & list_of_menu_item_names)
{
  list<string>::const_iterator iter;
  assert (list_of_menu_item_names.size() >= 0);
  for (iter = list_of_menu_item_names.begin();
       iter != list_of_menu_item_names.end();
       ++iter) {
    menu_items.push_back(new Gtk::MenuItem(*iter));
    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 string & label) const
{
  list<Gtk::MenuItem *>::const_iterator iter;
  for (iter = menu_items.begin(); iter != menu_items.end(); ++iter) {
    Gtk::Label * gtk_label = dynamic_cast<Gtk::Label *>((*iter)->get_child());
    assert (gtk_label != 0);
    if (gtk_label->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 string &       menu_name,
         const list<string> & list_of_menu_item_names)
{
  assert (list_of_menu_item_names.size() >= 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(list_of_menu_item_names));
  
  menu_names.back()->set_submenu(*menus.back());
}
  

Gtk::MenuItem *
CardWords_Gtk_MenuBar::
get_menu_item(const string & menu_name, const string & 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)) {
    Gtk::Label * gtk_label = dynamic_cast<Gtk::Label *>((*iter)->get_child());
    assert (gtk_label != 0);
    if (gtk_label->get_text() == menu_name) {
      return (*follow)->get_menu_item(menu_item_name);
    }
  }
  return 0;
}
