/**********************************************************************
 *
 *   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 "ui.h"
#include "splash_screen.h"
#include "cards.h"
#include "icons.h"
#include "party_settings.h"
#include "rules.h"
#include "table.h"
#include "hand.h"
#include "icongroup.h"

#include "../../basistypes.h"
#include "../../party/party.h"
#include "../../misc/setting.h"

#include <gtkmm/label.h>
#include <gtkmm/button.h>
#include <gtkmm/textview.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** updates all language elements
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::language_update()
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::language_update()");

      for (vector<Gtk::Label*>::iterator l = this->translation_label.begin();
	   l != this->translation_label.end();
	   l++) {
	(*l)->set_text(Glib::locale_to_utf8((static_cast<Translator::Translation*>((*l)->get_data("translation"))->translation())));
      }

      for (vector<Gtk::Button*>::iterator b = this->translation_button.begin();
	   b != this->translation_button.end();
	   b++) {
	(*b)->set_label(Glib::locale_to_utf8((static_cast<Translator::Translation*>((*b)->get_data("translation"))->translation())));
      }

      for (vector<Gtk::Window*>::iterator w = this->translation_window.begin();
	   w != this->translation_window.end();
	    w++) {
	(*w)->set_title(Glib::locale_to_utf8((static_cast<Translator::Translation*>((*w)->get_data("translation"))->translation())));
      }

      for (vector<Gtk::TextView*>::iterator tv
	   = this->translation_text_view.begin();
	   tv != this->translation_text_view.end();
	   tv++)
	(*tv)->get_buffer()->set_text(Glib::locale_to_utf8((static_cast<Translator::Translation*>((*tv)->get_data("translation"))->translation())));

      for (vector<Base*>::iterator p = this->parts.begin();
	   p != this->parts.end();
	   p++)
	(*p)->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::language_update()");
    } // void UI_GTKMM::language_update()

  /**
   **
   ** updates all cards
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::cards_update()
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::cards_update()");

      for (vector<Base*>::iterator p = this->parts.begin();
	   p != this->parts.end();
	   p++)
	(*p)->cards_update();

      if (this->table)
	this->table->mouse_cursor_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::cards_update()");
    } // void UI_GTKMM::cards_update()

  /**
   **
   ** updates all cards back
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::cards_back_update()
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::cards_back_update()");

      for (vector<Base*>::iterator p = this->parts.begin();
	   p != this->parts.end();
	   p++)
	(*p)->cards_back_update();

      if (this->table)
	this->table->mouse_cursor_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::cards_back_update()");
    } // void UI_GTKMM::cards_back_update()

  /**
   **
   ** updates all icons
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::icons_update()
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::icons_update()");

      for (vector<Base*>::iterator p = this->parts.begin();
	   p != this->parts.end();
	   p++)
	(*p)->icons_update();

      if (this->table)
	this->table->mouse_cursor_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::icons_update()");
    } // void UI_GTKMM::icons_update()

  /**
   **
   ** redraw all (of the playfield)
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::redraw_all()
    { 
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::redraw_all()");

      if (this->table) {
	this->table->draw_all();
	this->table->mouse_cursor_update();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::redraw_all()");
    } // void UI_GTKMM::redraw_all()

  /**
   **
   ** update the gametype icon
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::gametype_update()
    { 
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::gametype_update()");

      if (this->table)
	this->table->draw_all();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::gametype_update()");
    } // void UI_GTKMM::gametype_udpate()

  /**
   **
   ** update the 'player'
   **
   ** @param	player	the player
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   ** @todo	all
   **
   **/
  void
    UI_GTKMM::player_update(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::player_update(player)");

      if (this->party_settings)
	this->party_settings->player_update(player);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::player_update(player)");
    } // void UI_GTKMM::player_update(player)

  /**
   **
   ** update the name of 'player'
   **
   ** @param	player	the player
   **
   ** @return	-
   **
   ** @version	0.5.2
   **
   ** @author	Diether Knof
   **
   ** @todo	all
   **
   **/
  void
    UI_GTKMM::name_update(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::name_update(player)");

      if (this->party_settings)
	this->party_settings->name_update(player);
      if (this->table) {
	this->table->name_changed(player);
	this->table->mouse_cursor_update();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::name_update(player)");
    } // void UI_GTKMM::name_update(player)

  /**
   **
   ** update the hand of 'player'
   **
   ** @param	player	the player
   **
   ** @return	-
   **
   ** @version	0.6.0
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::hand_update(Player const& player)
    { 
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::hand_update(player)");

      if (this->table) {
	this->table->hand(player).draw();
	this->table->mouse_cursor_update();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::hand_update(player)");
    } // void UI_GTKMM::hand_update(player)

  /**
   **
   ** update the team icon of 'player'
   **
   ** @param	player	the player
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::teaminfo_update(Player const& player)
    { 
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::teaminfo_update(player)");

      if (this->table) {
	this->table->icongroup(player).draw_team();
	this->table->mouse_cursor_update();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::teaminfo_update(player)");
    } // void UI_GTKMM::teaminfo_udpate(player)

  /**
   **
   ** update the rule
   **
   ** @param	type		the rule, that has changed
   ** @param	old_value	old value
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::rule_update(int const type, void const* const old_value)
    {
      DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		    "UI_GTKMM::rule_update(type, old_value)");

      if (this->party_settings)
	this->party_settings->rules->update(type);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		      "UI_GTKMM::rule_update(type, old_value)");
    } // void UI_GTKMM::rule_update(int const type, void const* const old_value)

  /**
   **
   ** update the setting
   **
   ** @param	type		the setting, that has changed
   ** @param	old_value	old value
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   ** @todo	some
   **
   **/
  void
    UI_GTKMM::setting_update(int const type, void const* const old_value)
    {
      DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		    "UI_GTKMM::setting_update(type)");

      switch(type) {
      case Setting::NAME:
	for (unsigned p = 0; p < ::party.playerno(); p++)
	  this->name_update(::party.player(p));
	break;
      case Setting::CARDSET:
	if (this->cards)
	  this->cards->load_all();
	if (this->icons)
	  this->icons->load();
	if (this->cards)
	  this->cards_update();
	break;
      case Setting::CARDS_BACK:
	if (this->cards) {
	  this->cards->load_back();
	  this->cards_update();
	}
	break;
      case Setting::ORIGINAL_CARDS_SIZE:
	if (::setting(Setting::ORIGINAL_CARDS_SIZE) == true)
	  ::setting.set(Setting::CARDS_HEIGHT, this->cards->height_original());
      case Setting::CARDS_HEIGHT:
	if (this->cards)
	  this->cards->update_scaling();
	if (this->icons)
	  this->icons->update_scaling();
	if (this->cards)
	  this->cards_update();
	break;
      case Setting::SHOW_KNOWN_TEAMS_IN_GAME:
      case Setting::SHOW_SOLOPLAYER_IN_GAME:
	if (this->table)
	  this->table->draw_all();
	break;
      default:
	break;
      } // switch(type)

      for (vector<Base*>::iterator p = this->parts.begin();
	   p != this->parts.end();
	   p++)
	(*p)->setting_update(type);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		      "UI_GTKMM::setting_update(type)");
    } // void UI_GTKMM::setting_update(int const type, void const* const old_value)

  /**
   **
   ** the status message has changed
   **
   ** @param	status_message	the status message
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::status_message_changed(Translator::Translation const& status_message)
    {
      DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		    "UI_GTKMM::status_message_changed(status_message)");

      if (this->splash_screen)
	this->splash_screen->status_message_changed(status_message);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		      "UI_GTKMM::status_message_changed(status_message)");
    } // void UI_GTKMM::status_message_changed(Translator::Translation const& status_message)

  /**
   **
   ** the progress has changed
   **
   ** @param	progress	the progress
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::progress_changed(double const progress)
    {
      DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		    "UI_GTKMM::progress_changed(progress)");

      if (this->splash_screen)
	this->splash_screen->progress_changed(progress);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		      "UI_GTKMM::progress_changed(progress)");
    } // void UI_GTKMM::progress_changed(double const progress)

  /**
   **
   ** the progress is finished
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::progress_finished()
    {
      DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		    "UI_GTKMM::progress_finished()");

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI && INFO_GTKMM_UPDATE,
		      "UI_GTKMM::progress_finished()");
    } // void UI_GTKMM::progress_finished()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
