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

namespace GTK_DOKO {
#include <gtk/gtklabel.h>
#include <gtk/gtktooltips.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtklist.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtksignal.h>

  // todo: remove items from the list

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

      this->bookmarks_container = gtk_vbox_new(FALSE, 0);
      this->bookmarks_label = gtk_label_new("bookmarks");
      this->bookmarks_list = gtk_list_new();
      this->bookmark_add_button = gtk_button_new_with_label("add current page");


      { // the add button
	// add a bookmark
	gtk_signal_connect(GTK_OBJECT(this->bookmark_add_button), "clicked",
			   GTK_SIGNAL_FUNC(Help::bookmark_add_event),
			   gpointer(this));

	// tooltip
	GtkTooltips* tooltips = gtk_tooltips_new();
	gtk_tooltips_enable(GTK_TOOLTIPS(tooltips));
	gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips),
			     this->bookmark_add_button,
			     "add current page to the bookmarks",
			     "");
	gtk_object_set_data(GTK_OBJECT(this->bookmark_add_button),
			    "tooltips",
			    tooltips);

	gtk_box_pack_start(GTK_BOX(this->bookmarks_container),
			   this->bookmark_add_button,
			   FALSE, TRUE, 0);
      } // the add button

      { // the list with a scrollbar
	GtkWidget* viewport = gtk_viewport_new(NULL, NULL);

	gtk_container_add(GTK_CONTAINER(viewport),
			  this->bookmarks_list);

	GtkWidget* scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	gtk_container_add(GTK_CONTAINER(scrolled_window),
			  viewport);

	gtk_box_pack_end(GTK_BOX(this->bookmarks_container),
			 scrolled_window,
			 TRUE, TRUE, 0);
      } // the list with a scrollbar


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

  /**
   ** 
   ** load the page of the bookmark
   **
   ** @param	bookmark	bookmark widget with the location
   ** @param	help		the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::bookmark_load_event(GtkWidget* bookmark, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::bookmark_load_event()");

      static_cast<Help*>(help)->load(*static_cast<string*>(gtk_object_get_data(GTK_OBJECT(bookmark),
									       "location")
							  ));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::bookmark_load_event()");
    } // static void Help::bookmark_load_event(GtkWidget* bookmark, gpointer help)

  /**
   ** 
   ** add the current page to the bookmarks
   **
   ** @param	help		the help of the widget
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::bookmark_add_event(GtkWidget*, gpointer help)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::bookmark_add_event()");

      static_cast<Help*>(help)->bookmark_add(static_cast<Help*>(help)->location(),
					     static_cast<Help*>(help)->title());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::bookmark_add_event()");
    } // static void Help::bookmark_add_event(GtkWidget*, gpointer help)

  /**
   ** 
   ** add the page to the bookmarks:
   ** create a new widget for the list and add it to the bookmark list
   **
   ** @param	location	the location of the page to be added
   ** @param	title		the name for the bookmark
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.4.4
   **
   **/
  void
    Help::bookmark_add(string const& location, string const& title)
    {
      DEBUG_CALLING(INFO_GTK_HELP && INFO_GTK_EVENT,
		    "Help::bookmark_add(location)");

      GtkWidget* bookmark = gtk_list_item_new_with_label(title.c_str());
      gtk_object_set_data(GTK_OBJECT(bookmark), "location",
			  new string(location));
      gtk_signal_connect(GTK_OBJECT(bookmark), "select",
			 GTK_SIGNAL_FUNC(Help::bookmark_load_event),
			 gpointer(this));

      GList* list = g_list_append(NULL, bookmark);

      gtk_list_append_items(GTK_LIST(this->bookmarks_list),
			    list);
      gtk_widget_show(bookmark);

      gtk_widget_show_all(this->bookmarks_list);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HELP && INFO_GTK_EVENT,
		      "Help::bookmark_add(location)");
    } // static void Help::bookmark_add(string const& location)

} // namespace GTK_DOKO

#endif // #ifdef USE_UI_GTK
