/**********************************************************************
 *
 *   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_GTKMM

#include "genscher_info.h"
#include "table.h"
#include "ui.h"
#include "icons.h"

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

#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>

namespace UI_GTKMM_NS {

  // position of the window
  int GenscherInfo::position_x = INT_MAX;
  int GenscherInfo::position_y = INT_MAX;

  /**
   **
   ** Constructor
   **
   ** @param	table		the table
   ** @param	genscher	the player who has the genscher
   ** @param	partner		the new partner
   **
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  GenscherInfo::GenscherInfo(Table* const table,
			     Player const& genscher,
			     Player const& partner) :
    GameinfoDialog(table),
  icon(NULL),
  text(NULL),
  genscher(&genscher),
  partner(&partner)
  {
    DEBUG_CALLING(INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_INIT,
		  "GenscherInfo::GenscherInfo(table, genscher, partner)");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_INIT,
		    "GenscherInfo::GenscherInfo(table, genscher, partner)");
  } // GenscherInfo::GenscherInfo(Table* const table, Player const& genscher, Player const& partner)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  GenscherInfo::~GenscherInfo()
  {
    DEBUG_CALLING(INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_INIT,
		  "GenscherInfo::~GenscherInfo()");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_INIT,
		    "GenscherInfo::~GenscherInfo()");
  } // GenscherInfo::~GenscherInfo()

  /**
   **
   ** init the dialog
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherInfo::init()
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_INIT,
		    "GenscherInfo::init()");

      this->GameinfoDialog::init();

      this->ui->add_for_translation(*this,
				    ::translator("Genscher"));

      this->icon = Gtk::manage(new Gtk::Image(this->ui->icons->icon(this->partner->game().type())));
      this->text = Gtk::manage(new Gtk::Label("Genscher: Player chooses partner."));
      this->ui->add_for_translation(*(this->text),
				    ::translator("Genscher: %splayer% chooses %spartner%.",
						 this->genscher->name(),
						 this->partner->name()
						));

      Gtk::VBox* vbox = Gtk::manage(new Gtk::VBox(false, 20));
      vbox->set_border_width(20);

      vbox->add(*(this->icon));
      vbox->add(*(this->text));

      this->get_vbox()->add(*vbox);

      this->show_all_children();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_INIT,
		      "GenscherInfo::init()");
    } // void GenscherInfo::init()

  /**
   **
   ** show the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherInfo::show_information()
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_GAMEPLAY,
		    "GenscherInfo::show_information()");

      this->GameinfoDialog::show_information(::setting(Setting::SHOW_GENSCHER_WINDOW),
					     ::setting(Setting::CLOSE_GENSCHER_WINDOW_AUTOMATICALLY),
					     ::setting(Setting::GENSCHER_WINDOW_CLOSE_DELAY));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_GAMEPLAY,
		      "GenscherInfo::show_information()");
    } // void GenscherInfo::show_information()

  /**
   **
   ** the name of 'player' has changed
   **
   ** @param	player	the player whose name has changed
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherInfo::name_changed(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_UPDATE,
		    "GenscherInfo::name_changed(player)");

      if ((player == *(this->genscher))
	  || (player == *(this->partner))) {
	this->ui->change_translation(*(this->text),
				     ::translator("Genscher: %splayer% chooses %spartner%.",
						  this->genscher->name(),
						  this->partner->name()
						 ));
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_UPDATE,
		      "GenscherInfo::name_changed(player)");
    } // void GenscherInfo::name_changed(Player const& player)

  /**
   **
   ** update the icons
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherInfo::icons_update()
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_UPDATE,
		    "GenscherInfo::icons_update()");

      this->icon->set(this->ui->icons->icon(this->partner->game().type()));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_INFO && INFO_GTKMM_UPDATE,
		      "GenscherInfo::icons_update()");
    } // void GenscherInfo::icons_update()

  /**
   **
   ** save the position of the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GenscherInfo::save_position()
    {
      // Note: 'Gtk::Window::get_position()' does not give the actual position
      this->get_window()->get_root_origin(this->position_x, this->position_y);

      return ;
    } // void GenscherInfo::save_position()

  /**
   **
   ** move the window to the last position
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GenscherInfo::move_to_last_position()
    {
      if (this->position_x != INT_MAX)
	this->move(this->position_x, this->position_y);

      return ;
    } // void GenscherInfo::move_to_last_position()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
