/**********************************************************************
 *
 *   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 "ui.h"
#include "ui.gtkmmwrap.h"
#include <gtkmm/main.h>

#include "cards.h"
#include "icons.h"
#include "splash_screen.h"
#include "main_window.h"
#include "table.h"
#include "menu.h"
#include "party_settings.h"
#include "about.h"
#include "license.h"
#include "cardset_license.h"
#include "preferences.h"
#include "bug_report.h"

#include "../../misc/setting.h"

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

  bool UI_GTKMM::sleeping = false;

  /**
   **
   ** Constructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  UI_GTKMM::UI_GTKMM() :
    UI(UI_TYPE::GTKMM_DOKO),
  Base(this, this),
  ui_gtkmm_wrap(NULL),
  kit(NULL),
  colormap(NULL),
  logo(NULL),
  cards(NULL),
  icons(NULL),
  splash_screen(NULL),
  main_window(NULL),
  table(NULL),
  party_settings(NULL),
  about(NULL),
  license(NULL),
  cardset_license(NULL),
  preferences(NULL),
  bug_report(NULL),
  parts(),
  translation_label(),
  translation_button(),
  translation_window(),
  translation_text_view()
  {
    DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_INIT,
		  "UI_GTKMM::UI_GTKMM()");

    // the only option is the programname
    int argc_gtkmm = 1;
    char** argv_gtkmm = new char*[2];
    argv_gtkmm[0] = "FreeDoko";
    argv_gtkmm[1] = NULL;

    this->kit = new Gtk::Main(argc_gtkmm, argv_gtkmm);

    this->ui_gtkmm_wrap = new UI_GTKMM_Wrap(this);

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_UI && INFO_GTKMM_INIT,
		    "UI_GTKMM::UI_GTKMM()");
  } // UI_GTKMM::UI_GTKMM()

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   ** @todo	mostly all
   **
   **/
  UI_GTKMM::~UI_GTKMM()
  {
    DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_INIT,
		  "UI_GTKMM::~UI_GTKMM()");

    delete ui_gtkmm_wrap;
    delete this->cards;
    delete this->main_window;
    delete this->table;
    delete this->party_settings;
    delete this->about;
    delete this->license;
    delete this->cardset_license;
    delete this->preferences;
    delete this->bug_report;

    if (this->kit && this->kit->level())
      Gtk::Main::quit();
    delete this->kit;

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_UI && INFO_GTKMM_INIT,
		    "UI_GTKMM::~UI_GTKMM()");
  } // UI_GTKMM::~UI_GTKMM()


  /**
   **
   ** Initialises the UI with the command line arguments.
   ** GTKMM removes the arguments it understands from the list.
   **
   ** @param	argc	number of arguments
   ** @param	argv	arguments
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   ** @todo	mostly all
   **
   **/
  void
    UI_GTKMM::init(int& argc, char**& argv)
    {
      DEBUG_CALLING(INFO_GTKMM_UI && INFO_GTKMM_INIT,
		    "UI_GTKMM::init(int argc, char** argv)");

#if GTKMM_MINOR_VERSION < 4
      this->colormap = Gdk::Colormap::create(Gdk::Visual::get_system(), true);
#else
      this->colormap = Gdk::Colormap::get_system();
#endif
      { // the logo
#include "../../logo.xpm"
	this->logo = Gdk::Pixbuf::create_from_xpm_data(logo_xpm);
      } // the logo

      this->set_max_progress(3);

      // I have always to construct the window for 'Icons'
      // (I need one window and now I take the root window from 'splash_screen')
      this->splash_screen = new SplashScreen(this);
      if (::setting(Setting::SHOW_SPLASH_SCREEN))
	this->splash_screen->show();

      this->add_status_message(::translator("Loading the cards"));
      this->cards = new Cards(this);
      this->add_status_message(::translator("Loading the icons"));
      this->remove_status_message(::translator("Loading the cards"));
      this->icons = new Icons(this);
      this->add_status_message(::translator("Initiating the GUI"));
      this->remove_status_message(::translator("Loading the icons"));

      // there are some orders on initializing:
      //   bug_report < party_settings
      //   main_window < party_settings
      //   main_window < table
      this->about = new About(this);
      this->license = new License(this);
      this->cardset_license = new CardsetLicense(this);

      this->bug_report = new BugReport(this);
      this->preferences = new Preferences(this);

      this->main_window = new MainWindow(this);
      this->table = new Table(this);

      this->party_settings = new PartySettings(this);


      this->main_window->menu->set_signals();

      this->language_update();

      this->main_window->set_default_size(static_cast<int>((5.2 + 1) * this->cards->height()),
					  static_cast<int>(5.2 * this->cards->height()));

      this->main_window->show();

      this->splash_screen->hide();

      this->finish_progress();
      this->remove_status_message(::translator("Initiating the GUI"));

      delete this->splash_screen;
      this->splash_screen = NULL;

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI && INFO_GTKMM_INIT,
		      "UI_GTKMM::init(int argc, char** argv)");
    } // UI_GTKMM::init(argc, argv)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
