/**********************************************************************
 *
 *   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 "names.h"
#include "gtk_gui.h"

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

namespace GTK_DOKO {

  /**
   *
   * constructor
   *
   * @param	table	the table, the names belong to
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   * @bug		takes the players from 'party' and not from 'game'
   *
   */
  Names::Names(Table* table) :
    HTINBaseGroup(table),
  font_p(NULL),
  gc_p(gdk_gc_new(this->gui().ref_window)),
  gc_active_p(gdk_gc_new(this->gui().ref_window))
  {
    DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_INIT,
		  "Names::Names()");

    for (unsigned p = 0; p < ::party.playerno(); p++)
      this->object_p.push_back(new Name(this, ::party.player(p)));

    this->font_load();
    this->font_color_set();
    this->active_font_color_set();

    DEBUG_RETURNING(VOID,
		    INFO_GTK_NAME && INFO_GTK_INIT,
		    "Names::Names()");
  } // Names::Names()

  /**
   *
   * -> result
   *
   * @param	player	the player, whose name is returned
   *
   * @return	the name of the given player
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  Name&
    Names::name(Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_VALUE,
		    "Names::name(player)");

      DEBUG_RETURNING(static_cast<Name&>(this->object(player)),
		      INFO_GTK_NAME && INFO_GTK_VALUE,
		      "Names::name(player)");
    } // Name& Names::name(Player const& player)

  /**
   *
   * -> result
   *
   * @param	-
   *
   * @return	the font for the names
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  GdkFont*
    Names::font()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_VALUE,
		    "Names::font()");

      DEBUG_ASSERTION(this->font_p != NULL,
		      "Names::font()\n"
		      "  'font_p' == NULL");

      DEBUG_RETURNING(this->font_p,
		      INFO_GTK_NAME && INFO_GTK_VALUE,
		      "Names::font()");
    } // GdkFont* Names::font()

  /**
   *
   * GdkGC* Names::gc()
   *
   * @param	-
   *
   * @return	the gc for the names
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  GdkGC*
    Names::gc()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_VALUE,
		    "Names::gc()");

      DEBUG_ASSERTION(this->gc_p != NULL,
		      "Names::gc()\n"
		      "  'gc_p' == NULL");

      DEBUG_RETURNING(this->gc_p,
		      INFO_GTK_NAME && INFO_GTK_VALUE,
		      "Names::gc()");
    } // GdkGC* Names::gc()

  /**
   **
   ** GdkGC* Names::gc()
   **
   ** @param	-
   **
   ** @return	the gc for the names of the active players
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  GdkGC*
    Names::gc_active()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_VALUE,
		    "Names::gc_active()");

      DEBUG_ASSERTION(this->gc_active_p != NULL,
		      "Names::gc_active()\n"
		      "  'gc_active_p' == NULL");

      DEBUG_RETURNING(this->gc_active_p,
		      INFO_GTK_NAME && INFO_GTK_VALUE,
		      "Names::gc_active()");
    } // GdkGC* Names::gc_active()

  /**
   *
   * sets the names of all players
   *
   * @param	-
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  void
    Names::set()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_UPDATE,
		    "Names::set()");

      if (::game_status & GAMESTATUS::GAME)
	for(vector<Player*>::const_iterator player = ::party.game().player_begin();
	    player != ::party.game().player_end();
	    player++)
	  this->name(**player).set();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_NAME && INFO_GTK_UPDATE,
		      "Names::set()");
    } // void Names::set()

  /**
   *
   * load the font
   *
   * @param	-
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   * @todo	GTK 2.0
   *
   */
  void
    Names::font_load()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_UPDATE,
		    "Names::font_load()");

#ifdef USE_UI_GTK_2_0
      // *** ToDo DK
      this->font_p = gdk_fontset_load("-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
#else
      GdkFont* font_new =
	gdk_fontset_load(::setting(Setting::NAME_FONT).c_str());
      if (font_new == NULL) {

	// stay by the old font
	if (this->font_p) {
	  DEBUG_RETURNING(VOID,
			  INFO_GTK_NAME && INFO_GTK_UPDATE,
			  "Names::font_load()");
	}


	font_new = gdk_fontset_load("-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
      } // if (font_new == NULL)

      if (this->font_p)
	gdk_font_unref(this->font_p);
      this->font_p = font_new;
#endif

      DEBUG_RETURNING(VOID,
		      INFO_GTK_NAME && INFO_GTK_UPDATE,
		      "Names::font_load()");
    } // int Names::font_load()

  /**
   *
   * set the color for the font
   *
   * @param	-
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  void
    Names::font_color_set()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_UPDATE,
		    "Names::font_color_set()");

      GdkColor color;
      // translate the color from the name to a GdkColor
      if (gdk_color_parse(::setting(Setting::NAME_FONT_COLOR).c_str(),
			  &color)) {
	if (gdk_colormap_alloc_color(gdk_colormap_get_system(),
				     &color, FALSE, TRUE)) {
	  // set the color
	  gdk_gc_set_foreground(this->gc_p, &color);

	} // if (!gdk_colormap_alloc_color)
      } // if (!gdk_color_parse)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_NAME && INFO_GTK_UPDATE,
		      "Names::font_color_set()");
    } // int Names::font_color_set()

  /**
   **
   ** set the color for the font of the active player
   **
   *** @param	-
   **
   ** @return	-
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    Names::active_font_color_set()
    {
      DEBUG_CALLING(INFO_GTK_NAME && INFO_GTK_UPDATE,
		    "Names::active_font_color_set()");

      GdkColor color;
      // translate the color from the name to a GdkColor
      if (gdk_color_parse(::setting(Setting::NAME_ACTIVE_FONT_COLOR).c_str(),
			  &color)) {
	if (gdk_colormap_alloc_color(gdk_colormap_get_system(),
				     &color, FALSE, TRUE)) {
	  // set the color
	  gdk_gc_set_foreground(this->gc_active_p, &color);

	} // if (!gdk_colormap_alloc_color)
      } // if (!gdk_color_parse)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_NAME && INFO_GTK_UPDATE,
		      "Names::active_font_color_set()");
    } // int Names::active_font_color_set()

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