/**********************************************************************
 *
 *   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 "genscher.info.h"

#include "gtk_gui.h"

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


namespace GTK_DOKO {
#include <gtk/gtklabel.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkdialog.h>
#include <gtk/gtkmain.h>

  /**
   **
   ** constructor
   **
   ** @param	playfield	the playfield
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  GenscherInfo::GenscherInfo(Playfield* const playfield) :
    PlayfieldDialog(playfield, "Genscher"),
  genscher_player(NULL),
  genscher_partner(NULL),
  information(gtk_label_new("Genscher: %splayer% chooses %spartner%."))
  {
    DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_INIT,
		  "GenscherInfo::GenscherInfo(playfield)");

    GtkWidget* vbox = gtk_vbox_new(FALSE, 10);

    gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);

    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->vbox),
		      vbox);
    gtk_container_add(GTK_CONTAINER(vbox),
		      this->information);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->action_area),
		      this->button_close);

    DEBUG_RETURNING(VOID,
		    INFO_GTK_GENSCHER && INFO_GTK_INIT,
		    "GenscherInfo::GenscherInfo(playfield)");
  } // GenscherInfo::GenscherInfo(Playfield* const playfield)

  /**
   **
   ** destructor
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  GenscherInfo::~GenscherInfo()
  {
    DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_INIT,
		  "GenscherInfo::~GenscherInfo()");

    delete this->information;

    DEBUG_RETURNING(VOID,
		    INFO_GTK_GENSCHER && INFO_GTK_INIT,
		    "GenscherInfo::~GenscherInfo()");
  } // GenscherInfo::~GenscherInfo()

  /**
   **
   ** event:  a player is selected
   **
   ** @param	player	player with genscher
   ** @param	partner	selected partner
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    GenscherInfo::genscher(Player const& player,
			   Player const& partner)
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_EVENT,
		    "GenscherInfo::genscher(player, partner)");

      this->genscher_player = &player;
      this->genscher_partner = &partner;

      gtk_widget_show_all(GTK_DIALOG(this->window())->vbox);
      gtk_widget_show_all(GTK_DIALOG(this->window())->action_area);

      this->language_update();
      this->show();

      // wait until the window is closed
      while ((::game_status & GAMESTATUS::GAME_PLAY)
	     && (GTK_WIDGET_VISIBLE(this->window())))
	gtk_main();

      // close the window
      this->close();

      this->genscher_player = NULL;
      this->genscher_partner = NULL;

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_EVENT,
		      "GenscherInfo::genscher(player, partner)");
    } // void GenscherInfo::genscher(Player const& player, Player const& partner)

  /**
   **
   ** update the playername
   **
   ** @param	player	player with the new name
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    GenscherInfo::name_update(Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		    "GenscherInfo::name_update(player)");

      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		      "GenscherInfo::name_update(player)");
    } // void GenscherInfo::name_update(Player const& player)

  /**
   **
   ** update the language
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    GenscherInfo::language_update()
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		    "GenscherInfo::language_update()");

      if (this->genscher_player && this->genscher_partner)
	gtk_label_set_text(GTK_LABEL(this->information),
			   (" "
			    + ::language("Genscher: %splayer% chooses %spartner%.",
					 this->genscher_player->name().c_str(),
					 this->genscher_partner->name().c_str())
			    + " "
			   ).c_str());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		      "GenscherInfo::language_update()");
    } // void GenscherInfo::language_update()

} // namespace GTK_DOKO
#endif // #ifdef USE_UI_GTK
