/**********************************************************************
 *
 *   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 "gameinfo_dialog.h"
#include "table.h"
#include "ui.h"
#include "main_window.h"

#include "widgets/stock_button.h"
#include <gtkmm/stock.h>
#include <gtkmm/button.h>
#include <gtkmm/label.h>
#include <gtkmm/main.h>
#include <gdk/gdkkeysyms.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	table			the table
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   **/
  GameinfoDialog::GameinfoDialog(Table* const table) :
    Base(table),
  Gtk::Dialog("Gameinfo", *(table->ui->main_window), false, true),
  close_button(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_INIT,
		  "GameinfoDialog::GameinfoDialog(table)");

    this->signal_realize().connect(SigC::slot(*this,
					      &GameinfoDialog::init));

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_INIT,
		    "GameinfoDialog::GameinfoDialog(table)");
  } // GameinfoDialog::GameinfoDialog(Table* const table)

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_INIT,
		    "GameinfoDialog::~GameinfoDialog()");
  } // GameinfoDialog::~GameinfoDialog()

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

      this->set_icon(this->ui->main_window->icon);

      this->set_skip_taskbar_hint();

      this->close_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::CLOSE, "close"));
      this->ui->add_for_translation(*(this->close_button),
				    ::translator("close"));
      this->add_action_widget(*(this->close_button), Gtk::RESPONSE_CLOSE);

      this->close_button->grab_default();
      this->close_button->signal_clicked().connect(SigC::slot(*this, &GameinfoDialog::close));

      this->show_all_children();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_INIT,
		      "GameinfoDialog::init()");
    } // void GameinfoDialog::init()

  /**
   **
   ** show the information
   **
   ** @param	show			whether to show the window
   ** @param	close_automatically	whether to close the window automatically
   ** @param	close_delay		the time (in tenth of seconds) to wait before closing the window
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GameinfoDialog::show_information(bool const show,
				     bool const close_automatically,
				     unsigned const close_delay)
    {
      DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_GAMEPLAY,
		    "GameinfoDialog::show_information(show, close_automatically, close_delay)");

      if (show) {
	{ // move the window to the upper left corner of the main window
	  int x, y;
	  // I have to go over 'Gdk::Window' to get the position of the table
	  this->ui->table->get_window()->get_origin(x, y);
	  this->move(x, y);
	}
	this->move_to_last_position();
	this->present();

	SigC::Connection timeout_connection;
	if (close_automatically)
	  timeout_connection
	    = Glib::signal_timeout().connect(SigC::Slot0<bool>(SigC::slot(*this, &GameinfoDialog::on_timeout)),
					     close_delay);

	while ((::game_status & GAMESTATUS::GAME)
	       && (this->is_visible()))
	  Gtk::Main::iteration();

	if (close_automatically)
	  timeout_connection.disconnect();

	this->save_position();
	this->hide();

      } // if (show)

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_GAMEPLAY,
		      "GameinfoDialog::show_information(show, close_automatically, close_delay)");
    } // void GameinfoDialog::show_information(bool const show, bool const close_automatically, unsigned const close_delay)

  /**
   **
   ** show the information and close it on a click
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GameinfoDialog::show_information()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_GAMEPLAY,
		    "GameinfoDialog::show_information()");

      this->show_information(true, false, UINT_MAX);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_GAMEPLAY,
		      "GameinfoDialog::show_information()");
    } // void GameinfoDialog::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
    GameinfoDialog::name_changed(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_UPDATE,
		    "GameinfoDialog::name_changed(player)");

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

  /**
   **
   ** close the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GameinfoDialog::close()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_UPDATE,
		    "GameinfoDialog::close()");

      this->hide();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_UPDATE,
		      "GameinfoDialog::close()");
    } // void GameinfoDialog::close()

  /**
   **
   ** the timeout event:
   ** close the window and exit the main loop
   **
   ** @param	-
   **
   ** @return	false (to stop the timeout event)
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  bool
    GameinfoDialog::on_timeout()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_UPDATE,
		    "GameinfoDialog::on_timeout()");

      this->close();
      if (Gtk::Main::level() > 0)
	Gtk::Main::quit();

      DEBUG_RETURNING(false,
		      INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_UPDATE,
		      "GameinfoDialog::on_timeout()");
    } // bool GameinfoDialog::on_timeout()

  /**
   **
   ** a key has been pressed
   **
   ** @param	key	the key
   **
   ** @return	whether the key was accepted
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  bool
    GameinfoDialog::on_key_press_event(GdkEventKey* key)
    {
      DEBUG_CALLING(INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_EVENT,
		    "GameinfoDialog::on_key_press_event(key)");

      bool accepted = false;

      if ((key->state & ~GDK_SHIFT_MASK) == 0) {
	switch (key->keyval) {
	case GDK_Escape: // close the window
	  accepted = true;
	  this->close();
	  break;
	} // switch (key->keyval)
      } // if ((key->state & ~GDK_SHIFT_MASK) == 0)

      DEBUG_RETURNING((accepted
		       || this->Dialog::on_key_press_event(key)
		       || this->ui->key_press(key)),
		      INFO_GTKMM_GAMEINFO_DIALOG && INFO_GTKMM_EVENT,
		      "GameinfoDialog::on_key_press_event(key)");
    } // bool GameinfoDialog::on_key_press_event(GdkEventKey* key)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
