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

#include "cardwords_gtk_addword.hh"
#include "cardwords_gtkclient.hh"
#include <strstream.h>
#include <gtk--/button.h>

CardWords_Gtk_AddWord::CardWords_Gtk_AddWord(CardWords_GtkClient * client)
  : gtkclient(client)
{
  question   = new Gtk::Label("");
  yes_button = new Gtk::Button(gettext("Yes"));
  no_button  = new Gtk::Button(gettext("No "));
  retry_button=new Gtk::Button(gettext("Yes and retry\nlast move"));
  button_box = new Gtk::HBox;

  button_box->pack_start(*yes_button);
  yes_button->show();
  button_box->pack_start(*retry_button);
  retry_button->show();
  button_box->pack_start(*no_button);
  no_button->show();

  pack_start(*question, false, false);
  question->show();
  pack_start(*button_box, false, false);
  button_box->show();

  yes_button->clicked.
    connect(SigC::slot(this, &CardWords_Gtk_AddWord::yes_button_callback));

  retry_button->clicked
    .connect(SigC::slot(this, &CardWords_Gtk_AddWord::retry_button_callback));

  no_button->clicked.
    connect(SigC::slot(this, &CardWords_Gtk_AddWord::no_button_callback));
}

CardWords_Gtk_AddWord::~CardWords_Gtk_AddWord()
{
  question  ->hide();
  yes_button->hide();
  no_button ->hide();
  button_box->hide();
  retry_button->hide();

  button_box->remove(*yes_button);
  button_box->remove(*no_button);
  button_box->remove(*retry_button);
  remove(*button_box);
  remove(*question);
  
  delete question;
  delete yes_button;
  delete retry_button;
  delete no_button;
  delete button_box;
}

void
CardWords_Gtk_AddWord::add_word(const CardWords_String & new_word)
{
  ostrstream qstream;
  word = new_word;
  qstream << gettext("Would you like to add the word")
          << endl
          << word
          << endl
          << gettext("to the dictionary?")
          << ends;
  qstream.freeze(true);
  question->set(qstream.str());
  qstream.freeze(false);
  if (gtkclient->is_my_go() == true) {
    retry_button->show();
  }
  else {
    retry_button->hide();
  }
  show();
}

void
CardWords_Gtk_AddWord::yes_button_callback(void)
{
  gtkclient->add_word(word);
  hide();
}
void
CardWords_Gtk_AddWord::retry_button_callback(void)
{  
  yes_button_callback();
  if (gtkclient->is_my_go() == true
      && gtkclient->am_i_trying_trade() == false
      && gtkclient->am_i_trying_pass() == false
      && gtkclient->am_i_trying_move() == false) {
    gtkclient->try_move(gtkclient->get_current_move());
  }
}
void
CardWords_Gtk_AddWord::no_button_callback(void)
{
  hide();
}
