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

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

namespace GTK_DOKO {

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

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

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

    DEBUG_RETURNING(VOID,
		    INFO_GTK_TRICKPILE && INFO_GTK_INIT,
		    "TrickPiles::TrickPiles()");
  } // TrickPiles::TrickPiles()

  /**
   *
   * destructor
   *
   * @param	-
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  TrickPiles::~TrickPiles()
  {
    DEBUG_CALLING(INFO_GTK_TRICKPILE && INFO_GTK_INIT,
		  "TrickPiles::~TrickPiles()");

    DEBUG_RETURNING(VOID,
		    INFO_GTK_TRICKPILE && INFO_GTK_INIT,
		    "TrickPiles::~TrickPiles()");
  } // TrickPiles::~TrickPiles()

  /**
   *
   * -> result
   *
   * @param	player	the player, whose trickpile is returned
   *
   * @return	The trickpile of the given player
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  TrickPile&
    TrickPiles::trickpile(Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_TRICKPILE && INFO_GTK_VALUE,
		    "TrickPiles::trickpile(player)");

      DEBUG_RETURNING(static_cast<TrickPile&>(this->object(player)),
		      INFO_GTK_TRICKPILE && INFO_GTK_VALUE,
		      "TrickPiles::trickpile(player)");
    } // TrickPile& TrickPiles::trickpile(player)

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

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

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

  /**
   *
   * -> result
   *
   * @param	-
   *
   * @return	the gc for the trickpiles
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  GdkGC*
    TrickPiles::gc()
    {
      DEBUG_CALLING(INFO_GTK_TRICKPILE && INFO_GTK_VALUE,
		    "TrickPiles::gc()");

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

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

  /**
   *
   * loads the font for the trickpile points
   *
   * @param	-
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   * @todo	GTK 2.0
   *
   */
  void
    TrickPiles::font_load()
    {
      DEBUG_CALLING(INFO_GTK_TRICKPILE && INFO_GTK_UPDATE,
		    "TrickPiles::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::TRICKPILE_POINTS_FONT).c_str());
      if (font_new == NULL) {

	// stay by the old font
	if (this->font_p) {
	  DEBUG_RETURNING(VOID,
			  INFO_GTK_TRICKPILE && INFO_GTK_UPDATE,
			  "TrickPiles::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_TRICKPILE && INFO_GTK_UPDATE,
		      "TrickPiles::font_load()");
    } // void TrickPiles::font_load()

  /**
   *
   * sets the color for the trickpile points
   *
   * @param	-
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  void
    TrickPiles::font_color_set()
    {
      DEBUG_CALLING(INFO_GTK_TRICKPILE && INFO_GTK_UPDATE,
		    "TrickPiles::font_color_set()");

      GdkColor color;
      // translate the color from the name to a GdkColor
      if (gdk_color_parse(::setting(Setting::TRICKPILE_POINTS_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_TRICKPILE && INFO_GTK_UPDATE,
		      "TrickPiles::font_color_set()");
    } // int TrickPiles::font_color_set()

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