/**********************************************************************
 *
 *   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 "XGtkLabelSpinbutton.h"

#include "../../../misc/language.h"

namespace GTK_DOKO
{

  /**
   **
   ** constructor
   **
   ** @param	text	text for the label
   **
   ** @return	-
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  XGtkLabelSpinbutton::XGtkLabelSpinbutton(string const& text) :
    XGtkLabelAdjustment(text),
  spin_button(gtk_spin_button_new(GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 1,
								    1, 0, 0)
						),
				  1, 0)) // the spin_button
  {
    DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_INIT,
		  "XGtkLabelSpinbutton::XGtkLabelSpinbutton()");

    gtk_box_pack_end(GTK_BOX(this->container()),
		     this->spin_button,
		     FALSE, FALSE, 0);

    // set the text of the label widget
    this->language_update();

    // show all widgets
    gtk_widget_show_all(this->widget());

    DEBUG_RETURNING(VOID,
		    INFO_GTK_WIDGET && INFO_GTK_INIT,
		    "XGtkLabelSpinbutton::XGtkLabelSpinbutton()");
  } // XGtkLabelSpinbutton::XGtkLabelSpinbutton()

  /**
   **
   ** destructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  XGtkLabelSpinbutton::~XGtkLabelSpinbutton()
  {
    DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_INIT,
		  "XGtkLabelSpinbutton::~XGtkLabelSpinbutton()");

    gtk_widget_destroy(this->spin_button);

    DEBUG_RETURNING(VOID,
		    INFO_GTK_WIDGET && INFO_GTK_INIT,
		    "XGtkLabelSpinbutton::~XGtkLabelSpinbutton()");
  } // XGtkLabelSpinbutton::XGtkLabelSpinbutton()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the adjustment
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  GtkAdjustment*
    XGtkLabelSpinbutton::adjustment()
    {
      DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_VALUE,
		    "XGtkLabelSpinbutton::adjustment()");

      DEBUG_RETURNING(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(this->spin_button)),
		      INFO_GTK_WIDGET && INFO_GTK_VALUE,
		      "XGtkLabelSpinbutton::adjustment()");
    } // GtkAdjustment* XGtkLabelSpinbutton::adjustment()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the widget with the adjustment
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  GtkWidget*
    XGtkLabelSpinbutton::adjustment_widget()
    {
      DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_VALUE,
		    "XGtkLabelSpinbutton::adjustment_widget()");

      DEBUG_RETURNING(this->spin_button,
		      INFO_GTK_WIDGET && INFO_GTK_VALUE,
		      "XGtkLabelSpinbutton::adjustment_widget()");
    } // GtkWidget* XGtkLabelSpinbutton::adjustment_widget()

  /**
   **
   ** sets the minimum and the maximum of the adjustment
   **
   ** @param	min	the new minimum
   ** @param	max	the new maximum
   **
   ** @return	-
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  void
    XGtkLabelSpinbutton::minmax_set(int const min, int const max)
    {
      DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_VALUE,
		    "XGtkLabelSpinbutton::minmax_set(min, max)");

      gtk_spin_button_set_adjustment(GTK_SPIN_BUTTON(this->spin_button),
				     GTK_ADJUSTMENT(gtk_adjustment_new(this->adjustment()->value,
								       min,
								       max,
								       this->adjustment()->step_increment,
								       this->adjustment()->page_increment,
								       this->adjustment()->page_size
								      )
						   )
				    );

      DEBUG_RETURNING(VOID,
		      INFO_GTK_WIDGET && INFO_GTK_VALUE,
		      "XGtkLabelSpinbutton::minmax_set(min, max)");
    } // GtkWidget* XGtkLabelSpinbutton::minmax_set(int const min, int const max)

  /**
   **
   ** sets the value
   **
   ** @param	value	the new value
   **
   ** @return	-
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  void
    XGtkLabelSpinbutton::value_set(unsigned const value)
    {
      DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_VALUE,
		    "XGtkLabelSpinbutton::value_set(value)");

      gtk_spin_button_set_value(GTK_SPIN_BUTTON(this->spin_button),
				((value == UINT_MAX)
				 ? -1
				 : static_cast<int>(value))
			       );

      DEBUG_RETURNING(VOID,
		      INFO_GTK_WIDGET && INFO_GTK_VALUE,
		      "XGtkLabelSpinbutton::value_set(value)");
    } // GtkWidget* XGtkLabelSpinbutton::value_set(unsigned const value)

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the value
   ** 
   ** author	Diether Knof
   **
   ** @version	0.5.2
   **
   **/
  unsigned
    XGtkLabelSpinbutton::value()
    {
      DEBUG_CALLING(INFO_GTK_WIDGET && INFO_GTK_VALUE,
		    "XGtkLabelSpinbutton::value()");

      int const value = static_cast<int const>(gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(this->spin_button)));

      DEBUG_RETURNING(((value == -1) ? UINT_MAX : static_cast<unsigned>(value)),
		      INFO_GTK_WIDGET && INFO_GTK_VALUE,
		      "XGtkLabelSpinbutton::value()");
    } // GtkWidget* XGtkLabelSpinbutton::value()

}
#endif
