/*
  This file is part of CardWords.
  (C) 1999 Tobias Peters

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

// cardwords_gtk_playerlist.cc

#include "cardwords_gtk_playerlist.hh"
#include "cardwords_exceptions.hh"
#include <gtk--/style.h>

CardWords_Gtk_PlayerList::
CardWords_Gtk_PlayerList(const CardWords_PlayerList * p)
  : Gtk::CList(5),
    min_width(0)
{
  size_t act_width;
  
  set_selection_mode(GTK_SELECTION_SINGLE);
  //  set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); // No scrollbars

  // set_border(GTK_SHADOW_NONE);

  column_titles_show();

  set_column_title(0, gettext("Type"));
  set_column_justification(0, GTK_JUSTIFY_LEFT);
  act_width = gdk_string_width(get_style()->get_font(),
                               gettext("Type"));
  set_column_width(0, act_width);
  min_width += act_width;
  
  set_column_title(1, gettext("Player's name"));
  set_column_justification(1, GTK_JUSTIFY_LEFT);
  act_width = gdk_string_width(get_style()->get_font(),
                               gettext("Player's name"));
  set_column_width(1, act_width);
  min_width += act_width;
  
  set_column_title(2, gettext("Cards"));
  set_column_justification(2, GTK_JUSTIFY_RIGHT);
  act_width = gdk_string_width(get_style()->get_font(),
                               gettext("Cards"));
  set_column_width(2, act_width);
  min_width += act_width;
  
  set_column_title(3, gettext("Points"));
  set_column_justification(3, GTK_JUSTIFY_RIGHT);
  act_width = gdk_string_width(get_style()->get_font(),
                               gettext("Points"));
  set_column_width(3, act_width);
  min_width += act_width;

  set_column_title(4, gettext("Last"));
  set_column_justification(4, GTK_JUSTIFY_RIGHT);
  act_width = gdk_string_width(get_style()->get_font(),
                               gettext("Last"));
  set_column_width(4, act_width);
  min_width += act_width;
  
  column_titles_passive();
  update(p);
}

void
CardWords_Gtk_PlayerList::update(const CardWords_PlayerList *playerlist)
{
  size_t index;
  vector<string> columns(5);

  freeze();
  clear();

  if (playerlist != 0) {
    for (index = 0; index < playerlist->size(); ++index) {
      columns[0] = ( playerlist->is_robot(index)
                     ? gettext("R") // R from Robot
                     : gettext("H") // H from Human
                     );
      columns[1] = playerlist->get_name(index);

      ostrstream ostr[3];

      ostr[0] << playerlist->get_no_of_cards_of(index)
              << ends;
      ostr[1] << playerlist->get_points_of(index)
              << ends;
      ostr[2] << playerlist->get_last_points_of(index)
              << ends;
      ostr[0].freeze(true);
      ostr[1].freeze(true);
      ostr[2].freeze(true);
      
      columns[2] = ostr[0].str();
      columns[3] = ostr[1].str();
      columns[4] = ostr[2].str();

      ostr[0].freeze(false);
      ostr[1].freeze(false);
      ostr[2].freeze(false);

      append(columns);

      if (playerlist->get_active_player_pos() == index) {
        cause_select_row(index,1);
      }
    }
  }
  set_usize(min_width + 5 * 10,
            (get_style()->get_font().gdkobj()->ascent
             + get_style()->get_font().gdkobj()->descent
             + 3) * (playerlist->size() + 3)
            );
  thaw();
}
