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

#include "cardwords_gtk_appearancedialog.hh"
#include "cardwords_gettext.hh"
#include <stdlib.h>
#include "cardwords_readfile.hh"

CardWords_Gtk_AppearanceDialog::
CardWords_Gtk_AppearanceDialog(CardWords_Options * opt)
  : Gtk_Table(6, 2, false),
    cell_width_adjustment(34, 15, 99, 1, 5, 5),
    cell_height_adjustment(34, 15, 99, 1, 5, 5),
    options(opt)
{
  help_button = new Gtk_Button(gettext("Help"));

  cell_width_label = new Gtk_Label(gettext("Card width"));
  cell_width_entry = new Gtk_SpinButton(cell_width_adjustment,1,0);
  switch (options->get_type("gtk_card_width")) {
  case UNKNOWN_TYPE:
    break;
  case UNSIGNED_TYPE:
    {
      unsigned card_width;
      options->get("gtk_card_width", &card_width);
      cell_width_entry->set_value(card_width);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("gtk_card_width", "unsigned");
    // This exits the program.
  }
  
  cell_height_label = new Gtk_Label(gettext("Card height"));
  cell_height_entry = new Gtk_SpinButton(cell_height_adjustment,1,0);
  switch (options->get_type("gtk_card_height")) {
  case UNKNOWN_TYPE:
    break;
  case UNSIGNED_TYPE:
    {
      unsigned card_height;
      options->get("gtk_card_width", &card_height);
      cell_width_entry->set_value(card_height);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("gtk_card_height", "unsigned");
    // This exits the program.
  }

  digits_for_columns_button =
    new Gtk_CheckButton(gettext("Use digits for the columns"));
  switch(options->get_type("use_digits_for_the_columns")) {
  case UNKNOWN_TYPE:
    digits_for_columns_button->set_active(true);
  case BOOL_TYPE:
    {
      bool use_digits_for_the_columns;
      options->get("use_digits_for_the_columns",
                   &use_digits_for_the_columns);
      digits_for_columns_button->set_active(use_digits_for_the_columns);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("use_digits_for_the_columns", "bool");
    // This exits the program.
  }

  column_indices_grow_rightwards_button =
    new Gtk_CheckButton(gettext("Column indices grow rightwards"));
  switch(options->get_type("column_indices_grow_rightwards")) {
  case UNKNOWN_TYPE:
    column_indices_grow_rightwards_button->set_active(true);
  case BOOL_TYPE:
    {
      bool column_indices_grow_rightwards;
      options->get("column_indices_grow_rightwards",
                   &column_indices_grow_rightwards);
      column_indices_grow_rightwards_button->set_active(column_indices_grow_rightwards);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("column_indices_grow_rightwards", "bool");
    // This exits the program.
  }

  row_indices_grow_upwards_button =
    new Gtk_CheckButton(gettext("Row indices grow upwards"));
  switch(options->get_type("row_indices_grow_upwards")) {
  case UNKNOWN_TYPE:
    row_indices_grow_upwards_button->set_active(true);
  case BOOL_TYPE:
    {
      bool row_indices_grow_upwards;
      options->get("row_indices_grow_upwards",
                   &row_indices_grow_upwards);
      row_indices_grow_upwards_button->set_active(row_indices_grow_upwards);
    }
    break;
  default:
    // There must be a false entry in the options file
    options->false_entry("row_indices_grow_upwards", "bool");
    // This exits the program.
  }

  attach(*cell_width_label, 0,1, 0,1, 0);
  attach(*cell_width_entry, 1,2, 0,1);
  attach(*cell_height_label, 0,1, 1,2, 0);
  attach(*cell_height_entry, 1,2, 1,2);
  attach(*digits_for_columns_button, 0,2, 2,3);
  attach(*column_indices_grow_rightwards_button, 0,2, 3,4);
  attach(*row_indices_grow_upwards_button, 0,2, 4,5);
  attach(*help_button, 0,2, 5,6);
  
  help_button->show();
  digits_for_columns_button->show();
  column_indices_grow_rightwards_button->show();
  row_indices_grow_upwards_button->show();
  cell_width_label->show();
  cell_width_entry->show();
  cell_height_label->show();
  cell_height_entry->show();

  connect_to_method(help_button->clicked, this, &emit_help_signal);
}
CardWords_Gtk_AppearanceDialog::~CardWords_Gtk_AppearanceDialog()
{
  help_button->hide();
  digits_for_columns_button->hide();
  column_indices_grow_rightwards_button->hide();
  row_indices_grow_upwards_button->hide();
  cell_width_label->hide();
  cell_width_entry->hide();
  cell_height_label->hide();
  cell_height_entry->hide();

  remove(help_button);
  remove(digits_for_columns_button);
  remove(column_indices_grow_rightwards_button);
  remove(row_indices_grow_upwards_button);
  remove(cell_width_label);
  remove(cell_width_entry);
  remove(cell_height_label);
  remove(cell_height_entry);

  delete help_button;
  delete digits_for_columns_button;
  delete column_indices_grow_rightwards_button;
  delete row_indices_grow_upwards_button;
  delete cell_width_label;
  delete cell_width_entry;
  delete cell_height_label;
  delete cell_height_entry;
}

void
CardWords_Gtk_AppearanceDialog::emit_help_signal(void)
{
  CardWords_ReadFile help_file(gettext("/gtk_appearancedialog.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_appearancedialog.help_text")
         << endl;
    return;
  }

  help_signal((const gchar *)help_file.str());
}
// Put the entries' contents into the options object.
void
CardWords_Gtk_AppearanceDialog::store_options(void)
{
  options->set("use_digits_for_the_columns",
               digits_for_columns_button->get_active());
  options->set("column_indices_grow_rightwards",
               column_indices_grow_rightwards_button->get_active());
  options->set("row_indices_grow_upwards",
               row_indices_grow_upwards_button->get_active());
  options->set("gtk_card_width",
               unsigned(cell_width_entry->get_value_as_int()));
  options->set("gtk_card_height",
               unsigned(cell_height_entry->get_value_as_int()));
}

