/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   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
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#include "constants.h"

#ifdef USE_UI_GTK

#include "menu.h"
#include "gtk_gui.h"
#include "dialog.h"
#include "settings.h"

namespace GTK_DOKO {

  /**
   **
   ** inits the settings submenu
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::settings_init()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_INIT,
		    "Menu::settings_init()");

      { // create the submenu
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->settings_item)->child),
			    "text",
			    new string("settings"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->settings_item)->child));
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->settings_show_all_hands)->child),
			    "text",
			    new string("show all hands"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->settings_show_all_hands)->child));
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->settings_cards_order)->child),
			    "text",
			    new string("cards order"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->settings_cards_order)->child));
	gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->settings_settings)->child),
			    "text",
			    new string("settings"));
	this->labels.push_back(GTK_LABEL(GTK_BIN(this->settings_settings)->child));

	GtkWidget* menu = gtk_menu_new();
	gtk_menu_append(GTK_MENU(menu), this->settings_show_all_hands);
	gtk_menu_append(GTK_MENU(menu), this->settings_cards_order);
	gtk_menu_append(GTK_MENU(menu), this->settings_settings);
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(this->settings_item), menu);
	gtk_menu_bar_append(GTK_MENU_BAR(this->bar), this->settings_item);
      } // create the submenu

      // the events
      gtk_signal_connect(GTK_OBJECT(this->settings_show_all_hands), "toggled",
			 GTK_SIGNAL_FUNC(Menu::settings_show_all_hands_toggled_event),
			 this);
      gtk_signal_connect(GTK_OBJECT(this->settings_cards_order), "activate",
			 GTK_SIGNAL_FUNC(Menu::settings_cards_order_event),
			 this);
      gtk_signal_connect(GTK_OBJECT(this->settings_settings), "activate",
			 GTK_SIGNAL_FUNC(Dialog::show_event),
			 &(this->gui().settings()));

      this->settings_show_all_hands_button_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_INIT,
		      "void Menu::settings_init()");
    } // void Menu::settings_init()

  /**
   **
   ** change the setting
   **
   ** @param	menu	the menu
   **
   ** @return	-
   ** 
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Menu::settings_show_all_hands_toggled_event(GtkWidget* widget, gpointer menu)
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_EVENT,
		    "Menu::settings_show_all_hands_toggled_event()");

      DEBUG_ASSERTION((menu != NULL),
		      "Menu::settings_show_all_hands_toggled_event():\n"
		      "  'menu' == NULL");

      ::setting.set(Setting::SHOW_ALL_HANDS,
		    GTK_CHECK_MENU_ITEM(widget)->active);
      // automatic save the changing
      ofstream ostr(::setting(Setting::SETTINGS_FILE).c_str(), ios::app);
      if (!ostr.fail()) {
	ostr << name(Setting::SHOW_ALL_HANDS) << " = "
	  << ::setting.value(Setting::SHOW_ALL_HANDS) << "\n";
      } // if (!ostr.fail())



      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_EVENT,
		      "Menu::settings_show_all_hands_toggled_event()");
    } // void Menu::settings_show_all_hands_toggled_event(GtkWidget* widget, gpointer menu)

  /**
   **
   ** updates the 'show hands button'
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Menu::settings_show_all_hands_button_update()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_EVENT,
		    "Menu::settings_show_all_hands_button_update()");

      gtk_signal_handler_block_by_func(GTK_OBJECT(this->settings_show_all_hands),
				       GTK_SIGNAL_FUNC(Menu::settings_show_all_hands_toggled_event),
				       gpointer(this));

      gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(this->settings_show_all_hands),
				     (::setting(Setting::SHOW_ALL_HANDS)
				      ? TRUE
				      : FALSE));


      gtk_signal_handler_unblock_by_func(GTK_OBJECT(this->settings_show_all_hands),
					 GTK_SIGNAL_FUNC(Menu::settings_show_all_hands_toggled_event),
					 gpointer(this));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_EVENT,
		      "Menu::settings_show_all_hands_button_update()");
    } // void Menu::settings_show_all_hands_button_update()

  /**
   **
   ** calls menu_game->new_party()
   **
   ** @param	menu	the menu
   **
   ** @return	-
   ** 
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Menu::settings_cards_order_event(GtkWidget* widget, gpointer menu)
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_EVENT,
		    "Menu::settings_cards_order_event()");

      DEBUG_ASSERTION((menu != NULL),
		      "Menu::settings_cards_order_event():\n"
		      "  'menu' == NULL");

      static_cast<Menu*>(menu)->gui().settings().show_cards_order();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_EVENT,
		      "Menu::settings_cards_order_event()");
    } // void Menu::settings_cards_order_event(GtkWidget*, gpointer menu)

} // namespace GTK_DOKO

#endif // #ifdef USE_UI_GTK
