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

#include "cardwords_gtk_clientoptionsdialog.hh"
#include "cardwords_gettext.hh"
#include <stdlib.h>
#include "cardwords_readfile.hh"
#include <gtk--/spinbutton.h>
#include <gtk--/box.h>
#include <gtk--/label.h>
#include <gtk--/checkbutton.h>

string
CardWords_Gtk_CientOptionsDialog::get_name(void) const
{
  return name_entry->get_text();
}
string
CardWords_Gtk_CientOptionsDialog::get_alt_name(void) const
{
  return alt_name_entry->get_text();
} 
size_t
CardWords_Gtk_CientOptionsDialog::get_cell_width() const
{
  return cell_width_entry->get_value_as_int();
}
size_t
CardWords_Gtk_CientOptionsDialog::get_cell_height() const
{
  return cell_height_entry->get_value_as_int();
}
CardWords_Gtk_CientOptionsDialog::CardWords_Gtk_CientOptionsDialog()
  : Gtk::Table(2, 6, false),
    cell_width_adjustment(34, 15, 99, 1, 5, 5),
    cell_height_adjustment(34, 15, 99, 1, 5, 5)
{
  local_button = new Gtk::Button(gettext("Start a local server"));
  remote_button = new Gtk::Button(gettext("Connect to a remote server"));
  help_button = new Gtk::Button(gettext("Help"));

  button_box = new Gtk::HBox;

  player_button = new Gtk::CheckButton(gettext("Log in as player"));
  watcher_button = new Gtk::CheckButton(gettext("Log in as watcher"));
  radio_box = new Gtk::HBox;

  name_label = new Gtk::Label(gettext("Your name:"));
  name_entry = new Gtk::Entry();
  name_entry->set_text(getenv("LOGNAME"));
  
  alt_name_label = new Gtk::Label(gettext("Alternative name:"));
  alt_name_entry = new Gtk::Entry();

  cell_width_label = new Gtk::Label(gettext("Card width"));
  cell_width_entry = new Gtk::SpinButton(cell_width_adjustment,1,0);

  cell_height_label = new Gtk::Label(gettext("Card height"));
  cell_height_entry = new Gtk::SpinButton(cell_height_adjustment,1,0);

  button_box->pack_start(*local_button, false, false);
  button_box->pack_start(*remote_button, false, false);
  button_box->pack_end(*help_button, false, false);

  attach(*button_box, 0,2, 5,6);

  radio_box->pack_start(*player_button);
  radio_box->pack_start(*watcher_button);
  
  attach(*radio_box, 0,2, 4,5);

  attach(*name_label, 0,1, 0,1);
  attach(*name_entry, 1,2, 0,1);
  attach(*alt_name_label, 0,1, 1,2);
  attach(*alt_name_entry, 1,2, 1,2);
  attach(*cell_width_label, 0,1, 2,3);
  attach(*cell_width_entry, 1,2, 2,3);
  attach(*cell_height_label, 0,1, 3,4);
  attach(*cell_height_entry, 1,2, 3,4);

  local_button->show();
  remote_button->show();
  help_button->show();
  button_box->show();
  player_button->show();
  watcher_button->show();
  radio_box->show();
  name_label->show();
  name_entry->show();
  alt_name_label->show();
  alt_name_entry->show();
  cell_width_label->show();
  cell_width_entry->show();
  cell_height_label->show();
  cell_height_entry->show();

  local_button->clicked.connect(this->local_button_clicked.slot());

  remote_button->clicked.connect(this->remote_button_clicked.slot());

  help_button->clicked.
    connect(SigC::slot(this, &CardWords_Gtk_CientOptionsDialog::emit_help_signal));

  player_button->set_active(true);

  player_button->toggled.
    connect(SigC::slot(this,
                 &CardWords_Gtk_CientOptionsDialog::player_button_toggled));

  watcher_button->toggled.
    connect(SigC::slot(this,
                 &CardWords_Gtk_CientOptionsDialog::watcher_button_toggled));

  currently_handling_toggle = false;
}

CardWords_Gtk_CientOptionsDialog::~CardWords_Gtk_CientOptionsDialog()
{
  local_button->hide();
  remote_button->hide();
  help_button->hide();
  button_box->hide();
  player_button->hide();
  watcher_button->hide();
  radio_box->hide();
  name_label->hide();
  name_entry->hide();
  alt_name_label->hide();
  alt_name_entry->hide();
  cell_width_label->hide();
  cell_width_entry->hide();
  cell_height_label->hide();
  cell_height_entry->hide();

  button_box->remove(*local_button);
  button_box->remove(*remote_button);
  button_box->remove(*help_button);
  remove(*button_box);
  radio_box->remove(*player_button);
  radio_box->remove(*watcher_button);
  remove(*radio_box);

  remove(*name_label);
  remove(*name_entry);
  remove(*alt_name_label);
  remove(*alt_name_entry);
  remove(*cell_width_label);
  remove(*cell_width_entry);
  remove(*cell_height_label);
  remove(*cell_height_entry);

  delete local_button;
  delete remote_button;
  delete help_button;
  delete button_box;
  delete player_button;
  delete watcher_button;
  delete radio_box;
  delete name_label;
  delete name_entry;
  delete alt_name_label;
  delete alt_name_entry;
  delete cell_width_label;
  delete cell_width_entry;
  delete cell_height_label;
  delete cell_height_entry;
}

void
CardWords_Gtk_CientOptionsDialog::emit_help_signal(void)
{
  CardWords_ReadFile help_file(gettext("/gtk_clientoptionsdialog.help_text"));
  // Note to *translators*: this is the name of a file containing a help text.
  // The help text is generated from the README.<locale>.sgml.m4 file.
  // The filename above is then to be translated to
  // "/gtk_clientoptionsdialog.<locale>.help_text".
  // But leave this untranslated as long as there is no translated version
  // of the README. If you want to translate the README, please contact me at
  // cardwords@crosswinds.net . Tobias.
  
  if (help_file == false) {
    // Help text file not found
    cerr << gettext("WARNING: Cannot open help text file ")
         << LIBDIR_STR
         << gettext("/gtk_clientoptionsdialog.help_text")
         << endl;
    return;
  }

  help_signal(help_file.str());
}

bool
CardWords_Gtk_CientOptionsDialog::does_user_want_to_be_watcher(void) const
{
  return watcher_button->get_active();
}
void
CardWords_Gtk_CientOptionsDialog::player_button_toggled(void)
{
  if (currently_handling_toggle == false) {
    currently_handling_toggle = true;
    watcher_button->set_active(!player_button->get_active());
    currently_handling_toggle = false;
  }
}
void
CardWords_Gtk_CientOptionsDialog::watcher_button_toggled(void)
{
  if (currently_handling_toggle == false) {
    currently_handling_toggle = true;
    player_button->set_active(!watcher_button->get_active());
    currently_handling_toggle = false;
  }
}
