/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   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_cardtableinfo.cc

#include "cardwords_gtk_cardtableinfo.hh"
#include "cardwords_stringconverters.hh"
#include <gtk--/label.h>

CardWords_Gtk_CardTableInfo::
CardWords_Gtk_CardTableInfo(const CardWords_CardTable * card_table,
                      
                            bool use_digits_for_the_columns,
                            // and chars for the rows, otherwise the other way
                            // round

                            bool column_indices_grow_rightwards,
                            // if true, the rightmost column has the greatest
                            // index

                            bool row_indices_grow_upwards)
  
  : card_table_width(card_table->get_width()),
    card_table_height(card_table->get_height()),
    info_rows(9)
{
  this->use_digits_for_the_columns = use_digits_for_the_columns;
  this->column_indices_grow_rightwards = column_indices_grow_rightwards;
  this->row_indices_grow_upwards = row_indices_grow_upwards;

  size_t act_width;

  column_width = 0;
  
  title_label = new Gtk::Label(gettext("Cell under pointer info:"));
  pack_start(*title_label, false, false);
  title_label->show();

  list = new Gtk::CList(2);

  pack_start(*list);

  list->column_titles_hide();
  list->set_column_justification(0, GTK_JUSTIFY_LEFT);
  list->set_column_justification(1, GTK_JUSTIFY_LEFT);
  
  info_rows[0].push_back(gettext("Cell at:"));
  info_rows[0].push_back("");
  list->append(info_rows[0]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               gettext("Cell at:"));
  if (act_width > column_width) {
    column_width = act_width;
  }
  
  info_rows[1].push_back(gettext("Word factor:"));
  info_rows[1].push_back("");
  list->append(info_rows[1]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Word factor:")));
  if (act_width > column_width) {
    column_width = act_width;
  }
  
  info_rows[2].push_back(gettext("Letter factor:"));
  info_rows[2].push_back("");
  list->append(info_rows[2]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Letter factor:")));
  if (act_width > column_width) {
    column_width = act_width;
  }

  info_rows[3].push_back(gettext("Cell points:"));
  info_rows[3].push_back("");
  list->append(info_rows[3]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Cell points:")));
  if (act_width > column_width) {
    column_width = act_width;
  }

  info_rows[4].push_back(gettext("Cell blocked:"));
  info_rows[4].push_back("");
  list->append(info_rows[4]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Cell points:")));
  if (act_width > column_width) {
    column_width = act_width;
  }
  info_rows[5].push_back(gettext("Contains a card:"));
  info_rows[5].push_back("");
  list->append(info_rows[5]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Contains a card:")));
  if (act_width > column_width) {
    column_width = act_width;
  }

  info_rows[6].push_back(gettext("Is wildcard:"));
  info_rows[6].push_back("");
  list->append(info_rows[6]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Is wildcard:")));
  if (act_width > column_width) {
    column_width = act_width;
  }
  
  info_rows[7].push_back(gettext("Card means:"));
  info_rows[7].push_back("");
  list->append(info_rows[7]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Card means:")));
  if (act_width > column_width) {
    column_width = act_width;
  }
         
  info_rows[8].push_back(gettext("Card's points:"));
  info_rows[8].push_back("");
  list->append(info_rows[8]);
  act_width = gdk_string_width(list->get_style()->get_font(),
                               (gettext("Card's points:")));
  if (act_width > column_width) {
    column_width = act_width;
  }
  list->set_column_width(0,column_width);
  list->set_column_width(1,column_width);
  list->set_selection_mode(GTK_SELECTION_EXTENDED);
  //  list->set_policy(GTK_POLICY_AUTOMATIC,
  //                   GTK_POLICY_AUTOMATIC); // No scrollbars

  list->set_usize(2 * ( 12 + column_width) ,
                  (list->get_style()->get_font().gdkobj()->ascent
                   + list->get_style()->get_font().gdkobj()->descent
                   + 3) * 10
                  );
  //  list->set_border(GTK_SHADOW_NONE);

  list->show();
}

CardWords_Gtk_CardTableInfo::~CardWords_Gtk_CardTableInfo()
{
  title_label->hide();
  remove(*title_label);
  delete title_label;
  
  list->hide();
  remove(*list);
  delete list;
}  

void
CardWords_Gtk_CardTableInfo::
show_info_about_cell(const CardWords_Gtk_CardCell *cell)
{
  char str[4] = {0,0,0,0};
  CardWords_CardTableLocation loc;
  char user_char_coordinate;
  size_t user_digit_coordinate;
  
  list->freeze();
  
  loc.first  = cell->get_coordinate() % card_table_width;
  loc.second = cell->get_coordinate() / card_table_width;

  if (use_digits_for_the_columns) {
    if (column_indices_grow_rightwards) {
      user_digit_coordinate = loc.first + 1;
    }
    else {
      // !column_indices_grow_rightwards
      user_digit_coordinate = card_table_width - loc.first;
    }
    if (row_indices_grow_upwards) {
      user_char_coordinate = 'A' + 
        card_table_height - loc.second - 1 ;
    }
    else {
      // !row_indices_grow_upwards
      user_char_coordinate = loc.second + 'A';
    }
  }
  else {
    // !use_digits_for_the_columns
    if (row_indices_grow_upwards) {
      user_digit_coordinate = card_table_height - loc.second;
    }
    else {
      // !row_indices_grow_upwards
      user_digit_coordinate = loc.second + 1;
    }
    if (column_indices_grow_rightwards) {
      user_char_coordinate = loc.first + 'A';
    }
    else {
      // !column_indices_grow_rightwards
      user_char_coordinate = 'A' - 1 + 
        card_table_width - loc.first;
    }
  }

  
  str[0] = user_char_coordinate;

  if (user_digit_coordinate < 10) {
    str[1] = '0' + user_digit_coordinate;
  }
  else {
    str[1] = '0' + user_digit_coordinate / 10;
    str[2] = '0' + user_digit_coordinate % 10;
  }
  
  list->set_text(0, 1, str);

  if (cell->is_cell_blocked() == true) {
    list->set_text(1, 1, "-"); // word_factor
    list->set_text(2, 1, "-"); // letter_factor
    list->set_text(3, 1, "-"); // cell_points
    list->set_text(4, 1, gettext("yes")); // cell_blocked
    list->set_text(5, 1, "-"); // contains_card
    list->set_text(6, 1, "-"); // Is wildcard
    list->set_text(7, 1, "-"); // Card means
  }
  else {
    // cell not blocked:
    list->set_text(1, 1, int2string(cell->get_word_factor()));
    list->set_text(2, 1, int2string(cell->get_letter_factor()));
    list->set_text(3, 1, int2string(cell->get_cell_points()));

    list->set_text(4, 1, gettext("no")); // cell_blocked entry
    
    if (cell->get_cellcard() == 0) {
      // no card contained:
      list->set_text(5, 1, gettext("no")); // contains_card

      list->set_text(6, 1, "-"); // Is wildcard

      list->set_text(7, 1, "-"); // Card means

      list->set_text(8, 1, "-"); // Card's points
    }
    else {
      list->// Mark the pre-cards here.
        set_text(5, 1, ( cell->get_cellcard()->is_pre()
                         ? gettext("as test")
                         : gettext("yes")));

      list->
        set_text(6, 1, (cell->get_cellcard()->is_wildcard()
                        ? gettext("yes")
                        : gettext("no")));

      str[0] = cell->get_cellcard()->get_visible();
      str[1] = 0;
      list->set_text(7, 1, str);

      list->set_text(8, 1, int2string(cell->get_cellcard()->get_points()));
    }
  }
  list->thaw();
}
