
/*
 * gnoclSpinButton.c --
 *
 * This file implements the spinButton widget
 *
 * Copyright (c) 2001 - 2002 P.G. Baum
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

/*
   TODO:
      - getValue -type string|integer|float
   History:
   2002-01: bugfixes concerning behavior of -variable and -command
   2001-10: Begin of developement
*/

#include "gnocl.h"
#include <string.h>
#include <assert.h>

typedef struct
{
   GNOCL_STD_OPTIONS_VARS;

   Gnocl_StringBool  variable;
   Gnocl_StringBool  command;
   Gnocl_IntBool     maxLength;
   Gnocl_BoolBool    editable;
   Gnocl_DoubleBool  lower;
   Gnocl_DoubleBool  upper;
   Gnocl_DoubleBool  value;
   Gnocl_DoubleBool  stepInc;
   Gnocl_DoubleBool  pageInc;
   Gnocl_BoolBool    numeric;
   Gnocl_IntBool     digits;
} SpinButtonOptions;

static const GnoclWidgetOptions widgetOptions[] = 
{
   GNOCL_STD_OPTIONS_NAMES( SpinButtonOptions ),

   { "-variable", GNOCL_STRING, GNOCL_OFFSET( SpinButtonOptions, variable ) },
   { "-command", GNOCL_STRING, GNOCL_OFFSET( SpinButtonOptions, command ) },
   { "-maxLength", GNOCL_INT, GNOCL_OFFSET( SpinButtonOptions, maxLength ) },
   { "-editable", GNOCL_BOOL, GNOCL_OFFSET( SpinButtonOptions, editable ) },
   { "-lower", GNOCL_DOUBLE, GNOCL_OFFSET( SpinButtonOptions, lower ) },
   { "-upper", GNOCL_DOUBLE, GNOCL_OFFSET( SpinButtonOptions, upper ) },
   { "-value", GNOCL_DOUBLE, GNOCL_OFFSET( SpinButtonOptions, value ) },
   { "-stepInc", GNOCL_DOUBLE, GNOCL_OFFSET( SpinButtonOptions, stepInc ) },
   { "-pageInc", GNOCL_DOUBLE, GNOCL_OFFSET( SpinButtonOptions, pageInc ) },
   { "-numeric", GNOCL_BOOL, GNOCL_OFFSET( SpinButtonOptions, numeric ) },
   { "-digits", GNOCL_INT, GNOCL_OFFSET( SpinButtonOptions, digits ) },
   { NULL, 0, 0 }
};

typedef enum {  EMPTY = 0, 
                DONT_COMMAND = 1 << 0, 
                DONT_SET     = 1 << 1, 
                DONT_GET     = 1 << 2,
                ALL          = 0xFF } InSignalType;

typedef struct
{
   char          *name;
   char          *variable;
   char          *command;
   GtkSpinButton *spinButton;
   Tcl_Interp    *interp;
   InSignalType  inSignal;
} SpinButtonParams;


static void freeOpts( SpinButtonOptions *p )
{
   GNOCL_STD_OPTIONS_FREE( p );
   GNOCL_FREE_MEM_OPT( p->variable );
   GNOCL_FREE_MEM_OPT( p->command );
}

static int doCommand( SpinButtonParams *para, Tcl_Obj *val )
{
   if( para->command )
   {
      static Gnocl_PercSubst ps[] = {
         { 'w', GNOCL_STRING },  /* widget */
         { 'v', GNOCL_OBJ },     /* value */
         { 0 }
      };

      ps[0].val.str = para->name;
      ps[1].val.obj = val;

      return gnoclPercentSubstAndEval( para->interp, ps, para->command );
   }
   return TCL_OK;
}

static int setValue( SpinButtonParams *para, Tcl_Obj *val )
{
   /* first try to set as double (no string conversion) */
   double d;
   para->inSignal |= DONT_SET|DONT_COMMAND;
   if( Tcl_GetDoubleFromObj( NULL, val, &d ) == TCL_OK )
   {
      gtk_adjustment_set_value(
         gtk_spin_button_get_adjustment( para->spinButton ), d );
   }
   else
   {
      gtk_entry_set_text( GTK_ENTRY( para->spinButton ), 
            Tcl_GetString( val ) );
   }
   para->inSignal &= ~(DONT_SET|DONT_COMMAND);

   return 0;
}

static char *traceFunc( ClientData data,
      Tcl_Interp *interp, char *name1, char *name2, int flags)
{
   SpinButtonParams *para = (SpinButtonParams *)data;

   if( ( para->inSignal & DONT_GET ) == 0 )
   {
      Tcl_Obj *val = name1 ? Tcl_GetVar2Ex( interp, name1, name2, flags ): NULL;
      if( val )
      {
         setValue( para, val );
         doCommand( para, val );
      }
   }

   return NULL;
}

static int changedFuncRet( SpinButtonParams *para, Tcl_Obj *val )
{
   int ret = TCL_OK;

   if( ( para->inSignal & DONT_SET ) == 0 && para->variable )
   {
      para->inSignal |= DONT_GET;
      if( Tcl_SetVar2Ex( para->interp, para->variable, NULL, 
            val, TCL_GLOBAL_ONLY ) != TCL_OK )
         ret = TCL_ERROR;
      para->inSignal &= ~DONT_GET;
   }

   if( ( para->inSignal & DONT_COMMAND ) == 0 )
   {
      if( doCommand( para, val ) != TCL_OK )
         ret = TCL_ERROR;
   }
   return ret;
}

static void changedFunc( GtkWidget *widget, gpointer data )
{
   SpinButtonParams *para = (SpinButtonParams *)data;
   GtkAdjustment *adjust = gtk_spin_button_get_adjustment( para->spinButton );
   Tcl_Obj *valObj = Tcl_NewDoubleObj( adjust->value );

   changedFuncRet( para, valObj );
}

static void spinButtonDestroyFunc ( GtkWidget *widget, gpointer data )
{
   SpinButtonParams *para = (SpinButtonParams *)data;

   gnoclForgetWidgetFromName( para->name );
   Tcl_DeleteCommand( para->interp, para->name );

   gnoclAttacheCmdAndVar( 
         1, &para->command, NULL,
         1, &para->variable, NULL,
         "changed", GTK_OBJECT( para->spinButton ), 
         changedFunc, para->interp, traceFunc, para );

   g_free( para->variable );
   g_free( para->name );
   g_free( para );
}

static int spinButtonConfigure( Tcl_Interp *interp, SpinButtonParams *para,
      SpinButtonOptions *popt )
{
   GNOCL_STD_OPTIONS_HANDLE( popt, GTK_WIDGET( para->spinButton ) );

   gnoclAttacheCmdAndVar( 
         popt->command.changed, &para->command, &popt->command.val,
         popt->variable.changed, &para->variable, &popt->variable.val,
         "changed", GTK_OBJECT( para->spinButton ), 
         changedFunc, interp, traceFunc, para );

   if( popt->variable.changed && para->variable )
   {
      Tcl_Obj *var = Tcl_GetVar2Ex( interp, para->variable, NULL, 
            TCL_GLOBAL_ONLY );
      if( var == NULL )
      {
         GtkAdjustment *adjust = gtk_spin_button_get_adjustment( 
               para->spinButton );
         para->inSignal |= DONT_GET;
         Tcl_SetVar2Ex( para->interp, para->variable, NULL, 
               Tcl_NewDoubleObj( adjust->value ), TCL_GLOBAL_ONLY );
         para->inSignal &= ~DONT_GET;
      }
      else
      {
         double d;
         if( Tcl_GetDoubleFromObj( interp, var, &d ) != TCL_OK )
            return TCL_ERROR;
         para->inSignal |= DONT_SET|DONT_COMMAND;
         gtk_spin_button_set_value( para->spinButton, d );
         para->inSignal &= ~(DONT_SET|DONT_COMMAND);
      }
 
   }

   /* see also scale.c */
   if( popt->lower.changed || popt->upper.changed 
         || popt->stepInc.changed || popt->pageInc.changed )
   {
      GtkAdjustment *old = gtk_spin_button_get_adjustment( para->spinButton );
      gfloat lower = popt->lower.changed ? popt->lower.val : old->lower;
      gfloat upper = popt->upper.changed ? popt->upper.val : old->upper;
      /* gfloat value = popt->value.changed ? popt->value.val : old->value; */
      gfloat stepInc = popt->stepInc.changed ? popt->stepInc.val : 
            old->step_increment;
      gfloat pageInc = popt->pageInc.changed ? popt->pageInc.val : 
            old->page_increment;

      /* last parameter is pageSize, where it is used? */
      gtk_spin_button_set_adjustment( para->spinButton, 
            GTK_ADJUSTMENT( gtk_adjustment_new( old->value, lower, upper, 
            stepInc, pageInc, 0 ) ) ); 
      /* gtk_spin_button_update( para->spinButton ); */
   }

   if( popt->value.changed )
      gtk_spin_button_set_value( para->spinButton, popt->value.val );

   if( popt->maxLength.changed )
      gtk_entry_set_max_length( GTK_ENTRY( para->spinButton ), 
            popt->maxLength.val );

   if( popt->editable.changed )
      gtk_entry_set_editable( GTK_ENTRY( para->spinButton ), 
            popt->editable.val );
   
   if( popt->numeric.changed )
      gtk_spin_button_set_numeric( para->spinButton, popt->numeric.val );

   if( popt->digits.changed )
      gtk_spin_button_set_digits( para->spinButton, popt->digits.val );

   /*
   spinButtonTraceFunc( para, interp, para->variable, NULL, 0 );
   */

   return TCL_OK;
}

static int spinButtonFunc( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{
   static char *cmds[] = { GNOCL_STD_CMD_NAMES,
         "configure", "getValue", "setValue",
         NULL };
   enum cmdIdx { GNOCL_STD_CMD_ENUMS, 
         ConfigureIdx, GetValueIdx, SetValueIdx };
   SpinButtonParams *para = (SpinButtonParams *)data;
   GtkWidget *widget = GTK_WIDGET( para->spinButton );
   int idx;

   if( Tcl_GetIndexFromObj( interp, objv[1], cmds, "command", 
         TCL_EXACT, &idx ) != TCL_OK )
      return TCL_ERROR;

   switch( idx )
   {
      GNCOL_STD_CMD_CASE( interp, objc, objv, widget );
            break;

      case ConfigureIdx:
            {
               SpinButtonOptions opt;
               int ret = TCL_ERROR;
               memset( &opt, 0, sizeof( opt ) );
               if( gnoclParseAllWidgetOpts( interp, objc, objv, 1,
                     widgetOptions, (char *)&opt ) == TCL_OK )
                  ret = spinButtonConfigure( interp, para, &opt );

               freeOpts( &opt );
               if( ret != TCL_OK )
                  return TCL_ERROR;
            }
            break;
      case GetValueIdx:
            {
               Tcl_Obj *obj;
               GtkAdjustment *adjust;
               if( objc != 2 )
               {
                  Tcl_WrongNumArgs( interp, 2, objv, NULL );
                  return TCL_ERROR;
               }
               adjust = gtk_spin_button_get_adjustment( para->spinButton );
               obj = Tcl_NewDoubleObj( adjust->value );
               Tcl_SetObjResult( interp, obj );
            }
            break;
      case SetValueIdx:
            {
               if( objc != 3 )
               {
                  Tcl_WrongNumArgs( interp, 2, objv, "value" );
                  return TCL_ERROR;
               }

               setValue( para, objv[2] );
               changedFuncRet( para, objv[2] );
            }
            break;
   }

   return TCL_OK;
}

int gnoclSpinButtonCmd( ClientData data, Tcl_Interp *interp,
      int objc, Tcl_Obj * const objv[] )
{

   SpinButtonOptions opt;
   SpinButtonParams *para;

   memset( &opt, 0, sizeof( opt ) );
   if( gnoclParseAllWidgetOpts( interp, objc, objv, 0,
         widgetOptions, (char *)&opt ) != TCL_OK )
      return TCL_ERROR;

   para = g_new( SpinButtonParams, 1 );
   para->spinButton = GTK_SPIN_BUTTON( gtk_spin_button_new( 
            GTK_ADJUSTMENT( gtk_adjustment_new( 0, 0, 100, 
            1, 10, 20 ) ), 1.0, 1 ) );
   para->name = gnoclGetAutoWidgetId();
   para->interp = interp;
   para->variable = NULL;
   para->command = NULL;
   para->inSignal = 0;

   gtk_spin_button_set_numeric( para->spinButton, 1 );
   if( spinButtonConfigure( interp, para, &opt ) != TCL_OK )
   {
      /* FIXME: g_free para->name ... */
      gtk_widget_destroy( GTK_WIDGET( para->spinButton ) );
      return TCL_ERROR;
   }

   gtk_signal_connect( GTK_OBJECT( para->spinButton ), "destroy", 
         GTK_SIGNAL_FUNC( spinButtonDestroyFunc ), para );

   gnoclMemNameAndWidget( para->name, GTK_WIDGET( para->spinButton ) );
   gtk_widget_show( GTK_WIDGET( para->spinButton ) );

   Tcl_CreateObjCommand( interp, para->name, spinButtonFunc, para, NULL );

   Tcl_SetObjResult( interp, Tcl_NewStringObj( para->name, -1 ) );

   return TCL_OK;
}

