/**********************************************************************
 *
 *   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 "main_window.h"
#include "playfield.h"
#include "table.h"
#include "gtk_gui.h"

//#include "../../game/game.h"

namespace GTK_DOKO {
#include <gdk/gdkkeysyms.h>

  /*****************************************************************************
   *
   * MainWindow::MainWindow(GTK_GUI* gui_a)
   *
   * Parameters:	gui_a - pointer to the gui
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	Constructor: create the menu and the playfield
   *
   ****************************************************************************/
  MainWindow::MainWindow(GTK_GUI* gui_a) :
    gui_p(gui_a), // the gui
  window_p(gtk_window_new(GTK_WINDOW_TOPLEVEL)),
  container_p(gtk_vbox_new(FALSE, 0)),
  menu_p(this)
  {
    DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_INIT,
		  "MainWindow::MainWindow()");

    // the box with the elements of the window
    gtk_container_add(GTK_CONTAINER(window()), container());

    // Attach the widgets to the window
    gtk_box_pack_start(GTK_BOX(container()), menu().widget(),
		       FALSE, TRUE, 0);

    gtk_widget_realize(this->window());
    { // set the icon
#include "../icon.xpm"
      GdkPixmap* pixmap;
      GdkBitmap* mask;
      pixmap = gdk_pixmap_create_from_xpm_d(this->window()->window,
					    &mask,
					    NULL,
					    icon_xpm);

      gdk_window_set_icon(this->window()->window,
			  NULL,
			  pixmap,
			  mask);
    } // set the icon

    // show the container
    gtk_widget_show(container());

    DEBUG_RETURNING(VOID,
		    INFO_GTK_MAIN_WINDOW && INFO_GTK_INIT,
		    "MainWindow::MainWindow()");
  } // MainWindow::MainWindow(GTK_GUI* gui_a)

  /*****************************************************************************
   *
   * void MainWindow::init()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	inits window, the menu and the playfield
   *
   ****************************************************************************/
  void
    MainWindow::init()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_INIT,
		    "MainWindow::init()");

      // kill of the window
      gtk_signal_connect(GTK_OBJECT(window()), "delete_event",
			 GTK_SIGNAL_FUNC(MainWindow::delete_event), NULL);
      // Key press
      gtk_signal_connect(GTK_OBJECT(window()), "key_press_event",
			 GTK_SIGNAL_FUNC(MainWindow::key_event), gpointer(this));

      // center the window
      gtk_window_set_position(GTK_WINDOW(window()),
			      GTK_WIN_POS_CENTER);

      // the window can be resized
      gtk_window_set_policy(GTK_WINDOW(window()), FALSE, TRUE, TRUE);

      // init the menu
      menu().init();

      // pack the table
      gtk_box_pack_start(GTK_BOX(this->container()),
			 this->gui().playfield().table().widget(),
			 TRUE, TRUE, 0);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_INIT,
		      "void MainWindow::init()");
    } // void MainWindow::init()

  /*****************************************************************************
   *
   * GTK_GUI& MainWindow::gui()
   *
   * Parameters:	none
   *
   * Result:	the gui
   *
   * Version:	0.3.7
   *
   * Description:	Result
   *
   ****************************************************************************/
  GTK_GUI&
    MainWindow::gui()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		    "MainWindow::gui()");

      DEBUG_ASSERTION((gui_p != NULL),
		      "GTK_GUI& MainWindow::gui():\n"
		      "  'gui_p' == NULL");

      DEBUG_RETURNING(*gui_p,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		      "MainWindow::gui()");
    } // GTK_GUI& MainWindow::gui()

  /*****************************************************************************
   *
   * GtkWidget* MainWindow::window()
   *
   * Parameters:	none
   *
   * Result:	The window
   * 
   * Version:	0.3.6
   *
   * Description:	Result
   *
   ****************************************************************************/
  GtkWidget*
    MainWindow::window()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		    "MainWindow::window()");

      DEBUG_ASSERTION((window_p != NULL),
		      "MainWindow::window()\n"
		      "  `window_p` == NULL");

      DEBUG_RETURNING(window_p,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		      "MainWindow::window()");
    } // GtkWidget* MainWindow::window()


  /*****************************************************************************
   *
   * GtkWidget* MainWindow::container()
   *
   * Parameters:	none
   *
   * Result:	The container of the window
   * 
   * Version:	0.3.6
   *
   * Description:	Result
   *
   ****************************************************************************/
  GtkWidget*
    MainWindow::container()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		    "MainWindow::container()");

      DEBUG_ASSERTION((container_p != NULL),
		      "MainWindow::container()\n"
		      "  `container_p` == NULL");

      DEBUG_RETURNING(container_p,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		      "MainWindow::container()");
    } // GtkWidget* MainWindow::container()


  /*****************************************************************************
   *
   * Menu& MainWindow::menu()
   *
   * Parameters:	none
   *
   * Result:	The menu
   * 
   * Version:	0.3.6
   *
   * Description:	Result
   *
   ****************************************************************************/
  Menu&
    MainWindow::menu()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		    "MainWindow::menu()");

      DEBUG_RETURNING(menu_p,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_VALUE,
		      "MainWindow::menu()");
    } // Menu& MainWindow::menu()

  /*****************************************************************************
   *
   * void MainWindow::party_open()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	the party is opened
   *
   ****************************************************************************/
  void
    MainWindow::party_open()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::party_open()");

      menu().party_open();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::party_open()");
    } // void MainWindow::party_open()

  /*****************************************************************************
   *
   * void MainWindow::party_start()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	the party is started
   *
   ****************************************************************************/
  void
    MainWindow::party_start()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::party_start()");

      menu().party_start();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::party_start()");
    } // void MainWindow::party_start()

  /*****************************************************************************
   *
   * void MainWindow::game_open()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	the game is opened
   *
   ****************************************************************************/
  void
    MainWindow::game_open()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::game_open()");

      menu().game_open();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::game_open()");
    } // void MainWindow::game_open()

  /*****************************************************************************
   *
   * void MainWindow::game_start()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	the game is started
   *
   ****************************************************************************/
  void
    MainWindow::game_start()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::game_start()");

      menu().game_start();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::game_start()");
    } // void MainWindow::game_start()

  /*****************************************************************************
   *
   * void MainWindow::game_finish()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	the game is finished
   *
   ****************************************************************************/
  void
    MainWindow::game_finish()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::game_finish()");

      menu().game_finish();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::game_finish()");
    } // void MainWindow::game_finish()

  /*****************************************************************************
   *
   * void MainWindow::game_close()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	the game is closeed
   *
   ****************************************************************************/
  void
    MainWindow::game_close()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::game_close()");

      menu().game_close();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::game_close()");
    } // void MainWindow::game_close()

  /*****************************************************************************
   *
   * void MainWindow::party_finish()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	the party is finished
   *
   ****************************************************************************/
  void
    MainWindow::party_finish()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::party_finish()");

      menu().party_finish();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::party_finish()");
    } // void MainWindow::party_finish()

  /*****************************************************************************
   *
   * void MainWindow::party_close()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	the party is closeed
   *
   ****************************************************************************/
  void
    MainWindow::party_close()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		    "MainWindow::party_close()");

      menu().party_close();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_GAMEPLAY,
		      "MainWindow::party_close()");
    } // void MainWindow::party_close()

  /*****************************************************************************
   *
   * void MainWindow::language_update()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	updates the language in the main window
   *
   ****************************************************************************/
  void
    MainWindow::language_update()
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_UPDATE,
		    "MainWindow::language_update()");

      menu().language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_UPDATE,
		      "MainWindow::language_update()");
    } // void MainWindow::language_update()

  /*****************************************************************************
   *
   * static gboolean MainWindow::delete_event(GtkWidget*, GdkEvent*, gpointer main_window)
   *
   * Parameters:	main_window - main_window of the event
   *
   * Result:	none
   *
   * Version:	0.4
   *
   * Description:	the party points window is to be deleted
   *
   ****************************************************************************/
  gboolean
    MainWindow::delete_event(GtkWidget*, GdkEvent*, gpointer main_window)
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_EVENT,
		    "MainWindow::delete_event()");

#ifdef WINDOWS
      // so there aren't printed any errors
      // *** DK
      // find out, why there are so many errors
      exit(0);
#endif

      ((MainWindow*)main_window)->gui().quit();
      DEBUG_RETURNING(TRUE,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_EVENT,
		      "MainWindow::delete_event()");
    } // static void MainWindow::delete_event(GtkWidget*, GdkEvent*, gpointer main_window)

  /*****************************************************************************
   *
   * static gboolean MainWindow::key_event(GtkWidget* widget, GdkEventKey* key,
   *					 gpointer main_window)
   *
   * Parameters:	widget - the widget with the key
   *		key - the pressed key
   *		main_window - MainWindow of the event
   *
   * Result:	none
   *
   * Version:	0.3.7
   *
   * Description:	calls main_window->key(key)
   *
   ****************************************************************************/
  gboolean
    MainWindow::key_event(GtkWidget* widget, GdkEventKey* key, gpointer main_window)
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_KEY && INFO_GTK_EVENT,
		    "MainWindow::key_event()");

      DEBUG_ASSERTION((main_window != NULL),
		      "MainWindow::key_event():\n"
		      "  'main_window' == NULL");

      ((MainWindow*)main_window)->key(key);

      DEBUG_RETURNING(FALSE,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_KEY && INFO_GTK_EVENT,
		      "MainWindow::key_event()");
    } // static gboolean MainWindow::key_event(GtkWidget*, GdkEventKey*, gpointer)

  /*****************************************************************************
   *
   * void MainWindow::key(GdkEventKey* key)
   *
   * Parameters:	key - the pressed key
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	manages the pressed keys for the MainWindow window
   *
   ****************************************************************************/
  void
    MainWindow::key(GdkEventKey* key)
    {
      DEBUG_CALLING(INFO_GTK_MAIN_WINDOW && INFO_GTK_KEY,
		    "MainWindow::key()");

#include <gdk/gdkkeysyms.h>

      switch (key->keyval) {
      default: // pass the key to the playfield(!)
	this->gui().playfield().key(key);
	break;
      } // switch (key->keyval)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MAIN_WINDOW && INFO_GTK_KEY,
		      "MainWindow::key()");
    } // void MainWindow::key(GdkKeyEvent);


}
#endif
