/**********************************************************************
 *
 *   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_GTKMM

#include "main_window.h"
#include "menu.h"

#include "ui.h"

#include <gtkmm/box.h>
namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  MainWindow::MainWindow(Base* const parent) :
    Base(parent),
  Window(),
  icon(),
  menu(new Menu(this)),
  container(Gtk::manage(new Gtk::VBox(false, 0)))
  {
    DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_INIT,
		  "MainWindow::MainWindow(parent)");

    {
#include "../icon.xpm"
      this->icon = Gdk::Pixbuf::create_from_xpm_data(icon_xpm);
      this->set_icon(this->icon);
    }

    this->set_position(Gtk::WIN_POS_CENTER);

    this->signal_key_press_event().connect(SigC::slot(*this, &MainWindow::key_press));

    this->add(*(this->container));
    this->container->pack_start(*(this->menu), false, true);

    this->container->show();

    this->signal_hide().connect(SigC::slot(*this,
					  &MainWindow::quit_program_event)
			      );

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_INIT,
		    "MainWindow::MainWindow(parent)");
  } // MainWindow::MainWindow(Base* const parent)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.2
   **
   **
   **/
  MainWindow::~MainWindow()
  {
    DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_INIT,
		  "MainWindow::~MainWindow()");

    delete this->menu;

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_INIT,
		    "MainWindow::~MainWindow()");
  } // MainWindow::~MainWindow()

  /**
   **
   ** start a new party
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    MainWindow::start_new_party_event()
    {
      DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		    "MainWindow::start_new_party_event()");

      this->ui->start_new_party();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		      "MainWindow::start_new_party_event()");
    } // void MainWindow::start_new_party_event()

  /**
   **
   ** quits the program.
   ** This function is just a wrapper for 'UI_GTKMM::quit_program()',
   ** because 'UI_GTKMM' is no Gtk object, so it cannot be used by the signals.
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    MainWindow::quit_program_event()
    {
      DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		    "MainWindow::quit_program_event()");

      this->ui->quit_program();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		      "MainWindow::quit_program_event()");
    } // void MainWindow::quit_program_event()

  /**
   **
   ** generates an error
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    MainWindow::generate_error_event()
    {
      DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		    "MainWindow::generate_error_event()");

      this->ui->generate_error(::translator("generated error").translation());

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		      "MainWindow::generate_error_event()");
    } // void MainWindow::generate_error_event()

  /**
   **
   ** a key has been pressed
   ** this function is not in 'UI_GTKMM', because 'UI_GTKMM' is not derived
   ** from a 'Gtk::Widget'
   **
   ** @param	key	the key
   **
   ** @return	from 'ui->key_press(key)'
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  bool
    MainWindow::key_press(GdkEventKey* key)
    {
      DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		    "MainWindow::key_press(key)");

      DEBUG_RETURNING(this->ui->key_press(key),
		      INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		      "MainWindow::key_press(key)");
    } // bool MainWindow::key_press(GdkEventKey* key)

  /**
   **
   ** the window is hidden -- quit the program
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    MainWindow::on_hide()
    {
      DEBUG_CALLING(INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		    "MainWindow::on_hide()");

      this->ui->quit_program();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_MAIN_WINDOW && INFO_GTKMM_EVENT,
		      "MainWindow::on_hide()");
    } // void MainWindow::on_hide()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
