/* 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_handdisplay.cc

#include "cardwords_gtk_handdisplay.hh"

CardWords_Gtk_HandDisplay::cell::cell () // init with no card;
{
  gtk_card_cell = 0;
  card = old_card = 0;
}
CardWords_Gtk_HandDisplay::cell::~cell() // delete the card cell if != 0
{
  delete gtk_card_cell;
}
void
CardWords_Gtk_HandDisplay::cell::set(CardWords_Hand::const_iterator iter)
  // set to this card
{
  assert (gtk_card_cell != 0);
  old_card = 0;
  card = *iter;
  gtk_card_cell->set_card(card);
}
void
CardWords_Gtk_HandDisplay::cell::set(CardWords_Card * t)
{
  assert (gtk_card_cell != 0);
  if (t == 0) {
    old_card = card;
  }
  else {
    old_card = 0;
  }
  card = t;
  gtk_card_cell->set_card(card);
}



CardWords_Gtk_HandDisplay::
CardWords_Gtk_HandDisplay(const
                          CardWords_Gtk_Common_Card_Cell_Styles *
                          styles,
                          
                          const CardWords_Hand * hand)
  
  : cells(hand->total_places())
{
  this->styles = styles;
  this->hand = hand;

  set_homogeneous(false);
  set_spacing(0);

  size_t index;
  for (index = 0; index < cells.size(); ++index) {
    cells[index].gtk_card_cell =
      new CardWords_Gtk_CardCell (styles, size_t(-1));
    //                       The coordinate ^ doesn't matter
    pack_start(*cells[index].gtk_card_cell, false, false);
    cells[index].gtk_card_cell->show();
    cells[index].gtk_card_cell->clicked_signal.
      connect(SigC::slot(this, &CardWords_Gtk_HandDisplay::received_click_on_card));
  }
  update();
}
  
CardWords_Gtk_HandDisplay::~CardWords_Gtk_HandDisplay()
{
  size_t index;
  for (index = 0; index < cells.size(); ++index) {
    cells[index].gtk_card_cell->hide();
    remove (*cells[index].gtk_card_cell);
    delete cells[index].gtk_card_cell;
    cells[index].gtk_card_cell = static_cast<CardWords_Gtk_CardCell *>(0);
  }
}

// The hand has changed:
void
CardWords_Gtk_HandDisplay::update(void)
{
  size_t index;
  CardWords_Hand::const_iterator iter;
  
  for ((index = 0), (iter = hand->begin());
       index < hand->occupied_places()
#ifdef DEBUG
         && iter != hand->end()
#endif
         ;
       (++index),(++iter)) {
    if (*iter != cells[index].card) {
      cells[index].set(iter);
    }
  }
  for (index = hand->occupied_places(); index < cells.size(); ++index) {
    if (cells[index].card != 0) {
      cells[index].set(static_cast<CardWords_Card *>(0));
    }
  }
}

void
CardWords_Gtk_HandDisplay::
received_click_on_card(const CardWords_Gtk_CardCell *cardcell)
{
  size_t index;
  // Search that card:
  for (index = 0; index < cells.size(); ++index) {
    if (cells[index].gtk_card_cell == cardcell) {
      if (cells[index].card != 0) {
        card_clicked(cells[index].card);
      }
      return;
    }
  }
}
  
