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

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

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


      this->toc = gtk_moz_embed_new();
      this->toc_label = gtk_label_new("toc");

      // load the toc.short-page
      gtk_moz_embed_load_url(GTK_MOZ_EMBED(this->toc),
			     this->location_toc_short().c_str());

      // events

      // a new page shall be loaded
      gtk_signal_connect(GTK_OBJECT(this->toc), "open_uri",
			 GTK_SIGNAL_FUNC(Help::toc_open_uri_event),
			 gpointer(this));

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

  /**
   ** 
   ** a new page shall be loaded
   **
   ** @param	toc	the toc
   ** @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::toc_open_uri_event(GtkWidget* toc, char const* uri, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::toc_open_uri_event()");

      DEBUG_RETURNING((static_cast<Help*>(help)->toc_open_uri(uri)
		       ? TRUE
		       : FALSE),
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::toc_open_uri_event()");
    } // static gint Help::toc_open_uri_event(GtkWidget*, char const* uri, gpointer help)

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

      bool result = false;

      char* const location_old
	= gtk_moz_embed_get_location(GTK_MOZ_EMBED(this->toc));

      // only load the page, if th short toc is already loaded
      if (this->toc_loaded
	  || ((uri != this->location_toc_short())
	      && (uri
		  != "file://" + this->location_toc_short())
	      && (uri
		  != "http://" + this->location_toc_short())
	     )
	 ) {
	free(location_old);
	this->load(uri);
	result = true;
      } else {
	free(location_old);
	this->toc_loaded = true;
	result = false;
      }

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

  /**
   ** 
   ** load the toc of the sidebar
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::toc_short_load()
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::toc_short_load()");

      // load the toc.short-page
      this->toc_loaded = false;
      if (this->toc)
	gtk_moz_embed_load_url(GTK_MOZ_EMBED(this->toc),
			       this->location_toc_short().c_str());

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

} // namespace GTK_DOKO

#endif // #ifdef USE_UI_GTK
