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

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

namespace GTK_DOKO {
#include <gtkembedmoz/gtkmozembed.h>

  /**
   ** init the gecko part
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::gecko_init()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_INIT,
		    "Help::gecko_init()");

      this->gecko = gtk_moz_embed_new();

      // events

      // a new page shall be loaded
      gtk_signal_connect(GTK_OBJECT(this->gecko), "open_uri",
			 GTK_SIGNAL_FUNC(Help::gecko_open_uri_event),
			 gpointer(this));
      // the location has changed
      gtk_signal_connect(GTK_OBJECT(this->gecko), "location",
			 GTK_SIGNAL_FUNC(Help::gecko_location_change_event),
			 gpointer(this));
      // the title has changed
      gtk_signal_connect(GTK_OBJECT(this->gecko), "title",
			 GTK_SIGNAL_FUNC(Help::gecko_title_change_event),
			 gpointer(this));

      // load the index page
      this->load(this->location_index());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_INIT,
		      "Help::gecko_init()");
    } // void Help::gecko_init()

  /**
   ** 
   ** -> result
   **
   ** @param	-
   **
   ** @return	the current location
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  string
    Help::location() const
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_VALUE,
		    "Help::location()");

      gchar* const location
	= (this->gecko
	   ? gtk_moz_embed_get_location(GTK_MOZ_EMBED(this->gecko))
	   : static_cast<gchar*>(""));
      string const result = location;
      if (this->gecko)
	free(location);

      DEBUG_RETURNING(result,
		      INFO_GTK_HELP && INFO_GTK_VALUE,
		      "Help::location()");
    } // string Help::location() const

  /**
   ** 
   ** -> result
   **
   ** @param	-
   **
   ** @return	the current title
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  string
    Help::title() const
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_VALUE,
		    "Help::title()");

      gchar* const title
	= gtk_moz_embed_get_title(GTK_MOZ_EMBED(this->gecko));
      string const result = title;
      free(title);

      DEBUG_RETURNING(result,
		      INFO_GTK_HELP && INFO_GTK_VALUE,
		      "Help::title()");
    } // string Help::title() const

  /**
   ** 
   ** show the previous page
   **
   ** @param	help	the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::previous_page()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::previous_page()");

      gtk_moz_embed_go_back(GTK_MOZ_EMBED(this->gecko));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::previous_page()");
    } // static void Help::previous_page()

  /**
   ** 
   ** show the next page
   **
   ** @param	help	the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::next_page()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::next_page()");

      gtk_moz_embed_go_forward(GTK_MOZ_EMBED(this->gecko));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::next_page()");
    } // static void Help::next_page()

  /**
   ** 
   ** a new page shall be loaded
   **
   ** @param	uri	location of the page to be loaded
   ** @param	help	the help of the widget
   **
   ** @return	whether to loading is invalid
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  gint
    Help::gecko_open_uri_event(GtkWidget*, char const* uri, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::gecko_open_uri_event()");

      bool const result = static_cast<Help*>(help)->gecko_open_uri(uri);

      DEBUG_RETURNING((result ? TRUE : FALSE),
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::gecko_open_uri_event()");
    } // static gint Help::gecko_open_uri_event(GtkWidget*, char const* uri, gpointer help)

  /**
   ** 
   ** the location has changed
   **
   ** @param	help	the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::gecko_location_change_event(GtkWidget*, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::gecko_location_change_event()");

      static_cast<Help*>(help)->gecko_location_change();
      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::gecko_location_change_event()");
    } // static void Help::gecko_location_change_event(GtkWidget*, gpointer help)

  /**
   ** 
   ** the title has changed
   **
   ** @param	help	the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::gecko_title_change_event(GtkWidget*, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::gecko_title_change_event()");

      static_cast<Help*>(help)->gecko_title_change();
      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::gecko_title_change_event()");
    } // static void Help::gecko_title_change_event(GtkWidget*, gpointer help)

  /**
   ** 
   ** a new page shall be loaded
   ** return, whether the locatin is invalid
   ** (valid are only files under the directory)
   **
   ** @param	uri	location of the page to be loaded
   **
   ** @return	whether the location is invalid to load
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   ** @todo	call of an external browser, if the page is not local
   **
   **/
  bool
    Help::gecko_open_uri(char const* uri)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::gecko_open_uri(uri)");

      bool result = true;

      if (this->location_dir
	  == string(uri, strlen("file://"), this->location_dir.size())) {
	result = false;
      } else {
	// call an external browser
#ifdef LINUX
	::system((::setting(Setting::BROWSER) + " "
		  + this->location() + " &").c_str());
#endif
#ifdef WINDOWS
	::system((::setting(Setting::BROWSER) + " "
		  + this->location()).c_str());
#endif
      } // if (valid)

      DEBUG_RETURNING(result,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::gecko_open_uri(uri)");
    } // bool Help::gecko_open_uri(char const* uri)

  /**
   ** 
   ** the location has changed
   ** set the sensitivity of the previous and following button
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::gecko_location_change()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::gecko_location_change()");

      gtk_widget_set_sensitive(this->previous_page_button,
			       gtk_moz_embed_can_go_back(GTK_MOZ_EMBED(this->gecko))
			      );
      gtk_widget_set_sensitive(this->next_page_button,
			       gtk_moz_embed_can_go_forward(GTK_MOZ_EMBED(this->gecko))
			      );

      gchar* location = gtk_moz_embed_get_location(GTK_MOZ_EMBED(this->gecko));
      gtk_entry_set_text(GTK_ENTRY(this->location_entry), location);
      g_free(location);


      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::gecko_location_change()");
    } // static void Help::gecko_location_change()

  /**
   ** 
   ** the title has changed
   ** set the new title
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::gecko_title_change()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::gecko_title_change()");

      gchar* title = gtk_moz_embed_get_title(GTK_MOZ_EMBED(this->gecko));

      gtk_label_set_text(GTK_LABEL(this->title_label),
			 title);
      gtk_window_set_title(GTK_WINDOW(this->window),
			   (string() + "Help: " + title).c_str());

      g_free(title);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::gecko_title_change()");
    } // static void Help::gecko_title_change()

} // namespace GTK_DOKO

#endif // #ifdef USE_UI_GTK
