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

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


namespace GTK_DOKO {
#include <gtk/gtkwindow.h>
#include <gtk/gtkcontainer.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkprogressbar.h>
#include <gtk/gtkpixmap.h>
#include <gtk/gtksignal.h>
#include <gdk/gdkkeysyms.h>

  // Total progress
  unsigned const Splash::PROGRESS_TOTAL = 3;

  /*****************************************************************************
   *
   * Splash::Splash(GTK_GUI* gui_a)
   *
   * Parameters:	gui_a - pointer to the gui
   *
   * Result:	-
   * 
   * Version:	0.4.3
   *
   * Description:	Constructor
   *
   ****************************************************************************/
  Splash::Splash(GTK_GUI* gui_a) :
    gui_p(gui_a), // the gui
  window_p(gtk_window_new(GTK_WINDOW_POPUP)), // the window
  logo(this->gui().logo().widget()), // the logo
  logo_pm(this->gui().logo()),
  progressbar(gtk_progress_bar_new()), // the progressbar
  gc(gdk_gc_new(this->logo_pm.pixmap())),
  color(),
  text(),
  progress(0) // progress, that has been made
  {
    DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_INIT,
		  "Splash::Splash()");

    DEBUG_RETURNING(VOID,
		    INFO_GTK_SPLASH && INFO_GTK_INIT,
		    "Splash::Splash()");
  } // Splash::Splash(GTK_GUI* gui_a)

  /*****************************************************************************
   *
   * void Splash::init()
   *
   * Parameters:	-
   *
   * Result:	-
   * 
   * Version:	0.4.3
   *
   * Description:	inits the splash window
   *
   ****************************************************************************/
  void
    Splash::init()
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_INIT,
		    "Splash::init()");

      if (::setting(Setting::SPLASH_SCREEN_TRANSPARENT)) {
	gtk_container_add(GTK_CONTAINER(this->window()),
			  this->logo);

	gtk_widget_realize(this->window());

	gdk_window_shape_combine_mask(this->window()->window,
				      this->gui().logo().mask(),
				      0, 0);

	gdk_color_parse("red", &(this->color));
	gdk_colormap_alloc_color(gdk_colormap_get_system(),
				 &(this->color), FALSE, TRUE);
	// set the color
	gdk_gc_set_foreground(this->gc, &(this->color));
      } else { // if !(::setting(SPLAHS_SCREEN_TRANSPARENT))
	GtkWidget* container = gtk_vbox_new(FALSE, 0);

	// pack the widgets in the container
	gtk_container_add(GTK_CONTAINER(window()), container);
	gtk_container_set_border_width(GTK_CONTAINER(container), 5);
	gtk_box_pack_start(GTK_BOX(container), logo, FALSE, TRUE, 0);
	gtk_box_pack_end(GTK_BOX(container), progressbar, TRUE, TRUE, 0);

	// text can be written in the progressbar
	gtk_progress_set_show_text(GTK_PROGRESS(progressbar), TRUE);
      } // if !(::setting(SPLAHS_SCREEN_TRANSPARENT))

      // center the splash-screen
      gtk_window_set_position(GTK_WINDOW(window()), GTK_WIN_POS_CENTER);

      // set the progress to zero
      progress = 0;

      // update the language
      language_update();


      // Mouse button
      gtk_signal_connect(GTK_OBJECT(this->window()), "button_press_event",
			 GTK_SIGNAL_FUNC(Splash::mouse_click_event), this);


      // show all
      if (setting(Setting::SHOW_SPLASH_SCREEN))
	gtk_widget_show_all(window());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_SPLASH && INFO_GTK_INIT,
		      "Splash::init()");
    } // void Splash::init()

  /*****************************************************************************
   *
   * void Splash::deinit()
   *
   * Parameters:	-
   *
   * Result:	-
   * 
   * Version:	0.4.3
   *
   * Description:	deinits the splash window (hides it)
   *
   ****************************************************************************/
  void
    Splash::deinit()
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_INIT,
		    "Splash::deinit()");

      // set the progress to zero
      ASSERT((fabs(this->progress - PROGRESS_TOTAL) < 0.001),
	     "Splash::init()\n"
	     "  'progress' = " << this->progress
	     << " != PROGRESS_TOTAL = " << PROGRESS_TOTAL);

      // hide the window
      gtk_widget_hide(window());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_SPLASH && INFO_GTK_INIT,
		      "Splash::deinit()");
    } // void Splash::deinit()

  /*****************************************************************************
   *
   * GTK_GUI& Splash::gui()
   *
   * Parameters:	-
   *
   * Result:	the gui
   *
   * Version:	0.4.3
   *
   * Description:	Result
   *
   ****************************************************************************/
  GTK_GUI&
    Splash::gui()
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_VALUE,
		    "Splash::gui()");

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

      DEBUG_RETURNING(*gui_p,
		      INFO_GTK_SPLASH && INFO_GTK_VALUE,
		      "Splash::gui()");
    } // GTK_GUI& Splash::gui()

  /*****************************************************************************
   *
   * GtkWidget* Splash::window()
   *
   * Parameters:	-
   *
   * Result:	the window
   *
   * Version:	0.4.3
   *
   * Description:	Result
   *
   ****************************************************************************/
  GtkWidget*
    Splash::window()
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_VALUE,
		    "Splash::window()");

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

      DEBUG_RETURNING(window_p,
		      INFO_GTK_SPLASH && INFO_GTK_VALUE,
		      "Splash::window()");
    } // GtkWidget* Splash::window()


  /*****************************************************************************
   *
   * void Splash::set_text(string const& text)
   *
   * Parameters:	text - the text, that is to be displayed in the progressbar
   *
   * Result:	-
   * 
   * Version:	0.4.3
   *
   * Description:	set the text in the progressbar
   *
   ****************************************************************************/
  void
    Splash::set_text(string const& text)
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_UPDATE,
		    "Splash::set_text()");


      if (game_status == GAMESTATUS::PROGRAMSTART) {

	this->text = text;

	if (!::setting(Setting::SPLASH_SCREEN_TRANSPARENT))
	  gtk_progress_set_format_string(GTK_PROGRESS(progressbar),
					 text.c_str());

	this->gui().update();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTK_SPLASH && INFO_GTK_UPDATE,
		      "Splash::set_text()");
    } // void Splash::set_text()

  /*****************************************************************************
   *
   * void Splash::progress_add(const double progress_a)
   *
   * Parameters:	progress_a - progress, that has been made
   *
   * Result:	-
   * 
   * Version:	0.4.3
   *
   * Description:	updates the language in the splash window
   *
   ****************************************************************************/
  void
    Splash::progress_add(const double progress_a)
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_UPDATE,
		    "Splash::progress_add()");

      if (game_status == GAMESTATUS::PROGRAMSTART) {

	progress += progress_a;
	gtk_progress_set_percentage(GTK_PROGRESS(progressbar),
				    min(1.0,
					(max(0.0, progress)
					 / gfloat(PROGRESS_TOTAL)))
				   );


	if (::setting(Setting::SPLASH_SCREEN_TRANSPARENT)) {
	  gdk_draw_rectangle(this->logo_pm.pixmap(), this->gc, TRUE,
			     55, 191,
			     int((259 - 55) * this->progress / PROGRESS_TOTAL),
			     226 - 191);

	  gtk_pixmap_set(GTK_PIXMAP(this->logo),
			 this->logo_pm.pixmap(), this->logo_pm.mask());
	  gtk_widget_queue_draw(this->logo);
	} // if (::setting(Setting::SPLASH_SCREEN_TRANSPARENT))

	this->gui().update();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTK_SPLASH && INFO_GTK_UPDATE,
		      "Splash::progress_add()");
    } // void Splash::progress()

  /**
   **
   ** a mouse click on the splash screen
   **
   ** @param	button_event	the event
   ** @param	splash		the splash screen
   **
   ** @return	-
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  gboolean
    Splash::mouse_click_event(GtkWidget*, GdkEventButton* button_event,
			      gpointer splash)
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_EVENT,
		    "Splash::mouse_click_event()");

      cout << "mouse click" << endl;
      static_cast<Splash*>(splash)->mouse_click(button_event);

      DEBUG_RETURNING(TRUE,
		      INFO_GTK_SPLASH && INFO_GTK_EVENT,
		      "Splash::mouse_click_event()");
    } // static gboolean Splash::mouse_click_event(GtkWidget*, GdkEventButton* button_event, gpointer splash)

  /**
   **
   ** a mouse click on the splash screen
   **
   ** @param	button_event	the event
   **
   ** @return	-
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  void
    Splash::mouse_click(GdkEventButton* button_event)
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_EVENT,
		    "Splash::mouse_click()");

      gtk_widget_hide(this->window());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_SPLASH && INFO_GTK_EVENT,
		      "Splash::mouse_click()");
    } // void Splash::mouse_click(GdkEventButton* button_event)

  /*****************************************************************************
   *
   * void Splash::language_update()
   *
   * Parameters:	-
   *
   * Result:	-
   * 
   * Version:	0.4.3
   *
   * Description:	updates the language in the splash window
   *
   ****************************************************************************/
  void
    Splash::language_update()
    {
      DEBUG_CALLING(INFO_GTK_SPLASH && INFO_GTK_UPDATE,
		    "Splash::language_update()");

      // nothing to do

      DEBUG_RETURNING(VOID,
		      INFO_GTK_SPLASH && INFO_GTK_UPDATE,
		      "Splash::language_update()");
    } // void Splash::language_update()

}
#endif
