/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   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
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#include "constants.h"

#ifdef USE_UI_GTK

#include "menu.h"
#include "gtk_gui.h"
#include "party_points.h"
#include "last_trick.h"
#include "main_window.h"
#include "playfield.h"
#include "table.h"
#include "bug_report.h"

#include "../../party/party.h"
#include "../../misc/language.h"

namespace GTK_DOKO {

  /**
   **
   ** Constructor
   **
   ** @param	main_window	the main window
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  Menu::Menu(MainWindow* const main_window) :
    main_window_p(main_window),
  labels(0),
  bar(gtk_menu_bar_new()),

  game_item(gtk_menu_item_new_with_label("")),
  game_new_party(gtk_menu_item_new_with_label("")),
  game_party_settings(gtk_menu_item_new_with_label("")),
  game_quit(gtk_menu_item_new_with_label("")),

  settings_item(gtk_menu_item_new_with_label("")),
  settings_show_all_hands(gtk_check_menu_item_new_with_label("")),
  settings_cards_order(gtk_menu_item_new_with_label("")),
  settings_settings(gtk_menu_item_new_with_label("")),

  announcements_item(gtk_menu_item_new_with_label("")),
  announcements_name(0),
  announcements_announcement(0),

  last_trick(gtk_menu_item_new_with_label("")),
  party_points(gtk_menu_item_new_with_label("")),
  bug_report(gtk_menu_item_new_with_label("")),

  help_item(gtk_menu_item_new_with_label("help")),
  help_index(gtk_menu_item_new_with_label("index")),
  help_license(gtk_menu_item_new_with_label("license")),
  help_cardset_license(gtk_menu_item_new_with_label("cardset license")),
  help_about(gtk_menu_item_new_with_label("about"))
  {
    DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_INIT,
		  "Menu::Menu(main_window)");

    DEBUG_RETURNING(VOID,
		    INFO_GTK_MENU && INFO_GTK_INIT,
		    "Menu::Menu(main_window)");
  } // Menu::Menu(MainWindow* const main_window)

  /**
   **
   ** inits the menu
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::init()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_INIT,
		    "Menu::init()");

      this->game_init();
      this->settings_init();
      { // party points
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->party_points)->child), "text",
			    new string("party points"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->party_points)->child));
	gtk_menu_bar_append(GTK_MENU_BAR(this->bar), this->party_points);
	gtk_signal_connect(GTK_OBJECT(this->party_points), "activate",
			   GTK_SIGNAL_FUNC(PartyPoints::show_event),
			   &(this->gui().playfield().table().party_points()));

      } // party points
      { // last trick
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->last_trick)->child), "text",
			    new string("last trick"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->last_trick)->child));
	gtk_menu_bar_append(GTK_MENU_BAR(this->bar), this->last_trick);
	gtk_signal_connect(GTK_OBJECT(this->last_trick), "activate",
			   GTK_SIGNAL_FUNC(LastTrick::show_event),
			   &(this->gui().playfield().table().last_trick()));
      } // last trick
      this->announcements_init();
      { // bug report
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->bug_report)->child), "text",
			    new string("bug report"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->bug_report)->child));
	gtk_menu_bar_append(GTK_MENU_BAR(this->bar), this->bug_report);
	gtk_signal_connect(GTK_OBJECT(this->bug_report), "activate",
			   GTK_SIGNAL_FUNC(BugReport::show_event),
			   &(this->gui().bug_report()));
      } // bug report
      this->help_init();


      // update the language
      this->language_update();

      // show all
      gtk_widget_show_all(this->bar);

      this->game_finish();
      this->game_close();
      this->party_finish();
      this->party_close();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_INIT,
		      "void Menu::init()");
    } // void Menu::init()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the gui
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  GTK_GUI&
    Menu::gui()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_VALUE,
		    "Menu::gui()");

      DEBUG_RETURNING(this->main_window().gui(),
		      INFO_GTK_MENU && INFO_GTK_VALUE,
		      "Menu::gui()");
    } // GTK_GUI& Menu::gui()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the main window
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  MainWindow&
    Menu::main_window()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_VALUE,
		    "Menu::main_window()");

      DEBUG_ASSERTION((this->main_window_p != NULL),
		      "Menu::main_window()\n"
		      "  `main_window_p` == NULL");

      DEBUG_RETURNING(*(this->main_window_p),
		      INFO_GTK_MENU && INFO_GTK_VALUE,
		      "Menu::main_window()");
    } // MainWindow& Menu::main_window()


  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the widget (menu bar)
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  GtkWidget*
    Menu::widget()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_VALUE,
		    "Menu::widget()");

      DEBUG_ASSERTION((bar != NULL),
		      "Menu::Game::widget()\n"
		      "  `bar` == NULL");

      DEBUG_RETURNING(bar,
		      INFO_GTK_MENU && INFO_GTK_VALUE,
		      "Menu::widget()");
    } // GtkWidget* Menu::widget()


  /**
   **
   ** a party is opened
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::party_open()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::party_open()");

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::party_open()");
    } // void Menu::party_open()

  /**
   **
   ** a party is started:
   ** deactivate 'announcements' and 'party points'
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::party_start()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::party_start()");

      gtk_widget_show(this->game_party_settings);
      gtk_widget_show(this->announcements_item);
      gtk_widget_show(this->last_trick);
      gtk_widget_set_sensitive(this->announcements_item, FALSE);
      gtk_widget_set_sensitive(this->last_trick, FALSE);
      gtk_widget_set_sensitive(this->party_points, TRUE);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::party_start()");
    } // void Menu::party_start()

  /**
   **
   ** a game is opened
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::game_open()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::game_open()");

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::game_open()");
    } // void Menu::game_open()

  /**
   **
   ** a game is started:
   ** activate the announcements
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::game_start()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::game_start()");

      // set the title to 'last trick' (can also be 'tricks')
      this->language_update();
      this->announcements_game_start();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::game_start()");
    } // void Menu::game_start()

  /**
   **
   ** the trick is opened:
   ** update the announcements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @version	0.4.4
   **
   ** author	Diether Knof
   **
   **/
  void
    Menu::trick_open()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::trick_open()");

      this->announcements_sensitivity_update();
      if (::party.game().trick_current_no() == 1)
	gtk_widget_set_sensitive(this->last_trick, TRUE);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::trick_open()");
    } // void Menu::trick_open()

  /**
   **
   ** the trick is full:
   ** update the announcements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @version	0.4.4
   **
   ** author	Diether Knof
   **
   **/
  void
    Menu::trick_full()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::trick_full()");

      this->announcements_sensitivity_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::trick_full()");
    } // void Menu::trick_full()

  /**
   **
   ** a game is finished:
   ** deactivate the announcements,
   ** change label 'last trick' to 'tricks'
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::game_finish()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::game_finish()");

      gtk_widget_set_sensitive(this->announcements_item, FALSE);

      // the title 'last trick' changes to 'tricks'
      this->language_update();

      // ***DK there is the problem, that the 'bug report' entry does not realign

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::game_finish()");
    } // void Menu::game_finish()

  /**
   **
   ** a game is closed:
   ** deactivate the 'last trick'
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::game_close()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::game_close()");

      gtk_widget_set_sensitive(this->last_trick, FALSE);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::game_close()");
    } // void Menu::game_close()

  /**
   **
   ** the party is finished
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::party_finish()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::party_finish()");

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::party_finish()");
    } // void Menu::party_finish()

  /**
   **
   ** the party is closed:
   ** remove 'announcements',
   ** deactivate 'party points'
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::party_close()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::party_close()");

      gtk_widget_hide(this->game_party_settings);
      gtk_widget_hide(this->announcements_item);
      gtk_widget_hide(this->last_trick);
      gtk_widget_set_sensitive(this->party_points, FALSE);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::party_close()");
    } // void Menu::party_close()

  /**
   **
   ** update the name of 'player'
   **
   ** @param	player	the player
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::name_update(Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_UPDATE,
		    "Menu::name_update(player)");

      this->announcements_name_update(player);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_UPDATE,
		      "Menu::name_update(player)");
    } // void Menu::name_update(player)

  /**
   **
   ** update the language
   **
   ** @param	player	the player
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::language_update()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_UPDATE,
		    "Menu::language_update()");

      for (vector<GtkLabel*>::iterator label = this->labels.begin();
	   label != this->labels.end();
	   label++)
	gtk_label_set_text(GTK_LABEL(*label),
			   ::language(*(static_cast<string*>(gtk_object_get_data(GTK_OBJECT(*label), "text")))).c_str());

      gtk_label_set_text(GTK_LABEL(GTK_BIN(this->last_trick)->child),
			 ((game_status != GAMESTATUS::GAME_FINISHED)
			  ? ::language("Last trick")
			  : ::language("Tricks")).c_str());

      this->announcements_language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_UPDATE,
		      "Menu::language_update()");
    } // void Menu::language_update()

} // namespace GTK_DOKO

#endif // #ifdef USE_UI_GTK
